Search in sources :

Example 31 with OpenLedgerCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback in project incubator-pulsar by apache.

the class ServerCnxTest method testCreateProducerBookieTimeout.

@Test(timeOut = 30000, invocationCount = 1, skipFailedInvocations = true)
public void testCreateProducerBookieTimeout() throws Exception {
    resetChannel();
    setChannelConnected();
    // Delay the topic creation in a deterministic way
    CompletableFuture<Runnable> openFailedTopic = new CompletableFuture<>();
    doAnswer(invocationOnMock -> {
        openFailedTopic.complete(() -> {
            ((OpenLedgerCallback) invocationOnMock.getArguments()[2]).openLedgerComplete(ledgerMock, null);
        });
        return null;
    }).when(mlFactoryMock).asyncOpen(matches(".*fail.*"), any(ManagedLedgerConfig.class), any(OpenLedgerCallback.class), anyObject());
    // In a create producer timeout from client side we expect to see this sequence of commands :
    // 1. create a failure producer which will timeout creation after 100msec
    // 2. close producer
    // 3. Recreate producer (triggered by reconnection logic)
    // 4. Wait till the timeout of 1, and create producer again.
    // These operations need to be serialized, to allow the last create producer to finally succeed
    // (There can be more create/close pairs in the sequence, depending on the client timeout
    String producerName = "my-producer";
    ByteBuf createProducer1 = Commands.newProducer(failTopicName, 1, /* producer id */
    1, /* request id */
    producerName, Collections.emptyMap());
    channel.writeInbound(createProducer1);
    ByteBuf closeProducer = Commands.newCloseProducer(1, /* producer id */
    2);
    channel.writeInbound(closeProducer);
    ByteBuf createProducer2 = Commands.newProducer(successTopicName, 1, /* producer id */
    3, /* request id */
    producerName, Collections.emptyMap());
    channel.writeInbound(createProducer2);
    // Now the topic gets opened
    openFailedTopic.get().run();
    // Close succeeds
    Object response = getResponse();
    assertEquals(response.getClass(), CommandSuccess.class);
    assertEquals(((CommandSuccess) response).getRequestId(), 2);
    // 2nd producer fails
    response = getResponse();
    assertEquals(response.getClass(), CommandError.class);
    assertEquals(((CommandError) response).getRequestId(), 3);
    // Wait till the failtopic timeout interval
    Thread.sleep(500);
    ByteBuf createProducer3 = Commands.newProducer(successTopicName, 1, /* producer id */
    4, /* request id */
    producerName, Collections.emptyMap());
    channel.writeInbound(createProducer3);
    // 3rd producer succeeds
    response = getResponse();
    assertEquals(response.getClass(), CommandProducerSuccess.class);
    assertEquals(((CommandProducerSuccess) response).getRequestId(), 4);
    Thread.sleep(500);
    // We should not receive response for 1st producer, since it was cancelled by the close
    assertTrue(channel.outboundMessages().isEmpty());
    assertTrue(channel.isActive());
    channel.finish();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Matchers.anyObject(org.mockito.Matchers.anyObject) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ByteString(com.google.protobuf.ByteString) ByteBuf(io.netty.buffer.ByteBuf) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback) Test(org.testng.annotations.Test)

Aggregations

OpenLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)31 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)25 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)23 Test (org.testng.annotations.Test)20 ByteBuf (io.netty.buffer.ByteBuf)19 Matchers.anyObject (org.mockito.Matchers.anyObject)18 CompletableFuture (java.util.concurrent.CompletableFuture)17 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)17 CountDownLatch (java.util.concurrent.CountDownLatch)14 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)8 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)8 OpenCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback)7 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Executors (java.util.concurrent.Executors)6 TimeUnit (java.util.concurrent.TimeUnit)6 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)6 DeleteCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback)6