Search in sources :

Example 16 with BucketConfig

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

the class NodeLocatorHelper method replicaNodesForId.

/**
 * Returns all target replica nodes addresses for a given document ID on the bucket.
 *
 * @param id the document id to convert.
 * @return the node for the given document id.
 */
public List<String> replicaNodesForId(final String id) {
    BucketConfig config = bucketConfig.get();
    if (config instanceof CouchbaseBucketConfig) {
        CouchbaseBucketConfig cbc = (CouchbaseBucketConfig) config;
        List<String> replicas = new ArrayList<>();
        for (int i = 1; i <= cbc.numberOfReplicas(); i++) {
            replicas.add(replicaNodeForId(id, i));
        }
        return replicas;
    } else {
        throw new UnsupportedOperationException("Bucket type not supported: " + config.getClass().getName());
    }
}
Also used : CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) MemcachedBucketConfig(com.couchbase.client.core.config.MemcachedBucketConfig) BucketConfig(com.couchbase.client.core.config.BucketConfig)

Example 17 with BucketConfig

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

the class NodeLocatorHelper method replicaNodeForId.

/**
 * Returns the target replica node address for a given document ID and replica number on the bucket.
 *
 * @param id the document id to convert.
 * @param replicaNum the replica number.
 * @return the node for the given document id.
 */
public String replicaNodeForId(final String id, int replicaNum) {
    if (replicaNum < 1 || replicaNum > 3) {
        throw new IllegalArgumentException("Replica number must be between 1 and 3.");
    }
    BucketConfig config = bucketConfig.get();
    if (config instanceof CouchbaseBucketConfig) {
        CouchbaseBucketConfig cbc = (CouchbaseBucketConfig) config;
        int partitionId = (int) hashId(id) & cbc.numberOfPartitions() - 1;
        int nodeId = cbc.nodeIndexForReplica(partitionId, replicaNum - 1, false);
        if (nodeId == -1) {
            throw new IllegalStateException("No partition assigned to node for Document ID: " + id);
        }
        if (nodeId == -2) {
            throw new IllegalStateException("Replica not configured for this bucket.");
        }
        return cbc.nodeAtIndex(nodeId).hostname();
    } else {
        throw new UnsupportedOperationException("Bucket type not supported: " + config.getClass().getName());
    }
}
Also used : CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) MemcachedBucketConfig(com.couchbase.client.core.config.MemcachedBucketConfig) BucketConfig(com.couchbase.client.core.config.BucketConfig)

Example 18 with BucketConfig

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

the class CoreTest method addNodesAndServicesOnNewConfig.

/**
 * This test initializes with a first config and then pushes a second one, making sure that
 * the difference in services and nodes is enabled.
 */
@Test
@SuppressWarnings({ "unchecked" })
void addNodesAndServicesOnNewConfig() {
    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 oneNodeConfig = BucketConfigParser.parse(readResource("one_node_config.json", CoreTest.class), ENV, LOCALHOST);
    clusterConfig.setBucketConfig(oneNodeConfig);
    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, never()).addService(ServiceType.VIEWS, 8092, Optional.empty());
    verify(mock102, never()).addService(ServiceType.MANAGER, 8091, Optional.empty());
    verify(mock102, never()).addService(ServiceType.QUERY, 8093, Optional.empty());
    verify(mock102, never()).addService(ServiceType.KV, 11210, Optional.of("travel-sample"));
    BucketConfig twoNodeConfig = BucketConfigParser.parse(readResource("two_nodes_config.json", CoreTest.class), ENV, LOCALHOST);
    clusterConfig.setBucketConfig(twoNodeConfig);
    configs.onNext(clusterConfig);
    verify(mock101, times(2)).addService(ServiceType.VIEWS, 8092, Optional.empty());
    verify(mock101, times(2)).addService(ServiceType.MANAGER, 8091, Optional.empty());
    verify(mock101, times(2)).addService(ServiceType.QUERY, 8093, Optional.empty());
    verify(mock101, times(2)).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"));
}
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 19 with BucketConfig

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

the class CoreTest method addServicesOnNewConfig.

@Test
@SuppressWarnings("unchecked")
void addServicesOnNewConfig() {
    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.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"));
    BucketConfig twoNodesConfigMore = BucketConfigParser.parse(readResource("two_nodes_config_more_services.json", CoreTest.class), ENV, LOCALHOST);
    clusterConfig.setBucketConfig(twoNodesConfigMore);
    configs.onNext(clusterConfig);
    verify(mock101, times(2)).addService(ServiceType.VIEWS, 8092, Optional.empty());
    verify(mock101, times(2)).addService(ServiceType.MANAGER, 8091, Optional.empty());
    verify(mock101, times(2)).addService(ServiceType.QUERY, 8093, Optional.empty());
    verify(mock101, times(2)).addService(ServiceType.KV, 11210, Optional.of("travel-sample"));
    verify(mock102, times(2)).addService(ServiceType.VIEWS, 8092, Optional.empty());
    verify(mock102, times(2)).addService(ServiceType.MANAGER, 8091, Optional.empty());
    verify(mock102, times(2)).addService(ServiceType.QUERY, 8093, Optional.empty());
    verify(mock102, times(2)).addService(ServiceType.KV, 11210, Optional.of("travel-sample"));
    verify(mock102, times(1)).addService(ServiceType.SEARCH, 8094, 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 20 with BucketConfig

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

the class CoreTest method removesNodeIfNotPresentInConfigAnymore.

@Test
@SuppressWarnings("unchecked")
void removesNodeIfNotPresentInConfigAnymore() {
    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("one_node_config.json", CoreTest.class), ENV, LOCALHOST);
    clusterConfig.setBucketConfig(twoNodesLessServices);
    configs.onNext(clusterConfig);
    verify(mock102, times(1)).disconnect();
}
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

BucketConfig (com.couchbase.client.core.config.BucketConfig)20 Optional (java.util.Optional)12 ServiceType (com.couchbase.client.core.service.ServiceType)10 NodeIdentifier (com.couchbase.client.core.node.NodeIdentifier)9 HashMap (java.util.HashMap)9 SeedNode (com.couchbase.client.core.env.SeedNode)8 Duration (java.time.Duration)8 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)7 ConfigurationProvider (com.couchbase.client.core.config.ConfigurationProvider)7 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 Test (org.junit.jupiter.api.Test)7 Core (com.couchbase.client.core.Core)6 Stability (com.couchbase.client.core.annotation.Stability)6 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)6 Node (com.couchbase.client.core.node.Node)6 List (java.util.List)5 Flux (reactor.core.publisher.Flux)5 Reactor (com.couchbase.client.core.Reactor)4 EventBus (com.couchbase.client.core.cnc.EventBus)4