Search in sources :

Example 11 with Core

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());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SeedNode(com.couchbase.client.core.env.SeedNode) PasswordAuthenticator(com.couchbase.client.core.env.PasswordAuthenticator) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with Core

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"));
    }
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) SeedNode(com.couchbase.client.core.env.SeedNode) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with Core

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();
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) SeedNode(com.couchbase.client.core.env.SeedNode) Core(com.couchbase.client.core.Core) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with Core

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);
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 15 with Core

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;
    });
}
Also used : MutateInAccessor(com.couchbase.client.java.kv.MutateInAccessor) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) InsertOptions(com.couchbase.client.java.kv.InsertOptions) MutationResult(com.couchbase.client.java.kv.MutationResult) DEFAULT_REPLACE_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_REPLACE_OPTIONS) Validators.notNull(com.couchbase.client.core.util.Validators.notNull) PersistTo(com.couchbase.client.java.kv.PersistTo) ReplaceAccessor(com.couchbase.client.java.kv.ReplaceAccessor) TouchAccessor(com.couchbase.client.java.kv.TouchAccessor) DEFAULT_GET_AND_LOCK_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_AND_LOCK_OPTIONS) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) GetAndTouchOptions(com.couchbase.client.java.kv.GetAndTouchOptions) LookupInMacro(com.couchbase.client.java.kv.LookupInMacro) BucketConfig(com.couchbase.client.core.config.BucketConfig) GetAllReplicasOptions(com.couchbase.client.java.kv.GetAllReplicasOptions) TouchRequest(com.couchbase.client.core.msg.kv.TouchRequest) RemoveOptions(com.couchbase.client.java.kv.RemoveOptions) ExistsOptions(com.couchbase.client.java.kv.ExistsOptions) InsertAccessor(com.couchbase.client.java.kv.InsertAccessor) DEFAULT_INSERT_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_INSERT_OPTIONS) GetAndTouchRequest(com.couchbase.client.core.msg.kv.GetAndTouchRequest) GetReplicaResult(com.couchbase.client.java.kv.GetReplicaResult) ExistsResult(com.couchbase.client.java.kv.ExistsResult) UnlockAccessor(com.couchbase.client.java.kv.UnlockAccessor) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) ReplicaHelper(com.couchbase.client.core.service.kv.ReplicaHelper) Expiry(com.couchbase.client.java.kv.Expiry) List(java.util.List) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest) GetAndLockRequest(com.couchbase.client.core.msg.kv.GetAndLockRequest) LookupInOptions(com.couchbase.client.java.kv.LookupInOptions) SubdocMutateRequest(com.couchbase.client.core.msg.kv.SubdocMutateRequest) GetAccessor(com.couchbase.client.java.kv.GetAccessor) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) Optional(java.util.Optional) ReplaceRequest(com.couchbase.client.core.msg.kv.ReplaceRequest) DEFAULT_GET_ALL_REPLICAS_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_ALL_REPLICAS_OPTIONS) DEFAULT_MUTATE_IN_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_MUTATE_IN_OPTIONS) GetOptions(com.couchbase.client.java.kv.GetOptions) UpsertAccessor(com.couchbase.client.java.kv.UpsertAccessor) RemoveAccessor(com.couchbase.client.java.kv.RemoveAccessor) GetResult(com.couchbase.client.java.kv.GetResult) ExistsAccessor(com.couchbase.client.java.kv.ExistsAccessor) TouchOptions(com.couchbase.client.java.kv.TouchOptions) UnlockOptions(com.couchbase.client.java.kv.UnlockOptions) DEFAULT_GET_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_OPTIONS) RemoveRequest(com.couchbase.client.core.msg.kv.RemoveRequest) StoreSemantics(com.couchbase.client.java.kv.StoreSemantics) CompletableFuture(java.util.concurrent.CompletableFuture) CommonDurabilityOptions(com.couchbase.client.java.kv.CommonDurabilityOptions) GetMetaRequest(com.couchbase.client.core.msg.kv.GetMetaRequest) ArrayList(java.util.ArrayList) DEFAULT_TOUCH_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_TOUCH_OPTIONS) GetAnyReplicaOptions(com.couchbase.client.java.kv.GetAnyReplicaOptions) ReplaceOptions(com.couchbase.client.java.kv.ReplaceOptions) InsertRequest(com.couchbase.client.core.msg.kv.InsertRequest) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) GetAndLockOptions(com.couchbase.client.java.kv.GetAndLockOptions) DEFAULT_REMOVE_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_REMOVE_OPTIONS) MutateInSpec(com.couchbase.client.java.kv.MutateInSpec) Stability(com.couchbase.client.core.annotation.Stability) DEFAULT_EXISTS_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_EXISTS_OPTIONS) DEFAULT_GET_ANY_REPLICA_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_ANY_REPLICA_OPTIONS) UpsertOptions(com.couchbase.client.java.kv.UpsertOptions) LookupInResult(com.couchbase.client.java.kv.LookupInResult) MutateInOptions(com.couchbase.client.java.kv.MutateInOptions) MutateInResult(com.couchbase.client.java.kv.MutateInResult) DEFAULT_UPSERT_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_UPSERT_OPTIONS) UnlockRequest(com.couchbase.client.core.msg.kv.UnlockRequest) LookupInAccessor(com.couchbase.client.java.kv.LookupInAccessor) DEFAULT_GET_AND_TOUCH_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_AND_TOUCH_OPTIONS) UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) LookupInSpec(com.couchbase.client.java.kv.LookupInSpec) DEFAULT_LOOKUP_IN_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_LOOKUP_IN_OPTIONS) Transcoder(com.couchbase.client.java.codec.Transcoder) DurabilityLevel(com.couchbase.client.core.msg.kv.DurabilityLevel) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Core(com.couchbase.client.core.Core) ReducedKeyValueErrorContext(com.couchbase.client.core.error.context.ReducedKeyValueErrorContext) Comparator(java.util.Comparator) DEFAULT_UNLOCK_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_UNLOCK_OPTIONS) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) SubdocCommandType(com.couchbase.client.core.msg.kv.SubdocCommandType) BucketConfigUtil(com.couchbase.client.core.util.BucketConfigUtil) ArrayList(java.util.ArrayList) MutateInSpec(com.couchbase.client.java.kv.MutateInSpec) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) BucketConfig(com.couchbase.client.core.config.BucketConfig) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) CompletableFuture(java.util.concurrent.CompletableFuture) SubdocMutateRequest(com.couchbase.client.core.msg.kv.SubdocMutateRequest) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy)

Aggregations

Core (com.couchbase.client.core.Core)49 CoreContext (com.couchbase.client.core.CoreContext)31 Test (org.junit.jupiter.api.Test)25 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)17 Duration (java.time.Duration)13 ArrayList (java.util.ArrayList)12 Map (java.util.Map)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 Stability (com.couchbase.client.core.annotation.Stability)11 Optional (java.util.Optional)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 BucketConfig (com.couchbase.client.core.config.BucketConfig)10 Authenticator (com.couchbase.client.core.env.Authenticator)10 List (java.util.List)10 Flux (reactor.core.publisher.Flux)10 Mono (reactor.core.publisher.Mono)10 Reactor (com.couchbase.client.core.Reactor)9 SeedNode (com.couchbase.client.core.env.SeedNode)9 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)9 HashSet (java.util.HashSet)9