Search in sources :

Example 1 with InvalidRequestDetectedEvent

use of com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent in project couchbase-jvm-clients by couchbase.

the class NonChunkedHttpMessageHandler method write.

/**
 * Writes a given request and encodes it.
 *
 * @param ctx the channel handler context.
 * @param msg the msg to write.
 * @param promise the promise that will be passed along.
 */
@Override
@SuppressWarnings("unchecked")
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
    // toes.
    if (currentRequest != null) {
        RetryOrchestrator.maybeRetry(endpointContext, (Request<? extends Response>) msg, RetryReason.NOT_PIPELINED_REQUEST_IN_FLIGHT);
        if (endpoint != null) {
            endpoint.decrementOutstandingRequests();
        }
        return;
    }
    if (msg instanceof NonChunkedHttpRequest) {
        try {
            currentRequest = (NonChunkedHttpRequest<Response>) msg;
            FullHttpRequest encoded = ((NonChunkedHttpRequest<Response>) msg).encode();
            encoded.headers().set(HttpHeaderNames.HOST, remoteHost);
            encoded.headers().set(HttpHeaderNames.USER_AGENT, endpointContext.environment().userAgent().formattedLong());
            dispatchTimingStart = System.nanoTime();
            if (currentRequest.requestSpan() != null) {
                RequestTracer tracer = endpointContext.environment().requestTracer();
                currentDispatchSpan = tracer.requestSpan(TracingIdentifiers.SPAN_DISPATCH, currentRequest.requestSpan());
                if (!CbTracing.isInternalTracer(tracer)) {
                    setCommonDispatchSpanAttributes(currentDispatchSpan, ctx.channel().attr(ChannelAttributes.CHANNEL_ID_KEY).get(), ioContext.localHostname(), ioContext.localPort(), endpoint.remoteHostname(), endpoint.remotePort(), currentRequest.operationId());
                }
            }
            ctx.write(encoded, promise);
        } catch (Throwable t) {
            currentRequest.response().completeExceptionally(t);
            if (endpoint != null) {
                endpoint.decrementOutstandingRequests();
            }
        }
    } else {
        if (endpoint != null) {
            endpoint.decrementOutstandingRequests();
        }
        eventBus.publish(new InvalidRequestDetectedEvent(ioContext, serviceType, msg));
        ctx.channel().close().addListener(f -> eventBus.publish(new ChannelClosedProactivelyEvent(ioContext, ChannelClosedProactivelyEvent.Reason.INVALID_REQUEST_DETECTED)));
    }
}
Also used : FullHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse) Response(com.couchbase.client.core.msg.Response) FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) ChannelClosedProactivelyEvent(com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent) NonChunkedHttpRequest(com.couchbase.client.core.msg.NonChunkedHttpRequest) RequestTracer(com.couchbase.client.core.cnc.RequestTracer) InvalidRequestDetectedEvent(com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent)

Example 2 with InvalidRequestDetectedEvent

use of com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent in project couchbase-jvm-clients by couchbase.

the class ManagerMessageHandler method write.

@Override
@SuppressWarnings({ "unchecked" })
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
    if (msg instanceof ManagerRequest) {
        if (currentRequest != null) {
            RetryOrchestrator.maybeRetry(coreContext, (ManagerRequest<Response>) msg, RetryReason.NOT_PIPELINED_REQUEST_IN_FLIGHT);
            if (endpoint != null) {
                endpoint.decrementOutstandingRequests();
            }
            return;
        }
        try {
            currentRequest = (ManagerRequest<Response>) msg;
            FullHttpRequest encoded = currentRequest.encode();
            encoded.headers().set(HttpHeaderNames.HOST, remoteHost);
            encoded.headers().set(HttpHeaderNames.USER_AGENT, endpoint.context().environment().userAgent().formattedLong());
            ctx.writeAndFlush(encoded);
        } catch (Throwable t) {
            currentRequest.response().completeExceptionally(t);
            if (endpoint != null) {
                endpoint.decrementOutstandingRequests();
            }
        }
        currentContent.clear();
    } else {
        if (endpoint != null) {
            endpoint.decrementOutstandingRequests();
        }
        eventBus.publish(new InvalidRequestDetectedEvent(ioContext, ServiceType.MANAGER, msg));
        ctx.channel().close().addListener(f -> eventBus.publish(new ChannelClosedProactivelyEvent(ioContext, ChannelClosedProactivelyEvent.Reason.INVALID_REQUEST_DETECTED)));
    }
}
Also used : BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) Response(com.couchbase.client.core.msg.Response) ManagerRequest(com.couchbase.client.core.msg.manager.ManagerRequest) FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) ChannelClosedProactivelyEvent(com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent) InvalidRequestDetectedEvent(com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent)

Example 3 with InvalidRequestDetectedEvent

use of com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent in project couchbase-jvm-clients by couchbase.

the class KeyValueMessageHandler method write.

@Override
@SuppressWarnings({ "unchecked" })
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
    if (msg instanceof KeyValueRequest) {
        KeyValueRequest<Response> request = (KeyValueRequest<Response>) msg;
        int opaque = request.opaque();
        writtenRequests.put(opaque, request);
        try {
            ctx.write(request.encode(ctx.alloc(), opaque, channelContext), promise);
            writtenRequestDispatchTimings.put(opaque, (Long) System.nanoTime());
            if (request.requestSpan() != null) {
                RequestTracer tracer = endpointContext.environment().requestTracer();
                RequestSpan dispatchSpan = tracer.requestSpan(TracingIdentifiers.SPAN_DISPATCH, request.requestSpan());
                if (!isInternalTracer) {
                    setCommonDispatchSpanAttributes(dispatchSpan, ctx.channel().attr(ChannelAttributes.CHANNEL_ID_KEY).get(), ioContext.localHostname(), ioContext.localPort(), endpoint.remoteHostname(), endpoint.remotePort(), null);
                    setNumericOperationId(dispatchSpan, request.opaque());
                    setCommonKVSpanAttributes(dispatchSpan, request);
                }
                writtenRequestDispatchSpans.put(opaque, dispatchSpan);
            }
        } catch (Throwable err) {
            writtenRequests.remove(opaque);
            if (err instanceof CollectionNotFoundException) {
                if (channelContext.collectionsEnabled()) {
                    ConfigurationProvider cp = ioContext.core().configurationProvider();
                    if (cp.collectionRefreshInProgress(request.collectionIdentifier())) {
                        RetryOrchestrator.maybeRetry(ioContext, request, RetryReason.COLLECTION_MAP_REFRESH_IN_PROGRESS);
                    } else if (cp.config().bucketConfig(request.bucket()) instanceof MemcachedBucketConfig) {
                        request.fail(FeatureNotAvailableException.collectionsForMemcached());
                    } else {
                        handleOutdatedCollection(request, RetryReason.COLLECTION_NOT_FOUND);
                    }
                    return;
                }
            }
            request.fail(err);
        }
    } else {
        eventBus.publish(new InvalidRequestDetectedEvent(ioContext, ServiceType.KV, msg));
        ctx.channel().close().addListener(f -> eventBus.publish(new ChannelClosedProactivelyEvent(ioContext, ChannelClosedProactivelyEvent.Reason.INVALID_REQUEST_DETECTED)));
    }
}
Also used : Response(com.couchbase.client.core.msg.Response) KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) ChannelClosedProactivelyEvent(com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) MemcachedBucketConfig(com.couchbase.client.core.config.MemcachedBucketConfig) RequestTracer(com.couchbase.client.core.cnc.RequestTracer) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) CollectionNotFoundException(com.couchbase.client.core.error.CollectionNotFoundException) InvalidRequestDetectedEvent(com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Aggregations

ChannelClosedProactivelyEvent (com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent)3 InvalidRequestDetectedEvent (com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent)3 Response (com.couchbase.client.core.msg.Response)3 RequestTracer (com.couchbase.client.core.cnc.RequestTracer)2 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)2 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 ConfigurationProvider (com.couchbase.client.core.config.ConfigurationProvider)1 MemcachedBucketConfig (com.couchbase.client.core.config.MemcachedBucketConfig)1 FullHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse)1 HttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse)1 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)1 CollectionNotFoundException (com.couchbase.client.core.error.CollectionNotFoundException)1 NonChunkedHttpRequest (com.couchbase.client.core.msg.NonChunkedHttpRequest)1 KeyValueRequest (com.couchbase.client.core.msg.kv.KeyValueRequest)1 BucketConfigStreamingResponse (com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse)1 ManagerRequest (com.couchbase.client.core.msg.manager.ManagerRequest)1