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());
}
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());
}
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"));
}
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)));
}
}
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)));
}
Aggregations