Search in sources :

Example 26 with CoreContext

use of com.couchbase.client.core.CoreContext 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)

Example 27 with CoreContext

use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.

the class GlobalRefresher method attemptUpdateGlobalConfig.

private Flux<ProposedGlobalConfigContext> attemptUpdateGlobalConfig(final Flux<PortInfo> nodes) {
    return nodes.flatMap(nodeInfo -> {
        CoreContext ctx = core.context();
        CarrierGlobalConfigRequest request = new CarrierGlobalConfigRequest(configRequestTimeout, ctx, FailFastRetryStrategy.INSTANCE, nodeInfo.identifier());
        core.send(request);
        return Reactor.wrap(request, request.response(), true).filter(response -> {
            // TODO: debug event that it got ignored.
            return response.status().success();
        }).map(response -> new ProposedGlobalConfigContext(new String(response.content(), UTF_8), nodeInfo.hostname())).onErrorResume(t -> {
            // TODO: raise a warning that fetching a config individual failed.
            return Mono.empty();
        });
    });
}
Also used : Disposable(reactor.core.Disposable) Reactor(com.couchbase.client.core.Reactor) UTF_8(java.nio.charset.StandardCharsets.UTF_8) GlobalConfig(com.couchbase.client.core.config.GlobalConfig) Mono(reactor.core.publisher.Mono) ProposedGlobalConfigContext(com.couchbase.client.core.config.ProposedGlobalConfigContext) KeyValueBucketRefresher.clampConfigRequestTimeout(com.couchbase.client.core.config.refresher.KeyValueBucketRefresher.clampConfigRequestTimeout) CarrierGlobalConfigRequest(com.couchbase.client.core.msg.kv.CarrierGlobalConfigRequest) FailFastRetryStrategy(com.couchbase.client.core.retry.FailFastRetryStrategy) ArrayList(java.util.ArrayList) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) MAX_PARALLEL_FETCH(com.couchbase.client.core.config.refresher.KeyValueBucketRefresher.MAX_PARALLEL_FETCH) List(java.util.List) POLLER_INTERVAL(com.couchbase.client.core.config.refresher.KeyValueBucketRefresher.POLLER_INTERVAL) ServiceType(com.couchbase.client.core.service.ServiceType) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) Core(com.couchbase.client.core.Core) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) PortInfo(com.couchbase.client.core.config.PortInfo) CoreContext(com.couchbase.client.core.CoreContext) CarrierGlobalConfigRequest(com.couchbase.client.core.msg.kv.CarrierGlobalConfigRequest) ProposedGlobalConfigContext(com.couchbase.client.core.config.ProposedGlobalConfigContext)

Example 28 with CoreContext

use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.

the class KeyValueBucketRefresher method fetchConfigPerNode.

/**
 * Helper method to fetch a config per node provided.
 *
 * <p>Note that the bucket config request sent here has a fail fast strategy, so that if nodes are offline they
 * do not circle the system forever (given they have a specific node target). Since the refresher polls every
 * fixed interval anyways, fresh requests will flood the system eventually and there is no point in keeping
 * the old ones around.</p>
 *
 * <p>Also, the timeout is set to the poll interval since it does not make sense to keep them around any
 * longer.</p>
 *
 * @param name the bucket name.
 * @param nodes the flux of nodes that can be used to fetch a config.
 * @return returns configs for each node if found.
 */
private Flux<ProposedBucketConfigContext> fetchConfigPerNode(final String name, final Flux<NodeInfo> nodes) {
    return nodes.flatMap(nodeInfo -> {
        CoreContext ctx = core.context();
        CarrierBucketConfigRequest request = new CarrierBucketConfigRequest(configRequestTimeout, ctx, new CollectionIdentifier(name, Optional.empty(), Optional.empty()), FailFastRetryStrategy.INSTANCE, nodeInfo.identifier());
        core.send(request);
        return Reactor.wrap(request, request.response(), true).filter(response -> {
            if (!response.status().success()) {
                eventBus.publish(new BucketConfigRefreshFailedEvent(core.context(), BucketConfigRefreshFailedEvent.RefresherType.KV, BucketConfigRefreshFailedEvent.Reason.INDIVIDUAL_REQUEST_FAILED, Optional.of(response)));
            }
            return response.status().success();
        }).map(response -> new ProposedBucketConfigContext(name, new String(response.content(), UTF_8), nodeInfo.hostname())).onErrorResume(t -> {
            eventBus.publish(new BucketConfigRefreshFailedEvent(core.context(), BucketConfigRefreshFailedEvent.RefresherType.KV, BucketConfigRefreshFailedEvent.Reason.INDIVIDUAL_REQUEST_FAILED, Optional.of(t)));
            return Mono.empty();
        });
    });
}
Also used : Disposable(reactor.core.Disposable) ArrayList(java.util.ArrayList) EventBus(com.couchbase.client.core.cnc.EventBus) ServiceType(com.couchbase.client.core.service.ServiceType) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) Map(java.util.Map) Stability(com.couchbase.client.core.annotation.Stability) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) BucketConfig(com.couchbase.client.core.config.BucketConfig) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) Reactor(com.couchbase.client.core.Reactor) UTF_8(java.nio.charset.StandardCharsets.UTF_8) NodeInfo(com.couchbase.client.core.config.NodeInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Mono(reactor.core.publisher.Mono) FailFastRetryStrategy(com.couchbase.client.core.retry.FailFastRetryStrategy) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Optional(java.util.Optional) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) BucketConfigRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.BucketConfigRefreshFailedEvent) CarrierBucketConfigRequest(com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest) Core(com.couchbase.client.core.Core) CoreContext(com.couchbase.client.core.CoreContext) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) BucketConfigRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.BucketConfigRefreshFailedEvent) CarrierBucketConfigRequest(com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier)

Example 29 with CoreContext

use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.

the class SaslListMechanismsHandlerTest method failConnectIfPromiseTimesOut.

/**
 * This test makes sure that the timer fires if the connect future is not completed
 * otherwise.
 */
@Test
void failConnectIfPromiseTimesOut() throws Exception {
    channel = new EmbeddedChannel();
    eventBus = new SimpleEventBus(true);
    CoreEnvironment env = mock(CoreEnvironment.class);
    TimeoutConfig timeoutConfig = mock(TimeoutConfig.class);
    when(env.eventBus()).thenReturn(eventBus);
    when(env.timeoutConfig()).thenReturn(timeoutConfig);
    when(timeoutConfig.connectTimeout()).thenReturn(Duration.ofMillis(100));
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, mock(Authenticator.class));
    EndpointContext endpointContext = new EndpointContext(coreContext, new HostAndPort("127.0.0.1", 1234), null, ServiceType.KV, Optional.empty(), Optional.empty(), Optional.empty());
    SaslListMechanismsHandler handler = new SaslListMechanismsHandler(endpointContext);
    channel.pipeline().addLast(handler);
    final ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    channel.pipeline().fireChannelActive();
    Thread.sleep(Duration.ofMillis(100).toMillis() + 5);
    channel.runScheduledPendingTasks();
    assertTrue(connect.isDone());
    assertTrue(connect.cause() instanceof TimeoutException);
    assertEquals("SASL Mechanism listing timed out after 100ms", connect.cause().getMessage());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) HostAndPort(com.couchbase.client.core.util.HostAndPort) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 30 with CoreContext

use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.

the class SelectBucketHandlerTest method setup.

@BeforeEach
void setup() {
    channel = new EmbeddedChannel();
    SimpleEventBus simpleEventBus = new SimpleEventBus(true);
    CoreEnvironment env = mock(CoreEnvironment.class);
    TimeoutConfig timeoutConfig = mock(TimeoutConfig.class);
    when(env.eventBus()).thenReturn(simpleEventBus);
    when(env.timeoutConfig()).thenReturn(timeoutConfig);
    when(timeoutConfig.connectTimeout()).thenReturn(Duration.ofMillis(10));
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, mock(Authenticator.class));
    endpointContext = new EndpointContext(coreContext, new HostAndPort("127.0.0.1", 1234), null, ServiceType.KV, Optional.empty(), Optional.empty(), Optional.empty());
}
Also used : HostAndPort(com.couchbase.client.core.util.HostAndPort) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

CoreContext (com.couchbase.client.core.CoreContext)52 Core (com.couchbase.client.core.Core)46 Test (org.junit.jupiter.api.Test)25 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)22 Authenticator (com.couchbase.client.core.env.Authenticator)17 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)14 BeforeEach (org.junit.jupiter.api.BeforeEach)13 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 ArrayList (java.util.ArrayList)10 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)9 HostAndPort (com.couchbase.client.core.util.HostAndPort)9 List (java.util.List)8 TimeoutConfig (com.couchbase.client.core.env.TimeoutConfig)7 ServiceContext (com.couchbase.client.core.service.ServiceContext)7 Duration (java.time.Duration)7 Optional (java.util.Optional)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 SeedNode (com.couchbase.client.core.env.SeedNode)6 Event (com.couchbase.client.core.cnc.Event)5