Search in sources :

Example 6 with Service

use of com.couchbase.client.core.service.Service in project couchbase-jvm-clients by couchbase.

the class NodeTest method degradedIfAtLeastOneConnected.

@Test
void degradedIfAtLeastOneConnected() {
    Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {

        final AtomicInteger counter = new AtomicInteger();

        @Override
        protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
            Service s = mock(Service.class);
            when(s.state()).thenReturn(counter.incrementAndGet() > 1 ? ServiceState.CONNECTED : ServiceState.DISCONNECTED);
            when(s.states()).thenReturn(DirectProcessor.create());
            return s;
        }
    };
    assertEquals(NodeState.DISCONNECTED, node.state());
    assertFalse(node.serviceEnabled(ServiceType.KV));
    node.addService(ServiceType.KV, 11210, Optional.of("bucket")).block();
    assertTrue(node.serviceEnabled(ServiceType.KV));
    assertEquals(NodeState.DISCONNECTED, node.state());
    assertFalse(node.serviceEnabled(ServiceType.QUERY));
    node.addService(ServiceType.QUERY, 8093, Optional.empty()).block();
    assertTrue(node.serviceEnabled(ServiceType.QUERY));
    assertEquals(NodeState.DEGRADED, node.state());
    assertFalse(node.serviceEnabled(ServiceType.VIEWS));
    node.addService(ServiceType.VIEWS, 8092, Optional.empty()).block();
    assertTrue(node.serviceEnabled(ServiceType.VIEWS));
    assertEquals(NodeState.DEGRADED, node.state());
}
Also used : Optional(java.util.Optional) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceType(com.couchbase.client.core.service.ServiceType) Service(com.couchbase.client.core.service.Service) Test(org.junit.jupiter.api.Test)

Example 7 with Service

use of com.couchbase.client.core.service.Service in project couchbase-jvm-clients by couchbase.

the class Node method send.

/**
 * Sends the request into this {@link Node}.
 *
 * <p>Note that there is no guarantee that the request will actually dispatched, based on the
 * state this node is in.</p>
 *
 * @param request the request to send.
 */
public <R extends Request<? extends Response>> void send(final R request) {
    String bucket;
    if (request.serviceType().scope() == ServiceScope.BUCKET) {
        bucket = request.bucket();
        if (bucket == null) {
            // no bucket name present so this is a kv request that is not attached
            // to a specific bucket
            bucket = BUCKET_GLOBAL_SCOPE;
        }
    } else {
        bucket = GLOBAL_SCOPE;
    }
    Map<ServiceType, Service> scope = services.get(bucket);
    if (scope == null) {
        sendIntoRetry(request);
        return;
    }
    Service service = scope.get(request.serviceType());
    if (service == null) {
        sendIntoRetry(request);
        return;
    }
    request.context().lastDispatchedToNode(identifier);
    service.send(request);
}
Also used : ServiceType(com.couchbase.client.core.service.ServiceType) ManagerService(com.couchbase.client.core.service.ManagerService) AnalyticsService(com.couchbase.client.core.service.AnalyticsService) SearchService(com.couchbase.client.core.service.SearchService) EventingService(com.couchbase.client.core.service.EventingService) KeyValueService(com.couchbase.client.core.service.KeyValueService) QueryService(com.couchbase.client.core.service.QueryService) Service(com.couchbase.client.core.service.Service) ViewService(com.couchbase.client.core.service.ViewService) BackupService(com.couchbase.client.core.service.BackupService)

Example 8 with Service

use of com.couchbase.client.core.service.Service in project couchbase-jvm-clients by couchbase.

the class Node method addService.

/**
 * Adds a {@link Service} to this {@link Node}.
 *
 * @param type the type of the service.
 * @param port the port of the service.
 * @param bucket the bucket name (if present).
 * @return a {@link Mono} that completes once the service is added.
 */
public synchronized Mono<Void> addService(final ServiceType type, final int port, final Optional<String> bucket) {
    return Mono.defer(() -> {
        if (disconnect.get()) {
            ctx.environment().eventBus().publish(new ServiceAddIgnoredEvent(Event.Severity.DEBUG, ServiceAddIgnoredEvent.Reason.DISCONNECTED, ctx));
            return Mono.empty();
        }
        String name = type.scope() == ServiceScope.CLUSTER ? GLOBAL_SCOPE : bucket.orElse(BUCKET_GLOBAL_SCOPE);
        Map<ServiceType, Service> localMap = services.get(name);
        if (localMap == null) {
            localMap = new ConcurrentHashMap<>();
            services.put(name, localMap);
        }
        if (!localMap.containsKey(type)) {
            long start = System.nanoTime();
            Service service = createService(type, port, bucket);
            serviceStates.register(service, service);
            localMap.put(type, service);
            enabledServices.set(enabledServices.get() | 1 << type.ordinal());
            // todo: only return once the service is connected?
            service.connect();
            long end = System.nanoTime();
            ctx.environment().eventBus().publish(new ServiceAddedEvent(Duration.ofNanos(end - start), service.context()));
            return Mono.empty();
        } else {
            ctx.environment().eventBus().publish(new ServiceAddIgnoredEvent(Event.Severity.VERBOSE, ServiceAddIgnoredEvent.Reason.ALREADY_ADDED, ctx));
            return Mono.empty();
        }
    });
}
Also used : ServiceAddedEvent(com.couchbase.client.core.cnc.events.service.ServiceAddedEvent) ServiceType(com.couchbase.client.core.service.ServiceType) ServiceAddIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent) ManagerService(com.couchbase.client.core.service.ManagerService) AnalyticsService(com.couchbase.client.core.service.AnalyticsService) SearchService(com.couchbase.client.core.service.SearchService) EventingService(com.couchbase.client.core.service.EventingService) KeyValueService(com.couchbase.client.core.service.KeyValueService) QueryService(com.couchbase.client.core.service.QueryService) Service(com.couchbase.client.core.service.Service) ViewService(com.couchbase.client.core.service.ViewService) BackupService(com.couchbase.client.core.service.BackupService)

Example 9 with Service

use of com.couchbase.client.core.service.Service in project couchbase-jvm-clients by couchbase.

the class NodeTest method retriesIfLocalServiceNotFound.

@Test
@SuppressWarnings({ "unchecked" })
void retriesIfLocalServiceNotFound() {
    final Service s = mock(Service.class);
    final AtomicReference<Request<?>> retried = new AtomicReference<>();
    Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {

        @Override
        protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
            when(s.state()).thenReturn(ServiceState.CONNECTED);
            when(s.states()).thenReturn(DirectProcessor.create());
            when(s.type()).thenReturn(serviceType);
            return s;
        }

        @Override
        protected <R extends Request<? extends Response>> void sendIntoRetry(R request) {
            retried.set(request);
        }
    };
    node.addService(ServiceType.KV, 11210, Optional.of("bucket")).block();
    KeyValueRequest r = mock(KeyValueRequest.class);
    when(r.serviceType()).thenReturn(ServiceType.KV);
    when(r.bucket()).thenReturn("other_bucket");
    node.send(r);
    verify(s, never()).send(eq(r));
    assertEquals(r, retried.get());
}
Also used : Response(com.couchbase.client.core.msg.Response) KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) Optional(java.util.Optional) ServiceType(com.couchbase.client.core.service.ServiceType) QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) Request(com.couchbase.client.core.msg.Request) KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) Service(com.couchbase.client.core.service.Service) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 10 with Service

use of com.couchbase.client.core.service.Service in project couchbase-jvm-clients by couchbase.

the class NodeTest method canAddAndRemoveServices.

@Test
void canAddAndRemoveServices() {
    Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {

        @Override
        protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
            Service s = mock(Service.class);
            when(s.state()).thenReturn(ServiceState.CONNECTED);
            when(s.states()).thenReturn(DirectProcessor.create());
            when(s.type()).thenReturn(serviceType);
            return s;
        }
    };
    assertEquals(NodeState.DISCONNECTED, node.state());
    assertFalse(node.serviceEnabled(ServiceType.QUERY));
    node.addService(ServiceType.QUERY, 8091, Optional.empty()).block();
    assertTrue(node.serviceEnabled(ServiceType.QUERY));
    assertEquals(NodeState.CONNECTED, node.state());
    node.removeService(ServiceType.QUERY, Optional.empty()).block();
    assertFalse(node.serviceEnabled(ServiceType.QUERY));
    assertEquals(NodeState.DISCONNECTED, node.state());
}
Also used : Optional(java.util.Optional) ServiceType(com.couchbase.client.core.service.ServiceType) Service(com.couchbase.client.core.service.Service) Test(org.junit.jupiter.api.Test)

Aggregations

Service (com.couchbase.client.core.service.Service)18 ServiceType (com.couchbase.client.core.service.ServiceType)18 Optional (java.util.Optional)15 Test (org.junit.jupiter.api.Test)15 KeyValueRequest (com.couchbase.client.core.msg.kv.KeyValueRequest)3 QueryRequest (com.couchbase.client.core.msg.query.QueryRequest)3 AnalyticsService (com.couchbase.client.core.service.AnalyticsService)3 BackupService (com.couchbase.client.core.service.BackupService)3 EventingService (com.couchbase.client.core.service.EventingService)3 KeyValueService (com.couchbase.client.core.service.KeyValueService)3 ManagerService (com.couchbase.client.core.service.ManagerService)3 QueryService (com.couchbase.client.core.service.QueryService)3 SearchService (com.couchbase.client.core.service.SearchService)3 ViewService (com.couchbase.client.core.service.ViewService)3 ServiceAddIgnoredEvent (com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent)2 ServiceAddedEvent (com.couchbase.client.core.cnc.events.service.ServiceAddedEvent)2 ServiceRemoveIgnoredEvent (com.couchbase.client.core.cnc.events.service.ServiceRemoveIgnoredEvent)2 ServiceRemovedEvent (com.couchbase.client.core.cnc.events.service.ServiceRemovedEvent)2 Request (com.couchbase.client.core.msg.Request)2 RequestContext (com.couchbase.client.core.msg.RequestContext)2