io.grpc.health.v1.HealthCheckRequest Java Examples

The following examples show how to use io.grpc.health.v1.HealthCheckRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HealthStatusManagerTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void checkValidStatus() throws Exception {
  manager.setStatus(SERVICE1, ServingStatus.NOT_SERVING);
  manager.setStatus(SERVICE2, ServingStatus.SERVING);
  HealthCheckRequest request = HealthCheckRequest.newBuilder().setService(SERVICE1).build();
  HealthCheckResponse response =
      blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request);
  assertThat(response).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.NOT_SERVING).build());

  request = HealthCheckRequest.newBuilder().setService(SERVICE2).build();
  response = blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request);
  assertThat(response).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVING).build());
  assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(0);
  assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(0);
}
 
Example #2
Source File: HealthStatusManagerTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void checkValidStatus() throws Exception {
  manager.setStatus(SERVICE1, ServingStatus.NOT_SERVING);
  manager.setStatus(SERVICE2, ServingStatus.SERVING);
  HealthCheckRequest request = HealthCheckRequest.newBuilder().setService(SERVICE1).build();
  HealthCheckResponse response =
      blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request);
  assertThat(response).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.NOT_SERVING).build());

  request = HealthCheckRequest.newBuilder().setService(SERVICE2).build();
  response = blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request);
  assertThat(response).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVING).build());
  assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(0);
  assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(0);
}
 
Example #3
Source File: HealthServiceImpl.java    From modeldb with Apache License 2.0 6 votes vote down vote up
@Override
public void check(
    HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {
  ServingStatus globalStatus;
  if (!request.getService().equals("")) {
    globalStatus = getStatus("");
    if (request.getService().equals("ready")) {
      if (globalStatus == ServingStatus.SERVING) {
        setStatus("ready", ModelDBHibernateUtil.checkReady());
      }
    } else if (request.getService().equals("live")) {
      setStatus("live", ModelDBHibernateUtil.checkLive());
    }
  }
  ServingStatus status = getStatus(request.getService());
  if (status == null) {
    responseObserver.onError(
        new StatusException(
            Status.NOT_FOUND.withDescription("unknown service " + request.getService())));
  } else {
    HealthCheckResponse response = HealthCheckResponse.newBuilder().setStatus(status).build();
    responseObserver.onNext(response);
    responseObserver.onCompleted();
  }
}
 
Example #4
Source File: HealthServiceImplTest.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Test
public void healthException() throws Exception {
    // Generate a unique in-process server name.
    String serverName = InProcessServerBuilder.generateName();
    HealthCheckAggregator hca = mock(HealthCheckAggregator.class);
    CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class);
    when(hcsf.get()).thenThrow(InterruptedException.class);
    when(hca.check()).thenReturn(hcsf);
    HealthServiceImpl healthyService = new HealthServiceImpl(hca);

    addService(serverName, healthyService);
    HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub(
            // Create a client channel and register for automatic graceful shutdown.
            grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()));

    thrown.expect(StatusRuntimeException.class);
    thrown.expect(hasProperty("status", is(Status.INTERNAL)));
    blockingStub.check(HealthCheckRequest.newBuilder().build());

}
 
Example #5
Source File: HealthServiceImplTest.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Test
public void healthNotServing() throws Exception {
    // Generate a unique in-process server name.
    String serverName = InProcessServerBuilder.generateName();
    HealthCheckAggregator hca = mock(HealthCheckAggregator.class);
    CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class);
    HealthCheckStatus hcs = mock(HealthCheckStatus.class);
    when(hcs.isHealthy()).thenReturn(false);
    when(hcsf.get()).thenReturn(hcs);
    when(hca.check()).thenReturn(hcsf);
    HealthServiceImpl healthyService = new HealthServiceImpl(hca);

    addService(serverName, healthyService);
    HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub(
            // Create a client channel and register for automatic graceful shutdown.
            grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()));


    HealthCheckResponse reply = blockingStub.check(HealthCheckRequest.newBuilder().build());

    assertEquals(HealthCheckResponse.ServingStatus.NOT_SERVING, reply.getStatus());
}
 
Example #6
Source File: HealthServiceImplTest.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Test
public void healthServing() throws Exception {
    // Generate a unique in-process server name.
    String serverName = InProcessServerBuilder.generateName();
    HealthCheckAggregator hca = mock(HealthCheckAggregator.class);
    CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class);
    HealthCheckStatus hcs = mock(HealthCheckStatus.class);
    when(hcs.isHealthy()).thenReturn(true);
    when(hcsf.get()).thenReturn(hcs);
    when(hca.check()).thenReturn(hcsf);
    HealthServiceImpl healthyService = new HealthServiceImpl(hca);

    addService(serverName, healthyService);
    HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub(
            // Create a client channel and register for automatic graceful shutdown.
            grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()));


    HealthCheckResponse reply = blockingStub.check(HealthCheckRequest.newBuilder().build());

    assertEquals(HealthCheckResponse.ServingStatus.SERVING, reply.getStatus());
}
 
Example #7
Source File: HealthServiceImpl.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {
    try {
        if (healthCheck.check().get().isHealthy()) {
            responseObserver.onNext(
                    HealthCheckResponse.newBuilder().setStatus(HealthCheckResponse.ServingStatus.SERVING).build()
            );
        } else {
            responseObserver.onNext(
                    HealthCheckResponse.newBuilder().setStatus(HealthCheckResponse.ServingStatus.NOT_SERVING).build()
            );
        }
    } catch (Exception ex) {
        GRPC_HELPER.onError(responseObserver, ex);
    } finally {
        responseObserver.onCompleted();
    }
}
 
Example #8
Source File: HealthStatusManagerTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void watchRemovedWhenClientCloses() throws Exception {
  CancellableContext withCancellation = Context.current().withCancellation();
  Context prevCtx = withCancellation.attach();
  RespObserver respObs1 = new RespObserver();
  try {
    assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(0);
    stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE1).build(), respObs1);
  } finally {
    withCancellation.detach(prevCtx);
  }
  RespObserver respObs1b = new RespObserver();
  stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE1).build(), respObs1b);
  RespObserver respObs2 = new RespObserver();
  stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE2).build(), respObs2);

  assertThat(respObs1.responses.poll()).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build());
  assertThat(respObs1b.responses.poll()).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build());
  assertThat(respObs2.responses.poll()).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build());
  assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(2);
  assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(1);
  assertThat(respObs1.responses).isEmpty();
  assertThat(respObs1b.responses).isEmpty();
  assertThat(respObs2.responses).isEmpty();

  // This will cancel the RPC with respObs1
  withCancellation.close();

  assertThat(respObs1.responses.poll()).isInstanceOf(Throwable.class);
  assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(1);
  assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(1);
  assertThat(respObs1.responses).isEmpty();
  assertThat(respObs1b.responses).isEmpty();
  assertThat(respObs2.responses).isEmpty();
}
 
Example #9
Source File: HealthCheckClient.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
public void check() {
  HealthCheckRequest request = HealthCheckRequest.newBuilder().setService(GreeterGrpc.SERVICE_NAME).build();
  HealthCheckResponse response;
  try {
      response = blockingStub.check(request);
  } catch (Exception e) {
    logger.error("RPC failed:", e);
    return;
  }
  logger.info("Greeting response: " + response.getStatus());
}
 
Example #10
Source File: HealthStatusManagerTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void notFoundForClearedStatus() throws Exception {
  manager.setStatus(SERVICE1, ServingStatus.SERVING);
  manager.clearStatus(SERVICE1);
  HealthCheckRequest request
      = HealthCheckRequest.newBuilder().setService(SERVICE1).build();
  try {
    blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request);
    fail("Should've failed");
  } catch (StatusRuntimeException e) {
    assertThat(e.getStatus().getCode()).isEqualTo(Status.Code.NOT_FOUND);
  }
}
 
Example #11
Source File: HealthStatusManagerTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void checkStatusNotFound() throws Exception {
  manager.setStatus(SERVICE1, ServingStatus.SERVING);
  // SERVICE2's status is not set
  HealthCheckRequest request
      = HealthCheckRequest.newBuilder().setService(SERVICE2).build();
  try {
    blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request);
    fail("Should've failed");
  } catch (StatusRuntimeException e) {
    assertThat(e.getStatus().getCode()).isEqualTo(Status.Code.NOT_FOUND);
  }
  assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(0);
}
 
Example #12
Source File: HealthStatusManagerTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultIsServing() throws Exception {
  HealthCheckRequest request =
      HealthCheckRequest.newBuilder().setService(HealthStatusManager.SERVICE_NAME_ALL_SERVICES)
          .build();

  HealthCheckResponse response =
      blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request);

  assertThat(response).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVING).build());
}
 
Example #13
Source File: HealthCheckingLoadBalancerFactoryTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void watch(HealthCheckRequest request,
    StreamObserver<HealthCheckResponse> responseObserver) {
  final ServerSideCall call = new ServerSideCall(request, responseObserver);
  Context.current().addListener(
      new CancellationListener() {
        @Override
        public void cancelled(Context ctx) {
          call.cancelled = true;
        }
      }, MoreExecutors.directExecutor());
  calls.add(call);
}
 
Example #14
Source File: HealthServiceImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void check(HealthCheckRequest request,
    StreamObserver<HealthCheckResponse> responseObserver) {
  ServingStatus status = statusMap.get(request.getService());
  if (status == null) {
    responseObserver.onError(new StatusException(
        Status.NOT_FOUND.withDescription("unknown service " + request.getService())));
  } else {
    HealthCheckResponse response = HealthCheckResponse.newBuilder().setStatus(status).build();
    responseObserver.onNext(response);
    responseObserver.onCompleted();
  }
}
 
Example #15
Source File: DemoAppTest.java    From grpc-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Test
public void testHealthCheck() throws ExecutionException, InterruptedException {
    final HealthCheckRequest healthCheckRequest = HealthCheckRequest.newBuilder().setService(GreeterGrpc.getServiceDescriptor().getName()).build();
    final HealthGrpc.HealthFutureStub healthFutureStub = HealthGrpc.newFutureStub(channel);
    final HealthCheckResponse.ServingStatus servingStatus = healthFutureStub.check(healthCheckRequest).get().getStatus();
    assertNotNull(servingStatus);
    assertEquals(servingStatus, HealthCheckResponse.ServingStatus.SERVING);
}
 
Example #16
Source File: HealthServiceImpl.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public void check(HealthCheckRequest request,
    StreamObserver<HealthCheckResponse> responseObserver) {
  ServingStatus status = statusMap.get(request.getService());
  if (status == null) {
    responseObserver.onError(new StatusException(
        Status.NOT_FOUND.withDescription("unknown service " + request.getService())));
  } else {
    HealthCheckResponse response = HealthCheckResponse.newBuilder().setStatus(status).build();
    responseObserver.onNext(response);
    responseObserver.onCompleted();
  }
}
 
Example #17
Source File: GrpcHealthIndicator.java    From hedera-mirror-node with Apache License 2.0 5 votes vote down vote up
@Override
public Health health() {
    HealthGrpc.HealthImplBase healthService = (HealthGrpc.HealthImplBase) healthStatusManager
            .getHealthService();
    HealthCheckRequest healthcheckRequest = HealthCheckRequest.newBuilder().setService(serviceName).build();
    HealthStreamObserver healthStreamObserver = new HealthStreamObserver();
    healthService.check(healthcheckRequest, healthStreamObserver);
    return healthStreamObserver.getHealth();
}
 
Example #18
Source File: HealthStatusManagerTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void notFoundForClearedStatus() throws Exception {
  manager.setStatus(SERVICE1, ServingStatus.SERVING);
  manager.clearStatus(SERVICE1);
  HealthCheckRequest request
      = HealthCheckRequest.newBuilder().setService(SERVICE1).build();
  try {
    blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request);
    fail("Should've failed");
  } catch (StatusRuntimeException e) {
    assertThat(e.getStatus().getCode()).isEqualTo(Status.Code.NOT_FOUND);
  }
}
 
Example #19
Source File: HealthStatusManagerTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void checkStatusNotFound() throws Exception {
  manager.setStatus(SERVICE1, ServingStatus.SERVING);
  // SERVICE2's status is not set
  HealthCheckRequest request
      = HealthCheckRequest.newBuilder().setService(SERVICE2).build();
  try {
    blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request);
    fail("Should've failed");
  } catch (StatusRuntimeException e) {
    assertThat(e.getStatus().getCode()).isEqualTo(Status.Code.NOT_FOUND);
  }
  assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(0);
}
 
Example #20
Source File: HealthCheckingLoadBalancerFactoryTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public void watch(HealthCheckRequest request,
    StreamObserver<HealthCheckResponse> responseObserver) {
  final ServerSideCall call = new ServerSideCall(request, responseObserver);
  Context.current().addListener(
      new CancellationListener() {
        @Override
        public void cancelled(Context ctx) {
          call.cancelled = true;
        }
      }, MoreExecutors.directExecutor());
  calls.add(call);
}
 
Example #21
Source File: HealthCheckingLoadBalancerFactoryTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
private HealthCheckRequest makeRequest(String service) {
  return HealthCheckRequest.newBuilder().setService(service).build();
}
 
Example #22
Source File: HealthStatusManagerTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void watch() throws Exception {
  manager.setStatus(SERVICE1, ServingStatus.UNKNOWN);

  // Start a watch on SERVICE1
  assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(0);
  RespObserver respObs1 = new RespObserver();
  stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE1).build(), respObs1);
  // Will get the current status
  assertThat(respObs1.responses.poll()).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.UNKNOWN).build());        
  assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(1);

  // Status change is notified of to the RPC
  manager.setStatus(SERVICE1, ServingStatus.SERVING);
  assertThat(respObs1.responses.poll()).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVING).build());        

  // Start another watch on SERVICE1
  assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(1);
  RespObserver respObs1b = new RespObserver();
  stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE1).build(), respObs1b);
  assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(2);
  // Will get the current status
  assertThat(respObs1b.responses.poll()).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVING).build());

  // Start a watch on SERVICE2, which is not known yet
  assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(0);
  RespObserver respObs2 = new RespObserver();
  stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE2).build(), respObs2);
  assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(1);
  // Current status is SERVICE_UNKNOWN
  assertThat(respObs2.responses.poll()).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build());

  // Set status for SERVICE2, which will be notified of
  manager.setStatus(SERVICE2, ServingStatus.NOT_SERVING);
  assertThat(respObs2.responses.poll()).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.NOT_SERVING).build());

  // Clear the status for SERVICE1, which will be notified of
  manager.clearStatus(SERVICE1);
  assertThat(respObs1.responses.poll()).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build());
  assertThat(respObs1b.responses.poll()).isEqualTo(
      HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build());

  // All responses have been accounted for
  assertThat(respObs1.responses).isEmpty();
  assertThat(respObs1b.responses).isEmpty();
  assertThat(respObs2.responses).isEmpty();
}
 
Example #23
Source File: HealthCheckingLoadBalancerFactory.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
void start() {
  call.start(this, new Metadata());
  call.sendMessage(HealthCheckRequest.newBuilder().setService(serviceName).build());
  call.halfClose();
  call.request(1);
}
 
Example #24
Source File: HealthCheckingLoadBalancerFactoryTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
private HealthCheckRequest makeRequest(String service) {
  return HealthCheckRequest.newBuilder().setService(service).build();
}
 
Example #25
Source File: HealthCheckingLoadBalancerFactoryTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public void check(HealthCheckRequest request,
    StreamObserver<HealthCheckResponse> responseObserver) {
  responseObserver.onError(new UnsupportedOperationException("Should never be called"));
  checkCalled = true;
}
 
Example #26
Source File: HealthCheckingLoadBalancerFactoryTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
ServerSideCall(
    HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {
  this.request = request;
  this.responseObserver = responseObserver;
}
 
Example #27
Source File: HealthCheckingLoadBalancerFactoryTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
ServerSideCall(
    HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {
  this.request = request;
  this.responseObserver = responseObserver;
}
 
Example #28
Source File: HealthCheckingLoadBalancerFactoryTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void check(HealthCheckRequest request,
    StreamObserver<HealthCheckResponse> responseObserver) {
  responseObserver.onError(new UnsupportedOperationException("Should never be called"));
  checkCalled = true;
}
 
Example #29
Source File: AbstractHealthGrpcService.java    From flair-engine with Apache License 2.0 4 votes vote down vote up
@Override
public void watch(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {
    log.debug("Streaming heath endpoint called");
    performHealthCheck(responseObserver);
}
 
Example #30
Source File: HealthCheckingLoadBalancerFactory.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
void start() {
  call.start(this, new Metadata());
  call.sendMessage(HealthCheckRequest.newBuilder().setService(serviceName).build());
  call.halfClose();
  call.request(1);
}