Java Code Examples for io.grpc.LoadBalancer.PickResult#withError()

The following examples show how to use io.grpc.LoadBalancer.PickResult#withError() . 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: LoadBalancerTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void pickResult_withError() {
  PickResult result = PickResult.withError(status);
  assertThat(result.getSubchannel()).isNull();
  assertThat(result.getStatus()).isSameInstanceAs(status);
  assertThat(result.getStreamTracerFactory()).isNull();
  assertThat(result.isDrop()).isFalse();
}
 
Example 2
Source File: GrpcUtilTest.java    From grpc-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 3
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 4
Source File: OobChannel.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
void handleSubchannelStateChange(final ConnectivityStateInfo newState) {
  channelTracer.reportEvent(
      new ChannelTrace.Event.Builder()
          .setDescription("Entering " + newState.getState() + " state")
          .setSeverity(ChannelTrace.Event.Severity.CT_INFO)
          .setTimestampNanos(timeProvider.currentTimeNanos())
          .build());
  switch (newState.getState()) {
    case READY:
    case IDLE:
      delayedTransport.reprocess(subchannelPicker);
      break;
    case TRANSIENT_FAILURE:
      final class OobErrorPicker extends SubchannelPicker {
        final PickResult errorResult = PickResult.withError(newState.getStatus());

        @Override
        public PickResult pickSubchannel(PickSubchannelArgs args) {
          return errorResult;
        }

        @Override
        public String toString() {
          return MoreObjects.toStringHelper(OobErrorPicker.class)
              .add("errorResult", errorResult)
              .toString();
        }
      }

      delayedTransport.reprocess(new OobErrorPicker());
      break;
    default:
      // Do nothing
  }
}
 
Example 5
Source File: CachingRlsLbClient.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/** Uses Subchannel connected to default target. */
private PickResult useFallback(PickSubchannelArgs args) {
  String defaultTarget = lbPolicyConfig.getRouteLookupConfig().getDefaultTarget();
  if (fallbackChildPolicyWrapper == null
      || !fallbackChildPolicyWrapper.getTarget().equals(defaultTarget)) {
    // TODO(creamsoup) wait until lb is ready
    startFallbackChildPolicy();
  }
  switch (fallbackChildPolicyWrapper.getConnectivityStateInfo().getState()) {
    case IDLE:
      // fall through
    case CONNECTING:
      return PickResult.withNoResult();
    case TRANSIENT_FAILURE:
      // fall through
    case SHUTDOWN:
      return
          PickResult
              .withError(fallbackChildPolicyWrapper.getConnectivityStateInfo().getStatus());
    case READY:
      SubchannelPicker picker = fallbackChildPolicyWrapper.getPicker();
      if (picker == null) {
        return PickResult.withNoResult();
      }
      return picker.pickSubchannel(args);
    default:
      throw new AssertionError();
  }
}
 
Example 6
Source File: ClientLoadCounterTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void tracerWrappingSubchannelPicker_interceptPickResult_invalidPickResultNotIntercepted() {
  final SubchannelPicker picker = mock(SubchannelPicker.class);
  SubchannelPicker streamInstrSubchannelPicker = new TracerWrappingSubchannelPicker() {
    @Override
    protected SubchannelPicker delegate() {
      return picker;
    }

    @Override
    protected Factory wrapTracerFactory(Factory originFactory) {
      // NO-OP
      return originFactory;
    }
  };
  PickResult errorResult = PickResult.withError(Status.UNAVAILABLE.withDescription("Error"));
  PickResult droppedResult = PickResult.withDrop(Status.UNAVAILABLE.withDescription("Dropped"));
  PickResult emptyResult = PickResult.withNoResult();
  when(picker.pickSubchannel(any(PickSubchannelArgs.class)))
      .thenReturn(errorResult, droppedResult, emptyResult);
  PickSubchannelArgs args = mock(PickSubchannelArgs.class);

  PickResult interceptedErrorResult = streamInstrSubchannelPicker.pickSubchannel(args);
  PickResult interceptedDroppedResult = streamInstrSubchannelPicker.pickSubchannel(args);
  PickResult interceptedEmptyResult = streamInstrSubchannelPicker.pickSubchannel(args);
  assertThat(interceptedErrorResult).isSameInstanceAs(errorResult);
  assertThat(interceptedDroppedResult).isSameInstanceAs(droppedResult);
  assertThat(interceptedEmptyResult).isSameInstanceAs(emptyResult);
}
 
Example 7
Source File: LrsLoadBalancerTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleNameResolutionError(final Status error) {
  SubchannelPicker picker = new SubchannelPicker() {
    @Override
    public PickResult pickSubchannel(PickSubchannelArgs args) {
      return PickResult.withError(error.augmentDescription("handled by downstream balancer"));
    }
  };
  helper.updateBalancingState(ConnectivityState.TRANSIENT_FAILURE, picker);
}
 
Example 8
Source File: LoadBalancerTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void pickResult_equals() {
  PickResult sc1 = PickResult.withSubchannel(subchannel);
  PickResult sc2 = PickResult.withSubchannel(subchannel);
  PickResult sc3 = PickResult.withSubchannel(subchannel, tracerFactory);
  PickResult sc4 = PickResult.withSubchannel(subchannel2);
  PickResult nr = PickResult.withNoResult();
  PickResult error1 = PickResult.withError(status);
  PickResult error2 = PickResult.withError(status2);
  PickResult error3 = PickResult.withError(status2);
  PickResult drop1 = PickResult.withDrop(status);
  PickResult drop2 = PickResult.withDrop(status);
  PickResult drop3 = PickResult.withDrop(status2);

  assertThat(sc1).isNotEqualTo(nr);
  assertThat(sc1).isNotEqualTo(error1);
  assertThat(sc1).isNotEqualTo(drop1);
  assertThat(sc1).isEqualTo(sc2);
  assertThat(sc1).isNotEqualTo(sc3);
  assertThat(sc1).isNotEqualTo(sc4);

  assertThat(error1).isNotEqualTo(error2);
  assertThat(error2).isEqualTo(error3);

  assertThat(drop1).isEqualTo(drop2);
  assertThat(drop1).isNotEqualTo(drop3);

  assertThat(error1.getStatus()).isEqualTo(drop1.getStatus());
  assertThat(error1).isNotEqualTo(drop1);
}
 
Example 9
Source File: GrpcRoutePicker.java    From saluki with Apache License 2.0 5 votes vote down vote up
@Override
public PickResult pickSubchannel(PickSubchannelArgs args) {
  Map<String, Object> affinity =
      args.getCallOptions().getOption(GrpcCallOptions.CALLOPTIONS_CUSTOME_KEY);
  GrpcURL refUrl = (GrpcURL) affinity.get(GrpcCallOptions.GRPC_REF_URL);
  if (size > 0) {
    Subchannel subchannel = nextSubchannel(refUrl);
    affinity.put(GrpcCallOptions.GRPC_NAMERESOVER_ATTRIBUTES, nameResovleCache);
    return PickResult.withSubchannel(subchannel);
  }
  if (status != null) {
    return PickResult.withError(status);
  }
  return PickResult.withNoResult();
}
 
Example 10
Source File: LoadBalancerTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void pickResult_equals() {
  PickResult sc1 = PickResult.withSubchannel(subchannel);
  PickResult sc2 = PickResult.withSubchannel(subchannel);
  PickResult sc3 = PickResult.withSubchannel(subchannel, tracerFactory);
  PickResult sc4 = PickResult.withSubchannel(subchannel2);
  PickResult nr = PickResult.withNoResult();
  PickResult error1 = PickResult.withError(status);
  PickResult error2 = PickResult.withError(status2);
  PickResult error3 = PickResult.withError(status2);
  PickResult drop1 = PickResult.withDrop(status);
  PickResult drop2 = PickResult.withDrop(status);
  PickResult drop3 = PickResult.withDrop(status2);

  assertThat(sc1).isNotEqualTo(nr);
  assertThat(sc1).isNotEqualTo(error1);
  assertThat(sc1).isNotEqualTo(drop1);
  assertThat(sc1).isEqualTo(sc2);
  assertThat(sc1).isNotEqualTo(sc3);
  assertThat(sc1).isNotEqualTo(sc4);

  assertThat(error1).isNotEqualTo(error2);
  assertThat(error2).isEqualTo(error3);

  assertThat(drop1).isEqualTo(drop2);
  assertThat(drop1).isNotEqualTo(drop3);

  assertThat(error1.getStatus()).isEqualTo(drop1.getStatus());
  assertThat(error1).isNotEqualTo(drop1);
}
 
Example 11
Source File: LoadBalancerTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void pickResult_withError() {
  PickResult result = PickResult.withError(status);
  assertThat(result.getSubchannel()).isNull();
  assertThat(result.getStatus()).isSameAs(status);
  assertThat(result.getStreamTracerFactory()).isNull();
  assertThat(result.isDrop()).isFalse();
}
 
Example 12
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 13
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 14
Source File: GrpclbState.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
ErrorEntry(Status status) {
  result = PickResult.withError(status);
}
 
Example 15
Source File: GrpclbState.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
ErrorEntry(Status status) {
  result = PickResult.withError(status);
}
 
Example 16
Source File: XdsSubchannelPickers.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public PickResult pickSubchannel(PickSubchannelArgs args) {
  return PickResult.withError(error);
}
 
Example 17
Source File: AutoConfiguredLoadBalancerFactory.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public PickResult pickSubchannel(PickSubchannelArgs args) {
  return PickResult.withError(failure);
}
 
Example 18
Source File: RoundRobinLoadBalancer.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public PickResult pickSubchannel(PickSubchannelArgs args) {
  return status.isOk() ? PickResult.withNoResult() : PickResult.withError(status);
}
 
Example 19
Source File: AutoConfiguredLoadBalancerFactory.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public PickResult pickSubchannel(PickSubchannelArgs args) {
  return PickResult.withError(failure);
}