AggregatingLoadbalancerService implements LoadBalancerService { @Override public Observable<GetAllLoadBalancersResult> getAllLoadBalancers(GetAllLoadBalancersRequest request, CallMetadata callMetadata) { if (request.getPage().getPageSize() <= 0) { return Observable.just(GetAllLoadBalancersResult.newBuilder() .setPagination(emptyGrpcPagination(request.getPage())) .build()); } if (StringExt.isNotEmpty(request.getPage().getCursor()) || request.getPage().getPageNumber() == 0) { return findLoadBalancersWithCursorPagination(request, callMetadata); } return Observable.error(TitusServiceException.invalidArgument("pageNumbers are not supported, please use cursors")); } @Inject  AggregatingLoadbalancerService(CellConnector connector,
                                          CallMetadataResolver callMetadataResolver,
                                          GrpcConfiguration grpcConfiguration,
                                          AggregatingCellClient aggregatingClient,
                                          AggregatingJobManagementServiceHelper jobManagementServiceHelper); @Override Observable<GetAllLoadBalancersResult> getAllLoadBalancers(GetAllLoadBalancersRequest request, CallMetadata callMetadata); @Override Observable<GetJobLoadBalancersResult> getLoadBalancers(JobId jobId, CallMetadata callMetadata); @Override Completable addLoadBalancer(AddLoadBalancerRequest request, CallMetadata callMetadata); @Override Completable removeLoadBalancer(RemoveLoadBalancerRequest removeLoadBalancerRequest, CallMetadata callMetadata);  }
@Test public void getAllLoadBalancersNoPagination() { JobLoadBalancer jobLoadBalancer1 = new JobLoadBalancer(JOB_1, LB_1); JobLoadBalancer jobLoadBalancer2 = new JobLoadBalancer(JOB_2, LB_2); final CellWithLoadBalancers cellWithLoadBalancersOne = new CellWithLoadBalancers(singletonList(jobLoadBalancer1)); final CellWithLoadBalancers cellWithLoadBalancersTwo = new CellWithLoadBalancers(singletonList(jobLoadBalancer2)); cellOne.getServiceRegistry().addService(cellWithLoadBalancersOne); cellTwo.getServiceRegistry().addService(cellWithLoadBalancersTwo); final AssertableSubscriber<GetAllLoadBalancersResult> resultSubscriber = service.getAllLoadBalancers( GetAllLoadBalancersRequest.newBuilder().setPage(Page.newBuilder().setPageNumber(0).setPageSize(10)).build(), JUNIT_REST_CALL_METADATA ).test(); resultSubscriber.awaitValueCount(1, 1, TimeUnit.SECONDS); resultSubscriber.assertNoErrors(); final List<GetAllLoadBalancersResult> onNextEvents = resultSubscriber.getOnNextEvents(); assertThat(onNextEvents).hasSize(1); assertThat(onNextEvents.get(0).getJobLoadBalancersCount()).isEqualTo(2); }
@Test public void getAllLoadBalancersWithPagination() { final List<JobLoadBalancer> jobLoadBalancersOne = Observable.range(1, 5) .map(i -> new JobLoadBalancer(JOB_1, String.format("LB_1_%d", i.intValue()))) .toList().toBlocking().first(); final List<JobLoadBalancer> jobLoadBalancersTwo = Observable.range(1, 5) .map(i -> new JobLoadBalancer(JOB_2, String.format("LB_2_%d", i.intValue()))) .toList().toBlocking().first(); final CellWithLoadBalancers cellWithLoadBalancersOne = new CellWithLoadBalancers(jobLoadBalancersOne); final CellWithLoadBalancers cellWithLoadBalancersTwo = new CellWithLoadBalancers(jobLoadBalancersTwo); cellOne.getServiceRegistry().addService(cellWithLoadBalancersOne); cellTwo.getServiceRegistry().addService(cellWithLoadBalancersTwo); final List<JobLoadBalancer> results = ServiceTests.walkAllPages( 2, request -> service.getAllLoadBalancers(request, JUNIT_REST_CALL_METADATA), page -> GetAllLoadBalancersRequest.newBuilder().setPage(page).build(), GetAllLoadBalancersResult::getPagination, this::buildJobLoadBalancerList ); assertThat(results).hasSize(10); }
@Test public void getLoadBalancersWithOneFailingCell() { 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<GetAllLoadBalancersResult> resultSubscriber = service.getAllLoadBalancers( GetAllLoadBalancersRequest.newBuilder().setPage(Page.newBuilder().setPageSize(10)).build(), JUNIT_REST_CALL_METADATA ).test(); resultSubscriber.awaitTerminalEvent(1, TimeUnit.SECONDS); resultSubscriber.assertNoValues(); final List<Throwable> onErrorEvents = resultSubscriber.getOnErrorEvents(); assertThat(onErrorEvents).hasSize(1); assertThat(Status.fromThrowable(onErrorEvents.get(0))).isEqualTo(Status.INTERNAL); }
@Test public void getLoadBalancersWithTwoFailingCell() { cellOne.getServiceRegistry().addService(new CellWithFailingLoadBalancers(Status.UNAVAILABLE)); cellTwo.getServiceRegistry().addService(new CellWithFailingLoadBalancers(Status.INTERNAL)); final AssertableSubscriber<GetAllLoadBalancersResult> resultSubscriber = service.getAllLoadBalancers( GetAllLoadBalancersRequest.newBuilder().setPage(Page.newBuilder().setPageSize(10)).build(), JUNIT_REST_CALL_METADATA ).test(); resultSubscriber.awaitTerminalEvent(1, TimeUnit.SECONDS); resultSubscriber.assertNoValues(); final List<Throwable> onErrorEvents = resultSubscriber.getOnErrorEvents(); assertThat(onErrorEvents).hasSize(1); assertThat(Status.fromThrowable(onErrorEvents.get(0))).isEqualTo(Status.INTERNAL); }