Search in sources :

Example 1 with NodeIdentifier

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

the class GlobalLoaderIntegrationTest method loadGlobalConfigViaCarrierPublication.

/**
 * This is a very simplistic test that makes sure that we can "round trip" in the
 * {@link GlobalLoader} by grabbing a JSON decodable config through the full stack.
 */
@Test
void loadGlobalConfigViaCarrierPublication() {
    TestNodeConfig config = config().firstNodeWith(Services.KV).get();
    Core core = Core.create(env, authenticator(), seedNodes());
    GlobalLoader loader = new GlobalLoader(core);
    ProposedGlobalConfigContext globalConfigContext = loader.load(new NodeIdentifier(config.hostname(), config.ports().get(Services.MANAGER)), config.ports().get(Services.KV)).block();
    assertNotNull(globalConfigContext);
    assertNotNull(globalConfigContext.config());
    core.shutdown().block();
}
Also used : TestNodeConfig(com.couchbase.client.test.TestNodeConfig) ProposedGlobalConfigContext(com.couchbase.client.core.config.ProposedGlobalConfigContext) NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) Core(com.couchbase.client.core.Core) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with NodeIdentifier

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

the class KeyValueBucketLoaderIntegrationTest method loadConfigViaCarrierPublication.

/**
 * This is a very simplistic test that makes sure that we can "round trip" in the
 * {@link KeyValueBucketLoader} by grabbing a JSON decodable config through the full stack.
 */
@Test
void loadConfigViaCarrierPublication() {
    TestNodeConfig config = config().firstNodeWith(Services.KV).get();
    Core core = Core.create(env, authenticator(), seedNodes());
    KeyValueBucketLoader loader = new KeyValueBucketLoader(core);
    ProposedBucketConfigContext loaded = loader.load(new NodeIdentifier(config.hostname(), config.ports().get(Services.MANAGER)), config.ports().get(Services.KV), config().bucketname(), Optional.empty()).block();
    assertNotNull(loaded);
    assertEquals(config().bucketname(), loaded.bucketName());
    core.shutdown().block();
}
Also used : TestNodeConfig(com.couchbase.client.test.TestNodeConfig) NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) Core(com.couchbase.client.core.Core) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with NodeIdentifier

use of com.couchbase.client.core.node.NodeIdentifier 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 NodeIdentifier

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

the class CoreTest method removeNodesAndServicesOnNewConfig.

@Test
@SuppressWarnings("unchecked")
void removeNodesAndServicesOnNewConfig() {
    final ConfigurationProvider configProvider = mock(ConfigurationProvider.class);
    DirectProcessor<ClusterConfig> configs = DirectProcessor.create();
    ClusterConfig clusterConfig = new ClusterConfig();
    when(configProvider.configs()).thenReturn(configs);
    when(configProvider.config()).thenReturn(clusterConfig);
    Node mock101 = mock(Node.class);
    when(mock101.identifier()).thenReturn(new NodeIdentifier("10.143.190.101", 8091));
    when(mock101.addService(any(ServiceType.class), anyInt(), any(Optional.class))).thenReturn(Mono.empty());
    when(mock101.removeService(any(ServiceType.class), any(Optional.class))).thenReturn(Mono.empty());
    when(mock101.serviceEnabled(any(ServiceType.class))).thenReturn(true);
    when(mock101.disconnect()).thenReturn(Mono.empty());
    Node mock102 = mock(Node.class);
    when(mock102.identifier()).thenReturn(new NodeIdentifier("10.143.190.102", 8091));
    when(mock102.addService(any(ServiceType.class), anyInt(), any(Optional.class))).thenReturn(Mono.empty());
    when(mock102.removeService(any(ServiceType.class), any(Optional.class))).thenReturn(Mono.empty());
    when(mock102.serviceEnabled(any(ServiceType.class))).thenReturn(true);
    when(mock102.disconnect()).thenReturn(Mono.empty());
    final Map<String, Node> mocks = new HashMap<>();
    mocks.put("10.143.190.101", mock101);
    mocks.put("10.143.190.102", mock102);
    new Core(ENV, AUTHENTICATOR, SeedNode.LOCALHOST) {

        @Override
        public ConfigurationProvider createConfigurationProvider() {
            return configProvider;
        }

        @Override
        protected Node createNode(final NodeIdentifier target, final Optional<String> alternate) {
            return mocks.get(target.address());
        }
    };
    configs.onNext(clusterConfig);
    BucketConfig twoNodesConfig = BucketConfigParser.parse(readResource("two_nodes_config_more_services.json", CoreTest.class), ENV, LOCALHOST);
    clusterConfig.setBucketConfig(twoNodesConfig);
    configs.onNext(clusterConfig);
    verify(mock101, times(1)).addService(ServiceType.VIEWS, 8092, Optional.empty());
    verify(mock101, times(1)).addService(ServiceType.MANAGER, 8091, Optional.empty());
    verify(mock101, times(1)).addService(ServiceType.QUERY, 8093, Optional.empty());
    verify(mock101, times(1)).addService(ServiceType.KV, 11210, Optional.of("travel-sample"));
    verify(mock102, times(1)).addService(ServiceType.VIEWS, 8092, Optional.empty());
    verify(mock102, times(1)).addService(ServiceType.MANAGER, 8091, Optional.empty());
    verify(mock102, times(1)).addService(ServiceType.QUERY, 8093, Optional.empty());
    verify(mock102, times(1)).addService(ServiceType.KV, 11210, Optional.of("travel-sample"));
    verify(mock102, times(1)).addService(ServiceType.SEARCH, 8094, Optional.empty());
    BucketConfig twoNodesLessServices = BucketConfigParser.parse(readResource("two_nodes_config.json", CoreTest.class), ENV, LOCALHOST);
    clusterConfig.setBucketConfig(twoNodesLessServices);
    configs.onNext(clusterConfig);
    verify(mock102, times(1)).removeService(ServiceType.SEARCH, Optional.empty());
}
Also used : Optional(java.util.Optional) HashMap(java.util.HashMap) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) SeedNode(com.couchbase.client.core.env.SeedNode) Node(com.couchbase.client.core.node.Node) BucketConfig(com.couchbase.client.core.config.BucketConfig) ServiceType(com.couchbase.client.core.service.ServiceType) NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Example 5 with NodeIdentifier

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

the class CoreTest method addsSecondNodeIfBothSameHostname.

/**
 * With cluster_run it is possible to run more than one node on the same hostname. So we need to make sure that
 * the node is identified by a tuple of hostname and manager port, and this should work.
 */
@Test
@SuppressWarnings("unchecked")
void addsSecondNodeIfBothSameHostname() {
    final ConfigurationProvider configProvider = mock(ConfigurationProvider.class);
    DirectProcessor<ClusterConfig> configs = DirectProcessor.create();
    ClusterConfig clusterConfig = new ClusterConfig();
    when(configProvider.configs()).thenReturn(configs);
    when(configProvider.config()).thenReturn(clusterConfig);
    Node mock101 = mock(Node.class);
    when(mock101.identifier()).thenReturn(new NodeIdentifier(LOCALHOST, 9000));
    when(mock101.addService(any(ServiceType.class), anyInt(), any(Optional.class))).thenReturn(Mono.empty());
    when(mock101.removeService(any(ServiceType.class), any(Optional.class))).thenReturn(Mono.empty());
    when(mock101.serviceEnabled(any(ServiceType.class))).thenReturn(true);
    when(mock101.disconnect()).thenReturn(Mono.empty());
    Node mock102 = mock(Node.class);
    when(mock102.identifier()).thenReturn(new NodeIdentifier(LOCALHOST, 9001));
    when(mock102.addService(any(ServiceType.class), anyInt(), any(Optional.class))).thenReturn(Mono.empty());
    when(mock102.removeService(any(ServiceType.class), any(Optional.class))).thenReturn(Mono.empty());
    when(mock102.serviceEnabled(any(ServiceType.class))).thenReturn(true);
    when(mock102.disconnect()).thenReturn(Mono.empty());
    final Map<String, Node> mocks = new HashMap<>();
    mocks.put("127.0.0.1:9000", mock101);
    mocks.put("127.0.0.1:9001", mock102);
    new Core(ENV, AUTHENTICATOR, SeedNode.LOCALHOST) {

        @Override
        public ConfigurationProvider createConfigurationProvider() {
            return configProvider;
        }

        @Override
        protected Node createNode(final NodeIdentifier target, final Optional<String> alternate) {
            return mocks.get(target.address() + ":" + target.managerPort());
        }
    };
    configs.onNext(clusterConfig);
    BucketConfig oneNodeConfig = BucketConfigParser.parse(readResource("cluster_run_two_nodes.json", CoreTest.class), ENV, LOCALHOST);
    clusterConfig.setBucketConfig(oneNodeConfig);
    configs.onNext(clusterConfig);
    verify(mock101, times(1)).addService(ServiceType.VIEWS, 9500, Optional.empty());
    verify(mock101, times(1)).addService(ServiceType.MANAGER, 9000, Optional.empty());
    verify(mock101, times(1)).addService(ServiceType.KV, 12000, Optional.of("default"));
    verify(mock102, times(1)).addService(ServiceType.VIEWS, 9501, Optional.empty());
    verify(mock102, times(1)).addService(ServiceType.MANAGER, 9001, Optional.empty());
    verify(mock102, times(1)).addService(ServiceType.KV, 12002, Optional.of("default"));
}
Also used : Optional(java.util.Optional) HashMap(java.util.HashMap) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) SeedNode(com.couchbase.client.core.env.SeedNode) Node(com.couchbase.client.core.node.Node) BucketConfig(com.couchbase.client.core.config.BucketConfig) ServiceType(com.couchbase.client.core.service.ServiceType) NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Aggregations

NodeIdentifier (com.couchbase.client.core.node.NodeIdentifier)15 Test (org.junit.jupiter.api.Test)11 Optional (java.util.Optional)9 BucketConfig (com.couchbase.client.core.config.BucketConfig)8 SeedNode (com.couchbase.client.core.env.SeedNode)8 Core (com.couchbase.client.core.Core)7 ServiceType (com.couchbase.client.core.service.ServiceType)7 HashMap (java.util.HashMap)6 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)5 ConfigurationProvider (com.couchbase.client.core.config.ConfigurationProvider)5 Node (com.couchbase.client.core.node.Node)5 TestNodeConfig (com.couchbase.client.test.TestNodeConfig)4 ProposedBucketConfigContext (com.couchbase.client.core.config.ProposedBucketConfigContext)3 GlobalLoader (com.couchbase.client.core.config.loader.GlobalLoader)3 EventBus (com.couchbase.client.core.cnc.EventBus)2 BucketConfigUpdatedEvent (com.couchbase.client.core.cnc.events.config.BucketConfigUpdatedEvent)2 BucketOpenRetriedEvent (com.couchbase.client.core.cnc.events.config.BucketOpenRetriedEvent)2 CollectionMapRefreshFailedEvent (com.couchbase.client.core.cnc.events.config.CollectionMapRefreshFailedEvent)2 CollectionMapRefreshIgnoredEvent (com.couchbase.client.core.cnc.events.config.CollectionMapRefreshIgnoredEvent)2 CollectionMapRefreshSucceededEvent (com.couchbase.client.core.cnc.events.config.CollectionMapRefreshSucceededEvent)2