use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.
the class DefaultConfigurationProviderTest method canProposeNewBucketConfig.
@Test
void canProposeNewBucketConfig() {
Core core = mock(Core.class);
CoreContext ctx = new CoreContext(core, 1, ENVIRONMENT, mock(Authenticator.class));
when(core.context()).thenReturn(ctx);
DefaultConfigurationProvider provider = new DefaultConfigurationProvider(core, SeedNode.LOCALHOST);
final AtomicInteger configsPushed = new AtomicInteger(0);
provider.configs().skip(// ignore initial empty config
1).subscribe((c) -> configsPushed.incrementAndGet());
assertTrue(provider.config().bucketConfigs().isEmpty());
assertEquals(1, provider.currentSeedNodes().size());
String bucket = "default";
String config = readResource("config_with_external.json", DefaultConfigurationProviderTest.class);
provider.proposeBucketConfig(new ProposedBucketConfigContext(bucket, config, ORIGIN));
assertEquals(1, configsPushed.get());
assertFalse(provider.config().bucketConfigs().isEmpty());
assertEquals(1073, provider.config().bucketConfig("default").rev());
assertEquals(3, getSeedNodesFromConfig(provider).size());
for (SeedNode node : getSeedNodesFromConfig(provider)) {
assertEquals(11210, node.kvPort().get());
assertEquals(8091, node.clusterManagerPort().get());
}
assertEquals(Optional.empty(), ctx.alternateAddress());
}
use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.
the class DefaultConfigurationProviderTest method updatesSeedNodesFromGlobalConfig.
/**
* Makes sure that even if we only get a global config the seed nodes are properly updated from
* that config.
*/
@Test
void updatesSeedNodesFromGlobalConfig() {
Core core = mock(Core.class);
when(core.context()).thenReturn(new CoreContext(core, 1, ENVIRONMENT, mock(Authenticator.class)));
DefaultConfigurationProvider provider = new DefaultConfigurationProvider(core, SeedNode.LOCALHOST);
String newConfig = readResource("global_config_mad_hatter_multi_node.json", DefaultConfigurationProviderTest.class);
provider.proposeGlobalConfig(new ProposedGlobalConfigContext(newConfig, "127.0.0.1"));
assertEquals(2, getSeedNodesFromConfig(provider).size());
for (SeedNode sn : getSeedNodesFromConfig(provider)) {
assertEquals(11210, sn.kvPort().get());
assertEquals(8091, sn.clusterManagerPort().get());
assertTrue(sn.address().equals("10.143.193.101") || sn.address().equals("10.143.193.102"));
}
}
use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.
the class DefaultConfigurationProviderTest method externalModeSelectedIfAuto.
@Test
void externalModeSelectedIfAuto() {
Core core = mock(Core.class);
CoreEnvironment environment = CoreEnvironment.create();
CoreContext ctx = new CoreContext(core, 1, environment, PasswordAuthenticator.create("user", "pw"));
when(core.context()).thenReturn(ctx);
Set<SeedNode> seedNodes = new HashSet<>(Collections.singletonList(SeedNode.create("192.168.132.234")));
DefaultConfigurationProvider provider = new DefaultConfigurationProvider(core, seedNodes);
assertTrue(provider.config().bucketConfigs().isEmpty());
assertEquals(1, provider.currentSeedNodes().size());
String bucket = "default";
String config = readResource("config_with_external.json", DefaultConfigurationProviderTest.class);
provider.proposeBucketConfig(new ProposedBucketConfigContext(bucket, config, ORIGIN));
assertEquals("external", ctx.alternateAddress().get());
environment.shutdown();
}
use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.
the class BaseBucketLoaderTest method setup.
@BeforeEach
void setup() {
CoreEnvironment env = mock(CoreEnvironment.class);
core = mock(Core.class);
CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
when(core.context()).thenReturn(ctx);
}
use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.
the class AsyncCollection method mutateInRequest.
/**
* Helper method to create the underlying subdoc mutate request.
*
* @param id the outer document ID.
* @param specs the spec which specifies the type of mutations to perform.
* @param opts custom options to modify the mutation options.
* @return the subdoc mutate request.
*/
CompletableFuture<SubdocMutateRequest> mutateInRequest(final String id, final List<MutateInSpec> specs, final MutateInOptions.Built opts, final Duration timeout) {
notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
notNullOrEmpty(specs, "MutateInSpecs", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
if (specs.isEmpty()) {
throw SubdocMutateRequest.errIfNoCommands(ReducedKeyValueErrorContext.create(id, collectionIdentifier));
} else if (specs.size() > SubdocMutateRequest.SUBDOC_MAX_FIELDS) {
throw SubdocMutateRequest.errIfTooManyCommands(ReducedKeyValueErrorContext.create(id, collectionIdentifier));
}
final boolean requiresBucketConfig = opts.createAsDeleted() || opts.storeSemantics() == StoreSemantics.REVIVE;
CompletableFuture<BucketConfig> bucketConfigFuture;
if (requiresBucketConfig) {
bucketConfigFuture = BucketConfigUtil.waitForBucketConfig(core, bucketName(), timeout).toFuture();
} else {
// Nothing will be using the bucket config so just provide null
bucketConfigFuture = CompletableFuture.completedFuture(null);
}
return bucketConfigFuture.thenCompose(bucketConfig -> {
RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
JsonSerializer serializer = opts.serializer() == null ? environment.jsonSerializer() : opts.serializer();
final RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_MUTATE_IN, opts.parentSpan().orElse(null));
ArrayList<SubdocMutateRequest.Command> commands = new ArrayList<>(specs.size());
final RequestSpan encodeSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_ENCODING, span);
long start = System.nanoTime();
try {
for (int i = 0; i < specs.size(); i++) {
MutateInSpec spec = specs.get(i);
commands.add(spec.encode(serializer, i));
}
} finally {
encodeSpan.end();
}
long end = System.nanoTime();
// xattrs come first
commands.sort(Comparator.comparing(v -> !v.xattr()));
long expiry = opts.expiry().encode();
SubdocMutateRequest request = new SubdocMutateRequest(timeout, coreContext, collectionIdentifier, bucketConfig, retryStrategy, id, opts.storeSemantics() == StoreSemantics.INSERT, opts.storeSemantics() == StoreSemantics.UPSERT, opts.storeSemantics() == StoreSemantics.REVIVE, opts.accessDeleted(), opts.createAsDeleted(), commands, expiry, opts.preserveExpiry(), opts.cas(), opts.durabilityLevel(), span);
request.context().clientContext(opts.clientContext()).encodeLatency(end - start);
final CompletableFuture<SubdocMutateRequest> future = new CompletableFuture<>();
future.complete(request);
return future;
});
}
Aggregations