bowenliang123 commented on code in PR #5662:
URL: https://github.com/apache/kyuubi/pull/5662#discussion_r1389144020


##########
kyuubi-server/src/main/scala/org/apache/kyuubi/engine/EngineRef.scala:
##########
@@ -319,4 +323,50 @@ private[kyuubi] class EngineRef(
       }
     }
   }
+
+  def getAdaptivePoolId(poolSize: Int): Int = {
+    val sessionThreshold = conf.get(ENGINE_POOL_ADAPTIVE_SESSION_THRESHOLD)
+    val metricsSpace =
+      
s"/metrics/${serverSpace}_${KYUUBI_VERSION}_${shareLevel}_${engineType}/$user"
+    DiscoveryClientProvider.withDiscoveryClient(conf) { client =>
+      tryWithLock(client) {
+        if (client.pathNonExists(metricsSpace)) {
+          client.create(metricsSpace, "PERSISTENT")
+        }
+      }
+      val metrics = client.getChildren(metricsSpace)
+      if (metrics.isEmpty) {
+        return Random.nextInt(poolSize)
+      } else {
+        engineType match {
+          case SPARK_SQL =>
+            val engineMetricsMap = metrics.map(p =>
+              new String(client.getData(s"$metricsSpace/$p"))
+                .split(";")
+                .map(_.split("=", 2))
+                .filter(_.length == 2)
+                .map(kv => (kv.head, kv.last.toInt))
+                .toMap)
+            if (engineMetricsMap.isEmpty) {
+              return Random.nextInt(poolSize)
+            } else {
+              val sortedEngineMetrics = engineMetricsMap.sortBy { map =>
+                (
+                  map.getOrElse("openSessionCount", sessionThreshold),
+                  map.getOrElse("activeTask", 0))
+              }
+              val candidate = sortedEngineMetrics.head
+              if (candidate.contains("poolId") && (candidate(
+                  "openSessionCount") < sessionThreshold || metrics.size == 
poolSize)) {

Review Comment:
   Move the conditions into candidate filters.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to