use of com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction in project titus-control-plane by Netflix.
the class BasicTaskActions method updateTaskAndWriteItToStore.
/**
* Update a task, and write it to store before updating reference and store models.
* This action is used when handling user initiated updates.
*/
public static TitusChangeAction updateTaskAndWriteItToStore(String taskId, ReconciliationEngine<JobManagerReconcilerEvent> engine, Function<Task, Task> changeFunction, JobStore jobStore, Trigger trigger, String reason, VersionSupplier versionSupplier, TitusRuntime titusRuntime, CallMetadata callMetadata) {
return TitusChangeAction.newAction("updateTaskAndWriteItToStore").id(taskId).trigger(trigger).summary(reason).callMetadata(callMetadata).changeWithModelUpdates(self -> JobEntityHolders.expectTask(engine, taskId, titusRuntime).map(task -> {
Task newTask = VersionSuppliers.nextVersion(changeFunction.apply(task), versionSupplier);
TitusModelAction modelUpdate = TitusModelAction.newModelUpdate(self).taskUpdate(newTask);
return jobStore.updateTask(newTask).andThen(Observable.just(ModelActionHolder.referenceAndStore(modelUpdate)));
}).orElseGet(() -> Observable.error(JobManagerException.taskNotFound(taskId))));
}
use of com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction in project titus-control-plane by Netflix.
the class TaskTimeoutChangeActions method setTimeout.
public static TitusChangeAction setTimeout(String taskId, TaskState taskState, long timeoutMs, Clock clock) {
String tagName = STATE_TAGS.get(taskState);
Preconditions.checkArgument(tagName != null, "Timeout not tracked for state %s", taskState);
return TitusChangeAction.newAction("setTimeout").id(taskId).trigger(Trigger.Reconciler).summary("Setting timeout for task in state %s: %s", taskState, DateTimeExt.toTimeUnitString(timeoutMs)).callMetadata(JobManagerConstants.RECONCILER_CALLMETADATA.toBuilder().withCallReason("configure timeout").build()).applyModelUpdate(self -> {
TitusModelAction modelAction = TitusModelAction.newModelUpdate(self).taskMaybeUpdate(jobHolder -> jobHolder.findById(taskId).map(taskHolder -> {
EntityHolder newTaskHolder = taskHolder.addTag(tagName, clock.wallTime() + timeoutMs);
if (taskState == TaskState.KillInitiated) {
newTaskHolder = newTaskHolder.addTag(KILL_INITIATED_ATTEMPT_TAG, 0);
}
return Pair.of(jobHolder.addChild(newTaskHolder), newTaskHolder);
}));
return ModelActionHolder.running(modelAction);
});
}
use of com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction in project titus-control-plane by Netflix.
the class JobTransactionLogger method logJobModelUpdateReconcilerEvent.
private static String logJobModelUpdateReconcilerEvent(JobModelReconcilerEvent.JobModelUpdateReconcilerEvent event) {
String jobId = event.getJob().getId();
String entityId = event.getChangedEntityHolder().getId();
ModelActionHolder actionHolder = event.getModelActionHolder();
TitusModelAction action = (TitusModelAction) actionHolder.getAction();
String summary = event.getChangedEntityHolder().getEntity() instanceof Task ? action.getSummary() + "; " + taskChangeSummary(event) : action.getSummary();
return doFormat(jobId, event.getTransactionId(), "ok", "modelUpdate/" + actionHolder.getModel().name(), ((TitusModelAction) actionHolder.getAction()).getName(), event.getChangeAction().getTrigger(), toTargetName(jobId, entityId), entityId, 0, 0, event.getCallMetadata().getCallerId(), event.getCallMetadata().getCallReason(), summary);
}
use of com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction in project titus-control-plane by Netflix.
the class BasicTaskActions method launchTaskInKube.
/**
* Create pod for a task.
*/
public static TitusChangeAction launchTaskInKube(JobManagerConfiguration configuration, JobServiceRuntime runtime, ReconciliationEngine<JobManagerReconcilerEvent> engine, Job<?> job, Task task, CallMetadata callMetadata, VersionSupplier versionSupplier, TitusRuntime titusRuntime) {
return TitusChangeAction.newAction("launchTaskInKube").task(task).trigger(V3JobOperations.Trigger.Reconciler).summary("Adding task to Kube").callMetadata(callMetadata).changeWithModelUpdates(self -> {
EntityHolder taskHolder = JobEntityHolders.expectTaskHolder(engine, task.getId(), titusRuntime).orElse(null);
if (taskHolder == null) {
// This should never happen.
return Observable.just(Collections.emptyList());
}
return ReactorExt.toCompletable(runtime.getComputeProvider().launchTask(job, task).then()).andThen(Observable.fromCallable(() -> {
TaskStatus taskStatus = JobModel.newTaskStatus().withState(TaskState.Accepted).withReasonCode(TaskStatus.REASON_POD_CREATED).withReasonMessage("Created pod in Kubernetes via KubeScheduler. Needs to be scheduled on a node.").withTimestamp(titusRuntime.getClock().wallTime()).build();
Task taskWithPod = task.toBuilder().withTaskContext(CollectionsExt.copyAndAdd(task.getTaskContext(), TaskAttributes.TASK_ATTRIBUTES_POD_CREATED, "true")).withStatus(taskStatus).withStatusHistory(CollectionsExt.copyAndAdd(task.getStatusHistory(), task.getStatus())).build();
taskWithPod = VersionSuppliers.nextVersion(taskWithPod, versionSupplier);
TitusModelAction modelUpdateAction = TitusModelAction.newModelUpdate(self).taskUpdate(taskWithPod);
return ModelActionHolder.referenceAndRunning(modelUpdateAction);
})).onErrorReturn(error -> {
// Move task to the finished state after we failed to create a pod object for it.
String reasonCode = runtime.getComputeProvider().resolveReasonCode(error);
Task finishedTask = JobFunctions.changeTaskStatus(task, JobModel.newTaskStatus().withState(TaskState.Finished).withReasonCode(reasonCode).withReasonMessage("Failed to create pod: " + ExceptionExt.toMessageChain(error)).withTimestamp(titusRuntime.getClock().wallTime()).build());
finishedTask = VersionSuppliers.nextVersion(finishedTask, versionSupplier);
List<ModelActionHolder> modelActionHolders = new ArrayList<>();
modelActionHolders.add(ModelActionHolder.reference(attachRetryer(self, taskHolder, finishedTask, callMetadata, configuration, titusRuntime)));
modelActionHolders.add(ModelActionHolder.running(TitusModelAction.newModelUpdate(self).taskUpdate(finishedTask)));
return modelActionHolders;
});
});
}
use of com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction in project titus-control-plane by Netflix.
the class BasicTaskActions method writeReferenceTaskToStore.
/**
* Write updated task record to a store. If a task is completed, remove it from the scheduling service.
* This command calls {@link JobStore#updateTask(Task)}, which assumes that the task record was created already.
*/
public static TitusChangeAction writeReferenceTaskToStore(JobStore titusStore, ReconciliationEngine<JobManagerReconcilerEvent> engine, String taskId, CallMetadata callMetadata, TitusRuntime titusRuntime) {
return TitusChangeAction.newAction("writeReferenceTaskToStore").trigger(V3JobOperations.Trigger.Reconciler).id(taskId).summary("Persisting task to the store").callMetadata(callMetadata).changeWithModelUpdate(self -> {
Optional<EntityHolder> taskHolder = engine.getReferenceView().findById(taskId);
if (!taskHolder.isPresent()) {
// Should never happen
titusRuntime.getCodeInvariants().inconsistent("Reference task with id %s not found.", taskId);
return Observable.empty();
}
Task referenceTask = taskHolder.get().getEntity();
return titusStore.updateTask(referenceTask).andThen(Observable.fromCallable(() -> {
TitusModelAction modelUpdateAction = TitusModelAction.newModelUpdate(self).taskUpdate(storeRoot -> {
EntityHolder storedHolder = EntityHolder.newRoot(referenceTask.getId(), referenceTask);
return Pair.of(storeRoot.addChild(storedHolder), storedHolder);
});
return ModelActionHolder.store(modelUpdateAction);
}));
});
}
Aggregations