Search in sources :

Example 1 with UnknownResponseStatusReceivedEvent

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

the class KeyValueMessageHandler method decode.

/**
 * Main method to start dispatching the decode.
 *
 * @param ctx the channel handler context from netty.
 * @param response the response to decode and handle.
 */
private void decode(final ChannelHandlerContext ctx, final ByteBuf response) {
    int opaque = MemcacheProtocol.opaque(response);
    KeyValueRequest<Response> request = writtenRequests.remove(opaque);
    if (request == null) {
        handleUnknownResponseReceived(ctx, response);
        return;
    }
    long serverTime = MemcacheProtocol.parseServerDurationFromResponse(response);
    request.context().serverLatency(serverTime);
    long start = writtenRequestDispatchTimings.remove(opaque);
    request.context().dispatchLatency(System.nanoTime() - start);
    RequestSpan dispatchSpan = writtenRequestDispatchSpans.remove(opaque);
    if (dispatchSpan != null) {
        if (!isInternalTracer) {
            TracingUtils.setServerDurationAttribute(dispatchSpan, serverTime);
        }
        dispatchSpan.end();
    }
    short statusCode = MemcacheProtocol.status(response);
    ResponseStatus status = MemcacheProtocol.decodeStatus(statusCode);
    ErrorMap.ErrorCode errorCode = status != ResponseStatus.SUCCESS ? decodeErrorCode(statusCode) : null;
    if (errorCode != null) {
        request.errorCode(errorCode);
    }
    boolean errorUnknown = false;
    if (status == ResponseStatus.UNKNOWN) {
        errorUnknown = true;
        if (errorCode != null) {
            ioContext.environment().eventBus().publish(new KeyValueErrorMapCodeHandledEvent(ioContext, errorCode));
            status = handleErrorCode(ctx, errorCode);
        }
        ioContext.environment().eventBus().publish(new UnknownResponseStatusReceivedEvent(ioContext, statusCode));
    }
    if (status == ResponseStatus.NOT_MY_VBUCKET) {
        handleNotMyVbucket(request, response);
    } else if (status == ResponseStatus.UNKNOWN_COLLECTION) {
        handleOutdatedCollection(request, RetryReason.KV_COLLECTION_OUTDATED);
    } else if (errorUnknown && errorMapIndicatesRetry(errorCode)) {
        RetryOrchestrator.maybeRetry(ioContext, request, RetryReason.KV_ERROR_MAP_INDICATED);
    } else if (statusIndicatesInvalidChannel(status)) {
        closeChannelWithReason(ioContext, ctx, ChannelClosedProactivelyEvent.Reason.KV_RESPONSE_CONTAINED_CLOSE_INDICATION);
    } else {
        RetryReason retryReason = statusCodeIndicatesRetry(status, request);
        if (retryReason == null) {
            if (!request.completed()) {
                decodeAndComplete(request, response);
            } else {
                ioContext.environment().orphanReporter().report(request);
            }
        } else {
            RetryOrchestrator.maybeRetry(ioContext, request, retryReason);
        }
    }
}
Also used : BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) Response(com.couchbase.client.core.msg.Response) RetryReason(com.couchbase.client.core.retry.RetryReason) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) UnknownResponseStatusReceivedEvent(com.couchbase.client.core.cnc.events.io.UnknownResponseStatusReceivedEvent) KeyValueErrorMapCodeHandledEvent(com.couchbase.client.core.cnc.events.io.KeyValueErrorMapCodeHandledEvent)

Aggregations

RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 KeyValueErrorMapCodeHandledEvent (com.couchbase.client.core.cnc.events.io.KeyValueErrorMapCodeHandledEvent)1 UnknownResponseStatusReceivedEvent (com.couchbase.client.core.cnc.events.io.UnknownResponseStatusReceivedEvent)1 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)1 Response (com.couchbase.client.core.msg.Response)1 ResponseStatus (com.couchbase.client.core.msg.ResponseStatus)1 RetryReason (com.couchbase.client.core.retry.RetryReason)1