Search in sources :

Example 6 with TaskUpdateEvent

use of com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent in project titus-control-plane by Netflix.

the class JobUtil method loadActiveJobsAndTasks.

public static Pair<Map<String, Job>, Map<String, Map<String, Task>>> loadActiveJobsAndTasks(CommandContext context) {
    Map<String, Job> activeJobs = new HashMap<>();
    Map<String, Map<String, Task>> activeTasks = new HashMap<>();
    Iterator<JobManagerEvent<?>> it = context.getJobManagementClient().observeJobs(Collections.emptyMap()).toIterable().iterator();
    while (it.hasNext()) {
        JobManagerEvent<?> event = it.next();
        if (event instanceof JobUpdateEvent) {
            JobUpdateEvent je = (JobUpdateEvent) event;
            Job job = je.getCurrent();
            if (job.getStatus().getState() == JobState.Accepted) {
                activeJobs.put(job.getId(), job);
            }
        } else if (event instanceof TaskUpdateEvent) {
            TaskUpdateEvent te = (TaskUpdateEvent) event;
            Task task = te.getCurrent();
            if (activeJobs.containsKey(task.getJobId())) {
                activeTasks.computeIfAbsent(task.getJobId(), j -> new HashMap<>()).put(task.getId(), task);
            }
        } else if (event.equals(JobManagerEvent.snapshotMarker())) {
            break;
        }
    }
    return Pair.of(activeJobs, activeTasks);
}
Also used : Task(com.netflix.titus.api.jobmanager.model.job.Task) HashMap(java.util.HashMap) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) JobUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.JobUpdateEvent) Job(com.netflix.titus.api.jobmanager.model.job.Job) Map(java.util.Map) HashMap(java.util.HashMap) TaskUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent)

Example 7 with TaskUpdateEvent

use of com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent in project titus-control-plane by Netflix.

the class StreamDataReplicatorPerf method main.

public static void main(String[] args) throws InterruptedException {
    TitusRuntime titusRuntime = TitusRuntimes.internal();
    JobManagementClient client = Mockito.mock(JobManagementClient.class);
    JobConnectorConfiguration configuration = Mockito.mock(JobConnectorConfiguration.class);
    Mockito.when(client.observeJobs(ArgumentMatchers.any())).thenAnswer(invocation -> Flux.defer(() -> {
        JobManagerEvent jobUpdateEvent = JobUpdateEvent.newJob(JOB, JobManagerConstants.GRPC_REPLICATOR_CALL_METADATA);
        JobManagerEvent taskUpdateEvent = TaskUpdateEvent.newTask(JOB, TASK, JobManagerConstants.GRPC_REPLICATOR_CALL_METADATA);
        return Flux.just(jobUpdateEvent, JobManagerEvent.snapshotMarker()).concatWith(Flux.interval(Duration.ofSeconds(1)).take(1).map(tick -> taskUpdateEvent)).concatWith(Flux.interval(Duration.ofSeconds(1)).take(1).flatMap(tick -> Flux.error(new RuntimeException("Simulated error"))));
    }));
    JobDataReplicator replicator = new JobDataReplicatorProvider(configuration, client, JobSnapshotFactories.newDefault(titusRuntime), titusRuntime).get();
    replicator.events().subscribe(System.out::println);
    Thread.sleep(3600_000);
}
Also used : JobManagerConstants(com.netflix.titus.api.jobmanager.service.JobManagerConstants) Job(com.netflix.titus.api.jobmanager.model.job.Job) ArgumentMatchers(org.mockito.ArgumentMatchers) JobUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.JobUpdateEvent) JobSnapshotFactories(com.netflix.titus.runtime.connector.jobmanager.snapshot.JobSnapshotFactories) Task(com.netflix.titus.api.jobmanager.model.job.Task) JobDataReplicator(com.netflix.titus.runtime.connector.jobmanager.JobDataReplicator) JobGenerator(com.netflix.titus.testkit.model.job.JobGenerator) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) Mockito(org.mockito.Mockito) Flux(reactor.core.publisher.Flux) TaskUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent) TitusRuntimes(com.netflix.titus.common.runtime.TitusRuntimes) JobConnectorConfiguration(com.netflix.titus.runtime.connector.jobmanager.JobConnectorConfiguration) JobDataReplicatorProvider(com.netflix.titus.runtime.connector.jobmanager.replicator.JobDataReplicatorProvider) Duration(java.time.Duration) JobManagementClient(com.netflix.titus.runtime.connector.jobmanager.JobManagementClient) TitusRuntime(com.netflix.titus.common.runtime.TitusRuntime) JobDataReplicator(com.netflix.titus.runtime.connector.jobmanager.JobDataReplicator) JobDataReplicatorProvider(com.netflix.titus.runtime.connector.jobmanager.replicator.JobDataReplicatorProvider) JobConnectorConfiguration(com.netflix.titus.runtime.connector.jobmanager.JobConnectorConfiguration) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) JobManagementClient(com.netflix.titus.runtime.connector.jobmanager.JobManagementClient) TitusRuntime(com.netflix.titus.common.runtime.TitusRuntime)

Example 8 with TaskUpdateEvent

use of com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent in project titus-control-plane by Netflix.

the class DefaultV3JobOperations method toJobManagerEvent.

private List<JobManagerEvent<?>> toJobManagerEvent(Predicate<Pair<Job<?>, List<Task>>> jobsPredicate, Predicate<Pair<Job<?>, Task>> tasksPredicate, boolean withCheckpoints, JobManagerReconcilerEvent event) {
    if (event instanceof JobCheckpointReconcilerEvent) {
        if (withCheckpoints) {
            JobCheckpointReconcilerEvent checkpoint = (JobCheckpointReconcilerEvent) event;
            return Collections.singletonList(JobManagerEvent.keepAliveEvent(checkpoint.getTimestampNano()));
        }
        return Collections.emptyList();
    }
    if (event instanceof JobNewModelReconcilerEvent) {
        JobNewModelReconcilerEvent newModelEvent = (JobNewModelReconcilerEvent) event;
        return toNewJobUpdateEvent(newModelEvent, jobsPredicate);
    }
    if (!(event instanceof JobModelUpdateReconcilerEvent)) {
        return Collections.emptyList();
    }
    JobModelUpdateReconcilerEvent modelUpdateEvent = (JobModelUpdateReconcilerEvent) event;
    if (modelUpdateEvent.getModelActionHolder().getModel() != Model.Reference) {
        return Collections.emptyList();
    }
    if (modelUpdateEvent.getChangedEntityHolder().getEntity() instanceof Job) {
        // We have to emit for this case both task archived event followed by job update event.
        if (modelUpdateEvent.getChangeAction().getTrigger() == Trigger.ReconcilerServiceTaskRemoved) {
            Task archivedTask = modelUpdateEvent.getChangeAction().getTask().orElse(null);
            if (archivedTask != null) {
                Job<?> job = modelUpdateEvent.getJob();
                TaskUpdateEvent archiveEvent = TaskUpdateEvent.taskArchived(job, archivedTask, modelUpdateEvent.getCallMetadata());
                List<JobManagerEvent<?>> events = new ArrayList<>();
                events.add(archiveEvent);
                events.addAll(toJobUpdateEvent(modelUpdateEvent, jobsPredicate));
                return events;
            }
        }
        return toJobUpdateEvent(modelUpdateEvent, jobsPredicate);
    }
    return toTaskUpdateEvent(modelUpdateEvent, tasksPredicate);
}
Also used : JobCheckpointReconcilerEvent(com.netflix.titus.master.jobmanager.service.event.JobCheckpointReconcilerEvent) JobModelUpdateReconcilerEvent(com.netflix.titus.master.jobmanager.service.event.JobModelReconcilerEvent.JobModelUpdateReconcilerEvent) Task(com.netflix.titus.api.jobmanager.model.job.Task) JobNewModelReconcilerEvent(com.netflix.titus.master.jobmanager.service.event.JobModelReconcilerEvent.JobNewModelReconcilerEvent) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) ArrayList(java.util.ArrayList) Job(com.netflix.titus.api.jobmanager.model.job.Job) TaskUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent)

Example 9 with TaskUpdateEvent

use of com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent in project titus-control-plane by Netflix.

the class TaskHelpers method isStateTransition.

static boolean isStateTransition(TaskUpdateEvent event) {
    final Task currentTask = event.getCurrentTask();
    final Optional<Task> previousTask = event.getPreviousTask();
    boolean identical = previousTask.map(previous -> previous == currentTask).orElse(false);
    return !identical && previousTask.map(previous -> !previous.getStatus().getState().equals(currentTask.getStatus().getState())).orElse(false);
}
Also used : TaskUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent) Logger(org.slf4j.Logger) TaskAttributes(com.netflix.titus.api.jobmanager.TaskAttributes) Collection(java.util.Collection) Task(com.netflix.titus.api.jobmanager.model.job.Task) LoggerFactory(org.slf4j.LoggerFactory) Optional(java.util.Optional) Set(java.util.Set) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) TaskState(com.netflix.titus.api.jobmanager.model.job.TaskState) Task(com.netflix.titus.api.jobmanager.model.job.Task)

Example 10 with TaskUpdateEvent

use of com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent in project titus-control-plane by Netflix.

the class GrpcJobReplicatorEventStreamTest method testCacheTaskMove.

@Test
public void testCacheTaskMove() {
    Pair<Job, List<Task>> pair = jobServiceStub.createJobAndTasks(SERVICE_JOB);
    Job target = jobServiceStub.createJob(SERVICE_JOB);
    Task task = pair.getRight().get(0);
    String sourceJobId = pair.getLeft().getId();
    String targetJobId = target.getId();
    List<ReplicatorEvent<JobSnapshot, JobManagerEvent<?>>> events = new ArrayList<>();
    newConnectVerifier().assertNext(next -> assertThat(next.getSnapshot().getTaskMap().values()).allSatisfy(t -> assertThat(t.getStatus().getState()).isEqualTo(TaskState.Accepted))).then(() -> jobServiceStub.moveTaskToState(task, TaskState.Started)).assertNext(next -> {
        JobSnapshot snapshot = next.getSnapshot();
        Optional<Pair<Job<?>, Task>> taskOpt = snapshot.findTaskById(task.getId());
        assertThat(taskOpt).isPresent();
        assertThat(taskOpt.get().getRight().getStatus().getState()).isEqualTo(TaskState.Started);
        assertThat(snapshot.getTasks(sourceJobId)).containsKey(task.getId());
    }).then(() -> jobServiceStub.getJobOperations().moveServiceTask(sourceJobId, targetJobId, task.getId(), CallMetadata.newBuilder().withCallerId("Test").withCallReason("testing").build()).test().awaitTerminalEvent().assertNoErrors()).recordWith(() -> events).thenConsumeWhile(next -> {
        JobManagerEvent<?> trigger = next.getTrigger();
        if (!(trigger instanceof TaskUpdateEvent)) {
            return true;
        }
        TaskUpdateEvent taskUpdateEvent = (TaskUpdateEvent) trigger;
        return !taskUpdateEvent.isMovedFromAnotherJob();
    }).thenCancel().verify();
    assertThat(events).hasSize(3);
    events.stream().map(ReplicatorEvent::getTrigger).forEach(jobManagerEvent -> {
        if (jobManagerEvent instanceof JobUpdateEvent) {
            JobUpdateEvent jobUpdateEvent = (JobUpdateEvent) jobManagerEvent;
            String eventJobId = jobUpdateEvent.getCurrent().getId();
            assertThat(eventJobId).isIn(sourceJobId, targetJobId);
        } else if (jobManagerEvent instanceof TaskUpdateEvent) {
            TaskUpdateEvent taskUpdateEvent = (TaskUpdateEvent) jobManagerEvent;
            assertThat(taskUpdateEvent.isMovedFromAnotherJob()).isTrue();
            assertThat(taskUpdateEvent.getCurrentJob().getId()).isEqualTo(targetJobId);
            assertThat(taskUpdateEvent.getCurrent().getJobId()).isEqualTo(targetJobId);
            assertThat(taskUpdateEvent.getCurrent().getTaskContext().get(TaskAttributes.TASK_ATTRIBUTES_MOVED_FROM_JOB)).isEqualTo(sourceJobId);
        } else {
            fail("Unexpected event type: %s", jobManagerEvent);
        }
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) JobDescriptorGenerator(com.netflix.titus.testkit.model.job.JobDescriptorGenerator) BatchJobTask(com.netflix.titus.api.jobmanager.model.job.BatchJobTask) StepVerifier(reactor.test.StepVerifier) Task(com.netflix.titus.api.jobmanager.model.job.Task) CallMetadataConstants(com.netflix.titus.api.model.callmetadata.CallMetadataConstants) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) ReactorExt(com.netflix.titus.common.util.rx.ReactorExt) ArrayList(java.util.ArrayList) JobStatus(com.netflix.titus.api.jobmanager.model.job.JobStatus) ReplicatorEvent(com.netflix.titus.runtime.connector.common.replicator.ReplicatorEvent) JobSnapshot(com.netflix.titus.runtime.connector.jobmanager.snapshot.JobSnapshot) Pair(com.netflix.titus.common.util.tuple.Pair) CacheUpdater(com.netflix.titus.runtime.connector.jobmanager.replicator.GrpcJobReplicatorEventStream.CacheUpdater) TitusRuntimes(com.netflix.titus.common.runtime.TitusRuntimes) Duration(java.time.Duration) JobState(com.netflix.titus.api.jobmanager.model.job.JobState) Schedulers(reactor.core.scheduler.Schedulers) CallMetadata(com.netflix.titus.api.model.callmetadata.CallMetadata) Before(org.junit.Before) JobDescriptor(com.netflix.titus.api.jobmanager.model.job.JobDescriptor) Job(com.netflix.titus.api.jobmanager.model.job.Job) JobUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.JobUpdateEvent) JobSnapshotFactories(com.netflix.titus.runtime.connector.jobmanager.snapshot.JobSnapshotFactories) TaskStatus(com.netflix.titus.api.jobmanager.model.job.TaskStatus) JobGenerator(com.netflix.titus.testkit.model.job.JobGenerator) JobFunctions(com.netflix.titus.api.jobmanager.model.job.JobFunctions) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) TaskState(com.netflix.titus.api.jobmanager.model.job.TaskState) Capacity(com.netflix.titus.api.jobmanager.model.job.Capacity) Mockito(org.mockito.Mockito) List(java.util.List) TaskUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent) Assertions.fail(org.assertj.core.api.Assertions.fail) TaskAttributes(com.netflix.titus.api.jobmanager.TaskAttributes) JobConnectorConfiguration(com.netflix.titus.runtime.connector.jobmanager.JobConnectorConfiguration) JobComponentStub(com.netflix.titus.testkit.model.job.JobComponentStub) DataReplicatorMetrics(com.netflix.titus.runtime.connector.common.replicator.DataReplicatorMetrics) Optional(java.util.Optional) JobManagementClient(com.netflix.titus.runtime.connector.jobmanager.JobManagementClient) TitusRuntime(com.netflix.titus.common.runtime.TitusRuntime) Mockito.mock(org.mockito.Mockito.mock) BatchJobTask(com.netflix.titus.api.jobmanager.model.job.BatchJobTask) Task(com.netflix.titus.api.jobmanager.model.job.Task) Optional(java.util.Optional) ArrayList(java.util.ArrayList) ReplicatorEvent(com.netflix.titus.runtime.connector.common.replicator.ReplicatorEvent) JobUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.JobUpdateEvent) ArrayList(java.util.ArrayList) List(java.util.List) JobSnapshot(com.netflix.titus.runtime.connector.jobmanager.snapshot.JobSnapshot) Job(com.netflix.titus.api.jobmanager.model.job.Job) TaskUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent) Test(org.junit.Test)

Aggregations

TaskUpdateEvent (com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent)16 Task (com.netflix.titus.api.jobmanager.model.job.Task)11 Job (com.netflix.titus.api.jobmanager.model.job.Job)8 JobUpdateEvent (com.netflix.titus.api.jobmanager.model.job.event.JobUpdateEvent)7 JobManagerEvent (com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent)5 JobChangeNotification (com.netflix.titus.grpc.protogen.JobChangeNotification)4 List (java.util.List)4 Test (org.junit.Test)4 TaskStatus (com.netflix.titus.api.jobmanager.model.job.TaskStatus)3 ExtTestSubscriber (com.netflix.titus.testkit.rx.ExtTestSubscriber)3 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 TaskAttributes (com.netflix.titus.api.jobmanager.TaskAttributes)2 BatchJobTask (com.netflix.titus.api.jobmanager.model.job.BatchJobTask)2 TaskState (com.netflix.titus.api.jobmanager.model.job.TaskState)2 TitusRuntime (com.netflix.titus.common.runtime.TitusRuntime)2 TitusRuntimes (com.netflix.titus.common.runtime.TitusRuntimes)2 ObserveJobsQuery (com.netflix.titus.grpc.protogen.ObserveJobsQuery)2 JobConnectorConfiguration (com.netflix.titus.runtime.connector.jobmanager.JobConnectorConfiguration)2 JobManagementClient (com.netflix.titus.runtime.connector.jobmanager.JobManagementClient)2