Search in sources :

Example 1 with SelectBucketCompletedEvent

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

the class SelectBucketHandler method channelRead.

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
    if (msg instanceof ByteBuf) {
        short status = status((ByteBuf) msg);
        if (status == MemcacheProtocol.Status.SUCCESS.status()) {
            endpointContext.environment().eventBus().publish(new SelectBucketCompletedEvent(latency.orElse(Duration.ZERO), ioContext, bucketName));
            interceptedConnectPromise.trySuccess();
            ctx.pipeline().remove(this);
            ctx.fireChannelActive();
        } else if (status == MemcacheProtocol.Status.ACCESS_ERROR.status()) {
            // If the promise has been already completed with an error from a downstream handler, there is no
            // need to WARN because it will just spam and reduce the visibility of the actual source error.
            Event.Severity severity = interceptedConnectPromise.isDone() && !interceptedConnectPromise.isSuccess() ? Event.Severity.DEBUG : Event.Severity.WARN;
            endpointContext.environment().eventBus().publish(new SelectBucketFailedEvent(severity, ioContext, status));
            interceptedConnectPromise.tryFailure(new AuthenticationFailureException("Either the bucket with name \"" + redactMeta(bucketName) + "\" is not present or the user does not have " + "the right privileges to access it", new KeyValueIoErrorContext(MemcacheProtocol.decodeStatus(status), endpointContext, null), null));
        } else if (status == MemcacheProtocol.Status.NOT_FOUND.status()) {
            // NOT_FOUND severity is lowered to debug because it shows up when bootstrapping against
            // a node in a MDS setup without the kv service / bucket enabled. If the bucket is legit not
            // present, we'd get the access error from above (which is an error).
            endpointContext.environment().eventBus().publish(new SelectBucketFailedEvent(Event.Severity.DEBUG, ioContext, status));
            interceptedConnectPromise.tryFailure(BucketNotFoundException.forBucket(bucketName));
        } else {
            endpointContext.environment().eventBus().publish(new SelectBucketFailedEvent(Event.Severity.ERROR, ioContext, status));
            interceptedConnectPromise.tryFailure(new CouchbaseException("Select bucket failed with unexpected status code 0x" + Integer.toHexString(status)));
        }
    } else {
        interceptedConnectPromise.tryFailure(new CouchbaseException("Unexpected response type on channel read, this is a bug " + "- please report." + msg));
    }
    ReferenceCountUtil.release(msg);
}
Also used : SelectBucketFailedEvent(com.couchbase.client.core.cnc.events.io.SelectBucketFailedEvent) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) KeyValueIoErrorContext(com.couchbase.client.core.error.context.KeyValueIoErrorContext) SelectBucketCompletedEvent(com.couchbase.client.core.cnc.events.io.SelectBucketCompletedEvent) Duration(java.time.Duration) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Aggregations

SelectBucketCompletedEvent (com.couchbase.client.core.cnc.events.io.SelectBucketCompletedEvent)1 SelectBucketFailedEvent (com.couchbase.client.core.cnc.events.io.SelectBucketFailedEvent)1 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)1 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)1 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 KeyValueIoErrorContext (com.couchbase.client.core.error.context.KeyValueIoErrorContext)1 Duration (java.time.Duration)1