use of com.netflix.titus.common.util.limiter.tokenbucket.TokenBucket in project titus-control-plane by Netflix.
the class TokenBucketAdmissionController method consume.
private AdmissionControllerResponse consume(TokenBucketInstance tokenBucketInstance) {
AdmissionControllerResponse.Builder builder = AdmissionControllerResponse.newBuilder();
TokenBucket tokenBucket = tokenBucketInstance.getTokenBucket();
if (tokenBucket.tryTake()) {
builder.withAllowed(true).withReasonMessage(String.format("Consumed token of: bucketName=%s, remainingTokens=%s", tokenBucketInstance.getConfiguration().getName(), tokenBucket.getNumberOfTokens()));
} else {
builder.withAllowed(false).withReasonMessage(String.format("No more tokens: bucketName=%s, remainingTokens=%s", tokenBucketInstance.getConfiguration().getName(), tokenBucket.getNumberOfTokens()));
}
return builder.withDecisionPoint(TokenBucketAdmissionController.class.getSimpleName()).withEquivalenceGroup(tokenBucketInstance.getId()).build();
}
use of com.netflix.titus.common.util.limiter.tokenbucket.TokenBucket in project titus-control-plane by Netflix.
the class DefaultTokenBucketTest method tryTakeShouldGetToken.
@Test
public void tryTakeShouldGetToken() {
TestRefillStrategy testRefillStrategy = createTestRefillStrategy();
TokenBucket tokenBucket = createTokenBucket(10, testRefillStrategy, 0);
testRefillStrategy.setAmountToRefill(1);
boolean gotToken = tokenBucket.tryTake();
assertTrue(gotToken);
testRefillStrategy.setAmountToRefill(10);
gotToken = tokenBucket.tryTake(10);
assertTrue(gotToken);
}
use of com.netflix.titus.common.util.limiter.tokenbucket.TokenBucket in project titus-control-plane by Netflix.
the class DefaultTokenBucketTest method tryTakeShouldNotGetToken.
@Test
public void tryTakeShouldNotGetToken() {
TestRefillStrategy testRefillStrategy = createTestRefillStrategy();
TokenBucket tokenBucket = createTokenBucket(10, testRefillStrategy, 0);
boolean gotToken = tokenBucket.tryTake();
assertFalse(gotToken);
testRefillStrategy.setAmountToRefill(5);
gotToken = tokenBucket.tryTake(6);
assertFalse(gotToken);
}
use of com.netflix.titus.common.util.limiter.tokenbucket.TokenBucket in project titus-control-plane by Netflix.
the class FixedIntervalTokenBucketSupplierTest method testTokenBucketPersistsIfNotReconfigured.
@Test
public void testTokenBucketPersistsIfNotReconfigured() {
settableConfig.setProperty("junit.initialNumberOfTokens", "1");
settableConfig.setProperty("junit.capacity", "1");
settableConfig.setProperty("junit.numberOfTokensPerInterval", "0");
TokenBucket tokenBucket = supplier.get();
assertThat(tokenBucket.tryTake()).isTrue();
assertThat(tokenBucket.tryTake()).isFalse();
tokenBucket.refill(1);
assertThat(tokenBucket.tryTake()).isTrue();
assertThat(supplier.get() == tokenBucket).isTrue();
assertThat(buckets).hasSize(2);
assertThat(buckets.get(1)).isEqualTo(tokenBucket);
}
use of com.netflix.titus.common.util.limiter.tokenbucket.TokenBucket in project titus-control-plane by Netflix.
the class DifferenceResolverUtils method findTaskStateTimeouts.
/**
* Find all tasks that are stuck in a specific state. The number of {@link ChangeAction changes} will be limited
* by the {@link TokenBucket stuckInStateRateLimiter}
*/
public static List<ChangeAction> findTaskStateTimeouts(ReconciliationEngine<JobManagerReconcilerEvent> engine, JobView runningJobView, JobManagerConfiguration configuration, JobServiceRuntime runtime, JobStore jobStore, VersionSupplier versionSupplier, TokenBucket stuckInStateRateLimiter, TitusRuntime titusRuntime) {
Clock clock = titusRuntime.getClock();
List<ChangeAction> actions = new ArrayList<>();
runningJobView.getJobHolder().getChildren().forEach(taskHolder -> {
Task task = taskHolder.getEntity();
TaskState taskState = task.getStatus().getState();
if (JobFunctions.isBatchJob(runningJobView.getJob()) && taskState == TaskState.Started) {
Job<BatchJobExt> batchJob = runningJobView.getJob();
// We expect runtime limit to be always set, so this is just extra safety measure.
long runtimeLimitMs = Math.max(BatchJobExt.RUNTIME_LIMIT_MIN, batchJob.getJobDescriptor().getExtensions().getRuntimeLimitMs());
long deadline = task.getStatus().getTimestamp() + runtimeLimitMs;
if (deadline < clock.wallTime()) {
actions.add(KillInitiatedActions.reconcilerInitiatedTaskKillInitiated(engine, task, runtime, jobStore, versionSupplier, TaskStatus.REASON_RUNTIME_LIMIT_EXCEEDED, "Task running too long (runtimeLimit=" + runtimeLimitMs + "ms)", titusRuntime));
}
return;
}
TaskTimeoutChangeActions.TimeoutStatus timeoutStatus = TaskTimeoutChangeActions.getTimeoutStatus(taskHolder, clock);
switch(timeoutStatus) {
case Ignore:
case Pending:
break;
case NotSet:
long timeoutMs = -1;
switch(taskState) {
case Launched:
timeoutMs = configuration.getTaskInLaunchedStateTimeoutMs();
break;
case StartInitiated:
timeoutMs = isBatch(runningJobView.getJob()) ? configuration.getBatchTaskInStartInitiatedStateTimeoutMs() : configuration.getServiceTaskInStartInitiatedStateTimeoutMs();
break;
case KillInitiated:
timeoutMs = configuration.getTaskInKillInitiatedStateTimeoutMs();
break;
}
if (timeoutMs > 0) {
actions.add(TaskTimeoutChangeActions.setTimeout(taskHolder.getId(), task.getStatus().getState(), timeoutMs, clock));
}
break;
case TimedOut:
if (!stuckInStateRateLimiter.tryTake()) {
break;
}
if (task.getStatus().getState() == TaskState.KillInitiated) {
int attempts = TaskTimeoutChangeActions.getKillInitiatedAttempts(taskHolder) + 1;
if (attempts >= configuration.getTaskKillAttempts()) {
actions.add(BasicTaskActions.updateTaskInRunningModel(task.getId(), V3JobOperations.Trigger.Reconciler, configuration, engine, taskParam -> Optional.of(taskParam.toBuilder().withStatus(taskParam.getStatus().toBuilder().withState(TaskState.Finished).withReasonCode(TaskStatus.REASON_STUCK_IN_KILLING_STATE).withReasonMessage("stuck in " + taskState + "state").build()).build()), "TimedOut in KillInitiated state", versionSupplier, titusRuntime, JobManagerConstants.RECONCILER_CALLMETADATA.toBuilder().withCallReason("Kill initiated").build()));
} else {
actions.add(TaskTimeoutChangeActions.incrementTaskKillAttempt(task.getId(), configuration.getTaskInKillInitiatedStateTimeoutMs(), clock));
actions.add(KillInitiatedActions.reconcilerInitiatedTaskKillInitiated(engine, task, runtime, jobStore, versionSupplier, TaskStatus.REASON_STUCK_IN_KILLING_STATE, "Another kill attempt (" + (attempts + 1) + ')', titusRuntime));
}
} else {
actions.add(KillInitiatedActions.reconcilerInitiatedTaskKillInitiated(engine, task, runtime, jobStore, versionSupplier, TaskStatus.REASON_STUCK_IN_STATE, "Task stuck in " + taskState + " state", titusRuntime));
}
break;
}
});
return actions;
}
Aggregations