Search in sources :

Example 21 with CoreEnvironment

use of com.couchbase.client.core.env.CoreEnvironment in project couchbase-jvm-clients by couchbase.

the class ClusterManagerBucketLoaderTest method setup.

@BeforeEach
void setup() {
    CoreEnvironment env = mock(CoreEnvironment.class);
    when(env.timeoutConfig()).thenReturn(TimeoutConfig.create());
    when(env.retryStrategy()).thenReturn(BestEffortRetryStrategy.INSTANCE);
    core = mock(Core.class);
    CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
    when(core.context()).thenReturn(ctx);
    loader = new ClusterManagerBucketLoader(core);
}
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 22 with CoreEnvironment

use of com.couchbase.client.core.env.CoreEnvironment in project couchbase-jvm-clients by couchbase.

the class KeyValueBucketLoaderTest method setup.

@BeforeEach
void setup() {
    CoreEnvironment env = mock(CoreEnvironment.class);
    when(env.timeoutConfig()).thenReturn(TimeoutConfig.create());
    when(env.retryStrategy()).thenReturn(BestEffortRetryStrategy.INSTANCE);
    core = mock(Core.class);
    CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
    when(core.context()).thenReturn(ctx);
    loader = new KeyValueBucketLoader(core);
}
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 23 with CoreEnvironment

use of com.couchbase.client.core.env.CoreEnvironment in project couchbase-jvm-clients by couchbase.

the class DefaultConfigurationProviderTest method forceDefaultModeIfDefault.

@Test
void forceDefaultModeIfDefault() {
    Core core = mock(Core.class);
    CoreEnvironment environment = CoreEnvironment.builder().ioConfig(IoConfig.networkResolution(NetworkResolution.DEFAULT)).build();
    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(Optional.empty(), ctx.alternateAddress());
    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 24 with CoreEnvironment

use of com.couchbase.client.core.env.CoreEnvironment in project couchbase-jvm-clients by couchbase.

the class ErrorMapLoadingHandlerTest method beforeEach.

@BeforeEach
@Override
protected void beforeEach() {
    super.beforeEach();
    CoreEnvironment env = mock(CoreEnvironment.class);
    TimeoutConfig timeoutConfig = mock(TimeoutConfig.class);
    when(env.eventBus()).thenReturn(eventBus);
    when(env.timeoutConfig()).thenReturn(timeoutConfig);
    when(timeoutConfig.connectTimeout()).thenReturn(Duration.ofMillis(1000));
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, mock(Authenticator.class));
    endpointContext = new EndpointContext(coreContext, new HostAndPort("127.0.0.1", 1234), null, ServiceType.KV, Optional.empty(), Optional.empty(), Optional.empty());
}
Also used : HostAndPort(com.couchbase.client.core.util.HostAndPort) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 25 with CoreEnvironment

use of com.couchbase.client.core.env.CoreEnvironment in project couchbase-jvm-clients by couchbase.

the class ReplicaHelper method getAllReplicasRequests.

/**
 * Helper method to assemble a stream of requests to the active and all replicas
 *
 * @param core the core to execute the request
 * @param collectionIdentifier the collection containing the document
 * @param documentId the ID of the document
 * @param clientContext (nullable) client context info
 * @param retryStrategy the retry strategy to use
 * @param timeout the timeout until we need to stop the get all replicas
 * @param parent the "get all/any replicas" request span
 * @return a stream of requests.
 */
public static CompletableFuture<Stream<GetRequest>> getAllReplicasRequests(final Core core, final CollectionIdentifier collectionIdentifier, final String documentId, final Map<String, Object> clientContext, final RetryStrategy retryStrategy, final Duration timeout, final RequestSpan parent) {
    notNullOrEmpty(documentId, "Id");
    final CoreContext coreContext = core.context();
    final CoreEnvironment environment = coreContext.environment();
    final BucketConfig config = core.clusterConfig().bucketConfig(collectionIdentifier.bucket());
    if (config instanceof CouchbaseBucketConfig) {
        int numReplicas = ((CouchbaseBucketConfig) config).numberOfReplicas();
        List<GetRequest> requests = new ArrayList<>(numReplicas + 1);
        RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET, parent);
        GetRequest activeRequest = new GetRequest(documentId, timeout, coreContext, collectionIdentifier, retryStrategy, span);
        activeRequest.context().clientContext(clientContext);
        requests.add(activeRequest);
        for (short replica = 1; replica <= numReplicas; replica++) {
            RequestSpan replicaSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET_REPLICA, parent);
            ReplicaGetRequest replicaRequest = new ReplicaGetRequest(documentId, timeout, coreContext, collectionIdentifier, retryStrategy, replica, replicaSpan);
            replicaRequest.context().clientContext(clientContext);
            requests.add(replicaRequest);
        }
        return CompletableFuture.completedFuture(requests.stream());
    } else if (config == null) {
        // no bucket config found, it might be in-flight being opened so we need to reschedule the operation until
        // the timeout fires!
        final Duration retryDelay = Duration.ofMillis(100);
        final CompletableFuture<Stream<GetRequest>> future = new CompletableFuture<>();
        coreContext.environment().timer().schedule(() -> {
            getAllReplicasRequests(core, collectionIdentifier, documentId, clientContext, retryStrategy, timeout.minus(retryDelay), parent).whenComplete((getRequestStream, throwable) -> {
                if (throwable != null) {
                    future.completeExceptionally(throwable);
                } else {
                    future.complete(getRequestStream);
                }
            });
        }, retryDelay);
        return future;
    } else {
        final CompletableFuture<Stream<GetRequest>> future = new CompletableFuture<>();
        future.completeExceptionally(CommonExceptions.getFromReplicaNotCouchbaseBucket());
        return future;
    }
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) DefaultErrorUtil.keyValueStatusToException(com.couchbase.client.core.error.DefaultErrorUtil.keyValueStatusToException) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DocumentUnretrievableException(com.couchbase.client.core.error.DocumentUnretrievableException) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) GetResponse(com.couchbase.client.core.msg.kv.GetResponse) IndividualReplicaGetFailedEvent(com.couchbase.client.core.cnc.events.request.IndividualReplicaGetFailedEvent) BucketConfig(com.couchbase.client.core.config.BucketConfig) Reactor(com.couchbase.client.core.Reactor) CommonExceptions(com.couchbase.client.core.error.CommonExceptions) Mono(reactor.core.publisher.Mono) CompletionException(java.util.concurrent.CompletionException) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Collectors(java.util.stream.Collectors) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) Flux(reactor.core.publisher.Flux) List(java.util.List) Stream(java.util.stream.Stream) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ReplicaGetRequest(com.couchbase.client.core.msg.kv.ReplicaGetRequest) ErrorContext(com.couchbase.client.core.error.context.ErrorContext) 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) Collections(java.util.Collections) AggregateErrorContext(com.couchbase.client.core.error.context.AggregateErrorContext) CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) Duration(java.time.Duration) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) BucketConfig(com.couchbase.client.core.config.BucketConfig) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) CompletableFuture(java.util.concurrent.CompletableFuture) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ReplicaGetRequest(com.couchbase.client.core.msg.kv.ReplicaGetRequest) ReplicaGetRequest(com.couchbase.client.core.msg.kv.ReplicaGetRequest)

Aggregations

CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)30 Core (com.couchbase.client.core.Core)20 CoreContext (com.couchbase.client.core.CoreContext)20 Test (org.junit.jupiter.api.Test)15 Authenticator (com.couchbase.client.core.env.Authenticator)13 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)9 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)8 HostAndPort (com.couchbase.client.core.util.HostAndPort)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)6 Stability (com.couchbase.client.core.annotation.Stability)5 TimeoutConfig (com.couchbase.client.core.env.TimeoutConfig)5 Duration (java.time.Duration)5 List (java.util.List)5 Map (java.util.Map)5 Flux (reactor.core.publisher.Flux)5 Mono (reactor.core.publisher.Mono)5 Reactor (com.couchbase.client.core.Reactor)4 BucketConfig (com.couchbase.client.core.config.BucketConfig)4 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)4