Java Code Examples for io.grpc.ConnectivityState#READY

The following examples show how to use io.grpc.ConnectivityState#READY . 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: TripleClientTransport.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
protected boolean channelAvailable(ManagedChannel channel) {
    if (channel == null) {
        return false;
    }
    ConnectivityState state = channel.getState(false);
    if (ConnectivityState.READY == state) {
        return true;
    }
    if (ConnectivityState.SHUTDOWN == state || ConnectivityState.TRANSIENT_FAILURE == state) {
        return false;
    }
    if (ConnectivityState.IDLE == state || ConnectivityState.CONNECTING == state) {
        return true;
    }
    return false;
}
 
Example 2
Source File: GrpcClient.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private boolean checkChannel(final Endpoint endpoint) {
    final ManagedChannel ch = this.managedChannelPool.get(endpoint);
    if (ch == null) {
        return false;
    }
    final ConnectivityState st = ch.getState(true);
    return st == ConnectivityState.CONNECTING || st == ConnectivityState.READY || st == ConnectivityState.IDLE;
}
 
Example 3
Source File: MilvusGrpcClient.java    From milvus-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isConnected() {
  if (channel == null) {
    return false;
  }
  ConnectivityState connectivityState = channel.getState(false);
  return connectivityState == ConnectivityState.READY;
}
 
Example 4
Source File: MilvusGrpcClient.java    From milvus-sdk-java with Apache License 2.0 5 votes vote down vote up
private boolean channelIsReadyOrIdle() {
  if (channel == null) {
    return false;
  }
  ConnectivityState connectivityState = channel.getState(false);
  return connectivityState == ConnectivityState.READY
      || connectivityState
          == ConnectivityState.IDLE; // Since a new RPC would take the channel out of idle mode
}
 
Example 5
Source File: CachingRlsLbClient.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void onStatusChanged(ConnectivityState newState) {
  if (prevState == ConnectivityState.TRANSIENT_FAILURE
      && newState == ConnectivityState.READY) {
    synchronized (lock) {
      for (CacheEntry value : linkedHashLruCache.values()) {
        if (value instanceof BackoffCacheEntry) {
          ((BackoffCacheEntry) value).forceRefresh();
        }
      }
    }
  }
  prevState = newState;
}
 
Example 6
Source File: SubchannelStateManagerImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public ConnectivityState getAggregatedState() {
  if (stateMultiset.contains(ConnectivityState.READY)) {
    return ConnectivityState.READY;
  } else if (stateMultiset.contains(ConnectivityState.CONNECTING)) {
    return ConnectivityState.CONNECTING;
  } else if (stateMultiset.contains(ConnectivityState.IDLE)) {
    return ConnectivityState.IDLE;
  } else if (stateMultiset.contains(ConnectivityState.TRANSIENT_FAILURE)) {
    return ConnectivityState.TRANSIENT_FAILURE;
  }
  // empty or shutdown
  return ConnectivityState.IDLE;
}
 
Example 7
Source File: MilvusGrpcClient.java    From milvus-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public Response connect(ConnectParam connectParam) throws ConnectFailedException {
  if (channel != null && !(channel.isShutdown() || channel.isTerminated())) {
    logWarning("Channel is not shutdown or terminated");
    throw new ConnectFailedException("Channel is not shutdown or terminated");
  }

  try {

    channel =
        ManagedChannelBuilder.forAddress(connectParam.getHost(), connectParam.getPort())
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .keepAliveTime(
                connectParam.getKeepAliveTime(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS)
            .keepAliveTimeout(
                connectParam.getKeepAliveTimeout(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS)
            .keepAliveWithoutCalls(connectParam.isKeepAliveWithoutCalls())
            .idleTimeout(connectParam.getIdleTimeout(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS)
            .build();

    channel.getState(true);

    long timeout = connectParam.getConnectTimeout(TimeUnit.MILLISECONDS);
    logInfo("Trying to connect...Timeout in {} ms", timeout);

    final long checkFrequency = 100; // ms
    while (channel.getState(false) != ConnectivityState.READY) {
      if (timeout <= 0) {
        logError("Connect timeout!");
        throw new ConnectFailedException("Connect timeout");
      }
      TimeUnit.MILLISECONDS.sleep(checkFrequency);
      timeout -= checkFrequency;
    }

    blockingStub = MilvusServiceGrpc.newBlockingStub(channel);
    futureStub = MilvusServiceGrpc.newFutureStub(channel);

    // check server version
    String serverVersion = getServerVersion().getMessage();
    if (!serverVersion.contains("0.10.")) {
      logError(
          "Connect failed! Server version {} does not match SDK version 0.8.2", serverVersion);
      throw new ConnectFailedException("Failed to connect to Milvus server.");
    }

  } catch (Exception e) {
    if (!(e instanceof ConnectFailedException)) {
      logError("Connect failed! {}", e.toString());
    }
    throw new ConnectFailedException("Exception occurred: " + e.toString());
  }

  logInfo(
      "Connection established successfully to host={}, port={}",
      connectParam.getHost(),
      String.valueOf(connectParam.getPort()));
  return new Response(Response.Status.SUCCESS);
}
 
Example 8
Source File: GRPCChannel.java    From skywalking with Apache License 2.0 4 votes vote down vote up
public boolean isConnected(boolean requestConnection) {
    return originChannel.getState(requestConnection) == ConnectivityState.READY;
}
 
Example 9
Source File: GracefulSwitchLoadBalancer.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Gracefully switch to a new policy defined by the given factory, if the given factory isn't
 * equal to the current one.
 */
public void switchTo(LoadBalancer.Factory newBalancerFactory) {
  checkNotNull(newBalancerFactory, "newBalancerFactory");

  if (newBalancerFactory.equals(pendingBalancerFactory)) {
    return;
  }
  pendingLb.shutdown();
  pendingLb = defaultBalancer;
  pendingBalancerFactory = null;
  pendingState = ConnectivityState.CONNECTING;
  pendingPicker = BUFFER_PICKER;

  if (newBalancerFactory.equals(currentBalancerFactory)) {
    return;
  }

  class PendingHelper extends ForwardingLoadBalancerHelper {
    LoadBalancer lb;

    @Override
    protected Helper delegate() {
      return helper;
    }

    @Override
    public void updateBalancingState(ConnectivityState newState, SubchannelPicker newPicker) {
      if (lb == pendingLb) {
        checkState(currentLbIsReady, "there's pending lb while current lb has been out of READY");
        pendingState = newState;
        pendingPicker = newPicker;
        if (newState == ConnectivityState.READY) {
          swap();
        }
      } else if (lb == currentLb) {
        currentLbIsReady = newState == ConnectivityState.READY;
        if (!currentLbIsReady && pendingLb != defaultBalancer) {
          swap(); // current policy exits READY, so swap
        } else {
          helper.updateBalancingState(newState, newPicker);
        }
      }
    }
  }

  PendingHelper pendingHelper = new PendingHelper();
  pendingHelper.lb = newBalancerFactory.newLoadBalancer(pendingHelper);
  pendingLb = pendingHelper.lb;
  pendingBalancerFactory = newBalancerFactory;
  if (!currentLbIsReady) {
    swap(); // the old policy is not READY at the moment, so swap to the new one right now
  }
}