-
Notifications
You must be signed in to change notification settings - Fork 175
fix(kubernetes): terminate idle computing units #6046
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
Open
yrenat
wants to merge
15
commits into
apache:main
Choose a base branch
from
yrenat:fix/idle-kubernetes-cus
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
797849d
fix(kubernetes): terminate idle computing units
yrenat a11601f
1). add CU terminate reason. 2). return info about garbage CUs that h…
yrenat 6d94489
add test computing-unit-managing-service
yrenat 789339c
fix formatting issues
yrenat 34750a9
Merge branch 'main' into fix/idle-kubernetes-cus
yrenat 7ba142a
Merge branch 'main' into fix/idle-kubernetes-cus
yrenat a1e2ba4
test(computing-unit-managing-service), improve testing coverage
yrenat 1159e93
add more test coverage
yrenat 6d78eef
Merge branch 'main' into fix/idle-kubernetes-cus
yrenat 1c33c1b
add more test coverage
yrenat a1a6f66
Merge branch 'main' into fix/idle-kubernetes-cus
yrenat 82a57ee
add more coverage
yrenat e551284
add test coverage
yrenat 522a05d
Merge remote-tracking branch 'upstream/main' into fix/idle-kubernetes…
yrenat a624db6
empty commit, just to trigger the PR check
yrenat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,9 +42,11 @@ import org.apache.texera.common.config.{ | |
| } | ||
| import org.apache.texera.dao.SqlServer | ||
| import org.apache.texera.dao.SqlServer.withTransaction | ||
| import org.apache.texera.dao.jooq.generated.Tables.{WORKFLOW_COMPUTING_UNIT, WORKFLOW_EXECUTIONS} | ||
| import org.apache.texera.dao.jooq.generated.enums.{ | ||
| PrivilegeEnum, | ||
| UserRoleEnum, | ||
| WorkflowComputingUnitTerminationReasonEnum, | ||
| WorkflowComputingUnitTypeEnum | ||
| } | ||
| import org.apache.texera.dao.jooq.generated.tables.daos.{ | ||
|
|
@@ -53,6 +55,7 @@ import org.apache.texera.dao.jooq.generated.tables.daos.{ | |
| WorkflowComputingUnitDao | ||
| } | ||
| import org.apache.texera.dao.jooq.generated.tables.pojos.WorkflowComputingUnit | ||
| import org.apache.texera.service.ComputingUnitManagingService | ||
| import org.apache.texera.service.resource.ComputingUnitManagingResource._ | ||
| import org.apache.texera.service.util.{ | ||
| ComputingUnitHelpers, | ||
|
|
@@ -61,6 +64,8 @@ import org.apache.texera.service.util.{ | |
| KubernetesClient | ||
| } | ||
| import org.jooq.{DSLContext, EnumType} | ||
| import org.jooq.impl.DSL.max | ||
| import org.slf4j.LoggerFactory | ||
| import play.api.libs.json._ | ||
|
|
||
| import java.sql.Timestamp | ||
|
|
@@ -73,6 +78,136 @@ object ComputingUnitManagingResource { | |
| .getInstance() | ||
| .createDSLContext() | ||
|
|
||
| private[resource] final class IdleComputingUnitCleanupConfig( | ||
| val enabled: Boolean, | ||
| val idleTimeoutMinutes: Long | ||
| ) { | ||
| def copy( | ||
| enabled: Boolean = this.enabled, | ||
| idleTimeoutMinutes: Long = this.idleTimeoutMinutes | ||
| ): IdleComputingUnitCleanupConfig = | ||
| new IdleComputingUnitCleanupConfig(enabled, idleTimeoutMinutes) | ||
| } | ||
|
|
||
| private[resource] trait KubernetesPodOperations { | ||
| val podExists: Int => Boolean | ||
| val deletePod: Int => Unit | ||
| } | ||
|
|
||
| private[resource] object DefaultKubernetesPodOperations extends KubernetesPodOperations { | ||
| private[resource] var podExistsDelegate: Int => Boolean = KubernetesClient.podExists | ||
| private[resource] var deletePodDelegate: Int => Unit = KubernetesClient.deletePod | ||
|
|
||
| override val podExists: Int => Boolean = cuid => podExistsDelegate(cuid) | ||
| override val deletePod: Int => Unit = cuid => deletePodDelegate(cuid) | ||
| } | ||
|
|
||
| private[resource] def lastComputingUnitActivityTime( | ||
| unit: WorkflowComputingUnit, | ||
| latestUpdateTime: Option[Timestamp], | ||
| latestStartTime: Option[Timestamp] | ||
| ): Timestamp = | ||
| Seq( | ||
| latestUpdateTime, | ||
| latestStartTime, | ||
| Option(unit.getCreationTime) | ||
| ).flatten.maxBy(_.getTime) | ||
|
|
||
| private[resource] def shouldTerminateIdleComputingUnit( | ||
| hasActiveExecution: Boolean, | ||
| lastExecutionTime: Timestamp, | ||
| cutoff: Timestamp | ||
| ): Boolean = | ||
| !hasActiveExecution && lastExecutionTime.before(cutoff) | ||
|
|
||
| def terminateIdleKubernetesComputingUnits(): List[TerminatedComputingUnitInfo] = | ||
| terminateIdleKubernetesComputingUnits( | ||
| new IdleComputingUnitCleanupConfig( | ||
| KubernetesConfig.kubernetesComputingUnitEnabled, | ||
| KubernetesConfig.computingUnitIdleTimeoutMinutes | ||
| ), | ||
| () => new Timestamp(System.currentTimeMillis()), | ||
| DefaultKubernetesPodOperations | ||
| ) | ||
|
|
||
| private[resource] def terminateIdleKubernetesComputingUnits( | ||
| cleanupConfig: IdleComputingUnitCleanupConfig, | ||
| currentTime: () => Timestamp, | ||
| podOperations: KubernetesPodOperations | ||
| ): List[TerminatedComputingUnitInfo] = { | ||
| if (!cleanupConfig.enabled || cleanupConfig.idleTimeoutMinutes <= 0) { | ||
| return List.empty | ||
| } | ||
|
|
||
| val now = currentTime() | ||
| val cutoff = new Timestamp(now.getTime - cleanupConfig.idleTimeoutMinutes * 60 * 1000) | ||
| val activeStatuses = Seq(Short.box(0), Short.box(1), Short.box(2)) | ||
|
|
||
| withTransaction(context) { ctx => | ||
|
Contributor
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. A single DB transaction appears to handle the full scan and deletion of pods. I recommend deleting pods outside the transaction or committing per unit, so one failure does not undo the whole batch. |
||
| val cuDao = new WorkflowComputingUnitDao(ctx.configuration()) | ||
| val userDao = new UserDao(ctx.configuration()) | ||
| ctx | ||
| .selectFrom(WORKFLOW_COMPUTING_UNIT) | ||
| .where( | ||
| WORKFLOW_COMPUTING_UNIT.TYPE | ||
| .eq(WorkflowComputingUnitTypeEnum.kubernetes) | ||
| .and(WORKFLOW_COMPUTING_UNIT.TERMINATE_TIME.isNull) | ||
| ) | ||
| .fetchInto(classOf[WorkflowComputingUnit]) | ||
| .asScala | ||
| .flatMap { unit => | ||
| val cuid = unit.getCuid | ||
| val hasActiveExecution = ctx.fetchExists( | ||
| ctx | ||
| .selectOne() | ||
| .from(WORKFLOW_EXECUTIONS) | ||
| .where( | ||
| WORKFLOW_EXECUTIONS.CUID | ||
| .eq(cuid) | ||
| .and(WORKFLOW_EXECUTIONS.STATUS.in(activeStatuses: _*)) | ||
| ) | ||
| ) | ||
| val latestUpdateTime = ctx | ||
| .select(max(WORKFLOW_EXECUTIONS.LAST_UPDATE_TIME)) | ||
| .from(WORKFLOW_EXECUTIONS) | ||
| .where(WORKFLOW_EXECUTIONS.CUID.eq(cuid)) | ||
| .fetchOne(0, classOf[Timestamp]) | ||
| val latestStartTime = ctx | ||
| .select(max(WORKFLOW_EXECUTIONS.STARTING_TIME)) | ||
| .from(WORKFLOW_EXECUTIONS) | ||
| .where(WORKFLOW_EXECUTIONS.CUID.eq(cuid)) | ||
| .fetchOne(0, classOf[Timestamp]) | ||
| val lastExecutionTime = lastComputingUnitActivityTime( | ||
| unit, | ||
| Option(latestUpdateTime), | ||
| Option(latestStartTime) | ||
| ) | ||
|
|
||
| if (shouldTerminateIdleComputingUnit(hasActiveExecution, lastExecutionTime, cutoff)) { | ||
| if (podOperations.podExists(cuid)) { | ||
| podOperations.deletePod(cuid) | ||
| } | ||
| unit.setTerminateTime(now) | ||
| unit.setTerminationReason(WorkflowComputingUnitTerminationReasonEnum.GARBAGE_COLLECTED) | ||
| cuDao.update(unit) | ||
| val owner = Option(userDao.fetchOneByUid(unit.getUid)) | ||
| Some( | ||
| new TerminatedComputingUnitInfo( | ||
| cuid = unit.getCuid, | ||
| name = unit.getName, | ||
| uid = unit.getUid, | ||
| username = owner.flatMap(u => Option(u.getName).filter(_.nonEmpty)), | ||
| reason = WorkflowComputingUnitTerminationReasonEnum.GARBAGE_COLLECTED | ||
| ) | ||
| ) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
| .toList | ||
| } | ||
| } | ||
|
|
||
| private def icebergEnvironmentVariables: Map[String, Any] = { | ||
| val base = Map[String, Any]( | ||
| EnvironmentalVariable.ENV_ICEBERG_CATALOG_TYPE -> StorageConfig.icebergCatalogType | ||
|
|
@@ -131,6 +266,14 @@ object ComputingUnitManagingResource { | |
| .get | ||
| ) | ||
|
|
||
| final class TerminatedComputingUnitInfo( | ||
| val cuid: Integer, | ||
| val name: String, | ||
| val uid: Integer, | ||
| val username: Option[String], | ||
| val reason: WorkflowComputingUnitTerminationReasonEnum | ||
| ) | ||
|
|
||
| case class WorkflowComputingUnitCreationParams( | ||
| name: String, | ||
| unitType: String, | ||
|
|
@@ -177,6 +320,7 @@ object ComputingUnitManagingResource { | |
| @Produces(Array(MediaType.APPLICATION_JSON)) | ||
| @Path("/computing-unit") | ||
| class ComputingUnitManagingResource { | ||
| private val logger = LoggerFactory.getLogger(classOf[ComputingUnitManagingService]) | ||
|
|
||
| private def getComputingUnitByCuid(ctx: DSLContext, cuid: Int): WorkflowComputingUnit = { | ||
| val wcDao = new WorkflowComputingUnitDao(ctx.configuration()) | ||
|
|
@@ -467,6 +611,12 @@ class ComputingUnitManagingResource { | |
| @Path("") | ||
| def listComputingUnits( | ||
| @Auth user: SessionUser | ||
| ): List[DashboardWorkflowComputingUnit] = | ||
| listComputingUnits(user, DefaultKubernetesPodOperations) | ||
|
|
||
| private[resource] def listComputingUnits( | ||
| user: SessionUser, | ||
| podOperations: KubernetesPodOperations | ||
| ): List[DashboardWorkflowComputingUnit] = { | ||
| withTransaction(context) { ctx => | ||
| val computingUnitDao = new WorkflowComputingUnitDao(ctx.configuration()) | ||
|
|
@@ -621,8 +771,13 @@ class ComputingUnitManagingResource { | |
| KubernetesClient.deletePod(cuid) | ||
| } | ||
|
|
||
| val terminationReason = WorkflowComputingUnitTerminationReasonEnum.USER_REQUESTED | ||
| unit.setTerminateTime(new Timestamp(System.currentTimeMillis())) | ||
| unit.setTerminationReason(terminationReason) | ||
| cuDao.update(unit) | ||
| logger.info( | ||
| s"Terminated 1 Kubernetes computing unit(s): cuid=${unit.getCuid}, name=${unit.getName}, uid=${unit.getUid}, username=${user.getName}, reason=${terminationReason.getLiteral}" | ||
| ) | ||
| } | ||
| Response.ok().build() | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This function is defined 4 times, each with different input parameters and they all call each other, please consolidate them