Search in sources :

Example 26 with OpenLedgerCallback

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

the class ServerCnxTest method testCreateProducerMultipleTimeouts.

@Test(timeOut = 30000, enabled = false)
public void testCreateProducerMultipleTimeouts() throws Exception {
    resetChannel();
    setChannelConnected();
    // Delay the topic creation in a deterministic way
    CountDownLatch topicCreationDelayLatch = new CountDownLatch(1);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            topicCreationDelayLatch.await();
            ((OpenLedgerCallback) invocationOnMock.getArguments()[2]).openLedgerComplete(ledgerMock, null);
            return null;
        }
    }).when(mlFactoryMock).asyncOpen(matches(".*success.*"), 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 producer
    // 2. close producer (when the timeout is triggered, which may be before the producer was created on the broker
    // 3. create producer (triggered by reconnection logic)
    // 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(successTopicName, 1, /* producer id */
    1, /* request id */
    producerName);
    channel.writeInbound(createProducer1);
    ByteBuf closeProducer1 = Commands.newCloseProducer(1, /* producer id */
    2);
    channel.writeInbound(closeProducer1);
    ByteBuf createProducer2 = Commands.newProducer(successTopicName, 1, /* producer id */
    3, /* request id */
    producerName);
    channel.writeInbound(createProducer2);
    ByteBuf createProducer3 = Commands.newProducer(successTopicName, 1, /* producer id */
    4, /* request id */
    producerName);
    channel.writeInbound(createProducer3);
    ByteBuf createProducer4 = Commands.newProducer(successTopicName, 1, /* producer id */
    5, /* request id */
    producerName);
    channel.writeInbound(createProducer4);
    // Close succeeds
    Object response = getResponse();
    assertEquals(response.getClass(), CommandSuccess.class);
    assertEquals(((CommandSuccess) response).getRequestId(), 2);
    // Now allow topic creation to complete
    topicCreationDelayLatch.countDown();
    // 1st producer it's not acked
    // 2nd producer fails
    response = getResponse();
    assertEquals(response.getClass(), CommandError.class);
    assertEquals(((CommandError) response).getRequestId(), 3);
    // 3rd producer fails
    response = getResponse();
    assertEquals(response.getClass(), CommandError.class);
    assertEquals(((CommandError) response).getRequestId(), 4);
    // 4nd producer fails
    response = getResponse();
    assertEquals(response.getClass(), CommandError.class);
    assertEquals(((CommandError) response).getRequestId(), 5);
    Thread.sleep(100);
    // 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 : InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyObject(org.mockito.Matchers.anyObject) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback) Test(org.testng.annotations.Test)

Example 27 with OpenLedgerCallback

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

the class ServerCnxTest method testCreateProducerTimeout.

@Test(timeOut = 30000)
public void testCreateProducerTimeout() throws Exception {
    resetChannel();
    setChannelConnected();
    // Delay the topic creation in a deterministic way
    CompletableFuture<Runnable> openTopicFuture = new CompletableFuture<>();
    doAnswer(invocationOnMock -> {
        openTopicFuture.complete(() -> {
            ((OpenLedgerCallback) invocationOnMock.getArguments()[2]).openLedgerComplete(ledgerMock, null);
        });
        return null;
    }).when(mlFactoryMock).asyncOpen(matches(".*success.*"), 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 producer
    // 2. close producer (when the timeout is triggered, which may be before the producer was created on the broker
    // 3. create producer (triggered by reconnection logic)
    // 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(successTopicName, 1, /* producer id */
    1, /* request id */
    producerName);
    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);
    channel.writeInbound(createProducer2);
    // Complete the topic opening
    openTopicFuture.get().run();
    // Close succeeds
    Object response = getResponse();
    assertEquals(response.getClass(), CommandSuccess.class);
    assertEquals(((CommandSuccess) response).getRequestId(), 2);
    // 2nd producer fails to create
    response = getResponse();
    assertEquals(response.getClass(), CommandError.class);
    assertEquals(((CommandError) response).getRequestId(), 3);
    // 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) ByteBuf(io.netty.buffer.ByteBuf) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback) Test(org.testng.annotations.Test)

Example 28 with OpenLedgerCallback

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

the class ServerCnxTest method testSubscribeBookieTimeout.

@Test(timeOut = 30000)
public void testSubscribeBookieTimeout() throws Exception {
    resetChannel();
    setChannelConnected();
    // Delay the topic creation in a deterministic way
    CompletableFuture<Runnable> openTopicSuccess = new CompletableFuture<>();
    doAnswer(invocationOnMock -> {
        openTopicSuccess.complete(() -> {
            ((OpenLedgerCallback) invocationOnMock.getArguments()[2]).openLedgerComplete(ledgerMock, null);
        });
        return null;
    }).when(mlFactoryMock).asyncOpen(matches(".*success.*"), any(ManagedLedgerConfig.class), any(OpenLedgerCallback.class), anyObject());
    CompletableFuture<Runnable> openTopicFail = new CompletableFuture<>();
    doAnswer(invocationOnMock -> {
        openTopicFail.complete(() -> {
            ((OpenLedgerCallback) invocationOnMock.getArguments()[2]).openLedgerFailed(new ManagedLedgerException("Managed ledger failure"), null);
        });
        return null;
    }).when(mlFactoryMock).asyncOpen(matches(".*fail.*"), any(ManagedLedgerConfig.class), any(OpenLedgerCallback.class), anyObject());
    // In a subscribe timeout from client side we expect to see this sequence of commands :
    // 1. Subscribe against failtopic which will fail after 100msec
    // 2. close consumer
    // 3. Resubscribe (triggered by reconnection logic)
    // 4. Wait till the timeout of 1, and subscribe again.
    // These operations need to be serialized, to allow the last subscribe operation to finally succeed
    // (There can be more subscribe/close pairs in the sequence, depending on the client timeout
    ByteBuf subscribe1 = //
    Commands.newSubscribe(//
    failTopicName, successSubName, 1, /* consumer id */
    1, /* request id */
    SubType.Exclusive, 0, "test");
    channel.writeInbound(subscribe1);
    ByteBuf closeConsumer = Commands.newCloseConsumer(1, /* consumer id */
    2);
    channel.writeInbound(closeConsumer);
    ByteBuf subscribe2 = //
    Commands.newSubscribe(//
    successTopicName, successSubName, 1, /* consumer id */
    3, /* request id */
    SubType.Exclusive, 0, "test");
    channel.writeInbound(subscribe2);
    openTopicFail.get().run();
    Object response;
    // Close succeeds
    response = getResponse();
    assertEquals(response.getClass(), CommandSuccess.class);
    assertEquals(((CommandSuccess) response).getRequestId(), 2);
    // Subscribe fails
    response = getResponse();
    assertEquals(response.getClass(), CommandError.class);
    assertEquals(((CommandError) response).getRequestId(), 3);
    while (serverCnx.hasConsumer(1)) {
        Thread.sleep(10);
    }
    ByteBuf subscribe3 = //
    Commands.newSubscribe(//
    successTopicName, successSubName, 1, /* consumer id */
    4, /* request id */
    SubType.Exclusive, 0, "test");
    channel.writeInbound(subscribe3);
    openTopicSuccess.get().run();
    // Subscribe succeeds
    response = getResponse();
    assertEquals(response.getClass(), CommandSuccess.class);
    assertEquals(((CommandSuccess) response).getRequestId(), 4);
    Thread.sleep(100);
    // 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) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Matchers.anyObject(org.mockito.Matchers.anyObject) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ByteBuf(io.netty.buffer.ByteBuf) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback) Test(org.testng.annotations.Test)

Example 29 with OpenLedgerCallback

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

the class ManagedLedgerFactoryImpl method asyncOpen.

@Override
public void asyncOpen(final String name, final ManagedLedgerConfig config, final OpenLedgerCallback callback, final Object ctx) {
    // If the ledger state is bad, remove it from the map.
    CompletableFuture<ManagedLedgerImpl> existingFuture = ledgers.get(name);
    if (existingFuture != null && existingFuture.isDone()) {
        try {
            ManagedLedgerImpl l = existingFuture.get();
            if (l.getState().equals(State.Fenced.toString()) || l.getState().equals(State.Closed.toString())) {
                // Managed ledger is in unusable state. Recreate it.
                log.warn("[{}] Attempted to open ledger in {} state. Removing from the map to recreate it", name, l.getState());
                ledgers.remove(name, existingFuture);
            }
        } catch (Exception e) {
            // Unable to get the future
            log.warn("[{}] Got exception while trying to retrieve ledger", name, e);
        }
    }
    // Ensure only one managed ledger is created and initialized
    ledgers.computeIfAbsent(name, (mlName) -> {
        // Create the managed ledger
        CompletableFuture<ManagedLedgerImpl> future = new CompletableFuture<>();
        final ManagedLedgerImpl newledger = new ManagedLedgerImpl(this, bookKeeper, store, config, executor, orderedExecutor, name);
        newledger.initialize(new ManagedLedgerInitializeLedgerCallback() {

            @Override
            public void initializeComplete() {
                future.complete(newledger);
            }

            @Override
            public void initializeFailed(ManagedLedgerException e) {
                // Clean the map if initialization fails
                ledgers.remove(name, future);
                future.completeExceptionally(e);
            }
        }, null);
        return future;
    }).thenAccept(ml -> {
        callback.openLedgerComplete(ml, ctx);
    }).exceptionally(exception -> {
        callback.openLedgerFailed((ManagedLedgerException) exception.getCause(), ctx);
        return null;
    });
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ScheduledFuture(java.util.concurrent.ScheduledFuture) State(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.State) LoggerFactory(org.slf4j.LoggerFactory) MessageRangeInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo.MessageRangeInfo) MetaStoreCallback(org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback) States(org.apache.zookeeper.ZooKeeper.States) ManagedCursorInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedCursorInfo) LongProperty(org.apache.bookkeeper.mledger.proto.MLDataFormats.LongProperty) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Map(java.util.Map) ZooKeeper(org.apache.zookeeper.ZooKeeper) LedgerInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo.LedgerInfo) OrderedScheduler(org.apache.bookkeeper.common.util.OrderedScheduler) PositionInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo.PositionInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Futures(org.apache.bookkeeper.mledger.util.Futures) MLDataFormats(org.apache.bookkeeper.mledger.proto.MLDataFormats) BookKeeper(org.apache.bookkeeper.client.BookKeeper) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerFactoryMXBean(org.apache.bookkeeper.mledger.ManagedLedgerFactoryMXBean) ManagedLedgerInfoCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ManagedLedgerInfoCallback) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) DateFormatter(org.apache.pulsar.common.util.DateFormatter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Predicates(com.google.common.base.Predicates) MessageRange(org.apache.bookkeeper.mledger.proto.MLDataFormats.MessageRange) ManagedLedgerException.getManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException) ManagedLedgerInitializeLedgerCallback(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.ManagedLedgerInitializeLedgerCallback) Logger(org.slf4j.Logger) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) Watcher(org.apache.zookeeper.Watcher) ManagedLedgerInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Maps(com.google.common.collect.Maps) TimeUnit(java.util.concurrent.TimeUnit) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback) CursorInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo.CursorInfo) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerException.getManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException) ManagedLedgerInitializeLedgerCallback(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.ManagedLedgerInitializeLedgerCallback) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) ManagedLedgerException.getManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException)

Example 30 with OpenLedgerCallback

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

the class ServerCnxTest method testSubscribeBookieTimeout.

@Test(timeOut = 30000)
public void testSubscribeBookieTimeout() throws Exception {
    resetChannel();
    setChannelConnected();
    // Delay the topic creation in a deterministic way
    CompletableFuture<Runnable> openTopicSuccess = new CompletableFuture<>();
    doAnswer(invocationOnMock -> {
        openTopicSuccess.complete(() -> {
            ((OpenLedgerCallback) invocationOnMock.getArguments()[2]).openLedgerComplete(ledgerMock, null);
        });
        return null;
    }).when(mlFactoryMock).asyncOpen(matches(".*success.*"), any(ManagedLedgerConfig.class), any(OpenLedgerCallback.class), anyObject());
    CompletableFuture<Runnable> openTopicFail = new CompletableFuture<>();
    doAnswer(invocationOnMock -> {
        openTopicFail.complete(() -> {
            ((OpenLedgerCallback) invocationOnMock.getArguments()[2]).openLedgerFailed(new ManagedLedgerException("Managed ledger failure"), null);
        });
        return null;
    }).when(mlFactoryMock).asyncOpen(matches(".*fail.*"), any(ManagedLedgerConfig.class), any(OpenLedgerCallback.class), anyObject());
    // In a subscribe timeout from client side we expect to see this sequence of commands :
    // 1. Subscribe against failtopic which will fail after 100msec
    // 2. close consumer
    // 3. Resubscribe (triggered by reconnection logic)
    // 4. Wait till the timeout of 1, and subscribe again.
    // These operations need to be serialized, to allow the last subscribe operation to finally succeed
    // (There can be more subscribe/close pairs in the sequence, depending on the client timeout
    ByteBuf subscribe1 = // 
    Commands.newSubscribe(// 
    failTopicName, successSubName, 1, /* consumer id */
    1, /* request id */
    SubType.Exclusive, 0, "test");
    channel.writeInbound(subscribe1);
    ByteBuf closeConsumer = Commands.newCloseConsumer(1, /* consumer id */
    2);
    channel.writeInbound(closeConsumer);
    ByteBuf subscribe2 = // 
    Commands.newSubscribe(// 
    successTopicName, successSubName, 1, /* consumer id */
    3, /* request id */
    SubType.Exclusive, 0, "test");
    channel.writeInbound(subscribe2);
    openTopicFail.get().run();
    Object response;
    // Close succeeds
    response = getResponse();
    assertEquals(response.getClass(), CommandSuccess.class);
    assertEquals(((CommandSuccess) response).getRequestId(), 2);
    // Subscribe fails
    response = getResponse();
    assertEquals(response.getClass(), CommandError.class);
    assertEquals(((CommandError) response).getRequestId(), 3);
    while (serverCnx.hasConsumer(1)) {
        Thread.sleep(10);
    }
    ByteBuf subscribe3 = // 
    Commands.newSubscribe(// 
    successTopicName, successSubName, 1, /* consumer id */
    4, /* request id */
    SubType.Exclusive, 0, "test");
    channel.writeInbound(subscribe3);
    openTopicSuccess.get().run();
    // Subscribe succeeds
    response = getResponse();
    assertEquals(response.getClass(), CommandSuccess.class);
    assertEquals(((CommandSuccess) response).getRequestId(), 4);
    Thread.sleep(100);
    // 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) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Matchers.anyObject(org.mockito.Matchers.anyObject) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) 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