Search in sources :

Example 6 with BucketConfig

use of com.couchbase.client.core.config.BucketConfig in project couchbase-jvm-clients by couchbase.

the class Observe method poll.

public static Mono<Void> poll(final ObserveContext ctx) {
    if (ctx.persistTo() == ObservePersistTo.NONE && ctx.replicateTo() == ObserveReplicateTo.NONE) {
        return Mono.empty();
    }
    if (!ctx.environment().ioConfig().mutationTokensEnabled() || !ctx.mutationToken().isPresent()) {
        return Mono.error(new FeatureNotAvailableException("To use PersistTo and/or ReplicateTo, mutation tokens must " + "be enabled on the IO configuration"));
    }
    final RequestSpan parentSpan = ctx.environment().requestTracer().requestSpan("observe", ctx.parentSpan());
    Flux<ObserveItem> observed = Flux.defer(() -> {
        BucketConfig config = ctx.core().clusterConfig().bucketConfig(ctx.collectionIdentifier().bucket());
        return Flux.just(validateReplicas(config, ctx.persistTo(), ctx.replicateTo()));
    }).flatMap(replicas -> viaMutationToken(replicas, ctx, parentSpan));
    return maybeRetry(observed, ctx).timeout(ctx.timeout(), ctx.environment().scheduler()).doFinally(t -> parentSpan.end());
}
Also used : FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) BucketConfig(com.couchbase.client.core.config.BucketConfig) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 7 with BucketConfig

use of com.couchbase.client.core.config.BucketConfig in project couchbase-jvm-clients by couchbase.

the class KeyValueLocator method dispatch.

@Override
public void dispatch(final Request<? extends Response> request, final List<Node> nodes, final ClusterConfig config, final CoreContext ctx) {
    if (request.target() != null) {
        dispatchTargeted(request, nodes, ctx);
    } else {
        KeyValueRequest r = (KeyValueRequest) request;
        String bucket = r.bucket();
        BucketConfig bucketConfig = config.bucketConfig(bucket);
        if (bucketConfig == null) {
            // Since a bucket is opened lazily, it might not be available yet (or for some
            // other reason the config is gone) - send it into retry!
            RetryOrchestrator.maybeRetry(ctx, request, ctx.core().configurationProvider().bucketConfigLoadInProgress() ? RetryReason.BUCKET_OPEN_IN_PROGRESS : RetryReason.BUCKET_NOT_AVAILABLE);
            return;
        }
        if (bucketConfig instanceof CouchbaseBucketConfig) {
            couchbaseBucket(r, nodes, (CouchbaseBucketConfig) bucketConfig, ctx);
        } else if (bucketConfig instanceof MemcachedBucketConfig) {
            memcacheBucket(r, nodes, (MemcachedBucketConfig) bucketConfig, ctx);
        } else {
            throw new IllegalStateException("Unsupported Bucket Type: " + bucketConfig + " for request " + request);
        }
    }
}
Also used : KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) MemcachedBucketConfig(com.couchbase.client.core.config.MemcachedBucketConfig) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) MemcachedBucketConfig(com.couchbase.client.core.config.MemcachedBucketConfig) BucketConfig(com.couchbase.client.core.config.BucketConfig)

Example 8 with BucketConfig

use of com.couchbase.client.core.config.BucketConfig in project couchbase-jvm-clients by couchbase.

the class BaseBucketLoaderTest method loadsAndParsesConfig.

@Test
void loadsAndParsesConfig() {
    BucketLoader loader = new BaseBucketLoader(core, SERVICE) {

        @Override
        protected Mono<byte[]> discoverConfig(NodeIdentifier seed, String bucket) {
            return Mono.just(readResource("../config_with_external.json", BaseBucketLoaderTest.class).getBytes(UTF_8));
        }
    };
    when(core.ensureServiceAt(eq(SEED), eq(SERVICE), eq(PORT), eq(Optional.of(BUCKET)), eq(Optional.empty()))).thenReturn(Mono.empty());
    when(core.serviceState(eq(SEED), eq(SERVICE), eq(Optional.of(BUCKET)))).thenReturn(Optional.of(Flux.empty()));
    ProposedBucketConfigContext ctx = loader.load(SEED, PORT, BUCKET, Optional.empty()).block();
    BucketConfig config = BucketConfigParser.parse(ctx.config(), core.context().environment(), ctx.origin());
    assertEquals("default", config.name());
    assertEquals(1073, config.rev());
}
Also used : NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) BucketConfig(com.couchbase.client.core.config.BucketConfig) Test(org.junit.jupiter.api.Test)

Example 9 with BucketConfig

use of com.couchbase.client.core.config.BucketConfig in project couchbase-jvm-clients by couchbase.

the class WaitUntilReadyHelper method waitUntilReady.

@Stability.Internal
public static CompletableFuture<Void> waitUntilReady(final Core core, final Set<ServiceType> serviceTypes, final Duration timeout, final ClusterState desiredState, final Optional<String> bucketName) {
    final WaitUntilReadyState state = new WaitUntilReadyState();
    state.transition(WaitUntilReadyStage.CONFIG_LOAD);
    return Flux.interval(Duration.ofMillis(10), core.context().environment().scheduler()).onBackpressureDrop().filter(i -> !(core.configurationProvider().bucketConfigLoadInProgress() || core.configurationProvider().globalConfigLoadInProgress() || (bucketName.isPresent() && core.configurationProvider().collectionRefreshInProgress()) || (bucketName.isPresent() && core.clusterConfig().bucketConfig(bucketName.get()) == null))).filter(i -> {
        // created.
        if (bucketName.isPresent()) {
            state.transition(WaitUntilReadyStage.BUCKET_CONFIG_READY);
            BucketConfig bucketConfig = core.clusterConfig().bucketConfig(bucketName.get());
            long extNodes = bucketConfig.portInfos().stream().filter(p -> p.ports().containsKey(ServiceType.KV)).count();
            long visibleNodes = bucketConfig.nodes().stream().filter(n -> n.services().containsKey(ServiceType.KV)).count();
            return extNodes > 0 && extNodes == visibleNodes;
        } else {
            return true;
        }
    }).flatMap(i -> {
        if (bucketName.isPresent()) {
            state.transition(WaitUntilReadyStage.BUCKET_NODES_HEALTHY);
            // To avoid tmpfails on the bucket, we double check that all nodes from the nodes list are
            // in a healthy status - but for this we need to actually fetch the verbose config, since
            // the terse one doesn't have that status in it.
            GenericManagerRequest request = new GenericManagerRequest(core.context(), () -> new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/pools/default/buckets/" + bucketName.get()), true, null);
            core.send(request);
            return Reactor.wrap(request, request.response(), true).filter(response -> {
                if (response.status() != ResponseStatus.SUCCESS) {
                    return false;
                }
                ObjectNode root = (ObjectNode) Mapper.decodeIntoTree(response.content());
                ArrayNode nodes = (ArrayNode) root.get("nodes");
                long healthy = StreamSupport.stream(nodes.spliterator(), false).filter(node -> node.get("status").asText().equals("healthy")).count();
                return nodes.size() == healthy;
            }).map(ignored -> i);
        } else {
            return Flux.just(i);
        }
    }).take(1).flatMap(aLong -> {
        // to being ready at this point. Bucket level wait until ready is the way to go there.
        if (!bucketName.isPresent() && !core.clusterConfig().hasClusterOrBucketConfig()) {
            state.transition(WaitUntilReadyStage.COMPLETE);
            WaitUntilReadyContext waitUntilReadyContext = new WaitUntilReadyContext(servicesToCheck(core, serviceTypes, bucketName), timeout, desiredState, bucketName, core.diagnostics().collect(Collectors.groupingBy(EndpointDiagnostics::type)), state);
            core.context().environment().eventBus().publish(new WaitUntilReadyCompletedEvent(waitUntilReadyContext, WaitUntilReadyCompletedEvent.Reason.CLUSTER_LEVEL_NOT_SUPPORTED));
            return Flux.empty();
        }
        state.transition(WaitUntilReadyStage.PING);
        final Flux<ClusterState> diagnostics = Flux.interval(Duration.ofMillis(10), core.context().environment().scheduler()).onBackpressureDrop().map(i -> diagnosticsCurrentState(core)).takeUntil(s -> s == desiredState);
        return Flux.concat(ping(core, servicesToCheck(core, serviceTypes, bucketName), timeout, bucketName), diagnostics);
    }).then().timeout(timeout, Mono.defer(() -> {
        WaitUntilReadyContext waitUntilReadyContext = new WaitUntilReadyContext(servicesToCheck(core, serviceTypes, bucketName), timeout, desiredState, bucketName, core.diagnostics().collect(Collectors.groupingBy(EndpointDiagnostics::type)), state);
        CancellationErrorContext errorContext = new CancellationErrorContext(waitUntilReadyContext);
        return Mono.error(new UnambiguousTimeoutException("WaitUntilReady timed out", errorContext));
    }), core.context().environment().scheduler()).doOnSuccess(unused -> {
        state.transition(WaitUntilReadyStage.COMPLETE);
        WaitUntilReadyContext waitUntilReadyContext = new WaitUntilReadyContext(servicesToCheck(core, serviceTypes, bucketName), timeout, desiredState, bucketName, core.diagnostics().collect(Collectors.groupingBy(EndpointDiagnostics::type)), state);
        core.context().environment().eventBus().publish(new WaitUntilReadyCompletedEvent(waitUntilReadyContext, WaitUntilReadyCompletedEvent.Reason.SUCCESS));
    }).toFuture();
}
Also used : CbCollections.isNullOrEmpty(com.couchbase.client.core.util.CbCollections.isNullOrEmpty) UnambiguousTimeoutException(com.couchbase.client.core.error.UnambiguousTimeoutException) HttpVersion(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpVersion) RequestTarget(com.couchbase.client.core.msg.RequestTarget) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ServiceType(com.couchbase.client.core.service.ServiceType) Duration(java.time.Duration) Map(java.util.Map) Stability(com.couchbase.client.core.annotation.Stability) GenericManagerRequest(com.couchbase.client.core.msg.manager.GenericManagerRequest) StreamSupport(java.util.stream.StreamSupport) BucketConfig(com.couchbase.client.core.config.BucketConfig) ObjectNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ObjectNode) CancellationErrorContext(com.couchbase.client.core.error.context.CancellationErrorContext) ArrayNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ArrayNode) Reactor(com.couchbase.client.core.Reactor) HttpMethod(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpMethod) Mapper(com.couchbase.client.core.json.Mapper) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) Mono(reactor.core.publisher.Mono) Collectors(java.util.stream.Collectors) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) WaitUntilReadyCompletedEvent(com.couchbase.client.core.cnc.events.core.WaitUntilReadyCompletedEvent) TreeMap(java.util.TreeMap) Optional(java.util.Optional) Core(com.couchbase.client.core.Core) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) ObjectNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ObjectNode) UnambiguousTimeoutException(com.couchbase.client.core.error.UnambiguousTimeoutException) BucketConfig(com.couchbase.client.core.config.BucketConfig) GenericManagerRequest(com.couchbase.client.core.msg.manager.GenericManagerRequest) WaitUntilReadyCompletedEvent(com.couchbase.client.core.cnc.events.core.WaitUntilReadyCompletedEvent) ArrayNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ArrayNode) CancellationErrorContext(com.couchbase.client.core.error.context.CancellationErrorContext)

Example 10 with BucketConfig

use of com.couchbase.client.core.config.BucketConfig in project couchbase-jvm-clients by couchbase.

the class KeyValueBucketRefresher method filterEligibleNodes.

/**
 * Helper method to generate a stream of KV nodes that can be used to fetch a config.
 *
 * <p>To be more resilient to failures, this method (similar to 1.x) shifts the node
 * list on each invocation by one to make sure we hit all of them eventually.</p>
 *
 * @param name the bucket name.
 * @return a flux of nodes that have the KV service enabled.
 */
private Flux<NodeInfo> filterEligibleNodes(final String name) {
    return Flux.defer(() -> {
        BucketConfig config = provider.config().bucketConfig(name);
        if (config == null) {
            eventBus.publish(new BucketConfigRefreshFailedEvent(core.context(), BucketConfigRefreshFailedEvent.RefresherType.KV, BucketConfigRefreshFailedEvent.Reason.NO_BUCKET_FOUND, Optional.empty()));
            return Flux.empty();
        }
        List<NodeInfo> nodes = new ArrayList<>(config.nodes());
        shiftNodeList(nodes);
        return Flux.fromIterable(nodes).filter(n -> n.services().containsKey(ServiceType.KV) || n.sslServices().containsKey(ServiceType.KV)).take(MAX_PARALLEL_FETCH);
    });
}
Also used : Disposable(reactor.core.Disposable) ArrayList(java.util.ArrayList) EventBus(com.couchbase.client.core.cnc.EventBus) ServiceType(com.couchbase.client.core.service.ServiceType) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) Map(java.util.Map) Stability(com.couchbase.client.core.annotation.Stability) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) BucketConfig(com.couchbase.client.core.config.BucketConfig) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) Reactor(com.couchbase.client.core.Reactor) UTF_8(java.nio.charset.StandardCharsets.UTF_8) NodeInfo(com.couchbase.client.core.config.NodeInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Mono(reactor.core.publisher.Mono) FailFastRetryStrategy(com.couchbase.client.core.retry.FailFastRetryStrategy) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Optional(java.util.Optional) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) BucketConfigRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.BucketConfigRefreshFailedEvent) CarrierBucketConfigRequest(com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest) Core(com.couchbase.client.core.Core) NodeInfo(com.couchbase.client.core.config.NodeInfo) ArrayList(java.util.ArrayList) BucketConfigRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.BucketConfigRefreshFailedEvent) BucketConfig(com.couchbase.client.core.config.BucketConfig)

Aggregations

BucketConfig (com.couchbase.client.core.config.BucketConfig)20 Optional (java.util.Optional)12 ServiceType (com.couchbase.client.core.service.ServiceType)10 NodeIdentifier (com.couchbase.client.core.node.NodeIdentifier)9 HashMap (java.util.HashMap)9 SeedNode (com.couchbase.client.core.env.SeedNode)8 Duration (java.time.Duration)8 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)7 ConfigurationProvider (com.couchbase.client.core.config.ConfigurationProvider)7 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 Test (org.junit.jupiter.api.Test)7 Core (com.couchbase.client.core.Core)6 Stability (com.couchbase.client.core.annotation.Stability)6 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)6 Node (com.couchbase.client.core.node.Node)6 List (java.util.List)5 Flux (reactor.core.publisher.Flux)5 Reactor (com.couchbase.client.core.Reactor)4 EventBus (com.couchbase.client.core.cnc.EventBus)4