Search in sources :

Example 1 with Retry

use of com.couchbase.client.core.retry.reactor.Retry in project couchbase-jvm-clients by couchbase.

the class ClusterManagerBucketRefresher method registerStream.

/**
 * Registers the given bucket name with the http stream.
 *
 * <p>Note that this method deliberately subscribes "out of band" and not being flatMapped into the
 * {@link #register(String)} return value. The idea is that the flux config subscription keeps on going
 * forever until specifically unsubscribed through either {@link #deregister(String)} or {@link #shutdown()}.</p>
 *
 * @param ctx the core context to use.
 * @param name the name of the bucket.
 * @return once registered, returns the disposable so it can be later used to deregister.
 */
private Disposable registerStream(final CoreContext ctx, final String name) {
    return Mono.defer(() -> {
        BucketConfigStreamingRequest request = new BucketConfigStreamingRequest(ctx.environment().timeoutConfig().managementTimeout(), ctx, BestEffortRetryStrategy.INSTANCE, name, ctx.authenticator());
        core.send(request);
        return Reactor.wrap(request, request.response(), true);
    }).flux().flatMap(res -> {
        if (res.status().success()) {
            return res.configs().map(config -> new ProposedBucketConfigContext(name, config, res.address()));
        } else {
            eventBus.publish(new BucketConfigRefreshFailedEvent(core.context(), BucketConfigRefreshFailedEvent.RefresherType.MANAGER, BucketConfigRefreshFailedEvent.Reason.INDIVIDUAL_REQUEST_FAILED, Optional.of(res)));
            // and retry the whole thing
            return Flux.error(new ConfigException());
        }
    }).doOnError(e -> eventBus.publish(new BucketConfigRefreshFailedEvent(core.context(), BucketConfigRefreshFailedEvent.RefresherType.MANAGER, BucketConfigRefreshFailedEvent.Reason.STREAM_FAILED, Optional.of(e)))).doOnComplete(() -> {
        eventBus.publish(new BucketConfigRefreshFailedEvent(core.context(), BucketConfigRefreshFailedEvent.RefresherType.MANAGER, BucketConfigRefreshFailedEvent.Reason.STREAM_CLOSED, Optional.empty()));
        // handled in the retryWhen below.
        throw new ConfigException();
    }).retryWhen(Retry.any().exponentialBackoff(Duration.ofMillis(32), Duration.ofMillis(4096)).toReactorRetry()).subscribe(provider::proposeBucketConfig);
}
Also used : Disposable(reactor.core.Disposable) Reactor(com.couchbase.client.core.Reactor) Retry(com.couchbase.client.core.retry.reactor.Retry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BestEffortRetryStrategy(com.couchbase.client.core.retry.BestEffortRetryStrategy) Mono(reactor.core.publisher.Mono) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) Flux(reactor.core.publisher.Flux) EventBus(com.couchbase.client.core.cnc.EventBus) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) Map(java.util.Map) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) ConfigException(com.couchbase.client.core.error.ConfigException) Optional(java.util.Optional) BucketConfigRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.BucketConfigRefreshFailedEvent) Core(com.couchbase.client.core.Core) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) ConfigException(com.couchbase.client.core.error.ConfigException) BucketConfigRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.BucketConfigRefreshFailedEvent) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest)

Aggregations

Core (com.couchbase.client.core.Core)1 CoreContext (com.couchbase.client.core.CoreContext)1 Reactor (com.couchbase.client.core.Reactor)1 EventBus (com.couchbase.client.core.cnc.EventBus)1 BucketConfigRefreshFailedEvent (com.couchbase.client.core.cnc.events.config.BucketConfigRefreshFailedEvent)1 ConfigurationProvider (com.couchbase.client.core.config.ConfigurationProvider)1 ProposedBucketConfigContext (com.couchbase.client.core.config.ProposedBucketConfigContext)1 ConfigException (com.couchbase.client.core.error.ConfigException)1 BucketConfigStreamingRequest (com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest)1 BestEffortRetryStrategy (com.couchbase.client.core.retry.BestEffortRetryStrategy)1 Retry (com.couchbase.client.core.retry.reactor.Retry)1 Duration (java.time.Duration)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Disposable (reactor.core.Disposable)1 Flux (reactor.core.publisher.Flux)1 Mono (reactor.core.publisher.Mono)1