Search in sources :

Example 1 with DefaultFullHttpRequest

use of com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest in project couchbase-jvm-clients by couchbase.

the class RawManager method callManagement.

private static Mono<RawManagerResponse> callManagement(final Cluster cluster, final RawManagerRequest request, final RawManagerOptions options) {
    final ClusterEnvironment environment = cluster.environment();
    final RawManagerOptions.Built opts = options.build();
    JsonSerializer serializer = opts.serializer() != null ? opts.serializer() : environment.jsonSerializer();
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().managementTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    final GenericManagerRequest req = new GenericManagerRequest(timeout, cluster.core().context(), retryStrategy, () -> {
        FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, request.method(), request.uri());
        for (Map.Entry<String, Object> e : opts.httpHeaders().entrySet()) {
            httpRequest.headers().set(e.getKey(), e.getValue());
        }
        return httpRequest;
    }, request.method().equals(HttpMethod.GET), null);
    cluster.core().send(req);
    return Reactor.wrap(req, req.response(), true).map(res -> new RawManagerResponse(request.serviceType(), serializer, res.httpStatus(), res.content()));
}
Also used : FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) Duration(java.time.Duration) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) GenericManagerRequest(com.couchbase.client.core.msg.manager.GenericManagerRequest) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Map(java.util.Map)

Example 2 with DefaultFullHttpRequest

use of com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest in project couchbase-jvm-clients by couchbase.

the class ViewRequest method encode.

@Override
public FullHttpRequest encode() {
    StringBuilder path = new StringBuilder();
    path.append("/").append(bucket).append("/_design/");
    path.append(development ? "dev_" + design : design);
    path.append("/_view/");
    path.append(view);
    path.append("?").append(query);
    ByteBuf content = keysJson.isPresent() ? Unpooled.copiedBuffer(keysJson.get()) : Unpooled.EMPTY_BUFFER;
    HttpMethod method = keysJson.isPresent() ? HttpMethod.POST : HttpMethod.GET;
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, path.toString(), content);
    request.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON).set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes()).set(HttpHeaderNames.USER_AGENT, context().environment().userAgent().formattedLong());
    authenticator.authHttpRequest(serviceType(), request);
    return request;
}
Also used : FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) HttpMethod(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpMethod)

Example 3 with DefaultFullHttpRequest

use of com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest in project couchbase-jvm-clients by couchbase.

the class BucketConfigStreamingRequest method encode.

@Override
public FullHttpRequest encode() {
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, String.format(PATH, bucketName));
    authenticator.authHttpRequest(serviceType(), request);
    return request;
}
Also used : FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest)

Example 4 with DefaultFullHttpRequest

use of com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest 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 5 with DefaultFullHttpRequest

use of com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest in project couchbase-jvm-clients by couchbase.

the class AnalyticsRequest method encode.

@Override
public FullHttpRequest encode() {
    ByteBuf content = Unpooled.wrappedBuffer(query);
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, URI, content);
    request.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
    request.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
    request.headers().set(HttpHeaderNames.USER_AGENT, context().environment().userAgent().formattedLong());
    if (priority != NO_PRIORITY) {
        request.headers().set("Analytics-Priority", priority);
    }
    authenticator.authHttpRequest(serviceType(), request);
    return request;
}
Also used : FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Aggregations

DefaultFullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest)9 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)7 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)4 Core (com.couchbase.client.core.Core)2 HttpMethod (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpMethod)2 GenericManagerRequest (com.couchbase.client.core.msg.manager.GenericManagerRequest)2 Duration (java.time.Duration)2 Map (java.util.Map)2 CoreContext (com.couchbase.client.core.CoreContext)1 Reactor (com.couchbase.client.core.Reactor)1 Stability (com.couchbase.client.core.annotation.Stability)1 WaitUntilReadyCompletedEvent (com.couchbase.client.core.cnc.events.core.WaitUntilReadyCompletedEvent)1 BucketConfig (com.couchbase.client.core.config.BucketConfig)1 ArrayNode (com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ObjectNode)1 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)1 DefaultHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse)1 DefaultLastHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent)1 HttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent)1 HttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse)1