Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add check fabric setting #2163

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// Tenant setting status check by model name ("GPT-4", "gpt-35-turbo")
// eg: FabricTenant.getModelStatus("GPT-4")
// returnType: Boolean

// Part of the code are scala implementation of
// synapse.ml.internal_utils.session_utils
// synapse.ml.fabric.token_utils
// synapse.ml.mlflow.synapse_mlflow_utils

Comment on lines +1 to +9
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this docstring in the class, use the official header to pass the stylecheck

import spray.json._

import scala.io.Source
import scala.collection.immutable.Map
import scala.util.Try
import java.net.URI
import java.net.InetAddress
import StringifiedJsonProtocol._

import org.apache.http.impl.client.HttpClients
import org.apache.http.client.methods.{HttpGet, HttpPost}
import org.apache.http.entity.StringEntity
import org.apache.http.util.EntityUtils


object StringifiedJsonProtocol extends DefaultJsonProtocol {
implicit object StringifiedMapFormat extends RootJsonFormat[Map[String, String]] {
def write(map: Map[String, String]): JsValue = {
JsObject(map.mapValues(JsString(_)))
}

def read(value: JsValue): Map[String, String] = value.asJsObject.fields.map {
case (key, JsString(str)) => key -> str
case (key, other) => key -> other.toString
}
}
}
Comment on lines +25 to +36
Copy link
Collaborator

@mhamilton723 mhamilton723 Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import spray.json.DefaultProtocol._


object ConfigConstants {
final val CONTEXT_FILE_PATH = "/home/trusted-service-user/.trident-context"
final val TOKEN_PATH = "/opt/token-service/tokenservice.config.json"
final val TRIDENT_LAKEHOUSE_TOKEN_SERVICE_ENDPOINT = "trident.lakehouse.tokenservice.endpoint"
final val TRIDENT_SESSION_TOKEN = "trident.session.token"
Comment on lines +39 to +42
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit on Style: no camelcase in scala, you can just use the regular naming case (with a capital first letter because its an object

}

object Configs {
val pbiEnv = sc.getConf.get("spark.trident.pbienv").toLowerCase
val tokenServiceEndpoint = sc.hadoopConfiguration.get("trident.lakehouse.tokenservice.endpoint")
val capacityId = sc.hadoopConfiguration.get("trident.capacity.id")
val workspaceId = sc.hadoopConfiguration.get("trident.artifact.workspace.id")
Comment on lines +46 to +49
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make these lazy vals instead of vals

}

class FabricToken {

def readJsonFileAsMap(filePath: String): Try[Map[String, String]] = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make everytthing private or private[ml] unless you want people to call it

Try {
val fileContent = Source.fromFile(filePath).getLines.mkString
fileContent.parseJson.convertTo[Map[String, String]]//.asJsObject.fields
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reminder to remove comment

}
}

val tokenServiceConfig = readJsonFileAsMap(ConfigConstants.TOKEN_PATH)
val tokenServiceEndpoint = tokenServiceConfig.get("tokenServiceEndpoint")
val clusterIdentifier = tokenServiceConfig.get("clusterName")


def getTridentContext(contextFilePath: String): Map[String, String] = {
var tridentContext = Map[String, String]()
for (line <- Source.fromFile(contextFilePath).getLines()) {
val parts = line.split('=').map(_.trim)
if (parts.length == 2) {
val (k, v) = (parts(0), parts(1))
tridentContext += (k -> v)
}
}
tridentContext
}

val tridentContext = getTridentContext(ConfigConstants.CONTEXT_FILE_PATH)
val tridentLakehouseTokenServiceEndpoint = tridentContext.get(ConfigConstants.TRIDENT_LAKEHOUSE_TOKEN_SERVICE_ENDPOINT)
val sessionToken = tridentContext.get(ConfigConstants.TRIDENT_SESSION_TOKEN).get

def parseUrl(urlOption: Option[String]): Option[URI] = {
urlOption match {
case Some(url) =>
try {
Some(new URI(url))
} catch {
case e: Exception => None // Handle invalid URL or other exceptions
}
case None => None
}
}
val urlOption: Option[String] = tridentLakehouseTokenServiceEndpoint
val TargetUrl = parseUrl(urlOption)
val hostname = InetAddress.getLocalHost.getHostName
val scheme = TargetUrl.map(_.getScheme).get
val host = TargetUrl.map(_.getHost).get
val path = TargetUrl.map(_.getPath).get

def getAADToekn(): String = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: typo here Token

val url = s"${tokenServiceEndpoint}/api/v1/proxy${path}/access?resource=pbi"
val httpGet = new HttpGet(url)
httpGet.setHeader("x-ms-cluster-identifier", clusterIdentifier)
httpGet.setHeader("x-ms-workload-resource-moniker", clusterIdentifier)
httpGet.setHeader("Content-Type", "application/json;charset=utf-8")
httpGet.setHeader("x-ms-proxy-host", s"${scheme}://${host}")
httpGet.setHeader("x-ms-partner-token", sessionToken)
httpGet.setHeader("User-Agent", s"SynapseML check tenant setting - HostName:${hostname}")
val client = HttpClients.createDefault()
val response = client.execute(httpGet)
val entity = response.getEntity
val responseString = EntityUtils.toString(entity, "UTF-8")
response.close()
client.close()
responseString
}
}

object FabricTenant extends FabricToken {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factor out common code shared by this and logging infra

val _DEFAULT_GLOBAL_SERVICE_ENDPOINT = "https://api.powerbi.com/"
val _FETCH_CLUSTER_DETAIL_URI = "powerbi/globalservice/v201606/clusterDetails"
val MWC_WORKLOAD_TYPE_ML = "ML"

val aadToken = getAADToekn()

val PbiGlobalServiceEndpoints = Map(
"public" -> "https://api.powerbi.com/",
"fairfax" -> "https://api.powerbigov.us",
"mooncake" -> "https://api.powerbi.cn",
"blackforest" -> "https://app.powerbi.de",
"msit" -> "https://api.powerbi.com/",
"prod" -> "https://api.powerbi.com/",
"int3" -> "https://biazure-int-edog-redirect.analysis-df.windows.net/",
"dxt" -> "https://powerbistagingapi.analysis.windows.net/",
"edog" -> "https://biazure-int-edog-redirect.analysis-df.windows.net/",
"dev" -> "https://onebox-redirect.analysis.windows-int.net/",
"console" -> "http://localhost:5001/",
"daily" -> "https://dailyapi.powerbi.com/")

def getMLFlowSharedHost(): String = {
val url = PbiGlobalServiceEndpoints.getOrElse("msit",_DEFAULT_GLOBAL_SERVICE_ENDPOINT) + _FETCH_CLUSTER_DETAIL_URI
val httpGet = new HttpGet(url)

httpGet.setHeader("Authorization", s"Bearer ${aadToken}")
val client = HttpClients.createDefault()
val response = client.execute(httpGet)
val entity = response.getEntity
val content = EntityUtils.toString(entity)
val jsonData = content.parseJson.convertTo[Map[String, String]]
response.close()
client.close()
Comment on lines +144 to +151
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace with the safe HTTP calls like in other code

jsonData.getOrElse("clusterUrl","")

}
def getMLFlowWorkloadHost(): String = {
val client = HttpClients.createDefault()

val url = getMLFlowSharedHost + "/metadata/v201606/generatemwctokenv2"
val httpPost = new HttpPost(url)
httpPost.setHeader("Content-type", "application/json")
httpPost.setHeader("Authorization", s"Bearer ${aadToken}")

val payload = s"""{"capacityObjectId": "${Configs.capacityId}", "workspaceObjectId": "${Configs.workspaceId}", "workloadType": "${MWC_WORKLOAD_TYPE_ML}"}""""
httpPost.setEntity(new StringEntity(payload, "UTF-8"))
Comment on lines +159 to +164
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anytime you make a post you can use existing utility function


val response = client.execute(httpPost)
val entity = response.getEntity
val content = EntityUtils.toString(entity, "UTF-8")
val jsonData = content.parseJson.convertTo[Map[String, String]]
response.close()
client.close()
val targetUriHost = "https://" + jsonData.getOrElse("TargetUriHost", "")
targetUriHost
}

def getMLFlowWorkloadEndpoint(): String = {
val workloadHost = getMLFlowWorkloadHost
val mlflowWorkloadEndpoint = s"${workloadHost}/webapi/capacities/${Configs.capacityId}/workloads/ML/ML/Automatic/workspaceid/${Configs.workspaceId}/"
mlflowWorkloadEndpoint
}

def getModelStatus(modelName: String): Boolean = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe

def assertModelReady(modelName): Unit = {

.....

throw new IllegalArgumentException("Useful error message about the status and a link to the URL on how to set up the model endpoint stuff")


val models = List(modelName)
val jsonString = models.toJson.compactPrint

val client = HttpClients.createDefault()
val url = getMLFlowWorkloadEndpoint + "cognitive/openai/tenantsetting"
val httpPost = new HttpPost(url)
httpPost.setHeader("Content-type", "application/json")
httpPost.setHeader("Authorization", s"Bearer ${aadToken}")
httpPost.setEntity(new StringEntity(jsonString))

val response = client.execute(httpPost)
val responseString = EntityUtils.toString(response.getEntity)
Comment on lines +187 to +195
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use utility function


response.close()
client.close()
val responseField = responseString.parseJson.asJsObject.fields.get(modelName.toLowerCase).get
val resultString: String = responseField match {
case JsString(value) => value // Directly extract the string value
}
// Allowed, Disallowed, DisallowedForCrossGeo, ModelNotFound, InvalidResult
Comment on lines +199 to +203
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think theres a simpler 1-liner for this

resultString == "Allowed"
}
}
Loading