Java Code Examples for io.grpc.Status#UNAVAILABLE

The following examples show how to use io.grpc.Status#UNAVAILABLE . 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: GrpcRetryInterceptorTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryOnUnavailable() throws IOException {
  String uniqueName = InProcessServerBuilder.generateName();
  ExecutionImpl service = new ExecutionImpl(Status.UNAVAILABLE, 0);
  InProcessServerBuilder.forName(uniqueName).addService(service).build().start();
  CallCounter beforeRetry = new CallCounter();
  ManagedChannel channel =
      InProcessChannelBuilder.forName(uniqueName)
          .intercept(
              new RetryClientInterceptor(
                  RetryPolicy.builder().setMaxRetries(2).setBeforeRetry(beforeRetry).build()))
          .build();
  ExecutionBlockingStub stub = ExecutionGrpc.newBlockingStub(channel);
  try {
    stub.execute(ExecuteRequest.newBuilder().build()).forEachRemaining(resp -> {});
    Assert.fail("Final retry should cause an exception");
  } catch (StatusRuntimeException ex) {
    Assert.assertEquals(Status.Code.UNAVAILABLE, ex.getStatus().getCode());
  }

  Assert.assertEquals(3, service.calls);
  Assert.assertEquals(2, beforeRetry.calls);
}
 
Example 2
Source File: LocalityStore.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private void updatePicker(
    @Nullable ConnectivityState state,  List<WeightedChildPicker> childPickers) {
  SubchannelPicker picker;
  if (childPickers.isEmpty()) {
    if (state == TRANSIENT_FAILURE) {
      picker = new ErrorPicker(Status.UNAVAILABLE); // TODO: more details in status
    } else {
      picker = XdsSubchannelPickers.BUFFER_PICKER;
    }
  } else {
    picker = new WeightedRandomPicker(childPickers);
  }

  if (!dropOverloads.isEmpty()) {
    picker = new DroppablePicker(dropOverloads, picker, random, loadStatsStore);
  }

  if (state != null) {
    helper.updateBalancingState(state, picker);
  }
}
 
Example 3
Source File: GrpcUtilTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getTransportFromPickResult_errorPickResult_failFast() {
  Status status = Status.UNAVAILABLE;
  PickResult pickResult = PickResult.withError(status);
  ClientTransport transport = GrpcUtil.getTransportFromPickResult(pickResult, false);

  assertNotNull(transport);

  ClientStream stream = transport
      .newStream(TestMethodDescriptors.voidMethod(), new Metadata(), CallOptions.DEFAULT);
  ClientStreamListener listener = mock(ClientStreamListener.class);
  stream.start(listener);

  verify(listener).closed(eq(status), eq(RpcProgress.PROCESSED), any(Metadata.class));
}
 
Example 4
Source File: FailingClientStreamTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void droppedRpcProgressPopulatedToListener() {
  ClientStreamListener listener = mock(ClientStreamListener.class);
  Status status = Status.UNAVAILABLE;

  ClientStream stream = new FailingClientStream(status, RpcProgress.DROPPED);
  stream.start(listener);
  verify(listener).closed(eq(status), eq(RpcProgress.DROPPED), any(Metadata.class));
}
 
Example 5
Source File: FailingClientStreamTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void processedRpcProgressPopulatedToListener() {
  ClientStreamListener listener = mock(ClientStreamListener.class);
  Status status = Status.UNAVAILABLE;

  ClientStream stream = new FailingClientStream(status);
  stream.start(listener);
  verify(listener).closed(eq(status), eq(RpcProgress.PROCESSED), any(Metadata.class));
}
 
Example 6
Source File: GrpcUtilTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getTransportFromPickResult_dropPickResult_failFast() {
  Status status = Status.UNAVAILABLE;
  PickResult pickResult = PickResult.withDrop(status);
  ClientTransport transport = GrpcUtil.getTransportFromPickResult(pickResult, false);

  assertNotNull(transport);

  ClientStream stream = transport
      .newStream(TestMethodDescriptors.voidMethod(), new Metadata(), CallOptions.DEFAULT);
  ClientStreamListener listener = mock(ClientStreamListener.class);
  stream.start(listener);

  verify(listener).closed(eq(status), eq(RpcProgress.DROPPED), any(Metadata.class));
}
 
Example 7
Source File: GrpcUtilTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getTransportFromPickResult_dropPickResult_waitForReady() {
  Status status = Status.UNAVAILABLE;
  PickResult pickResult = PickResult.withDrop(status);
  ClientTransport transport = GrpcUtil.getTransportFromPickResult(pickResult, true);

  assertNotNull(transport);

  ClientStream stream = transport
      .newStream(TestMethodDescriptors.voidMethod(), new Metadata(), CallOptions.DEFAULT);
  ClientStreamListener listener = mock(ClientStreamListener.class);
  stream.start(listener);

  verify(listener).closed(eq(status), eq(RpcProgress.DROPPED), any(Metadata.class));
}
 
Example 8
Source File: GrpcUtilTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getTransportFromPickResult_errorPickResult_waitForReady() {
  Status status = Status.UNAVAILABLE;
  PickResult pickResult = PickResult.withError(status);
  ClientTransport transport = GrpcUtil.getTransportFromPickResult(pickResult, true);

  assertNull(transport);
}
 
Example 9
Source File: WeightedTargetLoadBalancer.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void updateOverallBalancingState() {
  List<WeightedChildPicker> childPickers = new ArrayList<>();

  ConnectivityState overallState = null;
  for (String name : targets.keySet()) {
    ChildHelper childHelper = childHelpers.get(name);
    ConnectivityState childState = childHelper.currentState;
    overallState = aggregateState(overallState, childState);
    if (READY == childState) {
      int weight = targets.get(name).weight;
      childPickers.add(new WeightedChildPicker(weight, childHelper.currentPicker));
    }
  }

  SubchannelPicker picker;
  if (childPickers.isEmpty()) {
    if (overallState == TRANSIENT_FAILURE) {
      picker = new ErrorPicker(Status.UNAVAILABLE); // TODO: more details in status
    } else {
      picker = XdsSubchannelPickers.BUFFER_PICKER;
    }
  } else {
    picker = new WeightedRandomPicker(childPickers);
  }

  if (overallState != null) {
    helper.updateBalancingState(overallState, picker);
  }
}
 
Example 10
Source File: GrpcExceptionMapper.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private Status toGrpcStatus(Throwable original) {
    Throwable cause = unwrap(original);
    if (cause instanceof SocketException) {
        return Status.UNAVAILABLE;
    } else if (cause instanceof TimeoutException) {
        return Status.DEADLINE_EXCEEDED;
    }
    for (Function<Throwable, Optional<Status>> mapper : serviceExceptionMappers) {
        Status status = mapper.apply(cause).orElse(null);
        if (status != null) {
            return status;
        }
    }
    return Status.INTERNAL;
}
 
Example 11
Source File: GrpcRetryInterceptorTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetryOnPartialResponse() throws IOException {
  String uniqueName = InProcessServerBuilder.generateName();
  ExecutionImpl service = new ExecutionImpl(Status.UNAVAILABLE, 1);
  InProcessServerBuilder.forName(uniqueName).addService(service).build().start();
  CallCounter beforeRetry = new CallCounter();
  ManagedChannel channel =
      InProcessChannelBuilder.forName(uniqueName)
          .intercept(
              new RetryClientInterceptor(
                  RetryPolicy.builder()
                      .setMaxRetries(2)
                      .setBeforeRetry(beforeRetry)
                      .setRestartAllStreamingCalls(true)
                      .build()))
          .build();
  ExecutionBlockingStub stub = ExecutionGrpc.newBlockingStub(channel);
  try {
    stub.execute(ExecuteRequest.newBuilder().build()).forEachRemaining(resp -> {});
    Assert.fail("Final retry should cause an exception");
  } catch (StatusRuntimeException ex) {
    Assert.assertEquals(Status.Code.UNAVAILABLE, ex.getStatus().getCode());
  }

  Assert.assertEquals(3, service.calls);
  Assert.assertEquals(2, beforeRetry.calls);
}
 
Example 12
Source File: FailingClientStreamTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void droppedRpcProgressPopulatedToListener() {
  ClientStreamListener listener = mock(ClientStreamListener.class);
  Status status = Status.UNAVAILABLE;

  ClientStream stream = new FailingClientStream(status, RpcProgress.DROPPED);
  stream.start(listener);
  verify(listener).closed(eq(status), eq(RpcProgress.DROPPED), any(Metadata.class));
}
 
Example 13
Source File: FailingClientStreamTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void processedRpcProgressPopulatedToListener() {
  ClientStreamListener listener = mock(ClientStreamListener.class);
  Status status = Status.UNAVAILABLE;

  ClientStream stream = new FailingClientStream(status);
  stream.start(listener);
  verify(listener).closed(eq(status), eq(RpcProgress.PROCESSED), any(Metadata.class));
}
 
Example 14
Source File: GrpcUtilTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getTransportFromPickResult_dropPickResult_failFast() {
  Status status = Status.UNAVAILABLE;
  PickResult pickResult = PickResult.withDrop(status);
  ClientTransport transport = GrpcUtil.getTransportFromPickResult(pickResult, false);

  assertNotNull(transport);

  ClientStream stream = transport
      .newStream(TestMethodDescriptors.voidMethod(), new Metadata(), CallOptions.DEFAULT);
  ClientStreamListener listener = mock(ClientStreamListener.class);
  stream.start(listener);

  verify(listener).closed(eq(status), eq(RpcProgress.DROPPED), any(Metadata.class));
}
 
Example 15
Source File: GrpcUtilTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getTransportFromPickResult_dropPickResult_waitForReady() {
  Status status = Status.UNAVAILABLE;
  PickResult pickResult = PickResult.withDrop(status);
  ClientTransport transport = GrpcUtil.getTransportFromPickResult(pickResult, true);

  assertNotNull(transport);

  ClientStream stream = transport
      .newStream(TestMethodDescriptors.voidMethod(), new Metadata(), CallOptions.DEFAULT);
  ClientStreamListener listener = mock(ClientStreamListener.class);
  stream.start(listener);

  verify(listener).closed(eq(status), eq(RpcProgress.DROPPED), any(Metadata.class));
}
 
Example 16
Source File: GrpcUtilTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getTransportFromPickResult_errorPickResult_waitForReady() {
  Status status = Status.UNAVAILABLE;
  PickResult pickResult = PickResult.withError(status);
  ClientTransport transport = GrpcUtil.getTransportFromPickResult(pickResult, true);

  assertNull(transport);
}
 
Example 17
Source File: UtilServerInterceptorsTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Test
public void statusRuntimeExceptionTransmitterIgnoresClosedCalls() {
  final Status expectedStatus = Status.UNAVAILABLE;
  final Status unexpectedStatus = Status.CANCELLED;
  final Metadata expectedMetadata = new Metadata();

  FakeServerCall<Void, Void> call =
      new FakeServerCall<Void, Void>(expectedStatus, expectedMetadata);
  final StatusRuntimeException exception =
      new StatusRuntimeException(expectedStatus, expectedMetadata);

  listener = new VoidCallListener() {
    @Override
    public void onMessage(Void message) {
      throw exception;
    }

    @Override
    public void onHalfClose() {
      throw exception;
    }
  };

  ServerServiceDefinition intercepted = ServerInterceptors.intercept(
      serviceDefinition,
      Arrays.asList(TransmitStatusRuntimeExceptionInterceptor.instance()));
  ServerCall.Listener<Void> callDoubleSreListener =
      getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers);
  callDoubleSreListener.onMessage(null); // the only close with our exception
  callDoubleSreListener.onHalfClose(); // should not trigger a close

  // this listener closes the call when it is initialized with startCall
  listener = new VoidCallListener() {
    @Override
    public void onCall(ServerCall<Void, Void> call, Metadata headers) {
      call.close(unexpectedStatus, headers);
    }

    @Override
    public void onHalfClose() {
      throw exception;
    }
  };

  ServerCall.Listener<Void> callClosedListener =
      getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers);
  // call is already closed, does not match exception
  callClosedListener.onHalfClose(); // should not trigger a close
  assertEquals(1, call.numCloses);
}
 
Example 18
Source File: UtilServerInterceptorsTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Test
public void statusRuntimeExceptionTransmitter() {
  final Status expectedStatus = Status.UNAVAILABLE;
  final Metadata expectedMetadata = new Metadata();
  FakeServerCall<Void, Void> call =
      new FakeServerCall<Void, Void>(expectedStatus, expectedMetadata);
  final StatusRuntimeException exception =
      new StatusRuntimeException(expectedStatus, expectedMetadata);
  listener = new VoidCallListener() {
    @Override
    public void onMessage(Void message) {
      throw exception;
    }

    @Override
    public void onHalfClose() {
      throw exception;
    }

    @Override
    public void onCancel() {
      throw exception;
    }

    @Override
    public void onComplete() {
      throw exception;
    }

    @Override
    public void onReady() {
      throw exception;
    }
  };

  ServerServiceDefinition intercepted = ServerInterceptors.intercept(
      serviceDefinition,
      Arrays.asList(TransmitStatusRuntimeExceptionInterceptor.instance()));
  // The interceptor should have handled the error by directly closing the ServerCall
  // and the exception should not propagate to the method's caller
  getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onMessage(null);
  getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onCancel();
  getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onComplete();
  getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onHalfClose();
  getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onReady();
  assertEquals(5, call.numCloses);
}
 
Example 19
Source File: UtilServerInterceptorsTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void statusRuntimeExceptionTransmitterIgnoresClosedCalls() {
  final Status expectedStatus = Status.UNAVAILABLE;
  final Status unexpectedStatus = Status.CANCELLED;
  final Metadata expectedMetadata = new Metadata();

  FakeServerCall<Void, Void> call =
      new FakeServerCall<>(expectedStatus, expectedMetadata);
  final StatusRuntimeException exception =
      new StatusRuntimeException(expectedStatus, expectedMetadata);

  listener = new VoidCallListener() {
    @Override
    public void onMessage(Void message) {
      throw exception;
    }

    @Override
    public void onHalfClose() {
      throw exception;
    }
  };

  ServerServiceDefinition intercepted = ServerInterceptors.intercept(
      serviceDefinition,
      Arrays.asList(TransmitStatusRuntimeExceptionInterceptor.instance()));
  ServerCall.Listener<Void> callDoubleSreListener =
      getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers);
  callDoubleSreListener.onMessage(null); // the only close with our exception
  callDoubleSreListener.onHalfClose(); // should not trigger a close

  // this listener closes the call when it is initialized with startCall
  listener = new VoidCallListener() {
    @Override
    public void onCall(ServerCall<Void, Void> call, Metadata headers) {
      call.close(unexpectedStatus, headers);
    }

    @Override
    public void onHalfClose() {
      throw exception;
    }
  };

  ServerCall.Listener<Void> callClosedListener =
      getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers);
  // call is already closed, does not match exception
  callClosedListener.onHalfClose(); // should not trigger a close
  assertEquals(1, call.numCloses);
}
 
Example 20
Source File: UtilServerInterceptorsTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void statusRuntimeExceptionTransmitter() {
  final Status expectedStatus = Status.UNAVAILABLE;
  final Metadata expectedMetadata = new Metadata();
  FakeServerCall<Void, Void> call =
      new FakeServerCall<>(expectedStatus, expectedMetadata);
  final StatusRuntimeException exception =
      new StatusRuntimeException(expectedStatus, expectedMetadata);
  listener = new VoidCallListener() {
    @Override
    public void onMessage(Void message) {
      throw exception;
    }

    @Override
    public void onHalfClose() {
      throw exception;
    }

    @Override
    public void onCancel() {
      throw exception;
    }

    @Override
    public void onComplete() {
      throw exception;
    }

    @Override
    public void onReady() {
      throw exception;
    }
  };

  ServerServiceDefinition intercepted = ServerInterceptors.intercept(
      serviceDefinition,
      Arrays.asList(TransmitStatusRuntimeExceptionInterceptor.instance()));
  // The interceptor should have handled the error by directly closing the ServerCall
  // and the exception should not propagate to the method's caller
  getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onMessage(null);
  getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onCancel();
  getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onComplete();
  getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onHalfClose();
  getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onReady();
  assertEquals(5, call.numCloses);
}