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

fix: Resolve race condition of Duchy claimTask in spanner implementation. #1726

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Changes from all commits
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
Expand Up @@ -144,37 +144,30 @@ class GcpSpannerComputationsDatabaseTransactor<
ownerId: String,
lockDuration: Duration,
prioritizedStages: List<StageT>,
): String? {
/** Claim a specific task represented by the results of running the above sql. */
suspend fun claimSpecificTask(result: UnclaimedTaskQueryResult<StageT>): Boolean =
databaseClient.readWriteTransaction().execute { txn ->
claim(
txn,
result.computationId,
result.computationStage,
result.nextAttempt,
result.updateTime,
ownerId,
lockDuration,
): String? =
databaseClient.readWriteTransaction().execute { txn ->
UnclaimedTasksQuery(
computationMutations.protocolEnumToLong(protocol),
prioritizedStages,
computationMutations::longValuesToComputationStageEnum,
computationMutations::computationStageEnumToLongValues,
clock.gcloudTimestamp(),
)
}
return UnclaimedTasksQuery(
computationMutations.protocolEnumToLong(protocol),
prioritizedStages,
computationMutations::longValuesToComputationStageEnum,
computationMutations::computationStageEnumToLongValues,
clock.gcloudTimestamp(),
)
.execute(databaseClient)
// First the possible tasks to claim are selected from the computations table, then for each
// item in the list we try to claim the lock in a transaction which will only succeed if the
// lock is still available. This pattern means only the item which is being updated
// would need to be locked and not every possible computation that can be worked on.
.filter { claimSpecificTask(it) }
// If the value is null, no tasks were claimed.
.firstOrNull()
?.globalId
}
.execute(txn)
.filter { result ->
claim(
txn,
result.computationId,
result.computationStage,
result.nextAttempt,
result.updateTime,
ownerId,
lockDuration,
)
}
.firstOrNull()
?.globalId
}

/**
* Tries to claim a specific computation for an owner, returning the result of the attempt. If a
Expand Down
Loading