yrenat commented on code in PR #6046:
URL: https://github.com/apache/texera/pull/6046#discussion_r4056137752


##########
computing-unit-managing-service/src/main/scala/org/apache/texera/service/ComputingUnitManagingService.scala:
##########
@@ -32,9 +32,65 @@ import org.apache.texera.service.resource.{
   ComputingUnitManagingResource,
   HealthCheckResource
 }
+import 
org.apache.texera.service.resource.ComputingUnitManagingResource.TerminatedComputingUnitInfo
+import org.slf4j.LoggerFactory
 import java.nio.file.Path
+import java.util.concurrent.TimeUnit
 
 class ComputingUnitManagingService extends 
Application[ComputingUnitManagingServiceConfiguration] {
+  private val logger = 
LoggerFactory.getLogger(classOf[ComputingUnitManagingService])
+
+  private def initSqlServer(): Unit =
+    SqlServer.initConnection(
+      StorageConfig.jdbcUrl,
+      StorageConfig.jdbcUsername,
+      StorageConfig.jdbcPassword
+    )
+
+  private[service] def registerIdleComputingUnitCleanup(
+      environment: Environment,
+      kubernetesComputingUnitEnabled: Boolean = 
KubernetesConfig.kubernetesComputingUnitEnabled,
+      idleTimeoutMinutes: Long = 
KubernetesConfig.computingUnitIdleTimeoutMinutes,
+      idleCheckIntervalMinutes: Long = 
KubernetesConfig.computingUnitIdleCheckIntervalMinutes,
+      terminateIdleComputingUnits: () => List[TerminatedComputingUnitInfo] = 
() =>
+        ComputingUnitManagingResource.terminateIdleKubernetesComputingUnits(),
+      logTerminatedUnits: String => Unit = message => logger.info(message),
+      logCleanupFailure: Throwable => Unit = throwable =>
+        logger.warn("Failed to terminate idle Kubernetes computing units", 
throwable),
+      scheduleWithFixedDelay: Option[(Runnable, Long, Long, TimeUnit) => Unit] 
= None
+  ): Unit = {
+    if (!kubernetesComputingUnitEnabled || idleTimeoutMinutes <= 0) {
+      return
+    }
+    // scheduleWithFixedDelay rejects a non-positive delay, which would abort 
service startup.
+    // A misconfigured interval leaves the rest of the service usable, so log 
it and skip the sweep.
+    if (idleCheckIntervalMinutes <= 0) {
+      logger.warn(
+        s"Idle Kubernetes computing unit cleanup is disabled: check interval 
must be positive " +
+          s"but is $idleCheckIntervalMinutes minute(s)"
+      )
+      return
+    }
+
+    val scheduler = scheduleWithFixedDelay.getOrElse((command, initialDelay, 
delay, unit) =>
+      environment.lifecycle
+        .scheduledExecutorService("idle-computing-unit-terminator")
+        .threads(1)
+        .build()
+        .scheduleWithFixedDelay(command, initialDelay, delay, unit)
+    )
+    scheduler(
+      () =>
+        ComputingUnitManagingService.runIdleComputingUnitCleanup(
+          terminateIdleComputingUnits,
+          logTerminatedUnits,
+          logCleanupFailure
+        ),
+      idleCheckIntervalMinutes,
+      idleCheckIntervalMinutes,

Review Comment:
   Done



-- 
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]

Reply via email to