Search in sources :

Example 6 with LoadBalancerTargetState

use of com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState in project titus-control-plane by Netflix.

the class DefaultLoadBalancerServiceTest method finishedTasksGetDeregistered.

@Test
public void finishedTasksGetDeregistered() {
    String jobId = UUID.randomUUID().toString();
    String loadBalancerId = "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);
    when(v3JobOperations.getTasks(jobId)).thenReturn(Collections.emptyList());
    LoadBalancerTests.applyValidGetJobMock(v3JobOperations, jobId);
    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);
    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();
    // a task that was prematurely killed before having an IP address associated to it should be ignored
    Task noIp = ServiceJobTask.newBuilder().withJobId(jobId).withId(UUID.randomUUID().toString()).withStatus(TaskStatus.newBuilder().withState(TaskState.KillInitiated).build()).build();
    Task noIpFinished = noIp.toBuilder().withStatus(TaskStatus.newBuilder().withState(TaskState.Finished).build()).build();
    taskEvents.onNext(TaskUpdateEvent.taskChange(null, noIpFinished, noIp, callMetadata));
    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();
    // 3 state transitions to 3 different terminal events
    Task first = noIp.toBuilder().withId(UUID.randomUUID().toString()).withStatus(TaskStatus.newBuilder().withState(TaskState.Started).build()).withTaskContext(CollectionsExt.asMap(TaskAttributes.TASK_ATTRIBUTES_CONTAINER_IP, "1.1.1.1")).build();
    Task firstFinished = first.toBuilder().withStatus(TaskStatus.newBuilder().withState(TaskState.Finished).build()).build();
    LoadBalancerTargetState expectedFirstTarget = new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, firstFinished.getId(), "1.1.1.1"), DEREGISTERED);
    assertThat(loadBalancerStore.getLoadBalancerTargets(loadBalancerId).collectList().block()).doesNotContain(expectedFirstTarget);
    taskEvents.onNext(TaskUpdateEvent.taskChange(null, firstFinished, first, callMetadata));
    testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
    testSubscriber.assertNoErrors().assertValueCount(1);
    assertThat(loadBalancerStore.getLoadBalancerTargets(loadBalancerId).collectList().block()).contains(expectedFirstTarget);
    verify(client, never()).registerAll(eq(loadBalancerId), any());
    verify(client).deregisterAll(eq(loadBalancerId), argThat(set -> set.contains("1.1.1.1")));
    verifyReconcilerIgnore(loadBalancerId, "1.1.1.1");
    Task second = first.toBuilder().withId(UUID.randomUUID().toString()).withTaskContext(CollectionsExt.asMap(TaskAttributes.TASK_ATTRIBUTES_CONTAINER_IP, "2.2.2.2")).build();
    Task secondKilling = second.toBuilder().withStatus(TaskStatus.newBuilder().withState(TaskState.KillInitiated).build()).build();
    LoadBalancerTargetState expectedSecondTarget = new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, secondKilling.getId(), "2.2.2.2"), DEREGISTERED);
    assertThat(loadBalancerStore.getLoadBalancerTargets(loadBalancerId).collectList().block()).doesNotContain(expectedSecondTarget);
    taskEvents.onNext(TaskUpdateEvent.taskChange(null, secondKilling, second, callMetadata));
    testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
    testSubscriber.assertNoErrors().assertValueCount(2);
    assertThat(loadBalancerStore.getLoadBalancerTargets(loadBalancerId).collectList().block()).contains(expectedSecondTarget);
    verify(client, never()).registerAll(eq(loadBalancerId), any());
    verify(client).deregisterAll(eq(loadBalancerId), argThat(set -> set.contains("2.2.2.2")));
    verifyReconcilerIgnore(loadBalancerId, "2.2.2.2");
    Task third = first.toBuilder().withId(UUID.randomUUID().toString()).withTaskContext(CollectionsExt.asMap(TaskAttributes.TASK_ATTRIBUTES_CONTAINER_IP, "3.3.3.3")).build();
    Task thirdDisconnected = third.toBuilder().withStatus(TaskStatus.newBuilder().withState(TaskState.Disconnected).build()).build();
    LoadBalancerTargetState expectedThirdTarget = new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, thirdDisconnected.getId(), "3.3.3.3"), DEREGISTERED);
    assertThat(loadBalancerStore.getLoadBalancerTargets(loadBalancerId).collectList().block()).doesNotContain(expectedThirdTarget);
    taskEvents.onNext(TaskUpdateEvent.taskChange(null, thirdDisconnected, third, callMetadata));
    testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
    testSubscriber.assertNoErrors().assertValueCount(3);
    assertThat(loadBalancerStore.getLoadBalancerTargets(loadBalancerId).collectList().block()).contains(expectedThirdTarget);
    verify(client, never()).registerAll(eq(loadBalancerId), any());
    verify(client).deregisterAll(eq(loadBalancerId), argThat(set -> set.contains("3.3.3.3")));
    verifyReconcilerIgnore(loadBalancerId, "3.3.3.3");
}
Also used : LoadBalancerConnector(com.netflix.titus.api.connector.cloud.LoadBalancerConnector) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Task(com.netflix.titus.api.jobmanager.model.job.Task) CollectionsExt(com.netflix.titus.common.util.CollectionsExt) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Single(rx.Single) TitusRuntimes(com.netflix.titus.common.runtime.TitusRuntimes) DefaultLoadBalancerJobValidator(com.netflix.titus.api.loadbalancer.model.sanitizer.DefaultLoadBalancerJobValidator) Schedulers(rx.schedulers.Schedulers) CallMetadata(com.netflix.titus.api.model.callmetadata.CallMetadata) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget) LoadBalancerStore(com.netflix.titus.api.loadbalancer.store.LoadBalancerStore) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) LoadBalancerValidationConfiguration(com.netflix.titus.api.loadbalancer.model.sanitizer.LoadBalancerValidationConfiguration) Collection(java.util.Collection) DEREGISTERED(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget.State.DEREGISTERED) TaskStatus(com.netflix.titus.api.jobmanager.model.job.TaskStatus) Set(java.util.Set) UUID(java.util.UUID) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) Collectors(java.util.stream.Collectors) TaskState(com.netflix.titus.api.jobmanager.model.job.TaskState) List(java.util.List) TaskUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent) Assert.assertFalse(org.junit.Assert.assertFalse) PublishSubject(rx.subjects.PublishSubject) Mockito.mock(org.mockito.Mockito.mock) Completable(rx.Completable) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) LoadBalancerJobValidator(com.netflix.titus.api.loadbalancer.model.sanitizer.LoadBalancerJobValidator) TestScheduler(rx.schedulers.TestScheduler) ArrayList(java.util.ArrayList) AssertableSubscriber(rx.observers.AssertableSubscriber) ServiceJobTask(com.netflix.titus.api.jobmanager.model.job.ServiceJobTask) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) JobManagerException(com.netflix.titus.api.jobmanager.service.JobManagerException) Before(org.junit.Before) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) LoadBalancer(com.netflix.titus.api.connector.cloud.LoadBalancer) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) V3JobOperations(com.netflix.titus.api.jobmanager.service.V3JobOperations) Batch(com.netflix.titus.common.util.rx.batch.Batch) Mockito.never(org.mockito.Mockito.never) TaskAttributes(com.netflix.titus.api.jobmanager.TaskAttributes) InMemoryLoadBalancerStore(com.netflix.titus.runtime.store.v3.memory.InMemoryLoadBalancerStore) REGISTERED(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget.State.REGISTERED) TitusRuntime(com.netflix.titus.common.runtime.TitusRuntime) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Task(com.netflix.titus.api.jobmanager.model.job.Task) ServiceJobTask(com.netflix.titus.api.jobmanager.model.job.ServiceJobTask) Batch(com.netflix.titus.common.util.rx.batch.Batch) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget) Test(org.junit.Test)

Example 7 with LoadBalancerTargetState

use of com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState in project titus-control-plane by Netflix.

the class DefaultLoadBalancerServiceTest method addLoadBalancerRegistersTasks.

@Test
public void addLoadBalancerRegistersTasks() {
    String jobId = UUID.randomUUID().toString();
    String loadBalancerId = "lb-" + UUID.randomUUID().toString();
    defaultStubs();
    LoadBalancerTests.applyValidGetJobMock(v3JobOperations, jobId);
    List<Task> tasks = LoadBalancerTests.buildTasksStarted(5, jobId);
    Collection<LoadBalancerTargetState> expectedTargets = tasks.stream().map(task -> new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, 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();
    assertTrue(service.addLoadBalancer(jobId, loadBalancerId).await(100, TimeUnit.MILLISECONDS));
    assertThat(service.getJobLoadBalancers(jobId).toBlocking().first()).isEqualTo(loadBalancerId);
    verify(v3JobOperations).getTasks(jobId);
    testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
    testSubscriber.assertNoErrors().assertValueCount(1);
    assertThat(loadBalancerStore.getLoadBalancerTargets(loadBalancerId).collectList().block()).containsExactlyInAnyOrderElementsOf(expectedTargets);
    verify(client).registerAll(eq(loadBalancerId), argThat(targets -> targets != null && targets.size() == tasks.size()));
    verify(client, never()).deregisterAll(eq(loadBalancerId), any());
    verifyReconcilerIgnore(loadBalancerId, LoadBalancerTests.ipAddresses(tasks));
}
Also used : LoadBalancerConnector(com.netflix.titus.api.connector.cloud.LoadBalancerConnector) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Task(com.netflix.titus.api.jobmanager.model.job.Task) CollectionsExt(com.netflix.titus.common.util.CollectionsExt) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Single(rx.Single) TitusRuntimes(com.netflix.titus.common.runtime.TitusRuntimes) DefaultLoadBalancerJobValidator(com.netflix.titus.api.loadbalancer.model.sanitizer.DefaultLoadBalancerJobValidator) Schedulers(rx.schedulers.Schedulers) CallMetadata(com.netflix.titus.api.model.callmetadata.CallMetadata) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget) LoadBalancerStore(com.netflix.titus.api.loadbalancer.store.LoadBalancerStore) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) LoadBalancerValidationConfiguration(com.netflix.titus.api.loadbalancer.model.sanitizer.LoadBalancerValidationConfiguration) Collection(java.util.Collection) DEREGISTERED(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget.State.DEREGISTERED) TaskStatus(com.netflix.titus.api.jobmanager.model.job.TaskStatus) Set(java.util.Set) UUID(java.util.UUID) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) Collectors(java.util.stream.Collectors) TaskState(com.netflix.titus.api.jobmanager.model.job.TaskState) List(java.util.List) TaskUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent) Assert.assertFalse(org.junit.Assert.assertFalse) PublishSubject(rx.subjects.PublishSubject) Mockito.mock(org.mockito.Mockito.mock) Completable(rx.Completable) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) LoadBalancerJobValidator(com.netflix.titus.api.loadbalancer.model.sanitizer.LoadBalancerJobValidator) TestScheduler(rx.schedulers.TestScheduler) ArrayList(java.util.ArrayList) AssertableSubscriber(rx.observers.AssertableSubscriber) ServiceJobTask(com.netflix.titus.api.jobmanager.model.job.ServiceJobTask) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) JobManagerException(com.netflix.titus.api.jobmanager.service.JobManagerException) Before(org.junit.Before) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) LoadBalancer(com.netflix.titus.api.connector.cloud.LoadBalancer) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) V3JobOperations(com.netflix.titus.api.jobmanager.service.V3JobOperations) Batch(com.netflix.titus.common.util.rx.batch.Batch) Mockito.never(org.mockito.Mockito.never) TaskAttributes(com.netflix.titus.api.jobmanager.TaskAttributes) InMemoryLoadBalancerStore(com.netflix.titus.runtime.store.v3.memory.InMemoryLoadBalancerStore) REGISTERED(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget.State.REGISTERED) TitusRuntime(com.netflix.titus.common.runtime.TitusRuntime) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Task(com.netflix.titus.api.jobmanager.model.job.Task) ServiceJobTask(com.netflix.titus.api.jobmanager.model.job.ServiceJobTask) Batch(com.netflix.titus.common.util.rx.batch.Batch) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget) Test(org.junit.Test)

Example 8 with LoadBalancerTargetState

use of com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState in project titus-control-plane by Netflix.

the class DefaultLoadBalancerReconciler method updatesForRemovedLoadBalancer.

private ReconciliationUpdates updatesForRemovedLoadBalancer(LoadBalancerWithKnownTargets loadBalancer, List<JobLoadBalancerState> associations) {
    logger.warn("Load balancer is gone, ignoring its associations (marking them to be GCed later), and removing known state for its targets: {}", loadBalancer.current.getId());
    for (JobLoadBalancerState association : associations) {
        if (association.isStateAssociated()) {
            logger.info("Marking association as orphan: {}", association.getJobLoadBalancer());
            markedAsOrphan.add(association.getJobLoadBalancer());
        }
    }
    // we will force deregistration of everything that was marked as REGISTERED to force its state to be updated
    Map<Boolean, Set<LoadBalancerTarget>> registeredOrNot = loadBalancer.knownTargets.stream().collect(partitioningBy(target -> target.getState().equals(State.REGISTERED), mapping(LoadBalancerTargetState::getLoadBalancerTarget, Collectors.toSet())));
    // all REGISTERED
    Set<LoadBalancerTarget> toDeregister = registeredOrNot.get(true);
    // all DEREGISTERED
    Set<LoadBalancerTarget> toRemove = registeredOrNot.get(false);
    return new ReconciliationUpdates(loadBalancer.current.getId(), Collections.emptySet(), toDeregister, toRemove);
}
Also used : Completable(rx.Completable) Collectors.partitioningBy(java.util.stream.Collectors.partitioningBy) LoadBalancerConnector(com.netflix.titus.api.connector.cloud.LoadBalancerConnector) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) CollectionsExt(com.netflix.titus.common.util.CollectionsExt) Counter(com.netflix.spectator.api.Counter) LoggerFactory(org.slf4j.LoggerFactory) ContinuousSubscriptionMetrics(com.netflix.titus.common.util.spectator.ContinuousSubscriptionMetrics) ReactorExt(com.netflix.titus.common.util.rx.ReactorExt) Supplier(java.util.function.Supplier) Id(com.netflix.spectator.api.Id) JobNotFound(com.netflix.titus.api.jobmanager.service.JobManagerException.ErrorCode.JobNotFound) ConcurrentMap(java.util.concurrent.ConcurrentMap) Observable(rx.Observable) SpectatorExt(com.netflix.titus.common.util.spectator.SpectatorExt) Duration(java.time.Duration) Map(java.util.Map) Collectors.mapping(java.util.stream.Collectors.mapping) Schedulers(rx.schedulers.Schedulers) JobManagerException(com.netflix.titus.api.jobmanager.service.JobManagerException) BasicTag(com.netflix.spectator.api.BasicTag) State(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget.State) METRIC_LOADBALANCER(com.netflix.titus.master.MetricConstants.METRIC_LOADBALANCER) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget) LoadBalancerStore(com.netflix.titus.api.loadbalancer.store.LoadBalancerStore) Logger(org.slf4j.Logger) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) Priority(com.netflix.titus.common.util.rx.batch.Priority) Tag(com.netflix.spectator.api.Tag) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Instant(java.time.Instant) LoadBalancer(com.netflix.titus.api.connector.cloud.LoadBalancer) Scheduler(rx.Scheduler) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Registry(com.netflix.spectator.api.Registry) PolledMeter(com.netflix.spectator.api.patterns.PolledMeter) Collections(java.util.Collections) ObservableExt(com.netflix.titus.common.util.rx.ObservableExt) Set(java.util.Set) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget)

Example 9 with LoadBalancerTargetState

use of com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState in project titus-control-plane by Netflix.

the class CassandraLoadBalancerStoreTest method testOnlyDeregisteredTargetsAreRemoved.

@Test
public void testOnlyDeregisteredTargetsAreRemoved() throws Exception {
    Map<LoadBalancerTarget, LoadBalancerTarget.State> targets = ImmutableMap.of(new LoadBalancerTarget("lb-1", "task1", "1.1.1.1"), LoadBalancerTarget.State.REGISTERED, new LoadBalancerTarget("lb-1", "task2", "2.2.2.2"), LoadBalancerTarget.State.DEREGISTERED, new LoadBalancerTarget("lb-2", "task1", "1.1.1.1"), LoadBalancerTarget.State.DEREGISTERED, new LoadBalancerTarget("lb-2", "task3", "3.3.3.3"), LoadBalancerTarget.State.DEREGISTERED);
    loadTestData(new TestData(Collections.emptyMap(), targets));
    CassandraLoadBalancerStore store = getInitdStore();
    store.removeDeregisteredTargets(targets.keySet()).block(Duration.ofSeconds(10));
    List<LoadBalancerTargetState> targets1 = store.getLoadBalancerTargets("lb-1").collectList().block(Duration.ofSeconds(5));
    assertThat(targets1).hasSize(1);
    assertThat(targets1.get(0).getIpAddress()).isEqualTo("1.1.1.1");
    List<LoadBalancerTargetState> targets2 = store.getLoadBalancerTargets("lb-2").collectList().block(Duration.ofSeconds(5));
    assertThat(targets2).isEmpty();
}
Also used : JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget) Test(org.junit.Test) IntegrationNotParallelizableTest(com.netflix.titus.testkit.junit.category.IntegrationNotParallelizableTest)

Example 10 with LoadBalancerTargetState

use of com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState 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);
}
Also used : LoadBalancerConnector(com.netflix.titus.api.connector.cloud.LoadBalancerConnector) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Task(com.netflix.titus.api.jobmanager.model.job.Task) CollectionsExt(com.netflix.titus.common.util.CollectionsExt) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Single(rx.Single) TitusRuntimes(com.netflix.titus.common.runtime.TitusRuntimes) DefaultLoadBalancerJobValidator(com.netflix.titus.api.loadbalancer.model.sanitizer.DefaultLoadBalancerJobValidator) Schedulers(rx.schedulers.Schedulers) CallMetadata(com.netflix.titus.api.model.callmetadata.CallMetadata) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget) LoadBalancerStore(com.netflix.titus.api.loadbalancer.store.LoadBalancerStore) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) LoadBalancerValidationConfiguration(com.netflix.titus.api.loadbalancer.model.sanitizer.LoadBalancerValidationConfiguration) Collection(java.util.Collection) DEREGISTERED(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget.State.DEREGISTERED) TaskStatus(com.netflix.titus.api.jobmanager.model.job.TaskStatus) Set(java.util.Set) UUID(java.util.UUID) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) Collectors(java.util.stream.Collectors) TaskState(com.netflix.titus.api.jobmanager.model.job.TaskState) List(java.util.List) TaskUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent) Assert.assertFalse(org.junit.Assert.assertFalse) PublishSubject(rx.subjects.PublishSubject) Mockito.mock(org.mockito.Mockito.mock) Completable(rx.Completable) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) LoadBalancerJobValidator(com.netflix.titus.api.loadbalancer.model.sanitizer.LoadBalancerJobValidator) TestScheduler(rx.schedulers.TestScheduler) ArrayList(java.util.ArrayList) AssertableSubscriber(rx.observers.AssertableSubscriber) ServiceJobTask(com.netflix.titus.api.jobmanager.model.job.ServiceJobTask) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) JobManagerException(com.netflix.titus.api.jobmanager.service.JobManagerException) Before(org.junit.Before) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) LoadBalancer(com.netflix.titus.api.connector.cloud.LoadBalancer) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) V3JobOperations(com.netflix.titus.api.jobmanager.service.V3JobOperations) Batch(com.netflix.titus.common.util.rx.batch.Batch) Mockito.never(org.mockito.Mockito.never) TaskAttributes(com.netflix.titus.api.jobmanager.TaskAttributes) InMemoryLoadBalancerStore(com.netflix.titus.runtime.store.v3.memory.InMemoryLoadBalancerStore) REGISTERED(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget.State.REGISTERED) TitusRuntime(com.netflix.titus.common.runtime.TitusRuntime) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Task(com.netflix.titus.api.jobmanager.model.job.Task) ServiceJobTask(com.netflix.titus.api.jobmanager.model.job.ServiceJobTask) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) ArrayList(java.util.ArrayList) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget) Batch(com.netflix.titus.common.util.rx.batch.Batch) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) Test(org.junit.Test)

Aggregations

LoadBalancerTarget (com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget)13 LoadBalancerTargetState (com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState)13 JobLoadBalancer (com.netflix.titus.api.loadbalancer.model.JobLoadBalancer)12 Test (org.junit.Test)12 LoadBalancer (com.netflix.titus.api.connector.cloud.LoadBalancer)11 JobLoadBalancerState (com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState)11 Task (com.netflix.titus.api.jobmanager.model.job.Task)8 Collections (java.util.Collections)8 List (java.util.List)8 TimeUnit (java.util.concurrent.TimeUnit)8 Collectors (java.util.stream.Collectors)8 LoadBalancerConnector (com.netflix.titus.api.connector.cloud.LoadBalancerConnector)7 JobManagerException (com.netflix.titus.api.jobmanager.service.JobManagerException)7 LoadBalancerStore (com.netflix.titus.api.loadbalancer.store.LoadBalancerStore)7 CollectionsExt (com.netflix.titus.common.util.CollectionsExt)7 Set (java.util.Set)7 UUID (java.util.UUID)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 Mockito.mock (org.mockito.Mockito.mock)7 Mockito.when (org.mockito.Mockito.when)7