Search in sources :

Example 11 with TitusModelAction

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))));
}
Also used : TitusModelAction(com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction) Task(com.netflix.titus.api.jobmanager.model.job.Task)

Example 12 with TitusModelAction

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);
    });
}
Also used : Trigger(com.netflix.titus.api.jobmanager.service.V3JobOperations.Trigger) DateTimeExt(com.netflix.titus.common.util.DateTimeExt) JobManagerConstants(com.netflix.titus.api.jobmanager.service.JobManagerConstants) ImmutableMap(com.google.common.collect.ImmutableMap) TitusChangeAction(com.netflix.titus.master.jobmanager.service.common.action.TitusChangeAction) Task(com.netflix.titus.api.jobmanager.model.job.Task) JobFunctions(com.netflix.titus.api.jobmanager.model.job.JobFunctions) TitusModelAction(com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction) TaskState(com.netflix.titus.api.jobmanager.model.job.TaskState) EntityHolder(com.netflix.titus.common.framework.reconciler.EntityHolder) ModelActionHolder(com.netflix.titus.common.framework.reconciler.ModelActionHolder) Pair(com.netflix.titus.common.util.tuple.Pair) Map(java.util.Map) JobManagerConfiguration(com.netflix.titus.master.jobmanager.service.JobManagerConfiguration) Preconditions(com.google.common.base.Preconditions) Clock(com.netflix.titus.common.util.time.Clock) TitusModelAction(com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction) EntityHolder(com.netflix.titus.common.framework.reconciler.EntityHolder)

Example 13 with TitusModelAction

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);
}
Also used : TitusModelAction(com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction) Task(com.netflix.titus.api.jobmanager.model.job.Task) ModelActionHolder(com.netflix.titus.common.framework.reconciler.ModelActionHolder)

Example 14 with TitusModelAction

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;
        });
    });
}
Also used : TitusModelAction(com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction) Task(com.netflix.titus.api.jobmanager.model.job.Task) ArrayList(java.util.ArrayList) EntityHolder(com.netflix.titus.common.framework.reconciler.EntityHolder) TaskStatus(com.netflix.titus.api.jobmanager.model.job.TaskStatus) ModelActionHolder(com.netflix.titus.common.framework.reconciler.ModelActionHolder)

Example 15 with TitusModelAction

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);
        }));
    });
}
Also used : Trigger(com.netflix.titus.api.jobmanager.service.V3JobOperations.Trigger) DateTimeExt(com.netflix.titus.common.util.DateTimeExt) JobModel(com.netflix.titus.api.jobmanager.model.job.JobModel) JobServiceRuntime(com.netflix.titus.master.jobmanager.service.JobServiceRuntime) TitusChangeAction(com.netflix.titus.master.jobmanager.service.common.action.TitusChangeAction) Task(com.netflix.titus.api.jobmanager.model.job.Task) CollectionsExt(com.netflix.titus.common.util.CollectionsExt) ReactorExt(com.netflix.titus.common.util.rx.ReactorExt) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Observable(rx.Observable) Pair(com.netflix.titus.common.util.tuple.Pair) JobManagerConfiguration(com.netflix.titus.master.jobmanager.service.JobManagerConfiguration) JobManagerException(com.netflix.titus.api.jobmanager.service.JobManagerException) ExceptionExt(com.netflix.titus.common.util.ExceptionExt) JobEntityHolders(com.netflix.titus.master.jobmanager.service.common.action.JobEntityHolders) JobStore(com.netflix.titus.api.jobmanager.store.JobStore) CallMetadata(com.netflix.titus.api.model.callmetadata.CallMetadata) TaskRetryers(com.netflix.titus.master.jobmanager.service.common.action.TaskRetryers) Job(com.netflix.titus.api.jobmanager.model.job.Job) TaskStatus(com.netflix.titus.api.jobmanager.model.job.TaskStatus) JobFunctions(com.netflix.titus.api.jobmanager.model.job.JobFunctions) TitusModelAction(com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction) TaskState(com.netflix.titus.api.jobmanager.model.job.TaskState) EntityHolder(com.netflix.titus.common.framework.reconciler.EntityHolder) ModelActionHolder(com.netflix.titus.common.framework.reconciler.ModelActionHolder) List(java.util.List) V3JobOperations(com.netflix.titus.api.jobmanager.service.V3JobOperations) VersionSupplier(com.netflix.titus.master.jobmanager.service.VersionSupplier) ReconciliationEngine(com.netflix.titus.common.framework.reconciler.ReconciliationEngine) VersionSuppliers(com.netflix.titus.master.jobmanager.service.VersionSuppliers) TaskAttributes(com.netflix.titus.api.jobmanager.TaskAttributes) Optional(java.util.Optional) JobManagerReconcilerEvent(com.netflix.titus.master.jobmanager.service.event.JobManagerReconcilerEvent) TitusRuntime(com.netflix.titus.common.runtime.TitusRuntime) Collections(java.util.Collections) TitusModelAction(com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction) Task(com.netflix.titus.api.jobmanager.model.job.Task) EntityHolder(com.netflix.titus.common.framework.reconciler.EntityHolder)

Aggregations

TitusModelAction (com.netflix.titus.master.jobmanager.service.common.action.TitusModelAction)20 ModelActionHolder (com.netflix.titus.common.framework.reconciler.ModelActionHolder)10 ArrayList (java.util.ArrayList)7 Task (com.netflix.titus.api.jobmanager.model.job.Task)6 Job (com.netflix.titus.api.jobmanager.model.job.Job)4 ServiceJobExt (com.netflix.titus.api.jobmanager.model.job.ext.ServiceJobExt)4 EntityHolder (com.netflix.titus.common.framework.reconciler.EntityHolder)4 JobFunctions (com.netflix.titus.api.jobmanager.model.job.JobFunctions)3 TaskState (com.netflix.titus.api.jobmanager.model.job.TaskState)3 TaskStatus (com.netflix.titus.api.jobmanager.model.job.TaskStatus)3 TitusChangeAction (com.netflix.titus.master.jobmanager.service.common.action.TitusChangeAction)3 Capacity (com.netflix.titus.api.jobmanager.model.job.Capacity)2 JobStatus (com.netflix.titus.api.jobmanager.model.job.JobStatus)2 JobManagerConstants (com.netflix.titus.api.jobmanager.service.JobManagerConstants)2 JobManagerException (com.netflix.titus.api.jobmanager.service.JobManagerException)2 V3JobOperations (com.netflix.titus.api.jobmanager.service.V3JobOperations)2 Trigger (com.netflix.titus.api.jobmanager.service.V3JobOperations.Trigger)2 JobStore (com.netflix.titus.api.jobmanager.store.JobStore)2 CallMetadata (com.netflix.titus.api.model.callmetadata.CallMetadata)2 ReconciliationEngine (com.netflix.titus.common.framework.reconciler.ReconciliationEngine)2