Java Code Examples for io.grpc.ConnectivityStateInfo#getState()

The following examples show how to use io.grpc.ConnectivityStateInfo#getState() . 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: RoundRobinLoadBalancer.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private void processSubchannelState(Subchannel subchannel, ConnectivityStateInfo stateInfo) {
  if (subchannels.get(stripAttrs(subchannel.getAddresses())) != subchannel) {
    return;
  }
  if (stateInfo.getState() == IDLE) {
    subchannel.requestConnection();
  }
  Ref<ConnectivityStateInfo> subchannelStateRef = getSubchannelStateInfoRef(subchannel);
  if (subchannelStateRef.value.getState().equals(TRANSIENT_FAILURE)) {
    if (stateInfo.getState().equals(CONNECTING) || stateInfo.getState().equals(IDLE)) {
      return;
    }
  }
  subchannelStateRef.value = stateInfo;
  updateBalancingState();
}
 
Example 2
Source File: OobChannel.java    From grpc-nebula-java with Apache License 2.0 6 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:
      delayedTransport.reprocess(new SubchannelPicker() {
          final PickResult errorResult = PickResult.withError(newState.getStatus());

          @Override
          public PickResult pickSubchannel(PickSubchannelArgs args) {
            return errorResult;
          }
        });
      break;
    default:
      // Do nothing
  }
}
 
Example 3
Source File: AgentClientMock.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public void handleSubchannelState(Subchannel subchannel, ConnectivityStateInfo stateInfo) {
    ConnectivityState currentState = stateInfo.getState();
    if (subchannel != this.subchannel || currentState == SHUTDOWN) {
        return;
    }

    PickResult pickResult;
    switch (currentState) {
        case CONNECTING:
            pickResult = PickResult.withNoResult();
            break;
        case READY:
        case IDLE:
            pickResult = PickResult.withSubchannel(subchannel);
            break;
        case TRANSIENT_FAILURE:
            pickResult = PickResult.withError(stateInfo.getStatus());
            break;
        default:
            throw new IllegalArgumentException("Unsupported state:" + currentState);
    }

    helper.updateBalancingState(currentState, new Picker(pickResult));
}
 
Example 4
Source File: GrpcRouteRoundRobinLbFactory.java    From saluki with Apache License 2.0 5 votes vote down vote up
/**
 * If all subchannels are TRANSIENT_FAILURE, return the Status associated with an arbitrary
 * subchannel otherwise, return null.
 */
@Nullable
private Status getAggregatedError() {
  Status status = null;
  for (Subchannel subchannel : getSubchannels()) {
    ConnectivityStateInfo stateInfo = getSubchannelStateInfoRef(subchannel).get();
    if (stateInfo.getState() != TRANSIENT_FAILURE) {
      return null;
    }
    status = stateInfo.getStatus();
  }
  return status;
}
 
Example 5
Source File: RoundRobinLoadBalancer.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Updates picker with the list of active subchannels (state == READY).
 */
@SuppressWarnings("ReferenceEquality")
private void updateBalancingState() {
  List<Subchannel> activeList = filterNonFailingSubchannels(getSubchannels());
  if (activeList.isEmpty()) {
    // No READY subchannels, determine aggregate state and error status
    boolean isConnecting = false;
    Status aggStatus = EMPTY_OK;
    for (Subchannel subchannel : getSubchannels()) {
      ConnectivityStateInfo stateInfo = getSubchannelStateInfoRef(subchannel).value;
      // This subchannel IDLE is not because of channel IDLE_TIMEOUT,
      // in which case LB is already shutdown.
      // RRLB will request connection immediately on subchannel IDLE.
      if (stateInfo.getState() == CONNECTING || stateInfo.getState() == IDLE) {
        isConnecting = true;
      }
      if (aggStatus == EMPTY_OK || !aggStatus.isOk()) {
        aggStatus = stateInfo.getStatus();
      }
    }
    updateBalancingState(isConnecting ? CONNECTING : TRANSIENT_FAILURE,
        // If all subchannels are TRANSIENT_FAILURE, return the Status associated with
        // an arbitrary subchannel, otherwise return OK.
        new EmptyPicker(aggStatus));
  } else {
    // initialize the Picker to a random start index to ensure that a high frequency of Picker
    // churn does not skew subchannel selection.
    int startIndex = random.nextInt(activeList.size());
    updateBalancingState(READY, new ReadyPicker(activeList, startIndex));
  }
}
 
Example 6
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 7
Source File: PickFirstLoadBalancer.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void processSubchannelState(Subchannel subchannel, ConnectivityStateInfo stateInfo) {
  ConnectivityState currentState = stateInfo.getState();
  if (currentState == SHUTDOWN) {
    return;
  }

  SubchannelPicker picker;
  switch (currentState) {
    case IDLE:
      picker = new RequestConnectionPicker(subchannel);
      break;
    case CONNECTING:
      // It's safe to use RequestConnectionPicker here, so when coming from IDLE we could leave
      // the current picker in-place. But ignoring the potential optimization is simpler.
      picker = new Picker(PickResult.withNoResult());
      break;
    case READY:
      picker = new Picker(PickResult.withSubchannel(subchannel));
      break;
    case TRANSIENT_FAILURE:
      picker = new Picker(PickResult.withError(stateInfo.getStatus()));
      break;
    default:
      throw new IllegalArgumentException("Unsupported state:" + currentState);
  }
  helper.updateBalancingState(currentState, picker);
}
 
Example 8
Source File: InternalSubchannel.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void gotoState(final ConnectivityStateInfo newState) {
  syncContext.throwIfNotInThisSynchronizationContext();

  if (state.getState() != newState.getState()) {
    Preconditions.checkState(state.getState() != SHUTDOWN,
        "Cannot transition out of SHUTDOWN to " + newState);
    state = newState;
    callback.onStateChange(InternalSubchannel.this, newState);
  }
}
 
Example 9
Source File: GrpclbState.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
void handleSubchannelState(Subchannel subchannel, ConnectivityStateInfo newState) {
  if (newState.getState() == SHUTDOWN || !subchannels.containsValue(subchannel)) {
    return;
  }
  if (config.getMode() == Mode.ROUND_ROBIN && newState.getState() == IDLE) {
    subchannel.requestConnection();
  }
  subchannel.getAttributes().get(STATE_INFO).set(newState);
  maybeUseFallbackBackends();
  maybeUpdatePicker();
}
 
Example 10
Source File: GrpclbState.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
void handleSubchannelState(Subchannel subchannel, ConnectivityStateInfo newState) {
  if (newState.getState() == SHUTDOWN || !subchannels.values().contains(subchannel)) {
    return;
  }
  if (newState.getState() == IDLE) {
    subchannel.requestConnection();
  }
  subchannel.getAttributes().get(STATE_INFO).set(newState);
  maybeUseFallbackBackends();
  maybeUpdatePicker();
}
 
Example 11
Source File: GrpcRouteRoundRobinLbFactory.java    From saluki with Apache License 2.0 5 votes vote down vote up
@Override
public void handleSubchannelState(Subchannel subchannel, ConnectivityStateInfo stateInfo) {
  if (!subchannels.containsValue(subchannel)) {
    return;
  }
  if (stateInfo.getState() == IDLE) {
    subchannel.requestConnection();
  }
  getSubchannelStateInfoRef(subchannel).set(stateInfo);
  updateBalancingState(getAggregatedState(), getAggregatedError());
}
 
Example 12
Source File: RoundRobinLoadBalancer.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * Updates picker with the list of active subchannels (state == READY).
 */
@SuppressWarnings("ReferenceEquality")
private void updateBalancingState() {
  List<Subchannel> activeList = filterNonFailingSubchannels(getSubchannels());
  if (activeList.isEmpty()) {
    // No READY subchannels, determine aggregate state and error status
    boolean isConnecting = false;
    Status aggStatus = EMPTY_OK;
    for (Subchannel subchannel : getSubchannels()) {
      ConnectivityStateInfo stateInfo = getSubchannelStateInfoRef(subchannel).value;
      // This subchannel IDLE is not because of channel IDLE_TIMEOUT,
      // in which case LB is already shutdown.
      // RRLB will request connection immediately on subchannel IDLE.
      if (stateInfo.getState() == CONNECTING || stateInfo.getState() == IDLE) {
        isConnecting = true;
      }
      if (aggStatus == EMPTY_OK || !aggStatus.isOk()) {
        aggStatus = stateInfo.getStatus();
      }
    }
    updateBalancingState(isConnecting ? CONNECTING : TRANSIENT_FAILURE,
        // If all subchannels are TRANSIENT_FAILURE, return the Status associated with
        // an arbitrary subchannel, otherwise return OK.
        new EmptyPicker(aggStatus));
  } else {
    // initialize the Picker to a random start index to ensure that a high frequency of Picker
    // churn does not skew subchannel selection.
    int startIndex = random.nextInt(activeList.size());
    updateBalancingState(READY, new ReadyPicker(activeList, startIndex, stickinessState));
  }
}
 
Example 13
Source File: RoundRobinLoadBalancer.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleSubchannelState(Subchannel subchannel, ConnectivityStateInfo stateInfo) {
  if (subchannels.get(subchannel.getAddresses()) != subchannel) {
    return;
  }
  if (stateInfo.getState() == SHUTDOWN && stickinessState != null) {
    stickinessState.remove(subchannel);
  }
  if (stateInfo.getState() == IDLE) {
    subchannel.requestConnection();
  }
  getSubchannelStateInfoRef(subchannel).value = stateInfo;
  updateBalancingState();
}
 
Example 14
Source File: PickFirstLoadBalancer.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleSubchannelState(Subchannel subchannel, ConnectivityStateInfo stateInfo) {
  ConnectivityState currentState = stateInfo.getState();
  if (currentState == SHUTDOWN) {
    return;
  }

  EquivalentAddressGroup addressGroup = subchannel.getAddresses();
  Subchannel theSubchannel = subchannels.get(addressGroup);
  if (theSubchannel == null) {
    return;
  }

  if (theSubchannel != currentSubchannel) {
    return;
  }

  SubchannelPicker picker;
  switch (currentState) {
    case IDLE:
      picker = new RequestConnectionPicker(subchannel);
      break;
    case CONNECTING:
      // It's safe to use RequestConnectionPicker here, so when coming from IDLE we could leave
      // the current picker in-place. But ignoring the potential optimization is simpler.
      picker = new Picker(PickResult.withNoResult());
      break;
    case READY:
      picker = new Picker(PickResult.withSubchannel(subchannel));
      break;
    case TRANSIENT_FAILURE:
      picker = new Picker(PickResult.withError(stateInfo.getStatus()));
      break;
    default:
      throw new IllegalArgumentException("Unsupported state:" + currentState);
  }

  helper.updateBalancingState(currentState, picker);
}
 
Example 15
Source File: InternalSubchannel.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
private void gotoState(final ConnectivityStateInfo newState) {
  if (state.getState() != newState.getState()) {
    Preconditions.checkState(state.getState() != SHUTDOWN,
        "Cannot transition out of SHUTDOWN to " + newState);
    state = newState;
    syncContext.executeLater(new Runnable() {
        @Override
        public void run() {
          callback.onStateChange(InternalSubchannel.this, newState);
        }
      });
  }
}
 
Example 16
Source File: GrpclbState.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * Make and use a picker out of the current lists and the states of subchannels if they have
 * changed since the last picker created.
 */
private void maybeUpdatePicker() {
  List<RoundRobinEntry> pickList = new ArrayList<>(backendList.size());
  Status error = null;
  boolean hasIdle = false;
  for (BackendEntry entry : backendList) {
    Subchannel subchannel = entry.result.getSubchannel();
    Attributes attrs = subchannel.getAttributes();
    ConnectivityStateInfo stateInfo = attrs.get(STATE_INFO).get();
    if (stateInfo.getState() == READY) {
      pickList.add(entry);
    } else if (stateInfo.getState() == TRANSIENT_FAILURE) {
      error = stateInfo.getStatus();
    } else if (stateInfo.getState() == IDLE) {
      hasIdle = true;
    }
  }
  ConnectivityState state;
  if (pickList.isEmpty()) {
    if (error != null && !hasIdle) {
      pickList.add(new ErrorEntry(error));
      state = TRANSIENT_FAILURE;
    } else {
      pickList.add(BUFFER_ENTRY);
      state = CONNECTING;
    }
  } else {
    state = READY;
  }
  maybeUpdatePicker(state, new RoundRobinPicker(dropList, pickList));
}
 
Example 17
Source File: ManagedChannelImpl.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
private void handleInternalSubchannelState(ConnectivityStateInfo newState) {
  if (newState.getState() == TRANSIENT_FAILURE || newState.getState() == IDLE) {
    refreshAndResetNameResolution();
  }
}
 
Example 18
Source File: ManagedChannelImpl.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
private void handleInternalSubchannelState(ConnectivityStateInfo newState) {
  if (newState.getState() == TRANSIENT_FAILURE || newState.getState() == IDLE) {
    nr.refresh();
  }
}