Search in sources :

Example 1 with ServiceAddIgnoredEvent

use of com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent 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 2 with ServiceAddIgnoredEvent

use of com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent in project couchbase-jvm-clients by couchbase.

the class NodeTest method sendsEventsIntoEventBus.

@Test
void sendsEventsIntoEventBus() {
    Core core = mock(Core.class);
    SimpleEventBus eventBus = new SimpleEventBus(true, Collections.singletonList(NodeStateChangedEvent.class));
    CoreEnvironment env = CoreEnvironment.builder().eventBus(eventBus).build();
    CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
    try {
        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.type()).thenReturn(serviceType);
                when(s.states()).thenReturn(DirectProcessor.create());
                when(s.state()).thenReturn(ServiceState.IDLE);
                return s;
            }
        };
        node.addService(ServiceType.QUERY, 2, Optional.empty()).block();
        node.addService(ServiceType.KV, 1, Optional.of("bucket")).block();
        node.addService(ServiceType.KV, 1, Optional.of("bucket")).block();
        node.removeService(ServiceType.KV, Optional.of("bucket")).block();
        node.removeService(ServiceType.QUERY, Optional.empty()).block();
        node.removeService(ServiceType.QUERY, Optional.empty()).block();
        node.disconnect().block();
        node.disconnect().block();
        node.addService(ServiceType.QUERY, 2, Optional.empty()).block();
        node.removeService(ServiceType.QUERY, Optional.empty()).block();
        List<Event> events = eventBus.publishedEvents();
        assertTrue(events.remove(0) instanceof NodeConnectedEvent);
        assertTrue(events.get(0) instanceof ServiceAddedEvent);
        assertTrue(events.get(1) instanceof ServiceAddedEvent);
        assertTrue(events.get(2) instanceof ServiceAddIgnoredEvent);
        assertTrue(events.get(3) instanceof ServiceRemovedEvent);
        assertTrue(events.get(4) instanceof ServiceRemovedEvent);
        assertTrue(events.get(5) instanceof ServiceRemoveIgnoredEvent);
        assertTrue(events.get(6) instanceof NodeDisconnectedEvent);
        assertTrue(events.get(7) instanceof NodeDisconnectIgnoredEvent);
        assertTrue(events.get(8) instanceof ServiceAddIgnoredEvent);
        assertTrue(events.get(9) instanceof ServiceRemoveIgnoredEvent);
    } finally {
        env.shutdown();
    }
}
Also used : ServiceAddedEvent(com.couchbase.client.core.cnc.events.service.ServiceAddedEvent) ServiceRemoveIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceRemoveIgnoredEvent) CoreContext(com.couchbase.client.core.CoreContext) NodeConnectedEvent(com.couchbase.client.core.cnc.events.node.NodeConnectedEvent) Optional(java.util.Optional) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) ServiceAddIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent) Service(com.couchbase.client.core.service.Service) NodeStateChangedEvent(com.couchbase.client.core.cnc.events.node.NodeStateChangedEvent) ServiceRemovedEvent(com.couchbase.client.core.cnc.events.service.ServiceRemovedEvent) ServiceType(com.couchbase.client.core.service.ServiceType) NodeDisconnectedEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectedEvent) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) NodeDisconnectIgnoredEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectIgnoredEvent) ServiceAddIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent) ServiceRemovedEvent(com.couchbase.client.core.cnc.events.service.ServiceRemovedEvent) NodeStateChangedEvent(com.couchbase.client.core.cnc.events.node.NodeStateChangedEvent) Event(com.couchbase.client.core.cnc.Event) ServiceRemoveIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceRemoveIgnoredEvent) NodeConnectedEvent(com.couchbase.client.core.cnc.events.node.NodeConnectedEvent) ServiceAddedEvent(com.couchbase.client.core.cnc.events.service.ServiceAddedEvent) NodeDisconnectedEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectedEvent) NodeDisconnectIgnoredEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectIgnoredEvent) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Aggregations

ServiceAddIgnoredEvent (com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent)2 ServiceAddedEvent (com.couchbase.client.core.cnc.events.service.ServiceAddedEvent)2 Service (com.couchbase.client.core.service.Service)2 ServiceType (com.couchbase.client.core.service.ServiceType)2 Core (com.couchbase.client.core.Core)1 CoreContext (com.couchbase.client.core.CoreContext)1 Event (com.couchbase.client.core.cnc.Event)1 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)1 NodeConnectedEvent (com.couchbase.client.core.cnc.events.node.NodeConnectedEvent)1 NodeDisconnectIgnoredEvent (com.couchbase.client.core.cnc.events.node.NodeDisconnectIgnoredEvent)1 NodeDisconnectedEvent (com.couchbase.client.core.cnc.events.node.NodeDisconnectedEvent)1 NodeStateChangedEvent (com.couchbase.client.core.cnc.events.node.NodeStateChangedEvent)1 ServiceRemoveIgnoredEvent (com.couchbase.client.core.cnc.events.service.ServiceRemoveIgnoredEvent)1 ServiceRemovedEvent (com.couchbase.client.core.cnc.events.service.ServiceRemovedEvent)1 Authenticator (com.couchbase.client.core.env.Authenticator)1 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)1 AnalyticsService (com.couchbase.client.core.service.AnalyticsService)1 BackupService (com.couchbase.client.core.service.BackupService)1 EventingService (com.couchbase.client.core.service.EventingService)1 KeyValueService (com.couchbase.client.core.service.KeyValueService)1