Search in sources :

Example 41 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf 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 42 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class GetResult method convertContentToString.

/**
 * Converts the content to a string representation if possible (for toString).
 */
protected String convertContentToString() {
    if (content.length == 0) {
        return "";
    }
    boolean printable = CodecFlags.hasCommonFormat(flags, CodecFlags.JSON_COMMON_FLAGS) || CodecFlags.hasCommonFormat(flags, CodecFlags.STRING_COMMON_FLAGS) || (flags == 0 && content[0] == '{');
    if (printable) {
        return new String(content, StandardCharsets.UTF_8);
    } else {
        ByteBuf buf = Unpooled.wrappedBuffer(content);
        String result = ByteBufUtil.prettyHexDump(buf);
        buf.release();
        return "\n" + result + "\n";
    }
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 43 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class MutateInUtil method convertDocsToBytes.

/**
 * Converts a list of objects (subdoc partials) into their low-level representation.
 * <p>
 * Per KV protocol each individual value is separated by a ",", which is not valid JSON on
 * the wire but the server will split it apart and apply each one of them individually.
 *
 * @param docs the docs to serialize.
 * @param serializer the serializer to use for JSON.
 * @return the serialized and converted byte array.
 */
static byte[] convertDocsToBytes(final List<?> docs, final JsonSerializer serializer) {
    if (docs.size() == 1) {
        return serializer.serialize(docs.get(0));
    } else {
        final ByteBuf bytes = UnpooledByteBufAllocator.DEFAULT.heapBuffer();
        try {
            final Iterator<?> it = docs.iterator();
            while (it.hasNext()) {
                Object d = it.next();
                bytes.writeBytes(serializer.serialize(d));
                if (it.hasNext()) {
                    bytes.writeByte(',');
                }
            }
            return ByteBufUtil.getBytes(bytes);
        } finally {
            bytes.release();
        }
    }
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 44 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class KeyValueMessageHandler method handleNotMyVbucket.

/**
 * Helper method to handle a "not my vbucket" response.
 *
 * @param request the request to retry.
 * @param response the response to extract the config from, potentially.
 */
private void handleNotMyVbucket(final KeyValueRequest<Response> request, final ByteBuf response) {
    request.indicateRejectedWithNotMyVbucket();
    eventBus.publish(new NotMyVbucketReceivedEvent(ioContext, request.partition()));
    final String origin = request.context().lastDispatchedTo() != null ? request.context().lastDispatchedTo().hostname() : null;
    RetryOrchestrator.maybeRetry(ioContext, request, RetryReason.KV_NOT_MY_VBUCKET);
    body(response).map(b -> b.toString(UTF_8).trim()).filter(c -> c.startsWith("{")).ifPresent(c -> ioContext.core().configurationProvider().proposeBucketConfig(new ProposedBucketConfigContext(request.bucket(), c, origin)));
}
Also used : IoContext(com.couchbase.client.core.io.IoContext) UnsignedLEB128(com.couchbase.client.core.util.UnsignedLEB128) HandlerUtils.closeChannelWithReason(com.couchbase.client.core.io.netty.HandlerUtils.closeChannelWithReason) ReferenceCountUtil(com.couchbase.client.core.deps.io.netty.util.ReferenceCountUtil) Request(com.couchbase.client.core.msg.Request) CompressionConfig(com.couchbase.client.core.env.CompressionConfig) RETRY_NOW(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.RETRY_NOW) EventBus(com.couchbase.client.core.cnc.EventBus) TracingUtils.setCommonKVSpanAttributes(com.couchbase.client.core.io.netty.TracingUtils.setCommonKVSpanAttributes) ServiceType(com.couchbase.client.core.service.ServiceType) CoreContext(com.couchbase.client.core.CoreContext) Map(java.util.Map) TracingUtils.setCommonDispatchSpanAttributes(com.couchbase.client.core.io.netty.TracingUtils.setCommonDispatchSpanAttributes) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) ITEM_LOCKED(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.ITEM_LOCKED) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) CbTracing(com.couchbase.client.core.cnc.CbTracing) RetryOrchestrator(com.couchbase.client.core.retry.RetryOrchestrator) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) TEMP(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.TEMP) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) Set(java.util.Set) KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) Collectors(java.util.stream.Collectors) TracingUtils(com.couchbase.client.core.io.netty.TracingUtils) RetryReason(com.couchbase.client.core.retry.RetryReason) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) DecodingFailureException(com.couchbase.client.core.error.DecodingFailureException) Optional(java.util.Optional) ChannelPromise(com.couchbase.client.core.deps.io.netty.channel.ChannelPromise) Response(com.couchbase.client.core.msg.Response) TracingUtils.setNumericOperationId(com.couchbase.client.core.io.netty.TracingUtils.setNumericOperationId) CollectionNotFoundException(com.couchbase.client.core.error.CollectionNotFoundException) UnsupportedResponseTypeReceivedEvent(com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent) HashMap(java.util.HashMap) KeyValueErrorMapCodeHandledEvent(com.couchbase.client.core.cnc.events.io.KeyValueErrorMapCodeHandledEvent) IntObjectMap(com.couchbase.client.core.deps.io.netty.util.collection.IntObjectMap) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) RETRY_LATER(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.RETRY_LATER) NotMyVbucketReceivedEvent(com.couchbase.client.core.cnc.events.io.NotMyVbucketReceivedEvent) MemcachedBucketConfig(com.couchbase.client.core.config.MemcachedBucketConfig) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ChannelDuplexHandler(com.couchbase.client.core.deps.io.netty.channel.ChannelDuplexHandler) CollectionOutdatedHandledEvent(com.couchbase.client.core.cnc.events.io.CollectionOutdatedHandledEvent) RequestTracer(com.couchbase.client.core.cnc.RequestTracer) InvalidRequestDetectedEvent(com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) UnknownResponseReceivedEvent(com.couchbase.client.core.cnc.events.io.UnknownResponseReceivedEvent) UTF_8(java.nio.charset.StandardCharsets.UTF_8) FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) UnlockRequest(com.couchbase.client.core.msg.kv.UnlockRequest) CollectionMap(com.couchbase.client.core.io.CollectionMap) ByteBufUtil(com.couchbase.client.core.deps.io.netty.buffer.ByteBufUtil) CONN_STATE_INVALIDATED(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.CONN_STATE_INVALIDATED) UnknownResponseStatusReceivedEvent(com.couchbase.client.core.cnc.events.io.UnknownResponseStatusReceivedEvent) ChannelClosedProactivelyEvent(com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent) IntObjectHashMap(com.couchbase.client.core.deps.io.netty.util.collection.IntObjectHashMap) AUTH(com.couchbase.client.core.io.netty.kv.ErrorMap.ErrorAttribute.AUTH) MemcacheProtocol.body(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.body) NotMyVbucketReceivedEvent(com.couchbase.client.core.cnc.events.io.NotMyVbucketReceivedEvent) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext)

Example 45 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class SaslAuthenticationHandler method failConnect.

/**
 * Refactored method which is called from many places to fail the connection
 * process because of an issue during SASL auth.
 *
 * <p>Usually errors during auth are very problematic and as a result we cannot continue
 * with this channel connect attempt.</p>
 *
 * @param ctx the channel context.
 */
private void failConnect(final ChannelHandlerContext ctx, final String message, final ByteBuf lastPacket, final Throwable cause, final short status) {
    Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
    byte[] packetCopy = Bytes.EMPTY_BYTE_ARRAY;
    Map<String, Object> serverContext = null;
    if (lastPacket != null) {
        if (MemcacheProtocol.verifyResponse(lastPacket)) {
            // This is a proper response, try to extract server context
            Optional<ByteBuf> body = MemcacheProtocol.body(lastPacket);
            if (body.isPresent()) {
                byte[] content = ByteBufUtil.getBytes(body.get());
                try {
                    serverContext = Mapper.decodeInto(content, new TypeReference<Map<String, Object>>() {
                    });
                } catch (Exception ex) {
                // Ignore, no displayable content
                }
            }
        } else {
            // This is not a proper memcache response, store the raw packet for debugging purposes
            int ridx = lastPacket.readerIndex();
            lastPacket.readerIndex(lastPacket.writerIndex());
            packetCopy = new byte[lastPacket.readableBytes()];
            lastPacket.readBytes(packetCopy);
            lastPacket.readerIndex(ridx);
        }
    }
    KeyValueIoErrorContext errorContext = new KeyValueIoErrorContext(MemcacheProtocol.decodeStatus(status), endpointContext, serverContext);
    endpointContext.environment().eventBus().publish(new SaslAuthenticationFailedEvent(latency.orElse(Duration.ZERO), errorContext, message, packetCopy));
    interceptedConnectPromise.tryFailure(new AuthenticationFailureException(message, errorContext, cause));
    ctx.pipeline().remove(this);
}
Also used : Duration(java.time.Duration) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) TimeoutException(java.util.concurrent.TimeoutException) SaslException(javax.security.sasl.SaslException) SaslAuthenticationFailedEvent(com.couchbase.client.core.cnc.events.io.SaslAuthenticationFailedEvent) KeyValueIoErrorContext(com.couchbase.client.core.error.context.KeyValueIoErrorContext) TypeReference(com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference)

Aggregations

ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)107 Test (org.junit.jupiter.api.Test)54 InetSocketAddress (java.net.InetSocketAddress)17 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)12 ChannelFuture (com.couchbase.client.core.deps.io.netty.channel.ChannelFuture)11 Duration (java.time.Duration)10 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)9 CoreContext (com.couchbase.client.core.CoreContext)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 CompressionConfig (com.couchbase.client.core.env.CompressionConfig)6 ResponseStatus (com.couchbase.client.core.msg.ResponseStatus)6 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)6 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)5 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)5 ArrayList (java.util.ArrayList)5 DurabilityTimeoutCoercedEvent (com.couchbase.client.core.cnc.events.io.DurabilityTimeoutCoercedEvent)4 FeaturesNegotiatedEvent (com.couchbase.client.core.cnc.events.io.FeaturesNegotiatedEvent)4 CompositeByteBuf (com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf)4 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)4 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)4