Search in sources :

Example 6 with ResponseStatus

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

the class ObserveViaCasRequest method decode.

@Override
public ObserveViaCasResponse decode(final ByteBuf response, KeyValueChannelContext ctx) {
    ResponseStatus status = decodeStatus(response);
    byte observed = ObserveViaCasResponse.ObserveStatus.UNKNOWN.value();
    long observedCas = 0;
    ResponseStatusDetails statusDetails = null;
    if (status.success()) {
        ByteBuf content = body(response).get();
        short keyLength = content.getShort(2);
        observed = content.getByte(keyLength + 4);
        observedCas = content.getLong(keyLength + 5);
    } else {
        // TODO: implement once xerror is fully implemented
        // ResponseStatus.convertDetails(datatype(response), );
        statusDetails = null;
    }
    return new ObserveViaCasResponse(status, observedCas, ObserveViaCasResponse.ObserveStatus.valueOf(observed), active, statusDetails);
}
Also used : ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 7 with ResponseStatus

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

the class ObserveViaSeqnoRequest method decode.

@Override
public ObserveViaSeqnoResponse decode(final ByteBuf response, final KeyValueChannelContext ctx) {
    ResponseStatus status = decodeStatus(response);
    if (status.success()) {
        ByteBuf content = body(response).get();
        byte format = content.readByte();
        short vbucketId = content.readShort();
        long vbucketUUID = content.readLong();
        long lastPersistedSeqno = content.readLong();
        long currentSeqno = content.readLong();
        switch(format) {
            case 0:
                return new ObserveViaSeqnoResponse(status, active, vbucketId, vbucketUUID, lastPersistedSeqno, currentSeqno, Optional.empty(), Optional.empty());
            case 1:
                return new ObserveViaSeqnoResponse(status, active, vbucketId, vbucketUUID, lastPersistedSeqno, currentSeqno, Optional.of(content.readLong()), Optional.of(content.readLong()));
            default:
                throw new IllegalStateException("Unsupported format 0x" + Integer.toHexString(format));
        }
    } else {
        return new ObserveViaSeqnoResponse(status, active, (short) 0, 0L, 0L, 0L, Optional.empty(), Optional.empty());
    }
}
Also used : ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 8 with ResponseStatus

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

the class MultiObserveViaCasRequest method decode.

@Override
public MultiObserveViaCasResponse decode(final ByteBuf response, final KeyValueChannelContext ctx) {
    final ResponseStatus status = decodeStatus(response);
    final Map<byte[], ObserveViaCasResponse.ObserveStatus> observed = new HashMap<>();
    if (status.success()) {
        Optional<ByteBuf> maybeContent = body(response);
        if (maybeContent.isPresent()) {
            ByteBuf content = maybeContent.get();
            while (content.isReadable()) {
                // skip the vbid
                content.skipBytes(Short.BYTES);
                short keyLength = content.readShort();
                if (ctx.collectionsEnabled()) {
                    int skipped = UnsignedLEB128.skip(content);
                    keyLength = (short) (keyLength - skipped);
                }
                byte[] keyEncoded = new byte[keyLength];
                content.readBytes(keyEncoded, 0, keyLength);
                byte obs = content.readByte();
                // skip reading the cas
                content.skipBytes(Long.BYTES);
                ObserveViaCasResponse.ObserveStatus decoded = ObserveViaCasResponse.ObserveStatus.valueOf(obs);
                if (responsePredicate.test(decoded)) {
                    observed.put(keyEncoded, decoded);
                }
            }
        }
    }
    return new MultiObserveViaCasResponse(this, status, observed);
}
Also used : HashMap(java.util.HashMap) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus)

Example 9 with ResponseStatus

use of com.couchbase.client.core.msg.ResponseStatus 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)

Example 10 with ResponseStatus

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

the class ViewChunkResponseParser method error.

@Override
public Optional<CouchbaseException> error() {
    return error.map(e -> {
        int httpStatus = responseHeader().status().code();
        ResponseStatus responseStatus = HttpProtocol.decodeStatus(responseHeader().status());
        ViewErrorContext errorContext = new ViewErrorContext(responseStatus, requestContext(), e, httpStatus);
        if (responseStatus == ResponseStatus.NOT_FOUND || e.error().equals("not_found") || e.reason().contains("not_found")) {
            return new ViewNotFoundException(errorContext);
        }
        return new CouchbaseException("Unknown view error: " + e.toString(), errorContext);
    });
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) ViewErrorContext(com.couchbase.client.core.error.context.ViewErrorContext) ViewNotFoundException(com.couchbase.client.core.error.ViewNotFoundException)

Aggregations

ResponseStatus (com.couchbase.client.core.msg.ResponseStatus)14 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)6 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)3 CompositeByteBuf (com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf)2 SubDocumentErrorContext (com.couchbase.client.core.error.context.SubDocumentErrorContext)2 DocumentNotJsonException (com.couchbase.client.core.error.subdoc.DocumentNotJsonException)2 DocumentTooDeepException (com.couchbase.client.core.error.subdoc.DocumentTooDeepException)2 XattrInvalidKeyComboException (com.couchbase.client.core.error.subdoc.XattrInvalidKeyComboException)2 Response (com.couchbase.client.core.msg.Response)2 CoreContext (com.couchbase.client.core.CoreContext)1 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 UnsupportedResponseTypeReceivedEvent (com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent)1 ByteBufAllocator (com.couchbase.client.core.deps.io.netty.buffer.ByteBufAllocator)1 ByteBufUtil (com.couchbase.client.core.deps.io.netty.buffer.ByteBufUtil)1 FullHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse)1 HttpResponseStatus (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus)1 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)1 HttpStatusCodeException (com.couchbase.client.core.error.HttpStatusCodeException)1