Search in sources :

Example 1 with EventBus

use of com.couchbase.client.core.cnc.EventBus in project couchbase-elasticsearch-connector by couchbase.

the class SeedNodeHelper method getKvNodes.

/**
 * Returns complete host and port information for every node running the KV service.
 */
public static Set<SeedNode> getKvNodes(Bucket bucket, String connectionString, boolean tls, NetworkResolution networkResolution, Duration timeout) {
    // Bucket config is most reliable way to determine KV ports
    BucketConfig config = CouchbaseHelper.getConfig(bucket, timeout);
    // so the actual node names can be used.
    if (networkResolution.equals(NetworkResolution.AUTO)) {
        EventBus eventBus = bucket.environment().eventBus();
        // The Java client calls seedNodesFromConnectionString during bootstrap, but the results
        // are not accessible here. Call it again.
        Set<String> seedHosts = ConnectionStringUtil.seedNodesFromConnectionString(connectionString, true, tls, eventBus).stream().map(SeedNode::address).collect(toSet());
        networkResolution = inferNetwork(config, networkResolution, seedHosts);
    }
    // Resolve alternate addresses
    // so lambda can access it
    NetworkResolution finalNetworkResolution = networkResolution;
    return config.nodes().stream().map(node -> new ResolvedNodeInfo(node, tls, finalNetworkResolution)).map(node -> SeedNode.create(node.host(), node.port(KV), node.port(MANAGER))).filter(node -> node.kvPort().isPresent()).collect(toSet());
}
Also used : KV(com.couchbase.client.core.service.ServiceType.KV) NodeInfo(com.couchbase.client.core.config.NodeInfo) CouchbaseException(com.couchbase.client.dcp.core.CouchbaseException) Set(java.util.Set) AlternateAddress(com.couchbase.client.core.config.AlternateAddress) ConnectionStringUtil(com.couchbase.client.core.util.ConnectionStringUtil) HashMap(java.util.HashMap) SeedNode(com.couchbase.client.core.env.SeedNode) MANAGER(com.couchbase.client.core.service.ServiceType.MANAGER) RedactableArgument.system(com.couchbase.client.dcp.core.logging.RedactableArgument.system) Bucket(com.couchbase.client.java.Bucket) EventBus(com.couchbase.client.core.cnc.EventBus) NetworkResolution(com.couchbase.client.core.env.NetworkResolution) ServiceType(com.couchbase.client.core.service.ServiceType) CouchbaseHelper(com.couchbase.connector.dcp.CouchbaseHelper) Duration(java.time.Duration) Map(java.util.Map) Optional(java.util.Optional) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) BucketConfig(com.couchbase.client.core.config.BucketConfig) Collectors.toSet(java.util.stream.Collectors.toSet) NetworkResolution(com.couchbase.client.core.env.NetworkResolution) EventBus(com.couchbase.client.core.cnc.EventBus) BucketConfig(com.couchbase.client.core.config.BucketConfig)

Example 2 with EventBus

use of com.couchbase.client.core.cnc.EventBus in project kafka-connect-couchbase by couchbase.

the class SeedNodeHelper method getKvNodes.

/**
 * Returns complete host and port information for every node running the KV service.
 */
public static Set<SeedNode> getKvNodes(Bucket bucket, String connectionString, boolean tls, NetworkResolution networkResolution, Duration timeout) {
    // Bucket config is most reliable way to determine KV ports
    BucketConfig config = CouchbaseHelper.getConfig(bucket, timeout);
    // so the actual node names can be used.
    if (networkResolution.equals(NetworkResolution.AUTO)) {
        EventBus eventBus = bucket.environment().eventBus();
        // The Java client calls seedNodesFromConnectionString during bootstrap, but the results
        // are not accessible here. Call it again.
        Set<String> seedHosts = ConnectionStringUtil.seedNodesFromConnectionString(connectionString, true, tls, eventBus).stream().map(SeedNode::address).collect(toSet());
        networkResolution = inferNetwork(config, networkResolution, seedHosts);
    }
    // Resolve alternate addresses
    // so lambda can access it
    NetworkResolution finalNetworkResolution = networkResolution;
    return config.nodes().stream().map(node -> new ResolvedNodeInfo(node, tls, finalNetworkResolution)).map(node -> SeedNode.create(node.host(), node.port(KV), node.port(MANAGER))).filter(node -> node.kvPort().isPresent()).collect(toSet());
}
Also used : KV(com.couchbase.client.core.service.ServiceType.KV) NodeInfo(com.couchbase.client.core.config.NodeInfo) CouchbaseException(com.couchbase.client.dcp.core.CouchbaseException) Set(java.util.Set) AlternateAddress(com.couchbase.client.core.config.AlternateAddress) ConnectionStringUtil(com.couchbase.client.core.util.ConnectionStringUtil) HashMap(java.util.HashMap) SeedNode(com.couchbase.client.core.env.SeedNode) MANAGER(com.couchbase.client.core.service.ServiceType.MANAGER) RedactableArgument.system(com.couchbase.client.dcp.core.logging.RedactableArgument.system) Bucket(com.couchbase.client.java.Bucket) EventBus(com.couchbase.client.core.cnc.EventBus) NetworkResolution(com.couchbase.client.core.env.NetworkResolution) ServiceType(com.couchbase.client.core.service.ServiceType) Duration(java.time.Duration) Map(java.util.Map) Optional(java.util.Optional) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) BucketConfig(com.couchbase.client.core.config.BucketConfig) Collectors.toSet(java.util.stream.Collectors.toSet) NetworkResolution(com.couchbase.client.core.env.NetworkResolution) EventBus(com.couchbase.client.core.cnc.EventBus) BucketConfig(com.couchbase.client.core.config.BucketConfig)

Example 3 with EventBus

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

the class DefaultConfigurationProvider method loadAndRefreshGlobalConfig.

@Override
public Mono<Void> loadAndRefreshGlobalConfig() {
    return Mono.defer(() -> {
        if (!shutdown.get()) {
            globalConfigLoadInProgress = true;
            boolean tls = core.context().environment().securityConfig().tlsEnabled();
            int kvPort = tls ? DEFAULT_KV_TLS_PORT : DEFAULT_KV_PORT;
            final AtomicBoolean hasErrored = new AtomicBoolean();
            return Flux.range(1, Math.min(MAX_PARALLEL_LOADERS, currentSeedNodes().size())).flatMap(index -> Flux.fromIterable(currentSeedNodes()).take(Math.min(index, currentSeedNodes().size())).last().flatMap(seed -> {
                long start = System.nanoTime();
                if (!currentSeedNodes().contains(seed)) {
                    // node we are about to load is still part of the list.
                    return Mono.empty();
                }
                NodeIdentifier identifier = new NodeIdentifier(seed.address(), seed.clusterManagerPort().orElse(DEFAULT_MANAGER_PORT));
                return globalLoader.load(identifier, seed.kvPort().orElse(kvPort)).doOnError(throwable -> core.context().environment().eventBus().publish(new IndividualGlobalConfigLoadFailedEvent(Duration.ofNanos(System.nanoTime() - start), core.context(), throwable, seed.address())));
            }).retryWhen(Retry.from(companion -> companion.flatMap(rs -> {
                Throwable f = rs.failure();
                if (shutdown.get()) {
                    return Mono.error(new AlreadyShutdownException());
                }
                if (f instanceof UnsupportedConfigMechanismException) {
                    return Mono.error(Exceptions.propagate(f));
                }
                Duration delay = Duration.ofMillis(1);
                eventBus.publish(new GlobalConfigRetriedEvent(delay, core.context(), f));
                return Mono.just(rs.totalRetries()).delayElement(delay, core.context().environment().scheduler());
            }))).onErrorResume(throwable -> {
                if (hasErrored.compareAndSet(false, true)) {
                    return Mono.error(throwable);
                }
                return Mono.empty();
            })).take(1).switchIfEmpty(Mono.error(new ConfigException("Could not locate a single global configuration"))).map(ctx -> {
                proposeGlobalConfig(ctx);
                return ctx;
            }).then(globalRefresher.start()).doOnTerminate(() -> globalConfigLoadInProgress = false);
        } else {
            return Mono.error(new AlreadyShutdownException());
        }
    });
}
Also used : KeyValueBucketRefresher(com.couchbase.client.core.config.refresher.KeyValueBucketRefresher) Retry(reactor.util.retry.Retry) UnsignedLEB128(com.couchbase.client.core.util.UnsignedLEB128) BucketConfigUpdatedEvent(com.couchbase.client.core.cnc.events.config.BucketConfigUpdatedEvent) ClusterManagerBucketLoader(com.couchbase.client.core.config.loader.ClusterManagerBucketLoader) BestEffortRetryStrategy(com.couchbase.client.core.retry.BestEffortRetryStrategy) JsonNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode) EventBus(com.couchbase.client.core.cnc.EventBus) NetworkResolution(com.couchbase.client.core.env.NetworkResolution) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceType(com.couchbase.client.core.service.ServiceType) Duration(java.time.Duration) Map(java.util.Map) GlobalConfigRetriedEvent(com.couchbase.client.core.cnc.events.config.GlobalConfigRetriedEvent) GlobalConfigUpdatedEvent(com.couchbase.client.core.cnc.events.config.GlobalConfigUpdatedEvent) CollectionMapRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshFailedEvent) Mapper(com.couchbase.client.core.json.Mapper) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) BucketNotFoundDuringLoadException(com.couchbase.client.core.error.BucketNotFoundDuringLoadException) Collectors(java.util.stream.Collectors) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) BucketOpenRetriedEvent(com.couchbase.client.core.cnc.events.config.BucketOpenRetriedEvent) ConfigIgnoredEvent(com.couchbase.client.core.cnc.events.config.ConfigIgnoredEvent) SeedNodesUpdatedEvent(com.couchbase.client.core.cnc.events.config.SeedNodesUpdatedEvent) Optional(java.util.Optional) ClusterManagerBucketRefresher(com.couchbase.client.core.config.refresher.ClusterManagerBucketRefresher) Exceptions(reactor.core.Exceptions) GlobalRefresher(com.couchbase.client.core.config.refresher.GlobalRefresher) UnsupportedConfigMechanismException(com.couchbase.client.core.error.UnsupportedConfigMechanismException) NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) FluxSink(reactor.core.publisher.FluxSink) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SeedNode(com.couchbase.client.core.env.SeedNode) AtomicReference(java.util.concurrent.atomic.AtomicReference) IndividualGlobalConfigLoadFailedEvent(com.couchbase.client.core.cnc.events.config.IndividualGlobalConfigLoadFailedEvent) ConfigException(com.couchbase.client.core.error.ConfigException) CollectionMapRefreshIgnoredEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshIgnoredEvent) RequestCanceledException(com.couchbase.client.core.error.RequestCanceledException) CancellationReason(com.couchbase.client.core.msg.CancellationReason) CbCollections.copyToUnmodifiableSet(com.couchbase.client.core.util.CbCollections.copyToUnmodifiableSet) BucketNotReadyDuringLoadException(com.couchbase.client.core.error.BucketNotReadyDuringLoadException) GlobalLoader(com.couchbase.client.core.config.loader.GlobalLoader) Mono(reactor.core.publisher.Mono) KeyValueBucketLoader(com.couchbase.client.core.config.loader.KeyValueBucketLoader) GetCollectionIdRequest(com.couchbase.client.core.msg.kv.GetCollectionIdRequest) ReplayProcessor(reactor.core.publisher.ReplayProcessor) Flux(reactor.core.publisher.Flux) SeedNodeOutdatedException(com.couchbase.client.core.error.SeedNodeOutdatedException) CollectionMap(com.couchbase.client.core.io.CollectionMap) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) AlreadyShutdownException(com.couchbase.client.core.error.AlreadyShutdownException) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) Core(com.couchbase.client.core.Core) CollectionMapRefreshSucceededEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshSucceededEvent) Duration(java.time.Duration) ConfigException(com.couchbase.client.core.error.ConfigException) IndividualGlobalConfigLoadFailedEvent(com.couchbase.client.core.cnc.events.config.IndividualGlobalConfigLoadFailedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) GlobalConfigRetriedEvent(com.couchbase.client.core.cnc.events.config.GlobalConfigRetriedEvent) AlreadyShutdownException(com.couchbase.client.core.error.AlreadyShutdownException) UnsupportedConfigMechanismException(com.couchbase.client.core.error.UnsupportedConfigMechanismException)

Example 4 with EventBus

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

the class MemcacheProtocolTest method before.

@BeforeEach
void before() {
    eventBus = mock(EventBus.class);
    CoreEnvironment env = mock(CoreEnvironment.class);
    context = new CoreContext(mock(Core.class), 1, env, mock(Authenticator.class));
    when(env.eventBus()).thenReturn(eventBus);
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) EventBus(com.couchbase.client.core.cnc.EventBus) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

EventBus (com.couchbase.client.core.cnc.EventBus)4 NetworkResolution (com.couchbase.client.core.env.NetworkResolution)3 SeedNode (com.couchbase.client.core.env.SeedNode)3 ServiceType (com.couchbase.client.core.service.ServiceType)3 Duration (java.time.Duration)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Set (java.util.Set)3 AlternateAddress (com.couchbase.client.core.config.AlternateAddress)2 BucketConfig (com.couchbase.client.core.config.BucketConfig)2 NodeInfo (com.couchbase.client.core.config.NodeInfo)2 KV (com.couchbase.client.core.service.ServiceType.KV)2 MANAGER (com.couchbase.client.core.service.ServiceType.MANAGER)2 ConnectionStringUtil (com.couchbase.client.core.util.ConnectionStringUtil)2 CouchbaseException (com.couchbase.client.dcp.core.CouchbaseException)2 RedactableArgument.system (com.couchbase.client.dcp.core.logging.RedactableArgument.system)2 Bucket (com.couchbase.client.java.Bucket)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)2 HashMap (java.util.HashMap)2 Collectors.toSet (java.util.stream.Collectors.toSet)2