use of com.couchbase.client.core.cnc.events.service.ServiceRemovedEvent in project couchbase-jvm-clients by couchbase.
the class Node method removeService.
private synchronized Mono<Void> removeService(final ServiceType type, final Optional<String> bucket, boolean ignoreDisconnect) {
return Mono.defer(() -> {
if (disconnect.get() && !ignoreDisconnect) {
ctx.environment().eventBus().publish(new ServiceRemoveIgnoredEvent(Event.Severity.DEBUG, ServiceRemoveIgnoredEvent.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.containsKey(type)) {
ctx.environment().eventBus().publish(new ServiceRemoveIgnoredEvent(Event.Severity.DEBUG, ServiceRemoveIgnoredEvent.Reason.NOT_PRESENT, ctx));
return Mono.empty();
}
Service service = localMap.remove(type);
serviceStates.deregister(service);
long start = System.nanoTime();
if (serviceCanBeDisabled(service.type())) {
enabledServices.set(enabledServices.get() & ~(1 << service.type().ordinal()));
}
// todo: only return once the service is disconnected?
service.disconnect();
long end = System.nanoTime();
ctx.environment().eventBus().publish(new ServiceRemovedEvent(Duration.ofNanos(end - start), service.context()));
return Mono.empty();
});
}
use of com.couchbase.client.core.cnc.events.service.ServiceRemovedEvent 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();
}
}
Aggregations