Search in sources :

Example 16 with JobLoadBalancer

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

the class DefaultLoadBalancerReconcilerTest method updatesAreIgnoredWhileCooldownIsActive.

@Test(timeout = TEST_TIMEOUT_MS)
public void updatesAreIgnoredWhileCooldownIsActive() {
    long cooldownPeriodMs = 5 * delayMs;
    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();
    for (Task task : tasks) {
        String ipAddress = task.getTaskContext().get(TaskAttributes.TASK_ATTRIBUTES_CONTAINER_IP);
        LoadBalancerTarget target = new LoadBalancerTarget(loadBalancerId, task.getId(), ipAddress);
        reconciler.activateCooldownFor(target, cooldownPeriodMs, TimeUnit.MILLISECONDS);
    }
    testScheduler.triggerActions();
    subscriber.assertNotCompleted().assertNoValues();
    // no updates while cooldown is active in the first iteration
    awaitReconciliationRuns(1);
    subscriber.assertNotCompleted().assertNoValues();
    awaitReconciliationRuns(4);
    subscriber.assertNotCompleted().assertValueCount(5);
    subscriber.getOnNextEvents().forEach(update -> {
        assertThat(update.getState()).isEqualTo(LoadBalancerTarget.State.REGISTERED);
        assertThat(update.getPriority()).isEqualTo(Priority.LOW);
        assertThat(update.getLoadBalancerId()).isEqualTo(loadBalancerId);
    });
    // try again since it still can't see updates applied on the connector
    awaitReconciliationRuns(1);
    subscriber.assertNotCompleted().assertValueCount(10);
}
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) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget) Test(org.junit.Test)

Example 17 with JobLoadBalancer

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

the class DefaultLoadBalancerServiceTest method removeLoadBalancerDeregisterKnownTargets.

@Test
public void removeLoadBalancerDeregisterKnownTargets() {
    String jobId = UUID.randomUUID().toString();
    String loadBalancerId = "lb-" + UUID.randomUUID().toString();
    JobLoadBalancer jobLoadBalancer = new JobLoadBalancer(jobId, loadBalancerId);
    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)), // final state expected after updates are applied
    DEREGISTERED)).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(loadBalancerStore.addOrUpdateLoadBalancer(jobLoadBalancer, JobLoadBalancer.State.ASSOCIATED).await(100, TimeUnit.MILLISECONDS));
    assertTrue(service.removeLoadBalancer(jobId, loadBalancerId).await(100, TimeUnit.MILLISECONDS));
    List<JobLoadBalancerState> jobLoadBalancers = loadBalancerStore.getAssociations().stream().filter(association -> jobId.equals(association.getJobId())).collect(Collectors.toList());
    assertThat(jobLoadBalancers).isNotEmpty();
    assertThat(jobLoadBalancers).hasSize(1);
    JobLoadBalancerState jobLoadBalancerState = jobLoadBalancers.iterator().next();
    assertEquals(loadBalancerId, jobLoadBalancerState.getLoadBalancerId());
    assertEquals(JobLoadBalancer.State.DISSOCIATED, jobLoadBalancerState.getState());
    assertFalse(service.getJobLoadBalancers(jobId).toBlocking().getIterator().hasNext());
    testScheduler.advanceTimeBy(FLUSH_WAIT_TIME_MS, TimeUnit.MILLISECONDS);
    testSubscriber.assertNoErrors().assertValueCount(1);
    assertThat(loadBalancerStore.getLoadBalancerTargets(loadBalancerId).collectList().block()).containsExactlyInAnyOrderElementsOf(expectedTargets);
    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));
}
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) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) 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)

Example 18 with JobLoadBalancer

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

the class GrpcModelConverters method toGetAllLoadBalancersResult.

public static GetAllLoadBalancersResult toGetAllLoadBalancersResult(List<JobLoadBalancer> jobLoadBalancerList, Pagination runtimePagination) {
    GetAllLoadBalancersResult.Builder allLoadBalancersResult = GetAllLoadBalancersResult.newBuilder();
    allLoadBalancersResult.setPagination(toGrpcPagination(runtimePagination));
    // We expect the list to be in a sorted-by-jobId order and iterate as such
    GetJobLoadBalancersResult.Builder getJobLoadBalancersResultBuilder = GetJobLoadBalancersResult.newBuilder();
    Set<String> addedJobIds = new HashSet<>();
    for (JobLoadBalancer jobLoadBalancer : jobLoadBalancerList) {
        String jobId = jobLoadBalancer.getJobId();
        // Check if we're processing a new Job ID
        if (!addedJobIds.contains(jobId)) {
            // Add any previous JobID's result if it existed
            if (getJobLoadBalancersResultBuilder.getLoadBalancersBuilderList().size() > 0) {
                allLoadBalancersResult.addJobLoadBalancers(getJobLoadBalancersResultBuilder.build());
            }
            getJobLoadBalancersResultBuilder = GetJobLoadBalancersResult.newBuilder().setJobId(jobId);
            addedJobIds.add(jobId);
        }
        getJobLoadBalancersResultBuilder.addLoadBalancers(LoadBalancerId.newBuilder().setId(jobLoadBalancer.getLoadBalancerId()).build());
    }
    if (getJobLoadBalancersResultBuilder.getLoadBalancersBuilderList().size() > 0) {
        allLoadBalancersResult.addJobLoadBalancers(getJobLoadBalancersResultBuilder.build());
    }
    return allLoadBalancersResult.build();
}
Also used : GetAllLoadBalancersResult(com.netflix.titus.grpc.protogen.GetAllLoadBalancersResult) GetJobLoadBalancersResult(com.netflix.titus.grpc.protogen.GetJobLoadBalancersResult) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) HashSet(java.util.HashSet)

Example 19 with JobLoadBalancer

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

the class LoadBalancerCursors method loadBalancerIndexOf.

public static Optional<Integer> loadBalancerIndexOf(List<JobLoadBalancer> sortedLoadBalancers, String cursor) {
    return decode(cursor).map(p -> {
        final JobLoadBalancer jobLoadBalancer = new JobLoadBalancer(p.getLeft(), p.getRight());
        final int idx = Collections.binarySearch(sortedLoadBalancers, jobLoadBalancer, loadBalancerComparator());
        return idx >= 0 ? idx : Math.max(-1, -idx - 2);
    });
}
Also used : JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer)

Example 20 with JobLoadBalancer

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

the class LoadBalancerCursorsTest method checkFirstMissingCursorElement.

@Test
public void checkFirstMissingCursorElement() {
    final String cursor = LoadBalancerCursors.newCursorFrom(new JobLoadBalancer("job1", "lb0"));
    final Optional<Integer> loadBalancerIndex = LoadBalancerCursors.loadBalancerIndexOf(loadBalancerList, cursor);
    assertThat(loadBalancerIndex.isPresent()).isTrue();
    assertThat(loadBalancerIndex.get()).isEqualTo(-1);
}
Also used : JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) 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