Search in sources :

Example 1 with ErrorMapLoadingFailedEvent

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

the class ErrorMapLoadingHandler 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) {
        if (successful((ByteBuf) msg)) {
            Optional<ErrorMap> loadedMap = extractErrorMap((ByteBuf) msg);
            loadedMap.ifPresent(errorMap -> ctx.channel().attr(ChannelAttributes.ERROR_MAP_KEY).set(errorMap));
            endpointContext.environment().eventBus().publish(new ErrorMapLoadedEvent(ioContext, latency.orElse(Duration.ZERO), loadedMap));
        } else {
            endpointContext.environment().eventBus().publish(new ErrorMapLoadingFailedEvent(ioContext, latency.orElse(Duration.ZERO), status((ByteBuf) msg)));
        }
        interceptedConnectPromise.trySuccess();
        ctx.pipeline().remove(this);
    } else {
        interceptedConnectPromise.tryFailure(new CouchbaseException("Unexpected response " + "type on channel read, this is a bug - please report. " + msg));
    }
    ReferenceCountUtil.release(msg);
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException) Duration(java.time.Duration) ErrorMapLoadingFailedEvent(com.couchbase.client.core.cnc.events.io.ErrorMapLoadingFailedEvent) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ErrorMapLoadedEvent(com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent)

Example 2 with ErrorMapLoadingFailedEvent

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

the class ErrorMapLoadingHandlerTest method decodeUnsuccessfulResponse.

/**
 * Make sure that when the server returns a non-successful response we still handle
 * it and not crash.
 */
@Test
void decodeUnsuccessfulResponse() {
    ErrorMapLoadingHandler handler = new ErrorMapLoadingHandler(endpointContext);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(ErrorMapLoadingHandler.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.ERROR_MAP.opcode(), false, false, true);
    assertNotNull(channel.pipeline().get(ErrorMapLoadingHandler.class));
    ReferenceCountUtil.release(writtenRequest);
    ByteBuf response = decodeHexDump(readResource("error_errormap_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    assertEquals(1, eventBus.publishedEvents().size());
    ErrorMapLoadingFailedEvent event = (ErrorMapLoadingFailedEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.WARN, event.severity());
    assertEquals("KV Error Map Negotiation failed (Status 0x1)", event.description());
    assertNull(channel.attr(ChannelAttributes.ERROR_MAP_KEY).get());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) ErrorMapLoadingFailedEvent(com.couchbase.client.core.cnc.events.io.ErrorMapLoadingFailedEvent) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Aggregations

ErrorMapLoadingFailedEvent (com.couchbase.client.core.cnc.events.io.ErrorMapLoadingFailedEvent)2 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)2 ErrorMapLoadedEvent (com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent)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 Duration (java.time.Duration)1 Test (org.junit.jupiter.api.Test)1