Search in sources :

Example 21 with Core

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

the class HealthPinger method pingKv.

private static Mono<EndpointPingReport> pingKv(final Core core, final RequestTarget target, final CoreCommonOptions options) {
    return Mono.defer(() -> {
        Duration timeout = options.timeout().orElse(core.context().environment().timeoutConfig().kvTimeout());
        CollectionIdentifier collectionIdentifier = CollectionIdentifier.fromDefault(target.bucketName());
        KvPingRequest request = new KvPingRequest(timeout, core.context(), options.retryStrategy().orElse(null), collectionIdentifier, target.nodeIdentifier());
        core.send(request);
        return Reactor.wrap(request, request.response(), true).map(response -> {
            request.context().logicallyComplete();
            return assembleSuccessReport(request.context(), ((KvPingResponse) response).channelId(), Optional.ofNullable(target.bucketName()));
        }).onErrorResume(throwable -> {
            request.context().logicallyComplete();
            return Mono.just(assembleFailureReport(throwable, request.context(), Optional.ofNullable(target.bucketName())));
        });
    });
}
Also used : ClusterConfig(com.couchbase.client.core.config.ClusterConfig) CbCollections.isNullOrEmpty(com.couchbase.client.core.util.CbCollections.isNullOrEmpty) CoreHttpRequest(com.couchbase.client.core.endpoint.http.CoreHttpRequest) TimeoutException(com.couchbase.client.core.error.TimeoutException) RequestTarget(com.couchbase.client.core.msg.RequestTarget) KvPingRequest(com.couchbase.client.core.msg.kv.KvPingRequest) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) HashSet(java.util.HashSet) ServiceType(com.couchbase.client.core.service.ServiceType) Duration(java.time.Duration) Map(java.util.Map) CoreHttpPath.path(com.couchbase.client.core.endpoint.http.CoreHttpPath.path) Stability(com.couchbase.client.core.annotation.Stability) RequestContext(com.couchbase.client.core.msg.RequestContext) BucketConfig(com.couchbase.client.core.config.BucketConfig) KvPingResponse(com.couchbase.client.core.msg.kv.KvPingResponse) Reactor(com.couchbase.client.core.Reactor) NodeInfo(com.couchbase.client.core.config.NodeInfo) Set(java.util.Set) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Flux(reactor.core.publisher.Flux) Optional(java.util.Optional) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Core(com.couchbase.client.core.Core) PortInfo(com.couchbase.client.core.config.PortInfo) KvPingRequest(com.couchbase.client.core.msg.kv.KvPingRequest) KvPingResponse(com.couchbase.client.core.msg.kv.KvPingResponse) Duration(java.time.Duration) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier)

Example 22 with Core

use of com.couchbase.client.core.Core 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 23 with Core

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

the class CoreBucketManager method updateBucket.

public CompletableFuture<Void> updateBucket(Map<String, String> settings, final CoreCommonOptions options) {
    String bucketName = getBucketName(settings);
    RequestSpan span = CbTracing.newSpan(core.context(), TracingIdentifiers.SPAN_REQUEST_MB_UPDATE_BUCKET, options.parentSpan().orElse(null));
    span.attribute(TracingIdentifiers.ATTR_NAME, bucketName);
    CoreCommonOptions getAllBucketOptions = options.withParentSpan(span);
    return Mono.fromFuture(() -> getAllBuckets(getAllBucketOptions)).map(buckets -> buckets.containsKey(bucketName)).flatMap(bucketExists -> {
        if (!bucketExists) {
            return Mono.error(BucketNotFoundException.forBucket(bucketName));
        }
        return Mono.fromFuture(httpClient.post(pathForBucket(bucketName), options).form(convertSettingsToParams(settings, true)).exec(core).thenApply(response -> null));
    }).then().doOnTerminate(span::end).toFuture();
}
Also used : HttpStatusCodeException.couchbaseResponseStatus(com.couchbase.client.core.error.HttpStatusCodeException.couchbaseResponseStatus) RequestTarget(com.couchbase.client.core.msg.RequestTarget) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CoreHttpPath(com.couchbase.client.core.endpoint.http.CoreHttpPath) Function(java.util.function.Function) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) HttpStatusCodeException.httpResponseBody(com.couchbase.client.core.error.HttpStatusCodeException.httpResponseBody) JsonNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode) BucketNotFlushableException(com.couchbase.client.core.error.BucketNotFlushableException) CbCollections.mapOf(com.couchbase.client.core.util.CbCollections.mapOf) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) Map(java.util.Map) CoreHttpPath.path(com.couchbase.client.core.endpoint.http.CoreHttpPath.path) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) CbTracing(com.couchbase.client.core.cnc.CbTracing) BucketNotFoundException(com.couchbase.client.core.error.BucketNotFoundException) CbThrowables.propagate(com.couchbase.client.core.util.CbThrowables.propagate) UrlQueryStringBuilder(com.couchbase.client.core.util.UrlQueryStringBuilder) Mapper(com.couchbase.client.core.json.Mapper) CoreHttpClient(com.couchbase.client.core.endpoint.http.CoreHttpClient) Mono(reactor.core.publisher.Mono) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) RedactableArgument.redactMeta(com.couchbase.client.core.logging.RedactableArgument.redactMeta) CoreHttpResponse(com.couchbase.client.core.endpoint.http.CoreHttpResponse) BucketExistsException(com.couchbase.client.core.error.BucketExistsException) Core(com.couchbase.client.core.Core) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 24 with Core

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

the class HealthPinger method pingHttpEndpoint.

private static Mono<EndpointPingReport> pingHttpEndpoint(final Core core, final RequestTarget target, final CoreCommonOptions options, final String path) {
    return Mono.defer(() -> {
        CoreHttpRequest request = core.httpClient(target).get(path(path), options).build();
        core.send(request);
        return Reactor.wrap(request, request.response(), true).map(response -> {
            request.context().logicallyComplete();
            return assembleSuccessReport(request.context(), response.channelId(), Optional.empty());
        }).onErrorResume(throwable -> {
            request.context().logicallyComplete();
            return Mono.just(assembleFailureReport(throwable, request.context(), Optional.empty()));
        });
    });
}
Also used : ClusterConfig(com.couchbase.client.core.config.ClusterConfig) CbCollections.isNullOrEmpty(com.couchbase.client.core.util.CbCollections.isNullOrEmpty) CoreHttpRequest(com.couchbase.client.core.endpoint.http.CoreHttpRequest) TimeoutException(com.couchbase.client.core.error.TimeoutException) RequestTarget(com.couchbase.client.core.msg.RequestTarget) KvPingRequest(com.couchbase.client.core.msg.kv.KvPingRequest) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) HashSet(java.util.HashSet) ServiceType(com.couchbase.client.core.service.ServiceType) Duration(java.time.Duration) Map(java.util.Map) CoreHttpPath.path(com.couchbase.client.core.endpoint.http.CoreHttpPath.path) Stability(com.couchbase.client.core.annotation.Stability) RequestContext(com.couchbase.client.core.msg.RequestContext) BucketConfig(com.couchbase.client.core.config.BucketConfig) KvPingResponse(com.couchbase.client.core.msg.kv.KvPingResponse) Reactor(com.couchbase.client.core.Reactor) NodeInfo(com.couchbase.client.core.config.NodeInfo) Set(java.util.Set) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Flux(reactor.core.publisher.Flux) Optional(java.util.Optional) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Core(com.couchbase.client.core.Core) PortInfo(com.couchbase.client.core.config.PortInfo) CoreHttpRequest(com.couchbase.client.core.endpoint.http.CoreHttpRequest)

Example 25 with Core

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

the class KeyValueChannelIntegrationTest method beforeAll.

@BeforeAll
static void beforeAll() {
    TestNodeConfig node = config().nodes().get(0);
    env = environment().eventBus(eventBus).build();
    Core core = Core.create(env, authenticator(), seedNodes());
    endpointContext = new EndpointContext(core.context(), new HostAndPort(node.hostname(), node.ports().get(Services.KV)), null, ServiceType.KV, Optional.empty(), Optional.of(config().bucketname()), Optional.empty());
    eventLoopGroup = new NioEventLoopGroup(1);
}
Also used : HostAndPort(com.couchbase.client.core.util.HostAndPort) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) NioEventLoopGroup(com.couchbase.client.core.deps.io.netty.channel.nio.NioEventLoopGroup) Core(com.couchbase.client.core.Core) BeforeAll(org.junit.jupiter.api.BeforeAll)

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