use of com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent in project titus-control-plane by Netflix.
the class GrpcJobReplicatorEventStreamTest method testCacheSnapshotFiltersCompletedJobs.
@Test
public void testCacheSnapshotFiltersCompletedJobs() {
Job<?> acceptedJob = JobGenerator.oneBatchJob();
BatchJobTask acceptedTask = JobGenerator.oneBatchTask().toBuilder().withJobId(acceptedJob.getId()).withStatus(TaskStatus.newBuilder().withState(TaskState.Accepted).build()).build();
Job<?> finishedJob = JobFunctions.changeJobStatus(acceptedJob, JobStatus.newBuilder().withState(JobState.Finished).build());
Task finishedTask = JobFunctions.changeTaskStatus(acceptedTask, TaskStatus.newBuilder().withState(TaskState.Finished).build());
CacheUpdater cacheUpdater = new CacheUpdater(JobSnapshotFactories.newDefault(titusRuntime), false, titusRuntime);
assertThat(cacheUpdater.onEvent(JobUpdateEvent.newJob(acceptedJob, CallMetadataConstants.UNDEFINED_CALL_METADATA))).isEmpty();
assertThat(cacheUpdater.onEvent(TaskUpdateEvent.newTask(acceptedJob, acceptedTask, CallMetadataConstants.UNDEFINED_CALL_METADATA))).isEmpty();
assertThat(cacheUpdater.onEvent(TaskUpdateEvent.taskChange(acceptedJob, finishedTask, acceptedTask, CallMetadataConstants.UNDEFINED_CALL_METADATA))).isEmpty();
assertThat(cacheUpdater.onEvent(JobUpdateEvent.jobChange(finishedJob, acceptedJob, CallMetadataConstants.UNDEFINED_CALL_METADATA))).isEmpty();
ReplicatorEvent<JobSnapshot, JobManagerEvent<?>> snapshotEvent = cacheUpdater.onEvent(JobManagerEvent.snapshotMarker()).orElse(null);
assertThat(snapshotEvent).isNotNull();
assertThat(snapshotEvent.getSnapshot().getJobMap()).isEmpty();
assertThat(snapshotEvent.getSnapshot().getTaskMap()).isEmpty();
}
use of com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent 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);
}
});
}
use of com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent in project titus-control-plane by Netflix.
the class DefaultLoadBalancerServiceTest method multipleLoadBalancersPerJob.
@Test
public void multipleLoadBalancersPerJob() {
PublishSubject<JobManagerEvent<?>> taskEvents = PublishSubject.create();
String jobId = UUID.randomUUID().toString();
String firstLoadBalancerId = "lb-" + UUID.randomUUID().toString();
String secondLoadBalancerId = "lb-" + UUID.randomUUID().toString();
int numberOfStartedTasks = 5;
when(client.registerAll(any(), any())).thenReturn(Completable.complete());
when(client.deregisterAll(any(), any())).thenReturn(Completable.complete());
when(v3JobOperations.observeJobs()).thenReturn(taskEvents);
LoadBalancerTests.applyValidGetJobMock(v3JobOperations, jobId);
List<Task> tasks = LoadBalancerTests.buildTasksStarted(numberOfStartedTasks, jobId);
Collection<LoadBalancerTargetState> firstExpectedTargets = tasks.stream().map(task -> new LoadBalancerTargetState(new LoadBalancerTarget(firstLoadBalancerId, task.getId(), task.getTaskContext().get(TaskAttributes.TASK_ATTRIBUTES_CONTAINER_IP)), REGISTERED)).collect(Collectors.toList());
Collection<LoadBalancerTargetState> secondExpectedTargets = tasks.stream().map(task -> new LoadBalancerTargetState(new LoadBalancerTarget(secondLoadBalancerId, task.getId(), task.getTaskContext().get(TaskAttributes.TASK_ATTRIBUTES_CONTAINER_IP)), REGISTERED)).collect(Collectors.toList());
when(v3JobOperations.getTasks(jobId)).thenReturn(CollectionsExt.merge(tasks, LoadBalancerTests.buildTasks(2, jobId, TaskState.StartInitiated), LoadBalancerTests.buildTasks(2, jobId, TaskState.KillInitiated), LoadBalancerTests.buildTasks(3, jobId, TaskState.Finished), LoadBalancerTests.buildTasks(1, jobId, TaskState.Disconnected)));
LoadBalancerConfiguration configuration = LoadBalancerTests.mockConfiguration(MIN_TIME_IN_QUEUE_MS);
DefaultLoadBalancerService service = new DefaultLoadBalancerService(runtime, configuration, client, loadBalancerStore, loadBalancerJobOperations, reconciler, validator, testScheduler);
AssertableSubscriber<Batch<TargetStateBatchable, String>> testSubscriber = service.events().test();
// associate two load balancers to the same job
assertTrue(service.addLoadBalancer(jobId, firstLoadBalancerId).await(100, TimeUnit.MILLISECONDS));
assertTrue(service.addLoadBalancer(jobId, secondLoadBalancerId).await(100, TimeUnit.MILLISECONDS));
assertThat(service.getJobLoadBalancers(jobId).toList().toBlocking().single()).containsOnly(firstLoadBalancerId, secondLoadBalancerId);
verify(v3JobOperations, times(2)).getTasks(jobId);
testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
// 1 batch per loadbalancer
testSubscriber.assertNoErrors().assertValueCount(2);
assertThat(loadBalancerStore.getLoadBalancerTargets(firstLoadBalancerId).collectList().block()).containsExactlyInAnyOrderElementsOf(firstExpectedTargets);
assertThat(loadBalancerStore.getLoadBalancerTargets(secondLoadBalancerId).collectList().block()).containsExactlyInAnyOrderElementsOf(secondExpectedTargets);
verify(client).registerAll(eq(firstLoadBalancerId), argThat(targets -> targets != null && targets.size() == numberOfStartedTasks));
verify(client).registerAll(eq(secondLoadBalancerId), argThat(targets -> targets != null && targets.size() == numberOfStartedTasks));
verify(client, never()).deregisterAll(eq(firstLoadBalancerId), any());
verify(client, never()).deregisterAll(eq(secondLoadBalancerId), any());
verifyReconcilerIgnore(firstLoadBalancerId, LoadBalancerTests.ipAddresses(tasks));
verifyReconcilerIgnore(secondLoadBalancerId, LoadBalancerTests.ipAddresses(tasks));
// now some more tasks are added to the job, check if both load balancers get updated
List<Task> newTasks = new ArrayList<>();
for (int i = 1; i <= numberOfStartedTasks; i++) {
String taskId = UUID.randomUUID().toString();
Task startingWithIp = ServiceJobTask.newBuilder().withJobId(jobId).withId(taskId).withStatus(TaskStatus.newBuilder().withState(TaskState.StartInitiated).build()).withTaskContext(CollectionsExt.asMap(TaskAttributes.TASK_ATTRIBUTES_CONTAINER_IP, String.format("%1$d.%1$d.%1$d.%1$d", i + numberOfStartedTasks))).build();
Task started = startingWithIp.toBuilder().withStatus(TaskStatus.newBuilder().withState(TaskState.Started).build()).build();
newTasks.add(started);
taskEvents.onNext(TaskUpdateEvent.taskChange(null, started, startingWithIp, callMetadata));
}
testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
// 2 more batches (one per load balancer)
testSubscriber.assertNoErrors().assertValueCount(4);
verify(client, times(2)).registerAll(eq(firstLoadBalancerId), argThat(targets -> targets != null && targets.size() == numberOfStartedTasks));
verify(client, times(2)).registerAll(eq(secondLoadBalancerId), argThat(targets -> targets != null && targets.size() == numberOfStartedTasks));
verify(client, never()).deregisterAll(eq(firstLoadBalancerId), any());
verify(client, never()).deregisterAll(eq(secondLoadBalancerId), any());
verifyReconcilerIgnore(firstLoadBalancerId, LoadBalancerTests.ipAddresses(newTasks));
verifyReconcilerIgnore(secondLoadBalancerId, LoadBalancerTests.ipAddresses(newTasks));
assertThat(loadBalancerStore.getLoadBalancerTargets(firstLoadBalancerId).collectList().block()).hasSize(firstExpectedTargets.size() + numberOfStartedTasks);
assertThat(loadBalancerStore.getLoadBalancerTargets(secondLoadBalancerId).collectList().block()).hasSize(secondExpectedTargets.size() + numberOfStartedTasks);
}
use of com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent in project titus-control-plane by Netflix.
the class DefaultLoadBalancerServiceTest method movedTaskOnlyTargetAssociatedWithLoadBalancer.
@Test
public void movedTaskOnlyTargetAssociatedWithLoadBalancer() {
String taskId = UUID.randomUUID().toString();
String sourceJobId = UUID.randomUUID().toString();
String targetJobId = UUID.randomUUID().toString();
String targetLoadBalancerId = "lb-" + UUID.randomUUID().toString();
PublishSubject<JobManagerEvent<?>> taskEvents = PublishSubject.create();
when(client.registerAll(any(), any())).thenReturn(Completable.complete());
when(client.deregisterAll(any(), any())).thenReturn(Completable.complete());
when(v3JobOperations.observeJobs()).thenReturn(taskEvents);
LoadBalancerTests.applyValidGetJobMock(v3JobOperations, sourceJobId);
LoadBalancerTests.applyValidGetJobMock(v3JobOperations, targetJobId);
LoadBalancerConfiguration configuration = LoadBalancerTests.mockConfiguration(MIN_TIME_IN_QUEUE_MS);
DefaultLoadBalancerService service = new DefaultLoadBalancerService(runtime, configuration, client, loadBalancerStore, loadBalancerJobOperations, reconciler, validator, testScheduler);
AssertableSubscriber<Batch<TargetStateBatchable, String>> testSubscriber = service.events().test();
assertThat(service.getJobLoadBalancers(sourceJobId).toBlocking().toIterable()).isEmpty();
assertTrue(service.addLoadBalancer(targetJobId, targetLoadBalancerId).await(100, TimeUnit.MILLISECONDS));
assertThat(service.getJobLoadBalancers(targetJobId).toBlocking().toIterable()).containsExactlyInAnyOrder(targetLoadBalancerId);
testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
testSubscriber.assertNoErrors().assertValueCount(0);
verify(client, never()).registerAll(any(), any());
verify(client, never()).deregisterAll(any(), any());
verifyNoReconcilerIgnore();
Task moved = ServiceJobTask.newBuilder().withJobId(targetJobId).withId(taskId).withStatus(TaskStatus.newBuilder().withState(TaskState.Started).build()).withTaskContext(CollectionsExt.asMap(TaskAttributes.TASK_ATTRIBUTES_CONTAINER_IP, "1.2.3.4", TaskAttributes.TASK_ATTRIBUTES_MOVED_FROM_JOB, sourceJobId)).build();
// detect the task is moved and gets registered on the target
taskEvents.onNext(TaskUpdateEvent.newTaskFromAnotherJob(null, moved, callMetadata));
testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
testSubscriber.assertNoErrors().assertValueCount(1);
verify(client).registerAll(eq(targetLoadBalancerId), argThat(set -> set.contains("1.2.3.4")));
verify(client, never()).deregisterAll(any(), any());
verifyReconcilerIgnore(targetLoadBalancerId, "1.2.3.4");
}
Aggregations