Search in sources :

Example 26 with JobLoadBalancer

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

the class AggregatingLoadBalancerServiceTest method getJobLoadBalancersWithOneFailingCell.

@Test
public void getJobLoadBalancersWithOneFailingCell() {
    JobLoadBalancer jobLoadBalancer1 = new JobLoadBalancer(JOB_1, LB_1);
    JobLoadBalancer jobLoadBalancer2 = new JobLoadBalancer(JOB_1, LB_2);
    final CellWithLoadBalancers cellWithLoadBalancersOne = new CellWithLoadBalancers(Arrays.asList(jobLoadBalancer1, jobLoadBalancer2));
    cellOne.getServiceRegistry().addService(cellWithLoadBalancersOne);
    cellTwo.getServiceRegistry().addService(new CellWithFailingLoadBalancers(Status.INTERNAL));
    final AssertableSubscriber<GetJobLoadBalancersResult> resultSubscriber = service.getLoadBalancers(JobId.newBuilder().setId(JOB_1).build(), JUNIT_REST_CALL_METADATA).test();
    resultSubscriber.awaitValueCount(1, 1, TimeUnit.SECONDS);
    resultSubscriber.assertNoErrors();
    final List<GetJobLoadBalancersResult> onNextEvents = resultSubscriber.getOnNextEvents();
    assertThat(onNextEvents).hasSize(1);
    final List<LoadBalancerId> loadBalancersList = onNextEvents.get(0).getLoadBalancersList();
    final List<String> resultLoadBalancers = loadBalancersList.stream().map(loadBalancerId -> loadBalancerId.getId()).collect(Collectors.toList());
    assertThat(resultLoadBalancers).contains(LB_1, LB_2);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) GetJobLoadBalancersResult(com.netflix.titus.grpc.protogen.GetJobLoadBalancersResult) Arrays(java.util.Arrays) GetAllLoadBalancersRequest(com.netflix.titus.grpc.protogen.GetAllLoadBalancersRequest) LoadBalancerId(com.netflix.titus.grpc.protogen.LoadBalancerId) AnonymousCallMetadataResolver(com.netflix.titus.runtime.endpoint.metadata.AnonymousCallMetadataResolver) ManagedChannel(io.grpc.ManagedChannel) Java6Assertions.assertThat(org.assertj.core.api.Java6Assertions.assertThat) AddLoadBalancerRequest(com.netflix.titus.grpc.protogen.AddLoadBalancerRequest) GetAllLoadBalancersResult(com.netflix.titus.grpc.protogen.GetAllLoadBalancersResult) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Observable(rx.Observable) Collections.singletonList(java.util.Collections.singletonList) AssertableSubscriber(rx.observers.AssertableSubscriber) JUNIT_REST_CALL_METADATA(com.netflix.titus.testkit.junit.spring.SpringMockMvcUtil.JUNIT_REST_CALL_METADATA) GrpcConfiguration(com.netflix.titus.federation.startup.GrpcConfiguration) Map(java.util.Map) RemoveLoadBalancerRequest(com.netflix.titus.grpc.protogen.RemoveLoadBalancerRequest) Status(io.grpc.Status) Before(org.junit.Before) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) Cell(com.netflix.titus.api.federation.model.Cell) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Page(com.netflix.titus.grpc.protogen.Page) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Rule(org.junit.Rule) Optional(java.util.Optional) GrpcServerRule(io.grpc.testing.GrpcServerRule) Collections(java.util.Collections) JobId(com.netflix.titus.grpc.protogen.JobId) Mockito.mock(org.mockito.Mockito.mock) GetJobLoadBalancersResult(com.netflix.titus.grpc.protogen.GetJobLoadBalancersResult) LoadBalancerId(com.netflix.titus.grpc.protogen.LoadBalancerId) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) Test(org.junit.Test)

Example 27 with JobLoadBalancer

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

the class LoadBalancerEngine method moveFromEvents.

private Observable<TargetStateBatchable> moveFromEvents(Observable<TaskUpdateEvent> taskMovedEvents) {
    return taskMovedEvents.flatMapIterable(taskMoved -> {
        ArrayList<TargetStateBatchable> changes = new ArrayList<>();
        Task task = taskMoved.getCurrentTask();
        String targetJobId = task.getJobId();
        Set<JobLoadBalancer> targetJobLoadBalancers = store.getAssociatedLoadBalancersSetForJob(targetJobId);
        String sourceJobId = task.getTaskContext().get(TaskAttributes.TASK_ATTRIBUTES_MOVED_FROM_JOB);
        if (StringExt.isEmpty(sourceJobId)) {
            invariants.inconsistent("Task moved to %s does not include the source job id: %s", task.getJobId(), task.getId());
            return Collections.emptyList();
        }
        Set<JobLoadBalancer> sourceJobLoadBalancers = store.getAssociatedLoadBalancersSetForJob(sourceJobId);
        // register on load balancers associated with the target job
        if (TaskHelpers.isStartedWithIp(task)) {
            Collection<JobLoadBalancer> toRegister = CollectionsExt.difference(targetJobLoadBalancers, sourceJobLoadBalancers, JobLoadBalancer::byLoadBalancerId);
            changes.addAll(updatesForLoadBalancers(toRegister, task, State.REGISTERED));
        }
        // deregister from load balancers associated with the source job
        Collection<JobLoadBalancer> toDeregister = CollectionsExt.difference(sourceJobLoadBalancers, targetJobLoadBalancers, JobLoadBalancer::byLoadBalancerId);
        changes.addAll(updatesForLoadBalancers(toDeregister, task, State.DEREGISTERED));
        if (!changes.isEmpty()) {
            logger.info("Task moved to {} from {}. Jobs are associated with one or more load balancers, generating {} load balancer updates", targetJobId, sourceJobId, changes.size());
        }
        return changes;
    });
}
Also used : Task(com.netflix.titus.api.jobmanager.model.job.Task) ArrayList(java.util.ArrayList) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer)

Example 28 with JobLoadBalancer

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

the class LoadBalancerCursorsTest method checkMissingCursor.

@Test
public void checkMissingCursor() {
    final String cursor = LoadBalancerCursors.newCursorFrom(new JobLoadBalancer("job30", "lb3"));
    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)

Example 29 with JobLoadBalancer

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

the class CassandraLoadBalancerStoreTest method checkDataSetExists.

/**
 * Checks if a provided data set fully exists in the store. The method is
 * expected to assert if the check is false.
 *
 * @throws Exception
 */
private void checkDataSetExists(CassandraLoadBalancerStore store, Map<JobLoadBalancer, JobLoadBalancer.State> testData) throws Exception {
    Set<JobLoadBalancer> observableVerificationSet = new HashSet<>(testData.keySet());
    Set<JobLoadBalancer> listVerificationSet = new HashSet<>(observableVerificationSet);
    Set<String> jobIdSet = getJobIdsFromTestData(testData);
    Map<String, List<JobLoadBalancerState>> byJobId = store.getAssociations().stream().collect(Collectors.groupingBy(JobLoadBalancerState::getJobId));
    jobIdSet.forEach(jobId -> {
        // Verify we get the correct load balancers in the correct state
        byJobId.get(jobId).forEach(loadBalancerState -> {
            // Verify that all of the returned data was in the test data.
            JobLoadBalancer jobLoadBalancer = loadBalancerState.getJobLoadBalancer();
            assertThat(jobLoadBalancer.getJobId().equals(jobId)).isTrue();
            assertThat(testData.containsKey(jobLoadBalancer)).isTrue();
            assertThat(testData.get(jobLoadBalancer)).isEqualTo(loadBalancerState.getState());
            // Mark that this job/load balancer was checked
            assertThat(observableVerificationSet.contains(jobLoadBalancer)).isTrue();
            assertThat(observableVerificationSet.remove(jobLoadBalancer)).isTrue();
            logger.debug("Verified job {} has load balancer id {} in state {}", jobId, loadBalancerState.getLoadBalancerId(), loadBalancerState.getState());
        });
        // Verify the secondary indexes return the correct state
        store.getAssociatedLoadBalancersSetForJob(jobId).forEach(jobLoadBalancer -> {
            logger.info("Verifying jobLoadBalancer {}", jobLoadBalancer);
            assertThat(jobLoadBalancer.getJobId().equals(jobId)).isTrue();
            assertThat(testData.containsKey(jobLoadBalancer)).isTrue();
            assertThat(testData.get(jobLoadBalancer)).isEqualTo(ASSOCIATED);
            // Mark that this job/load balancer was checked
            assertThat(listVerificationSet.contains(jobLoadBalancer)).isTrue();
            assertThat(listVerificationSet.remove(jobLoadBalancer)).isTrue();
            logger.debug("Verified job load balancer {}", jobLoadBalancer);
        });
    });
    // Verify that all of the test data was checked.
    assertThat(observableVerificationSet.isEmpty()).isTrue();
    assertThat(listVerificationSet.isEmpty()).isTrue();
}
Also used : List(java.util.List) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) HashSet(java.util.HashSet)

Example 30 with JobLoadBalancer

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

the class CassandraLoadBalancerStoreTest method generateTestData.

/**
 * Returns a map of data to be inserted that can be used for later verification.
 */
private TestData generateTestData(int numJobs, int numLoadBalancersPerJob, int numTasksPerJob) {
    Map<JobLoadBalancer, JobLoadBalancer.State> associations = new HashMap<>();
    Map<LoadBalancerTarget, LoadBalancerTarget.State> targets = new HashMap<>();
    for (int i = 0; i < numJobs; i++) {
        String jobId = UUID.randomUUID().toString();
        for (int j = 0; j < numLoadBalancersPerJob; j++) {
            String loadBalancerId = UUID.randomUUID().toString();
            JobLoadBalancer jobLoadBalancer = new JobLoadBalancer(jobId, jobId + "-" + loadBalancerId);
            associations.put(jobLoadBalancer, ASSOCIATED);
            for (int t = 0; t < numTasksPerJob; t++) {
                targets.put(new LoadBalancerTarget(loadBalancerId, "task-" + t, String.format("%s.%s.%s.%s", i, t, t, t)), LoadBalancerTarget.State.REGISTERED);
            }
        }
    }
    assertThat(associations.size()).isEqualTo(numJobs * numLoadBalancersPerJob);
    assertThat(targets.size()).isEqualTo(numJobs * numLoadBalancersPerJob * numTasksPerJob);
    return new TestData(associations, targets);
}
Also used : HashMap(java.util.HashMap) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) LoadBalancerTargetState(com.netflix.titus.api.loadbalancer.model.LoadBalancerTargetState) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer) LoadBalancerTarget(com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget)

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