Search in sources :

Example 1 with UnsolicitedFeaturesReturnedEvent

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

the class FeatureNegotiatingHandler method extractFeaturesFromBody.

/**
 * Helper method to safely extract the negotiated server features from the
 * body of the memcache payload.
 *
 * @param response the response to extract from.
 * @return the list of server features, may be empty but never null.
 */
private Set<ServerFeature> extractFeaturesFromBody(final ByteBuf response) {
    Optional<ByteBuf> body = MemcacheProtocol.body(response);
    Set<ServerFeature> negotiated = EnumSet.noneOf(ServerFeature.class);
    List<ServerFeature> unsolicited = new ArrayList<>();
    if (!body.isPresent()) {
        return negotiated;
    }
    while (body.get().isReadable()) {
        try {
            short featureRaw = body.get().readShort();
            ServerFeature feature = ServerFeature.from(featureRaw);
            if (features.contains(feature)) {
                negotiated.add(feature);
            } else {
                unsolicited.add(feature);
            }
        } catch (Exception ex) {
            interceptedConnectPromise.tryFailure(new CouchbaseException("Error while parsing negotiated server features.", ex));
        }
    }
    if (!unsolicited.isEmpty()) {
        endpointContext.environment().eventBus().publish(new UnsolicitedFeaturesReturnedEvent(ioContext, unsolicited));
    }
    return negotiated;
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException) UnsolicitedFeaturesReturnedEvent(com.couchbase.client.core.cnc.events.io.UnsolicitedFeaturesReturnedEvent) ArrayList(java.util.ArrayList) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with UnsolicitedFeaturesReturnedEvent

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

the class FeatureNegotiatingHandlerTest method decodeAndIgnoreNonAskedForFeaturesInResponse.

/**
 * Should the server return non-asked-for features, ignore them.
 */
@Test
void decodeAndIgnoreNonAskedForFeaturesInResponse() {
    Set<ServerFeature> toNegotiate = EnumSet.of(ServerFeature.SNAPPY, ServerFeature.TRACING);
    FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, toNegotiate);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(FeatureNegotiatingHandler.class));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertFalse(connectFuture.isDone());
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    ByteBuf writtenRequest = channel.readOutbound();
    verifyRequest(writtenRequest, MemcacheProtocol.Opcode.HELLO.opcode(), true, false, true);
    assertNotNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
    ByteBuf response = decodeHexDump(readResource("success_hello_response.txt", FeatureNegotiatingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    assertEquals(2, eventBus.publishedEvents().size());
    UnsolicitedFeaturesReturnedEvent unsolicitedEvent = (UnsolicitedFeaturesReturnedEvent) eventBus.publishedEvents().get(0);
    assertEquals("Received unsolicited features during HELLO [TCPNODELAY, XATTR, XERROR, SELECT_BUCKET]", unsolicitedEvent.description());
    assertEquals(Event.Severity.WARN, unsolicitedEvent.severity());
    FeaturesNegotiatedEvent event = (FeaturesNegotiatedEvent) eventBus.publishedEvents().get(1);
    assertEquals("Negotiated [SNAPPY, TRACING]", event.description());
    Set<ServerFeature> serverFeatures = channel.attr(ChannelAttributes.SERVER_FEATURE_KEY).get();
    assertEquals(toNegotiate, serverFeatures);
    assertNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
    ReferenceCountUtil.release(writtenRequest);
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) UnsolicitedFeaturesReturnedEvent(com.couchbase.client.core.cnc.events.io.UnsolicitedFeaturesReturnedEvent) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) FeaturesNegotiatedEvent(com.couchbase.client.core.cnc.events.io.FeaturesNegotiatedEvent) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

UnsolicitedFeaturesReturnedEvent (com.couchbase.client.core.cnc.events.io.UnsolicitedFeaturesReturnedEvent)2 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)2 FeaturesNegotiatedEvent (com.couchbase.client.core.cnc.events.io.FeaturesNegotiatedEvent)1 ChannelFuture (com.couchbase.client.core.deps.io.netty.channel.ChannelFuture)1 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1