-
Notifications
You must be signed in to change notification settings - Fork 77
Detect runtime env #427
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
base: main
Are you sure you want to change the base?
Detect runtime env #427
Changes from all commits
f40bddd
2993fae
f08eca9
ec3743b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import scala.annotation.tailrec | |
import scala.collection.mutable | ||
import scala.reflect.ClassTag | ||
import scala.util.{Failure, Success, Try} | ||
import scala.collection.JavaConverters.asScalaSetConverter | ||
|
||
object Utils extends Logging { | ||
|
||
|
@@ -186,4 +187,153 @@ object Utils extends Logging { | |
def setTesting(): Unit = System.setProperty(IS_TESTING, "true") | ||
|
||
def isTesting: Boolean = System.getProperty(IS_TESTING) == "true" | ||
|
||
object RuntimeDetector { | ||
|
||
def detectRuntime(): Option[String] = | ||
RuntimeDetector.detectViaStackTrace() | ||
.orElse(RuntimeDetector.detectViaClassLoader()) | ||
.orElse(RuntimeDetector.detectViaThreadNames()) | ||
|
||
/** | ||
* Examines the current stack trace and loaded classes for platform-specific signatures | ||
*/ | ||
def detectViaStackTrace(): Option[String] = { | ||
val stackTrace = Thread.currentThread().getStackTrace | ||
val stackClasses = stackTrace.map(_.getClassName.toLowerCase) | ||
|
||
// Check for platform-specific classes in stack | ||
if ( | ||
stackClasses.exists(c => | ||
c.contains("com.databricks.logging") || | ||
c.contains("databricks.spark") || | ||
c.contains("com.databricks.backend") | ||
) | ||
) { | ||
Some("Databricks") | ||
} else if ( | ||
stackClasses.exists { c => | ||
c.contains("com.amazonaws.services.glue") || | ||
c.contains("aws.glue") || | ||
c.contains("awsglue") | ||
} | ||
) { | ||
Some("Glue") | ||
} else if ( | ||
stackClasses.exists(c => | ||
c.contains("com.amazon.emr") || | ||
c.contains("amazon.emrfs") | ||
) | ||
) { | ||
Some("EMR") | ||
} else if ( | ||
stackClasses.exists(c => | ||
c.contains("com.google.cloud.dataproc") || | ||
c.contains("dataproc") | ||
) | ||
) { | ||
Some("Dataproc") | ||
} else if ( | ||
stackClasses.exists(c => | ||
c.contains("com.microsoft.azure.synapse") || | ||
c.contains("synapse.spark") | ||
) | ||
) { | ||
Some("Synapse") | ||
} else { | ||
None | ||
} | ||
} | ||
Comment on lines
+201
to
+246
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
/** | ||
* More comprehensive check using ClassLoader to find platform-specific classes | ||
*/ | ||
def detectViaClassLoader(): Option[String] = { | ||
val classLoader = Thread.currentThread().getContextClassLoader | ||
|
||
case class PlatformSignature(name: String, classNames: Seq[String]) | ||
|
||
val platformSignatures = Seq( | ||
PlatformSignature( | ||
"Databricks", | ||
Seq( | ||
"com.databricks.spark.util.DatabricksLogging", | ||
"com.databricks.backend.daemon.driver.DriverLocal", | ||
"com.databricks.dbutils_v1.DBUtilsHolder", | ||
"com.databricks.spark.util.FrameProfiler" | ||
) | ||
), | ||
PlatformSignature( | ||
"Glue", | ||
Seq( | ||
"com.amazonaws.services.glue.GlueContext", | ||
"com.amazonaws.services.glue.util.GlueArgParser", | ||
"com.amazonaws.services.glue.DynamicFrame" | ||
) | ||
), | ||
PlatformSignature( | ||
"EMR", | ||
Seq( | ||
"com.amazon.ws.emr.hadoop.fs.EmrFileSystem", | ||
"com.amazon.emr.kinesis.client.KinesisConnector", | ||
"com.amazon.emr.cloudwatch.CloudWatchSink" | ||
) | ||
), | ||
PlatformSignature( | ||
"Dataproc", | ||
Seq( | ||
"com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem", | ||
"com.google.cloud.dataproc.DataprocHadoopConfiguration", | ||
"com.google.cloud.spark.bigquery.BigQueryConnector" | ||
) | ||
), | ||
PlatformSignature( | ||
"Synapse", | ||
Seq( | ||
"com.microsoft.azure.synapse.ml.core.env.SynapseEnv", | ||
"com.microsoft.azure.synapse.ml.logging.SynapseMLLogging" | ||
) | ||
), | ||
PlatformSignature( | ||
"HDInsight", | ||
Seq( | ||
"com.microsoft.azure.hdinsight.spark.common.SparkBatchJob", | ||
"com.microsoft.hdinsight.spark.common.HttpFutureCallback" | ||
) | ||
) | ||
) | ||
|
||
// Try to load platform-specific classes | ||
def classExists(className: String): Boolean = | ||
try { | ||
Class.forName(className, false, classLoader) | ||
true | ||
} catch { | ||
case _: ClassNotFoundException => false | ||
} | ||
|
||
platformSignatures.collectFirst { | ||
case PlatformSignature(name, classes) if classes.exists(classExists) => name | ||
} | ||
} | ||
|
||
/** | ||
* Check running threads for platform-specific thread names | ||
*/ | ||
def detectViaThreadNames(): Option[String] = { | ||
val threadNames = Thread.getAllStackTraces.keySet().asScala.map(_.getName.toLowerCase) | ||
|
||
if (threadNames.exists(_.contains("databricks"))) { | ||
Some("Databricks") | ||
} else if (threadNames.exists(t => t.contains("glue") || t.contains("awsglue"))) { | ||
Some("Glue") | ||
} else if (threadNames.exists(_.contains("emr"))) { | ||
Some("EMR") | ||
} else if (threadNames.exists(_.contains("dataproc"))) { | ||
Some("Dataproc") | ||
} else { | ||
None | ||
} | ||
Comment on lines
+323
to
+336
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Comment on lines
+323
to
+336
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,10 +25,10 @@ import com.clickhouse.spark.format.{ | |
NamesAndTypes, | ||
SimpleOutput | ||
} | ||
import com.clickhouse.spark.Utils.RuntimeDetector.detectRuntime | ||
import com.clickhouse.spark.spec.NodeSpec | ||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import com.clickhouse.spark.format._ | ||
|
||
import java.io.InputStream | ||
import java.util.UUID | ||
|
@@ -42,23 +42,42 @@ class NodeClient(val nodeSpec: NodeSpec) extends AutoCloseable with Logging { | |
// TODO: add configurable timeout | ||
private val timeout: Int = 30000 | ||
|
||
private lazy val userAgent = { | ||
private lazy val userAgent: String = { | ||
val title = getClass.getPackage.getImplementationTitle | ||
val version = getClass.getPackage.getImplementationVersion | ||
if (version != null && title != null) { | ||
val versions = version.split("_") | ||
if (versions.length < 3) { | ||
"Spark-ClickHouse-Connector" | ||
} else { | ||
val sparkVersion = versions(0) | ||
val scalaVersion = versions(1) | ||
val connectorVersion = versions(2) | ||
s"${title}/${connectorVersion} (fv:spark/${sparkVersion}, lv:scala/${scalaVersion})" | ||
} | ||
buildUserAgent(title, version) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did we validate that this once, only once, on every node? Since it can be a heavy operation |
||
} | ||
|
||
private def buildUserAgent(title: String, version: String): String = | ||
(Option(title), Option(version)) match { | ||
case (Some(t), Some(v)) => | ||
parseVersionString(v) match { | ||
case Some((spark, scala, connector)) => | ||
val runtimeSuffix = getRuntimeEnvironmentSuffix() | ||
s"$t/$connector (fv:spark/$spark, lv:scala/$scala$runtimeSuffix)" | ||
case None => "Spark-ClickHouse-Connector" | ||
} | ||
case _ => "Spark-ClickHouse-Connector" | ||
} | ||
|
||
private def parseVersionString(version: String): Option[(String, String, String)] = | ||
version.split("_") match { | ||
case Array(spark, scala, connector, _*) => Some((spark, scala, connector)) | ||
case _ => None | ||
} | ||
|
||
private def getRuntimeEnvironmentSuffix(): String = | ||
if (shouldInferRuntime()) { | ||
detectRuntime() | ||
.filter(_.nonEmpty) | ||
.fold("")(env => s", env:$env") | ||
} else { | ||
"Spark-ClickHouse-Connector" | ||
"" | ||
} | ||
} | ||
|
||
private def shouldInferRuntime(): Boolean = | ||
nodeSpec.infer_runtime_env.equalsIgnoreCase("true") || nodeSpec.infer_runtime_env == "1" | ||
|
||
private val node: ClickHouseNode = ClickHouseNode.builder() | ||
.options(nodeSpec.options) | ||
.host(nodeSpec.host) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will convert the map to a set and just make exists and looks like it can applied to other detections