use of com.netflix.titus.api.jobmanager.model.job.Task in project titus-control-plane by Netflix.
the class MoveTaskBetweenJobsAction method apply.
@Override
public Observable<Map<String, List<ModelActionHolder>>> apply() {
return Observable.defer(() -> {
// Validate data
Job<ServiceJobExt> jobFrom = engineFrom.getReferenceView().getEntity();
Job<ServiceJobExt> jobTo = engineTo.getReferenceView().getEntity();
EntityHolder taskFromReferenceHolder = engineFrom.getReferenceView().findChildById(taskId).orElseThrow(() -> JobManagerException.taskJobMismatch(taskId, jobFrom.getId()));
if (jobFrom.getStatus().getState() != JobState.Accepted) {
throw JobManagerException.unexpectedJobState(jobTo, JobState.Accepted);
}
Capacity capacityFrom = jobFrom.getJobDescriptor().getExtensions().getCapacity();
if (capacityFrom.getMin() >= capacityFrom.getDesired()) {
throw JobManagerException.belowMinCapacity(jobFrom, 1);
}
if (jobTo.getStatus().getState() != JobState.Accepted) {
throw JobManagerException.unexpectedJobState(jobTo, JobState.Accepted);
}
Capacity capacityTo = jobTo.getJobDescriptor().getExtensions().getCapacity();
if (capacityTo.getDesired() >= capacityTo.getMax()) {
throw JobManagerException.aboveMaxCapacity(jobTo, 1);
}
Task taskFromReference = taskFromReferenceHolder.getEntity();
Optional<EntityHolder> taskFromRunningHolder = engineFrom.getRunningView().findChildById(taskId);
// Compute new model entities
// Decrement job size by 1
Job<ServiceJobExt> updatedJobFrom = nextVersion(JobFunctions.incrementJobSize(jobFrom, -1), versionSupplier);
Job<ServiceJobExt> updatedJobTo = nextVersion(JobFunctions.incrementJobSize(jobTo, 1), versionSupplier);
Task updatedReferenceTaskTo = VersionSuppliers.nextVersion(JobFunctions.moveTask(jobFrom.getId(), jobTo.getId(), taskFromReference), versionSupplier);
// Move the task
return titusStore.moveTask(updatedJobFrom, updatedJobTo, updatedReferenceTaskTo).andThen(Observable.fromCallable(() -> ImmutableMap.of(jobFrom.getId(), createModelUpdateActionsFrom(updatedJobFrom, updatedJobTo, taskFromReference, callMetadata), jobTo.getId(), createModelUpdateActionsTo(updatedJobFrom, updatedJobTo, updatedReferenceTaskTo, taskFromRunningHolder, callMetadata))));
});
}
use of com.netflix.titus.api.jobmanager.model.job.Task in project titus-control-plane by Netflix.
the class BatchDifferenceResolver method createNewTaskAction.
private Optional<TitusChangeAction> createNewTaskAction(BatchJobView refJobView, int taskIndex, Optional<EntityHolder> previousTask, List<String> unassignedIpAllocations, List<String> ebsVolumeIds) {
// Safety check
long numberOfNotFinishedTasks = refJobView.getJobHolder().getChildren().stream().filter(holder -> TaskState.isRunning(((Task) holder.getEntity()).getStatus().getState())).count();
if (numberOfNotFinishedTasks >= refJobView.getRequiredSize()) {
titusRuntime.getCodeInvariants().inconsistent("Batch job reconciler attempts to create too many tasks: jobId=%s, requiredSize=%s, current=%s", refJobView.getJob().getId(), refJobView.getRequiredSize(), numberOfNotFinishedTasks);
return Optional.empty();
}
Map<String, String> taskContext = getTaskContext(previousTask, unassignedIpAllocations, ebsVolumeIds);
JobDescriptor jobDescriptor = refJobView.getJob().getJobDescriptor();
ApplicationSLA capacityGroupDescriptor = JobManagerUtil.getCapacityGroupDescriptor(jobDescriptor, capacityGroupService);
String resourcePool = capacityGroupDescriptor.getResourcePool();
taskContext = CollectionsExt.copyAndAdd(taskContext, ImmutableMap.of(TaskAttributes.TASK_ATTRIBUTES_RESOURCE_POOL, resourcePool, TaskAttributes.TASK_ATTRIBUTES_TIER, capacityGroupDescriptor.getTier().name()));
TitusChangeAction storeAction = storeWriteRetryInterceptor.apply(createOrReplaceTaskAction(runtime, jobStore, refJobView.getJobHolder(), taskIndex, versionSupplier, clock, taskContext));
return Optional.of(storeAction);
}
use of com.netflix.titus.api.jobmanager.model.job.Task in project titus-control-plane by Netflix.
the class DifferenceResolverUtils method countActiveNotStartedTasks.
public static int countActiveNotStartedTasks(EntityHolder refJobHolder, EntityHolder runningJobHolder) {
Set<String> pendingTaskIds = new HashSet<>();
Consumer<EntityHolder> countingFun = jobHolder -> jobHolder.getChildren().forEach(taskHolder -> {
TaskState state = ((Task) taskHolder.getEntity()).getStatus().getState();
if (state != TaskState.Started && state != TaskState.Finished) {
pendingTaskIds.add(taskHolder.getId());
}
});
countingFun.accept(refJobHolder);
countingFun.accept(runningJobHolder);
return pendingTaskIds.size();
}
use of com.netflix.titus.api.jobmanager.model.job.Task in project titus-control-plane by Netflix.
the class DefaultDirectKubeApiServerIntegrator method launchTask.
@Override
public Mono<Void> launchTask(Job job, Task task) {
return Mono.fromCallable(() -> {
try {
V1Pod v1Pod = podFactory.buildV1Pod(job, task);
logger.info("creating pod: {}", formatPodEssentials(v1Pod));
logger.debug("complete pod data: {}", v1Pod);
return v1Pod;
} catch (Exception e) {
logger.error("Unable to convert job {} and task {} to pod: {}", job, task, KubeUtil.toErrorDetails(e), e);
throw new IllegalStateException("Unable to convert task to pod " + task.getId(), e);
}
}).flatMap(v1Pod -> launchPod(task, v1Pod)).subscribeOn(apiClientScheduler).timeout(Duration.ofMillis(configuration.getKubeApiClientTimeoutMs())).doOnError(TimeoutException.class, e -> metrics.launchTimeout(configuration.getKubeApiClientTimeoutMs())).ignoreElement().cast(Void.class);
}
use of com.netflix.titus.api.jobmanager.model.job.Task 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))));
}
Aggregations