Search in sources :

Example 1 with Broadcaster

use of com.couchbase.connector.cluster.consul.rpc.Broadcaster in project couchbase-elasticsearch-connector by couchbase.

the class DocumentKeys method waitForClusterToQuiesce.

private void waitForClusterToQuiesce(Duration timeout) throws TimeoutException {
    final TimeoutEnforcer timeoutEnforcer = new TimeoutEnforcer(timeout);
    try (Broadcaster broadcaster = new Broadcaster()) {
        while (true) {
            final List<RpcEndpoint> allEndpoints = listRpcEndpoints();
            final Map<RpcEndpoint, RpcResult<Boolean>> results = broadcaster.broadcast("stopped?", allEndpoints, WorkerService.class, WorkerService::stopped);
            boolean stopped = true;
            for (Map.Entry<RpcEndpoint, RpcResult<Boolean>> e : results.entrySet()) {
                if (e.getValue().isFailed()) {
                    LOGGER.warn("Status request failed for endpoint " + e.getKey());
                    stopped = false;
                } else if (!e.getValue().get()) {
                    LOGGER.warn("Endpoint is still working: " + e.getKey());
                    stopped = false;
                }
            }
            if (stopped) {
                return;
            }
            timeoutEnforcer.throwIfExpired();
            LOGGER.info("Retrying in just a moment...");
            SECONDS.sleep(2);
        }
    } catch (Exception e) {
        throwIfUnchecked(e);
        propagateIfPossible(e, TimeoutException.class);
        throw new RuntimeException(e);
    }
}
Also used : RpcEndpoint(com.couchbase.connector.cluster.consul.rpc.RpcEndpoint) RpcResult(com.couchbase.connector.cluster.consul.rpc.RpcResult) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Broadcaster(com.couchbase.connector.cluster.consul.rpc.Broadcaster) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Broadcaster (com.couchbase.connector.cluster.consul.rpc.Broadcaster)1 RpcEndpoint (com.couchbase.connector.cluster.consul.rpc.RpcEndpoint)1 RpcResult (com.couchbase.connector.cluster.consul.rpc.RpcResult)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 Map (java.util.Map)1 TimeoutException (java.util.concurrent.TimeoutException)1