Search in sources :

Example 11 with JobLoadBalancer

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

the class DefaultLoadBalancerService method getAllLoadBalancers.

@Override
public Pair<List<JobLoadBalancer>, Pagination> getAllLoadBalancers(Page page) {
    if (StringExt.isNotEmpty(page.getCursor())) {
        final List<JobLoadBalancer> allLoadBalancers = loadBalancerStore.getAssociations().stream().map(JobLoadBalancerState::getJobLoadBalancer).sorted(LoadBalancerCursors.loadBalancerComparator()).collect(Collectors.toList());
        return PaginationUtil.takePageWithCursor(Page.newBuilder().withPageSize(page.getPageSize()).withCursor(page.getCursor()).build(), allLoadBalancers, LoadBalancerCursors.loadBalancerComparator(), LoadBalancerCursors::loadBalancerIndexOf, LoadBalancerCursors::newCursorFrom);
    }
    // no cursor provided
    int offset = page.getPageSize() * page.getPageNumber();
    // Grab an extra item so we can tell if there's more to read after offset+limit.
    int limit = page.getPageSize() + 1;
    List<JobLoadBalancer> jobLoadBalancerPageList = loadBalancerStore.getAssociationsPage(offset, limit);
    boolean hasMore = jobLoadBalancerPageList.size() > page.getPageSize();
    jobLoadBalancerPageList = hasMore ? jobLoadBalancerPageList.subList(0, page.getPageSize()) : jobLoadBalancerPageList;
    final String cursor = jobLoadBalancerPageList.isEmpty() ? "" : LoadBalancerCursors.newCursorFrom(jobLoadBalancerPageList.get(jobLoadBalancerPageList.size() - 1));
    return Pair.of(jobLoadBalancerPageList, new Pagination(page, hasMore, 1, jobLoadBalancerPageList.size(), cursor, 0));
}
Also used : Pagination(com.netflix.titus.api.model.Pagination) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) LoadBalancerCursors(com.netflix.titus.runtime.loadbalancer.LoadBalancerCursors) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer)

Example 12 with JobLoadBalancer

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

the class DefaultLoadBalancerReconcilerTest method deregisterExtraTargetsPreviouslyRegisteredByUs.

@Test(timeout = TEST_TIMEOUT_MS)
public void deregisterExtraTargetsPreviouslyRegisteredByUs() {
    List<Task> tasks = LoadBalancerTests.buildTasksStarted(3, jobId);
    JobLoadBalancer jobLoadBalancer = new JobLoadBalancer(jobId, loadBalancerId);
    JobLoadBalancerState association = new JobLoadBalancerState(jobLoadBalancer, State.ASSOCIATED);
    when(v3JobOperations.getTasks(jobId)).thenReturn(tasks);
    reset(connector);
    when(connector.getLoadBalancer(loadBalancerId)).thenReturn(Single.just(new LoadBalancer(loadBalancerId, LoadBalancer.State.ACTIVE, CollectionsExt.asSet("1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4", "5.5.5.5", "6.6.6.6"))));
    store.addOrUpdateLoadBalancer(association.getJobLoadBalancer(), association.getState()).await();
    store.addOrUpdateTargets(// 3 running tasks were previously registered by us and are in the load balancer
    new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, tasks.get(0).getId(), "1.1.1.1"), LoadBalancerTarget.State.REGISTERED), new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, tasks.get(1).getId(), "2.2.2.2"), LoadBalancerTarget.State.REGISTERED), new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, tasks.get(2).getId(), "3.3.3.3"), LoadBalancerTarget.State.REGISTERED), // Next two ips were previously registered by us, but their tasks do not exist anymore
    new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "some-dead-task", "4.4.4.4"), LoadBalancerTarget.State.REGISTERED), new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "another-dead-task", "5.5.5.5"), LoadBalancerTarget.State.DEREGISTERED)).block();
    testScheduler.triggerActions();
    subscriber.assertNotCompleted().assertNoValues();
    awaitReconciliationRuns(1);
    subscriber.assertNotCompleted().assertValueCount(2);
    subscriber.getOnNextEvents().forEach(update -> {
        assertThat(update.getState()).isEqualTo(LoadBalancerTarget.State.DEREGISTERED);
        assertThat(update.getPriority()).isEqualTo(Priority.LOW);
        assertThat(update.getLoadBalancerId()).isEqualTo(loadBalancerId);
        assertThat(update.getIdentifier().getTaskId()).isIn("some-dead-task", "another-dead-task");
        assertThat(update.getIdentifier().getIpAddress()).isIn("4.4.4.4", "5.5.5.5");
    });
}
Also used : Task(com.netflix.titus.api.jobmanager.model.job.Task) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) LoadBalancer(com.netflix.titus.api.connector.cloud.LoadBalancer) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget) Test(org.junit.Test)

Example 13 with JobLoadBalancer

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

the class DefaultLoadBalancerReconcilerTest method orphanJobAssociationsAreSetAsDissociatedAndRemoved.

@Test
public void orphanJobAssociationsAreSetAsDissociatedAndRemoved() {
    JobLoadBalancer jobLoadBalancer = new JobLoadBalancer(jobId, loadBalancerId);
    when(v3JobOperations.getTasks(jobId)).thenThrow(JobManagerException.jobNotFound(jobId));
    when(v3JobOperations.getJob(jobId)).thenReturn(Optional.empty());
    assertThat(store.addOrUpdateLoadBalancer(jobLoadBalancer, State.ASSOCIATED).await(5, TimeUnit.SECONDS)).isTrue();
    testScheduler.triggerActions();
    subscriber.assertNotCompleted().assertNoValues();
    // let some reconciliation iterations run for:
    // 1. mark as orphan
    // 2. ensure no more targets are stored
    // 3. sweep
    awaitReconciliationRuns(3);
    assertThat(store.getAssociations()).isEmpty();
    assertThat(store.getAssociatedLoadBalancersSetForJob(jobId)).isEmpty();
}
Also used : JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) Test(org.junit.Test)

Example 14 with JobLoadBalancer

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

the class DefaultLoadBalancerReconcilerTest method registerMissingTargets.

@Test(timeout = TEST_TIMEOUT_MS)
public void registerMissingTargets() {
    List<Task> tasks = LoadBalancerTests.buildTasksStarted(5, jobId);
    JobLoadBalancer jobLoadBalancer = new JobLoadBalancer(jobId, loadBalancerId);
    JobLoadBalancerState association = new JobLoadBalancerState(jobLoadBalancer, State.ASSOCIATED);
    when(v3JobOperations.getTasks(jobId)).thenReturn(tasks);
    store.addOrUpdateLoadBalancer(association.getJobLoadBalancer(), association.getState()).await();
    testScheduler.triggerActions();
    subscriber.assertNotCompleted().assertNoValues();
    awaitReconciliationRuns(1);
    subscriber.assertNotCompleted().assertValueCount(5);
    subscriber.getOnNextEvents().forEach(update -> {
        assertThat(update.getState()).isEqualTo(LoadBalancerTarget.State.REGISTERED);
        // reconciliation always generates Priority.Low events that can be replaced by higher priority reactive updates
        assertThat(update.getPriority()).isEqualTo(Priority.LOW);
        assertThat(update.getLoadBalancerId()).isEqualTo(loadBalancerId);
    });
}
Also used : Task(com.netflix.titus.api.jobmanager.model.job.Task) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) Test(org.junit.Test)

Example 15 with JobLoadBalancer

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

the class DefaultLoadBalancerReconcilerTest method deregisteredTargetsAreCleanedUp.

@Test(timeout = TEST_TIMEOUT_MS)
public void deregisteredTargetsAreCleanedUp() {
    List<Task> tasks = LoadBalancerTests.buildTasksStarted(1, jobId);
    JobLoadBalancer jobLoadBalancer = new JobLoadBalancer(jobId, loadBalancerId);
    JobLoadBalancerState association = new JobLoadBalancerState(jobLoadBalancer, State.ASSOCIATED);
    when(v3JobOperations.getTasks(jobId)).thenReturn(tasks);
    reset(connector);
    when(connector.getLoadBalancer(loadBalancerId)).thenReturn(Single.just(new LoadBalancer(loadBalancerId, LoadBalancer.State.ACTIVE, CollectionsExt.asSet("1.1.1.1", "10.10.10.10"))));
    store.addOrUpdateLoadBalancer(association.getJobLoadBalancer(), association.getState()).await();
    store.addOrUpdateTargets(// running tasks was previously registered by us and are in the load balancer
    new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, tasks.get(0).getId(), "1.1.1.1"), LoadBalancerTarget.State.REGISTERED), // Next three ips were previously registered by us, but their tasks do not exist anymore and are not in the load balancer anymore
    new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "target-inconsistent", "2.2.2.2"), LoadBalancerTarget.State.REGISTERED), new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "target-not-in-lb", "3.3.3.3"), LoadBalancerTarget.State.DEREGISTERED)).block();
    // no reconciliation ran yet
    testScheduler.triggerActions();
    subscriber.assertNotCompleted().assertNoValues();
    assertThat(store.getLoadBalancerTargets(loadBalancerId).collectList().block()).hasSize(3);
    // first pass, the one stored as DEREGISTERED is cleaned up, the other in an inconsistent state is fixed
    awaitReconciliationRuns(1);
    subscriber.assertNotCompleted().assertValueCount(1);
    TargetStateBatchable inconsistencyFix = subscriber.getOnNextEvents().get(0);
    assertThat(inconsistencyFix.getState()).isEqualTo(LoadBalancerTarget.State.DEREGISTERED);
    assertThat(inconsistencyFix.getLoadBalancerId()).isEqualTo(loadBalancerId);
    assertThat(inconsistencyFix.getIpAddress()).isEqualTo("2.2.2.2");
    List<LoadBalancerTargetState> storedTargets = store.getLoadBalancerTargets(loadBalancerId).collectList().block();
    assertThat(storedTargets).hasSize(2);
    assertThat(storedTargets).doesNotContain(new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "target-not-in-lb", "3.3.3.3"), LoadBalancerTarget.State.DEREGISTERED));
    // update with fix not applied yet, keep trying
    awaitReconciliationRuns(1);
    subscriber.assertNotCompleted().assertValueCount(2);
    TargetStateBatchable update2 = subscriber.getOnNextEvents().get(0);
    assertThat(update2.getState()).isEqualTo(LoadBalancerTarget.State.DEREGISTERED);
    assertThat(update2.getLoadBalancerId()).isEqualTo(loadBalancerId);
    assertThat(update2.getIpAddress()).isEqualTo("2.2.2.2");
    assertThat(store.getLoadBalancerTargets(loadBalancerId).collectList().block()).hasSize(2);
    // simulate the update with the fix above being applied
    store.addOrUpdateTargets(new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "target-inconsistent", "2.2.2.2"), LoadBalancerTarget.State.DEREGISTERED)).block();
    // finally, corrected record is now cleaned up
    awaitReconciliationRuns(1);
    // no changes needed
    subscriber.assertNotCompleted().assertValueCount(2);
    assertThat(store.getLoadBalancerTargets(loadBalancerId).collectList().block()).containsOnly(new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, tasks.get(0).getId(), "1.1.1.1"), LoadBalancerTarget.State.REGISTERED));
}
Also used : Task(com.netflix.titus.api.jobmanager.model.job.Task) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) LoadBalancer(com.netflix.titus.api.connector.cloud.LoadBalancer) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget) Test(org.junit.Test)

Aggregations

JobLoadBalancer (com.netflix.titus.api.loadbalancer.model.JobLoadBalancer)36 Test (org.junit.Test)27 JobLoadBalancerState (com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState)14 Task (com.netflix.titus.api.jobmanager.model.job.Task)10 LoadBalancerTarget (com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget)10 LoadBalancerTargetState (com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState)10 LoadBalancer (com.netflix.titus.api.connector.cloud.LoadBalancer)7 List (java.util.List)6 GetJobLoadBalancersResult (com.netflix.titus.grpc.protogen.GetJobLoadBalancersResult)5 Collections (java.util.Collections)5 Collectors (java.util.stream.Collectors)5 JobManagerException (com.netflix.titus.api.jobmanager.service.JobManagerException)4 LoadBalancerStore (com.netflix.titus.api.loadbalancer.store.LoadBalancerStore)4 GetAllLoadBalancersResult (com.netflix.titus.grpc.protogen.GetAllLoadBalancersResult)4 Completable (rx.Completable)4 Schedulers (rx.schedulers.Schedulers)4 LoadBalancerConnector (com.netflix.titus.api.connector.cloud.LoadBalancerConnector)3 TaskAttributes (com.netflix.titus.api.jobmanager.TaskAttributes)3 V3JobOperations (com.netflix.titus.api.jobmanager.service.V3JobOperations)3 ArrayList (java.util.ArrayList)3