Search in sources :

Example 6 with ClusterConfig

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

the class RoundRobinLocatorTest method skipNodeWithoutServiceEnabled.

@Test
void skipNodeWithoutServiceEnabled() {
    Locator locator = new RoundRobinLocator(ServiceType.QUERY, 0);
    QueryRequest request = mock(QueryRequest.class);
    ClusterConfig configMock = mock(ClusterConfig.class);
    when(configMock.hasClusterOrBucketConfig()).thenReturn(true);
    Node node1Mock = mock(Node.class);
    when(node1Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.101", 8091));
    when(node1Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(false);
    Node node2Mock = mock(Node.class);
    when(node2Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.102", 8091));
    when(node2Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(true);
    Node node3Mock = mock(Node.class);
    when(node3Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.103", 8091));
    when(node3Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(false);
    List<Node> nodes = new ArrayList<>(Arrays.asList(node1Mock, node2Mock, node3Mock));
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, times(1)).send(request);
    verify(node3Mock, never()).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, times(2)).send(request);
    verify(node3Mock, never()).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, times(3)).send(request);
    verify(node3Mock, never()).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, times(4)).send(request);
    verify(node3Mock, never()).send(request);
}
Also used : QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) ArrayList(java.util.ArrayList) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Example 7 with ClusterConfig

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

the class RoundRobinLocatorTest method selectNextNode.

@Test
void selectNextNode() {
    Locator locator = new RoundRobinLocator(ServiceType.QUERY, 0);
    QueryRequest request = mock(QueryRequest.class);
    ClusterConfig configMock = mock(ClusterConfig.class);
    when(configMock.hasClusterOrBucketConfig()).thenReturn(true);
    Node node1Mock = mock(Node.class);
    when(node1Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(true);
    when(node1Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.101", 8091));
    Node node2Mock = mock(Node.class);
    when(node2Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(true);
    when(node2Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.102", 8091));
    List<Node> nodes = new ArrayList<>(Arrays.asList(node1Mock, node2Mock));
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, times(1)).send(request);
    verify(node2Mock, never()).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, times(1)).send(request);
    verify(node2Mock, times(1)).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, times(2)).send(request);
    verify(node2Mock, times(1)).send(request);
}
Also used : QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) ArrayList(java.util.ArrayList) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Example 8 with ClusterConfig

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

the class Core method reconfigure.

/**
 * Reconfigures the SDK topology to align with the current server configuration.
 *
 * <p>When reconfigure is called, it will grab a current configuration and then add/remove
 * nodes/services to mirror the current topology and configuration settings.</p>
 *
 * <p>This is a eventually consistent process, so in-flight operations might still be rescheduled
 * and then picked up later (or cancelled, depending on the strategy). For those coming from 1.x,
 * it works very similar.</p>
 */
private void reconfigure() {
    if (reconfigureInProgress.compareAndSet(false, true)) {
        final ClusterConfig configForThisAttempt = currentConfig;
        if (configForThisAttempt.bucketConfigs().isEmpty() && configForThisAttempt.globalConfig() == null) {
            reconfigureDisconnectAll();
            return;
        }
        final long start = System.nanoTime();
        Flux<BucketConfig> bucketConfigFlux = Flux.just(configForThisAttempt).flatMap(cc -> Flux.fromIterable(cc.bucketConfigs().values()));
        reconfigureBuckets(bucketConfigFlux).then(reconfigureGlobal(configForThisAttempt.globalConfig())).then(Mono.defer(() -> Flux.fromIterable(new ArrayList<>(nodes)).flatMap(n -> maybeRemoveNode(n, configForThisAttempt)).then())).subscribe(v -> {
        }, e -> {
            clearReconfigureInProgress();
            eventBus.publish(new ReconfigurationErrorDetectedEvent(context(), e));
        }, () -> {
            clearReconfigureInProgress();
            eventBus.publish(new ReconfigurationCompletedEvent(Duration.ofNanos(System.nanoTime() - start), coreContext));
        });
    } else {
        moreConfigsPending.set(true);
        eventBus.publish(new ReconfigurationIgnoredEvent(coreContext));
    }
}
Also used : ReconfigurationIgnoredEvent(com.couchbase.client.core.cnc.events.core.ReconfigurationIgnoredEvent) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ReconfigurationErrorDetectedEvent(com.couchbase.client.core.cnc.events.core.ReconfigurationErrorDetectedEvent) ReconfigurationCompletedEvent(com.couchbase.client.core.cnc.events.core.ReconfigurationCompletedEvent) BucketConfig(com.couchbase.client.core.config.BucketConfig) ClusterConfig(com.couchbase.client.core.config.ClusterConfig)

Example 9 with ClusterConfig

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

the class KeyValueLocatorTest method pickCurrentIfNoFFMapAndNmvbSeen.

@Test
@SuppressWarnings("unchecked")
void pickCurrentIfNoFFMapAndNmvbSeen() {
    Locator locator = new KeyValueLocator();
    // Setup 2 nodes
    NodeInfo nodeInfo1 = new NodeInfo("http://foo:1234", "192.168.56.101:8091", Collections.EMPTY_MAP, null);
    NodeInfo nodeInfo2 = new NodeInfo("http://foo:1234", "192.168.56.102:8091", Collections.EMPTY_MAP, null);
    Node node1Mock = mock(Node.class);
    when(node1Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.101", 8091));
    Node node2Mock = mock(Node.class);
    when(node2Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.102", 8091));
    List<Node> nodes = new ArrayList<>(Arrays.asList(node1Mock, node2Mock));
    // Configure Cluster and Bucket config
    ClusterConfig configMock = mock(ClusterConfig.class);
    CouchbaseBucketConfig bucketMock = mock(CouchbaseBucketConfig.class);
    when(configMock.bucketConfig("bucket")).thenReturn(bucketMock);
    when(bucketMock.nodes()).thenReturn(Arrays.asList(nodeInfo1, nodeInfo2));
    when(bucketMock.numberOfPartitions()).thenReturn(1024);
    when(bucketMock.nodeAtIndex(0)).thenReturn(nodeInfo1);
    when(bucketMock.nodeAtIndex(1)).thenReturn(nodeInfo2);
    when(bucketMock.hasFastForwardMap()).thenReturn(false);
    // Fake a vbucket move in ffwd map from node 0 to node 1
    when(bucketMock.nodeIndexForActive(656, false)).thenReturn((short) 0);
    when(bucketMock.nodeIndexForActive(656, true)).thenReturn((short) 1);
    // Create Request
    GetRequest getRequest = mock(GetRequest.class);
    when(getRequest.bucket()).thenReturn("bucket");
    when(getRequest.key()).thenReturn("key".getBytes(UTF_8));
    RequestContext requestCtx = mock(RequestContext.class);
    when(getRequest.context()).thenReturn(requestCtx);
    when(getRequest.rejectedWithNotMyVbucket()).thenReturn(9);
    locator.dispatch(getRequest, nodes, configMock, null);
    verify(node1Mock, times(1)).send(getRequest);
    verify(node2Mock, never()).send(getRequest);
    when(getRequest.rejectedWithNotMyVbucket()).thenReturn(1);
    locator.dispatch(getRequest, nodes, configMock, null);
    verify(node1Mock, times(2)).send(getRequest);
    verify(node2Mock, never()).send(getRequest);
}
Also used : CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) NodeInfo(com.couchbase.client.core.config.NodeInfo) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ArrayList(java.util.ArrayList) RequestContext(com.couchbase.client.core.msg.RequestContext) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Example 10 with ClusterConfig

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

the class RoundRobinLocatorTest method shouldDistributeFairlyUnderMDS.

@Test
void shouldDistributeFairlyUnderMDS() {
    Locator locator = new RoundRobinLocator(ServiceType.QUERY, 0);
    QueryRequest request = mock(QueryRequest.class);
    ClusterConfig configMock = mock(ClusterConfig.class);
    when(configMock.hasClusterOrBucketConfig()).thenReturn(true);
    Node node1Mock = mock(Node.class);
    when(node1Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.101", 8091));
    when(node1Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(false);
    Node node2Mock = mock(Node.class);
    when(node2Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.102", 8091));
    when(node2Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(false);
    Node node3Mock = mock(Node.class);
    when(node3Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.103", 8091));
    when(node3Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(true);
    Node node4Mock = mock(Node.class);
    when(node4Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.104", 8091));
    when(node4Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(true);
    List<Node> nodes = new ArrayList<>(Arrays.asList(node1Mock, node2Mock, node3Mock, node4Mock));
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, never()).send(request);
    verify(node3Mock, times(1)).send(request);
    verify(node4Mock, never()).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, never()).send(request);
    verify(node3Mock, times(1)).send(request);
    verify(node4Mock, times(1)).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, never()).send(request);
    verify(node3Mock, times(2)).send(request);
    verify(node4Mock, times(1)).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, never()).send(request);
    verify(node3Mock, times(2)).send(request);
    verify(node4Mock, times(2)).send(request);
}
Also used : QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) ArrayList(java.util.ArrayList) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Aggregations

ClusterConfig (com.couchbase.client.core.config.ClusterConfig)15 Test (org.junit.jupiter.api.Test)14 ArrayList (java.util.ArrayList)8 BucketConfig (com.couchbase.client.core.config.BucketConfig)6 ConfigurationProvider (com.couchbase.client.core.config.ConfigurationProvider)6 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)5 SeedNode (com.couchbase.client.core.env.SeedNode)5 Node (com.couchbase.client.core.node.Node)5 NodeIdentifier (com.couchbase.client.core.node.NodeIdentifier)5 ServiceType (com.couchbase.client.core.service.ServiceType)5 HashMap (java.util.HashMap)5 Optional (java.util.Optional)5 NodeInfo (com.couchbase.client.core.config.NodeInfo)4 RequestContext (com.couchbase.client.core.msg.RequestContext)4 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)4 QueryRequest (com.couchbase.client.core.msg.query.QueryRequest)3 Core (com.couchbase.client.core.Core)2 CoreContext (com.couchbase.client.core.CoreContext)1 ReconfigurationCompletedEvent (com.couchbase.client.core.cnc.events.core.ReconfigurationCompletedEvent)1 ReconfigurationErrorDetectedEvent (com.couchbase.client.core.cnc.events.core.ReconfigurationErrorDetectedEvent)1