Search in sources :

Example 1 with ConfigurationProvider

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

the class KeyValueMessageHandlerTest method setup.

@BeforeAll
static void setup() {
    ENV = CoreEnvironment.builder().eventBus(new SimpleEventBus(true)).build();
    Core core = mock(Core.class);
    CoreContext coreContext = new CoreContext(core, 1, ENV, PasswordAuthenticator.create("foo", "bar"));
    ConfigurationProvider configurationProvider = mock(ConfigurationProvider.class);
    when(configurationProvider.collectionMap()).thenReturn(new CollectionMap());
    when(core.configurationProvider()).thenReturn(configurationProvider);
    CTX = 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) CoreContext(com.couchbase.client.core.CoreContext) CollectionMap(com.couchbase.client.core.io.CollectionMap) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) Core(com.couchbase.client.core.Core) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 2 with ConfigurationProvider

use of com.couchbase.client.core.config.ConfigurationProvider 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 3 with ConfigurationProvider

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

Example 4 with ConfigurationProvider

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

the class KeyValueMessageHandler method write.

@Override
@SuppressWarnings({ "unchecked" })
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
    if (msg instanceof KeyValueRequest) {
        KeyValueRequest<Response> request = (KeyValueRequest<Response>) msg;
        int opaque = request.opaque();
        writtenRequests.put(opaque, request);
        try {
            ctx.write(request.encode(ctx.alloc(), opaque, channelContext), promise);
            writtenRequestDispatchTimings.put(opaque, (Long) System.nanoTime());
            if (request.requestSpan() != null) {
                RequestTracer tracer = endpointContext.environment().requestTracer();
                RequestSpan dispatchSpan = tracer.requestSpan(TracingIdentifiers.SPAN_DISPATCH, request.requestSpan());
                if (!isInternalTracer) {
                    setCommonDispatchSpanAttributes(dispatchSpan, ctx.channel().attr(ChannelAttributes.CHANNEL_ID_KEY).get(), ioContext.localHostname(), ioContext.localPort(), endpoint.remoteHostname(), endpoint.remotePort(), null);
                    setNumericOperationId(dispatchSpan, request.opaque());
                    setCommonKVSpanAttributes(dispatchSpan, request);
                }
                writtenRequestDispatchSpans.put(opaque, dispatchSpan);
            }
        } catch (Throwable err) {
            writtenRequests.remove(opaque);
            if (err instanceof CollectionNotFoundException) {
                if (channelContext.collectionsEnabled()) {
                    ConfigurationProvider cp = ioContext.core().configurationProvider();
                    if (cp.collectionRefreshInProgress(request.collectionIdentifier())) {
                        RetryOrchestrator.maybeRetry(ioContext, request, RetryReason.COLLECTION_MAP_REFRESH_IN_PROGRESS);
                    } else if (cp.config().bucketConfig(request.bucket()) instanceof MemcachedBucketConfig) {
                        request.fail(FeatureNotAvailableException.collectionsForMemcached());
                    } else {
                        handleOutdatedCollection(request, RetryReason.COLLECTION_NOT_FOUND);
                    }
                    return;
                }
            }
            request.fail(err);
        }
    } else {
        eventBus.publish(new InvalidRequestDetectedEvent(ioContext, ServiceType.KV, msg));
        ctx.channel().close().addListener(f -> eventBus.publish(new ChannelClosedProactivelyEvent(ioContext, ChannelClosedProactivelyEvent.Reason.INVALID_REQUEST_DETECTED)));
    }
}
Also used : Response(com.couchbase.client.core.msg.Response) KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) ChannelClosedProactivelyEvent(com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) MemcachedBucketConfig(com.couchbase.client.core.config.MemcachedBucketConfig) RequestTracer(com.couchbase.client.core.cnc.RequestTracer) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) CollectionNotFoundException(com.couchbase.client.core.error.CollectionNotFoundException) InvalidRequestDetectedEvent(com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 5 with ConfigurationProvider

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

the class KeyValueMessageHandler method handleNotMyVbucket.

/**
 * Helper method to handle a "not my vbucket" response.
 *
 * @param request the request to retry.
 * @param response the response to extract the config from, potentially.
 */
private void handleNotMyVbucket(final KeyValueRequest<Response> request, final ByteBuf response) {
    request.indicateRejectedWithNotMyVbucket();
    eventBus.publish(new NotMyVbucketReceivedEvent(ioContext, request.partition()));
    final String origin = request.context().lastDispatchedTo() != null ? request.context().lastDispatchedTo().hostname() : null;
    RetryOrchestrator.maybeRetry(ioContext, request, RetryReason.KV_NOT_MY_VBUCKET);
    body(response).map(b -> b.toString(UTF_8).trim()).filter(c -> c.startsWith("{")).ifPresent(c -> ioContext.core().configurationProvider().proposeBucketConfig(new ProposedBucketConfigContext(request.bucket(), c, origin)));
}
Also used : IoContext(com.couchbase.client.core.io.IoContext) UnsignedLEB128(com.couchbase.client.core.util.UnsignedLEB128) HandlerUtils.closeChannelWithReason(com.couchbase.client.core.io.netty.HandlerUtils.closeChannelWithReason) ReferenceCountUtil(com.couchbase.client.core.deps.io.netty.util.ReferenceCountUtil) Request(com.couchbase.client.core.msg.Request) CompressionConfig(com.couchbase.client.core.env.CompressionConfig) RETRY_NOW(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.RETRY_NOW) EventBus(com.couchbase.client.core.cnc.EventBus) TracingUtils.setCommonKVSpanAttributes(com.couchbase.client.core.io.netty.TracingUtils.setCommonKVSpanAttributes) ServiceType(com.couchbase.client.core.service.ServiceType) CoreContext(com.couchbase.client.core.CoreContext) Map(java.util.Map) TracingUtils.setCommonDispatchSpanAttributes(com.couchbase.client.core.io.netty.TracingUtils.setCommonDispatchSpanAttributes) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) ITEM_LOCKED(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.ITEM_LOCKED) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) CbTracing(com.couchbase.client.core.cnc.CbTracing) RetryOrchestrator(com.couchbase.client.core.retry.RetryOrchestrator) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) TEMP(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.TEMP) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) Set(java.util.Set) KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) Collectors(java.util.stream.Collectors) TracingUtils(com.couchbase.client.core.io.netty.TracingUtils) RetryReason(com.couchbase.client.core.retry.RetryReason) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) DecodingFailureException(com.couchbase.client.core.error.DecodingFailureException) Optional(java.util.Optional) ChannelPromise(com.couchbase.client.core.deps.io.netty.channel.ChannelPromise) Response(com.couchbase.client.core.msg.Response) TracingUtils.setNumericOperationId(com.couchbase.client.core.io.netty.TracingUtils.setNumericOperationId) CollectionNotFoundException(com.couchbase.client.core.error.CollectionNotFoundException) UnsupportedResponseTypeReceivedEvent(com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent) HashMap(java.util.HashMap) KeyValueErrorMapCodeHandledEvent(com.couchbase.client.core.cnc.events.io.KeyValueErrorMapCodeHandledEvent) IntObjectMap(com.couchbase.client.core.deps.io.netty.util.collection.IntObjectMap) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) RETRY_LATER(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.RETRY_LATER) NotMyVbucketReceivedEvent(com.couchbase.client.core.cnc.events.io.NotMyVbucketReceivedEvent) MemcachedBucketConfig(com.couchbase.client.core.config.MemcachedBucketConfig) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ChannelDuplexHandler(com.couchbase.client.core.deps.io.netty.channel.ChannelDuplexHandler) CollectionOutdatedHandledEvent(com.couchbase.client.core.cnc.events.io.CollectionOutdatedHandledEvent) RequestTracer(com.couchbase.client.core.cnc.RequestTracer) InvalidRequestDetectedEvent(com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) UnknownResponseReceivedEvent(com.couchbase.client.core.cnc.events.io.UnknownResponseReceivedEvent) UTF_8(java.nio.charset.StandardCharsets.UTF_8) FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) UnlockRequest(com.couchbase.client.core.msg.kv.UnlockRequest) CollectionMap(com.couchbase.client.core.io.CollectionMap) ByteBufUtil(com.couchbase.client.core.deps.io.netty.buffer.ByteBufUtil) CONN_STATE_INVALIDATED(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.CONN_STATE_INVALIDATED) UnknownResponseStatusReceivedEvent(com.couchbase.client.core.cnc.events.io.UnknownResponseStatusReceivedEvent) ChannelClosedProactivelyEvent(com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent) IntObjectHashMap(com.couchbase.client.core.deps.io.netty.util.collection.IntObjectHashMap) AUTH(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.AUTH) MemcacheProtocol.body(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.body) NotMyVbucketReceivedEvent(com.couchbase.client.core.cnc.events.io.NotMyVbucketReceivedEvent) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext)

Aggregations

ConfigurationProvider (com.couchbase.client.core.config.ConfigurationProvider)11 Test (org.junit.jupiter.api.Test)7 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)6 ServiceType (com.couchbase.client.core.service.ServiceType)6 HashMap (java.util.HashMap)6 Optional (java.util.Optional)6 BucketConfig (com.couchbase.client.core.config.BucketConfig)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 Core (com.couchbase.client.core.Core)3 CoreContext (com.couchbase.client.core.CoreContext)3 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)2 RequestTracer (com.couchbase.client.core.cnc.RequestTracer)2 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)2 ChannelClosedProactivelyEvent (com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent)2 InvalidRequestDetectedEvent (com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent)2 MemcachedBucketConfig (com.couchbase.client.core.config.MemcachedBucketConfig)2 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)2 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)2