use of com.netflix.titus.api.jobmanager.service.V3JobOperations 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.service.V3JobOperations in project titus-control-plane by Netflix.
the class LoadBalancerTests method getMockLoadBalancerService.
public static LoadBalancerService getMockLoadBalancerService() {
final TitusRuntime runtime = TitusRuntimes.internal();
final LoadBalancerConfiguration loadBalancerConfig = mockConfiguration(5_000);
final LoadBalancerConnector connector = mock(LoadBalancerConnector.class);
final V3JobOperations v3JobOperations = mock(V3JobOperations.class);
when(v3JobOperations.observeJobs()).thenReturn(PublishSubject.create());
final LoadBalancerJobOperations loadBalancerJobOperations = new LoadBalancerJobOperations(v3JobOperations);
final LoadBalancerReconciler reconciler = mock(LoadBalancerReconciler.class);
when(reconciler.events()).thenReturn(PublishSubject.create());
final LoadBalancerStore loadBalancerStore = new InMemoryLoadBalancerStore();
final LoadBalancerJobValidator validator = new NoOpLoadBalancerJobValidator();
final TestScheduler testScheduler = Schedulers.test();
return new DefaultLoadBalancerService(runtime, loadBalancerConfig, connector, loadBalancerStore, loadBalancerJobOperations, reconciler, validator, testScheduler);
}
use of com.netflix.titus.api.jobmanager.service.V3JobOperations in project titus-control-plane by Netflix.
the class DefaultLoadBalancerServiceTest method reconciliationErrorsDontHaltOthers.
@Test
public void reconciliationErrorsDontHaltOthers() {
String jobId = UUID.randomUUID().toString();
String loadBalancerId = "lb-" + UUID.randomUUID().toString();
ThreadLocalRandom random = ThreadLocalRandom.current();
int batchSize = random.nextInt(3, 10);
defaultStubs();
LoadBalancerTests.applyValidGetJobMock(v3JobOperations, jobId);
List<Task> tasks = LoadBalancerTests.buildTasksStarted(batchSize, jobId);
when(v3JobOperations.getTasks(jobId)).thenReturn(tasks);
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();
assertTrue(service.addLoadBalancer(jobId, loadBalancerId).await(100, TimeUnit.MILLISECONDS));
assertThat(service.getJobLoadBalancers(jobId).toBlocking().first()).isEqualTo(loadBalancerId);
reconcilerEvents.onError(new RuntimeException("first error"));
testScheduler.triggerActions();
testSubscriber.assertNoErrors();
testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
reconcilerEvents.onError(new RuntimeException("second error"));
testSubscriber.assertNoErrors().assertValueCount(1);
verify(client).registerAll(eq(loadBalancerId), argThat(targets -> targets != null && targets.size() == batchSize));
verify(client, never()).deregisterAll(any(), any());
verifyReconcilerIgnore(loadBalancerId, LoadBalancerTests.ipAddresses(tasks));
}
use of com.netflix.titus.api.jobmanager.service.V3JobOperations 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");
}
use of com.netflix.titus.api.jobmanager.service.V3JobOperations in project titus-control-plane by Netflix.
the class DefaultLoadBalancerServiceTest method removeSkipLoadBalancerOperationsOnErrors.
@Test
public void removeSkipLoadBalancerOperationsOnErrors() {
String firstJobId = UUID.randomUUID().toString();
String secondJobId = UUID.randomUUID().toString();
String loadBalancerId = "lb-" + UUID.randomUUID().toString();
JobLoadBalancer firstLoadBalancer = new JobLoadBalancer(firstJobId, loadBalancerId);
JobLoadBalancer secondLoadBalancer = new JobLoadBalancer(secondJobId, loadBalancerId);
defaultStubs();
LoadBalancerTests.applyValidGetJobMock(v3JobOperations, firstJobId);
LoadBalancerTests.applyValidGetJobMock(v3JobOperations, secondJobId);
when(v3JobOperations.getTasks(firstJobId)).thenThrow(new RuntimeException());
List<Task> tasks = LoadBalancerTests.buildTasksStarted(5, secondJobId);
when(v3JobOperations.getTasks(secondJobId)).thenReturn(tasks);
assertTrue(loadBalancerStore.addOrUpdateLoadBalancer(firstLoadBalancer, JobLoadBalancer.State.ASSOCIATED).await(100, TimeUnit.MILLISECONDS));
assertTrue(loadBalancerStore.addOrUpdateLoadBalancer(secondLoadBalancer, JobLoadBalancer.State.ASSOCIATED).await(100, TimeUnit.MILLISECONDS));
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();
// first fails
assertTrue(service.removeLoadBalancer(firstJobId, loadBalancerId).await(100, TimeUnit.MILLISECONDS));
assertFalse(service.getJobLoadBalancers(firstJobId).toBlocking().getIterator().hasNext());
testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
testSubscriber.assertNoErrors().assertValueCount(0);
verify(client, never()).deregisterAll(any(), any());
verify(client, never()).registerAll(any(), any());
verifyNoReconcilerIgnore();
// second succeeds
assertTrue(service.removeLoadBalancer(secondJobId, loadBalancerId).await(100, TimeUnit.MILLISECONDS));
assertFalse(service.getJobLoadBalancers(firstJobId).toBlocking().getIterator().hasNext());
testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
testSubscriber.assertNoErrors().assertValueCount(1);
verify(client, never()).registerAll(eq(loadBalancerId), any());
verify(client).deregisterAll(eq(loadBalancerId), argThat(targets -> targets != null && targets.size() == tasks.size()));
verifyReconcilerIgnore(loadBalancerId, LoadBalancerTests.ipAddresses(tasks));
}
Aggregations