org.apache.bookkeeper.common.concurrent.FutureUtils Java Examples

The following examples show how to use org.apache.bookkeeper.common.concurrent.FutureUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HDBConnection.java    From herddb with Apache License 2.0 6 votes vote down vote up
public CompletableFuture<List<DMLResult>> executeUpdatesAsync(
        String tableSpace, String query, long tx, boolean returnValues,
        boolean usePreparedStatement, List<List<Object>> batch
) {
    if (batch.isEmpty()) {
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    if (discoverTablespaceFromSql) {
        tableSpace = discoverTablespace(tableSpace, query);
    }
    if (closed) {
        return FutureUtils.exception(new HDBException("client is closed"));
    }
    CompletableFuture<List<DMLResult>> res = new CompletableFuture<>();

    AtomicInteger count = new AtomicInteger(0);
    executeStatementsAsyncInternal(tableSpace, res, query, tx, returnValues, usePreparedStatement, batch, count);
    return res;
}
 
Example #2
Source File: SchemaRegistryServiceImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public CompletableFuture<List<SchemaAndMetadata>> trimDeletedSchemaAndGetList(String schemaId) {
    return getAllSchemas(schemaId).thenCompose(FutureUtils::collect).thenApply(list -> {
        // Trim the prefix of schemas before the latest delete.
        int lastIndex = list.size() - 1;
        for (int i = lastIndex; i >= 0; i--) {
            if (list.get(i).schema.isDeleted()) {
                if (i == lastIndex) { // if the latest schema is a delete, there's no schemas to compare
                    return Collections.emptyList();
                } else {
                    return list.subList(i + 1, list.size());
                }
            }
        }
        return list;
    });
}
 
Example #3
Source File: SchemaRegistryServiceImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<Void> checkCompatibilityWithLatest(String schemaId, SchemaData schema,
                                                                SchemaCompatibilityStrategy strategy) {
    return getSchema(schemaId).thenCompose(existingSchema -> {
        if (existingSchema != null && !existingSchema.schema.isDeleted()) {
            CompletableFuture<Void> result = new CompletableFuture<>();
            try {
                checkCompatible(existingSchema, schema, strategy);
                result.complete(null);
            } catch (IncompatibleSchemaException e) {
                result.completeExceptionally(e);
            }
            return result;
        } else {
            return FutureUtils.exception(new IncompatibleSchemaException("Do not have existing schema."));
        }
    });
}
 
Example #4
Source File: PulsarMockBookKeeper.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncCreateLedger(int ensSize, int writeQuorumSize, int ackQuorumSize, final DigestType digestType,
        final byte[] passwd, final CreateCallback cb, final Object ctx, Map<String, byte[]> properties) {
    getProgrammedFailure().thenComposeAsync((res) -> {
            try {
                long id = sequence.getAndIncrement();
                log.info("Creating ledger {}", id);
                PulsarMockLedgerHandle lh = new PulsarMockLedgerHandle(PulsarMockBookKeeper.this, id, digestType, passwd);
                ledgers.put(id, lh);
                return FutureUtils.value(lh);
            } catch (Throwable t) {
                return FutureUtils.exception(t);
            }
        }, executor).whenCompleteAsync((lh, exception) -> {
                if (exception != null) {
                    cb.createComplete(getExceptionCode(exception), null, ctx);
                } else {
                    cb.createComplete(BKException.Code.OK, lh, ctx);
                }
            }, executor);
}
 
Example #5
Source File: PulsarMockBookKeeper.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncOpenLedger(long lId, DigestType digestType, byte[] passwd, OpenCallback cb, Object ctx) {
    getProgrammedFailure().thenComposeAsync((res) -> {
            PulsarMockLedgerHandle lh = ledgers.get(lId);
            if (lh == null) {
                return FutureUtils.exception(new BKException.BKNoSuchLedgerExistsException());
            } else if (lh.digest != digestType) {
                return FutureUtils.exception(new BKException.BKDigestMatchException());
            } else if (!Arrays.equals(lh.passwd, passwd)) {
                return FutureUtils.exception(new BKException.BKUnauthorizedAccessException());
            } else {
                return FutureUtils.value(lh);
            }
        }, executor).whenCompleteAsync((ledger, exception) -> {
                if (exception != null) {
                    cb.openComplete(getExceptionCode(exception), null, ctx);
                } else {
                    cb.openComplete(BKException.Code.OK, ledger, ctx);
                }
            }, executor);
}
 
Example #6
Source File: PulsarMockBookKeeper.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncDeleteLedger(long lId, DeleteCallback cb, Object ctx) {
    getProgrammedFailure().thenComposeAsync((res) -> {
            if (ledgers.containsKey(lId)) {
                ledgers.remove(lId);
                return FutureUtils.value(null);
            } else {
                return FutureUtils.exception(new BKException.BKNoSuchLedgerExistsException());
            }
        }, executor).whenCompleteAsync((res, exception) -> {
                if (exception != null) {
                    cb.deleteComplete(getExceptionCode(exception), ctx);
                } else {
                    cb.deleteComplete(BKException.Code.OK, ctx);
                }
            }, executor);
}
 
Example #7
Source File: PulsarMockLedgerHandle.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncClose(CloseCallback cb, Object ctx) {
    bk.getProgrammedFailure().thenComposeAsync((res) -> {
            fenced = true;

            Versioned<LedgerMetadata> current = getVersionedLedgerMetadata();
            Versioned<LedgerMetadata> newMetadata = new Versioned<>(
                    LedgerMetadataBuilder.from(current.getValue())
                    .withClosedState().withLastEntryId(getLastAddConfirmed())
                    .withLength(getLength()).build(),
                    new LongVersion(((LongVersion)current.getVersion()).getLongVersion() + 1));
            setLedgerMetadata(current, newMetadata);
            return FutureUtils.value(null);
        }, bk.executor).whenCompleteAsync((res, exception) -> {
                if (exception != null) {
                    cb.closeComplete(PulsarMockBookKeeper.getExceptionCode(exception), null, ctx);
                } else {
                    cb.closeComplete(BKException.Code.OK, this, ctx);
                }
            }, bk.executor);
}
 
Example #8
Source File: BookkeeperCommitLog.java    From herddb with Apache License 2.0 6 votes vote down vote up
private CommitFileWriter() throws LogNotAvailableException {
    try {
        Map<String, byte[]> metadata = new HashMap<>();
        metadata.put("tablespaceuuid", tableSpaceUUID.getBytes(StandardCharsets.UTF_8));
        metadata.put("tablespacename", tableSpaceName.getBytes(StandardCharsets.UTF_8));
        metadata.put("leader", localNodeId.getBytes(StandardCharsets.UTF_8));
        metadata.put("application", "herddb".getBytes(StandardCharsets.UTF_8));
        metadata.put("component", "commitlog".getBytes(StandardCharsets.UTF_8));
        this.out = FutureUtils.result(bookKeeper.
                newCreateLedgerOp()
                .withEnsembleSize(ensemble)
                .withWriteQuorumSize(writeQuorumSize)
                .withAckQuorumSize(ackQuorumSize)
                .withDigestType(DigestType.CRC32C)
                .withPassword(SHARED_SECRET.getBytes(StandardCharsets.UTF_8))
                .withCustomMetadata(metadata)
                .execute(), BKException.HANDLER);
        this.ledgerId = this.out.getId();
        LOGGER.log(Level.INFO, "{0} created ledger {1} (" + ensemble + "/" + writeQuorumSize + "/" + ackQuorumSize + ") bookies: {2}",
                new Object[]{tableSpaceDescription(), ledgerId, this.out.getLedgerMetadata().getAllEnsembles()});
        lastLedgerId = ledgerId;
        lastSequenceNumber.set(-1);
    } catch (BKException err) {
        throw new LogNotAvailableException(err);
    }
}
 
Example #9
Source File: PulsarMockBookKeeper.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public void shutdown() {
    try {
        super.close();
    } catch (Exception e) {
    }
    synchronized (this) {
        defaultResponse = FutureUtils.exception(new BKException.BKClientClosedException());
    }
    for (PulsarMockLedgerHandle ledger : ledgers.values()) {
        ledger.entries.clear();
    }

    ledgers.clear();
}
 
Example #10
Source File: PulsarMockBookKeeper.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public OpenBuilder newOpenLedgerOp() {
    return new OpenBuilderBase() {
        @Override
        public CompletableFuture<ReadHandle> execute() {
            return getProgrammedFailure().thenCompose(
                    (res) -> {
                        int rc = validate();
                        if (rc != BKException.Code.OK) {
                            return FutureUtils.exception(BKException.create(rc));
                        }

                        PulsarMockLedgerHandle lh = ledgers.get(ledgerId);
                        if (lh == null) {
                            return FutureUtils.exception(new BKException.BKNoSuchLedgerExistsException());
                        } else if (lh.digest != DigestType.fromApiDigestType(digestType)) {
                            return FutureUtils.exception(new BKException.BKDigestMatchException());
                        } else if (!Arrays.equals(lh.passwd, password)) {
                            return FutureUtils.exception(new BKException.BKUnauthorizedAccessException());
                        } else {
                            return FutureUtils.value(new PulsarMockReadHandle(PulsarMockBookKeeper.this, ledgerId,
                                                                              lh.getLedgerMetadata(), lh.entries));
                        }
                    });
        }
    };
}
 
Example #11
Source File: PulsarMockLedgerHandle.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncAddEntry(final ByteBuf data, final AddCallback cb, final Object ctx) {
    bk.getProgrammedFailure().thenComposeAsync((res) -> {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
            }

            if (fenced) {
                return FutureUtils.exception(new BKException.BKLedgerFencedException());
            } else {
                lastEntry = entries.size();
                byte[] storedData = new byte[data.readableBytes()];
                data.readBytes(storedData);
                entries.add(LedgerEntryImpl.create(ledgerId, lastEntry,
                                                   storedData.length, Unpooled.wrappedBuffer(storedData)));
                return FutureUtils.value(lastEntry);
            }

        }, bk.executor).whenCompleteAsync((entryId, exception) -> {
                data.release();
                if (exception != null) {
                    fenced = true;
                    cb.addComplete(PulsarMockBookKeeper.getExceptionCode(exception),
                                   PulsarMockLedgerHandle.this, LedgerHandle.INVALID_ENTRY_ID, ctx);
                } else {
                    cb.addComplete(BKException.Code.OK, PulsarMockLedgerHandle.this, entryId, ctx);
                }
            }, bk.executor);
}
 
Example #12
Source File: HDBConnection.java    From herddb with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<DMLResult> executeUpdateAsync(String tableSpace, String query, long tx, boolean returnValues, boolean usePreparedStatement, List<Object> params) {
    if (discoverTablespaceFromSql) {
        tableSpace = discoverTablespace(tableSpace, query);
    }
    if (closed) {
        return FutureUtils.exception(new HDBException("client is closed"));
    }
    CompletableFuture<DMLResult> res = new CompletableFuture<>();

    AtomicInteger count = new AtomicInteger(0);
    executeStatementAsyncInternal(tableSpace, res, query, tx, returnValues, usePreparedStatement, params, count);
    return res;
}
 
Example #13
Source File: MockExecutorController.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private static Answer<ScheduledFuture<?>> answerDelay(MockExecutorController executor) {
    return invocationOnMock -> {

       Runnable task = invocationOnMock.getArgument(0);
       long value = invocationOnMock.getArgument(1);
       TimeUnit unit = invocationOnMock.getArgument(2);
       DeferredTask deferredTask = executor.addDelayedTask(executor, unit.toMillis(value), task);
       if (value <= 0) {
           task.run();
           FutureUtils.complete(deferredTask.future, null);
       }
       return deferredTask;
   };
}
 
Example #14
Source File: StateContextImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetKeyNotPresent() throws Exception {
    when(mockTable.get(any(ByteBuf.class)))
            .thenReturn(FutureUtils.value(null));
    CompletableFuture<ByteBuffer> result = stateContext.get("test-key");
    assertTrue(result != null);
    assertEquals(result.get(), null);

}
 
Example #15
Source File: StateContextImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAmount() throws Exception {
    when(mockTable.getNumber(any(ByteBuf.class)))
        .thenReturn(FutureUtils.value(10L));
    assertEquals((Long)10L, stateContext.getCounter("test-key").get());
    verify(mockTable, times(1)).getNumber(
        eq(Unpooled.copiedBuffer("test-key", UTF_8))
    );
}
 
Example #16
Source File: PulsarMockReadHandle.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<LedgerEntries> readAsync(long firstEntry, long lastEntry) {
    return bk.getProgrammedFailure().thenComposeAsync((res) -> {
            log.debug("readEntries: first={} last={} total={}", firstEntry, lastEntry, entries.size());
            List<LedgerEntry> seq = new ArrayList<>();
            long entryId = firstEntry;
            while (entryId <= lastEntry && entryId < entries.size()) {
                seq.add(entries.get((int) entryId++).duplicate());
            }
            log.debug("Entries read: {}", seq);

            return FutureUtils.value(LedgerEntriesImpl.create(seq));
        });
}
 
Example #17
Source File: StateContextImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetValue() throws Exception {
    ByteBuf returnedValue = Unpooled.copiedBuffer("test-value", UTF_8);
    when(mockTable.get(any(ByteBuf.class)))
        .thenReturn(FutureUtils.value(returnedValue));
    ByteBuffer result = stateContext.get("test-key").get();
    assertEquals("test-value", new String(result.array(), UTF_8));
    verify(mockTable, times(1)).get(
        eq(Unpooled.copiedBuffer("test-key", UTF_8))
    );
}
 
Example #18
Source File: StateContextImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testDelete() throws Exception {
    DeleteResult<ByteBuf, ByteBuf> result = mock(DeleteResult.class);
    when(mockTable.delete(any(ByteBuf.class), eq(Options.delete())))
            .thenReturn(FutureUtils.value(result));
    stateContext.delete("test-key");
    verify(mockTable, times(1)).delete(
            eq(Unpooled.copiedBuffer("test-key", UTF_8)),
            eq(Options.delete())
    );
}
 
Example #19
Source File: StateContextImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testPut() throws Exception {
    when(mockTable.put(any(ByteBuf.class), any(ByteBuf.class)))
        .thenReturn(FutureUtils.Void());
    stateContext.put("test-key", ByteBuffer.wrap("test-value".getBytes(UTF_8))).get();
    verify(mockTable, times(1)).put(
        eq(Unpooled.copiedBuffer("test-key", UTF_8)),
        eq(Unpooled.copiedBuffer("test-value", UTF_8))
    );
}
 
Example #20
Source File: StateContextImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncr() throws Exception {
    when(mockTable.increment(any(ByteBuf.class), anyLong()))
        .thenReturn(FutureUtils.Void());
    stateContext.incrCounter("test-key", 10L).get();
    verify(mockTable, times(1)).increment(
        eq(Unpooled.copiedBuffer("test-key", UTF_8)),
        eq(10L)
    );
}
 
Example #21
Source File: Ledgers.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the Ledger with given LedgerId.
 *
 * @param ledgerId   The Id of the Ledger to delete.
 * @param bookKeeper A reference to the BookKeeper client to use.
 * @throws DurableDataLogException If an exception occurred. The causing exception is wrapped inside it.
 */
static void delete(long ledgerId, BookKeeper bookKeeper) throws DurableDataLogException {
    try {
        Exceptions.handleInterrupted(() -> FutureUtils.result(bookKeeper
                .newDeleteLedgerOp()
                .withLedgerId(ledgerId)
                .execute(), BK_EXCEPTION_HANDLER)
        );
    } catch (BKException bkEx) {
        throw new DurableDataLogException(String.format("Unable to delete Ledger %d.", ledgerId), bkEx);
    }
}
 
Example #22
Source File: Ledgers.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Opens a ledger for reading. This operation does not fence out the ledger.
 *
 * @param ledgerId   The Id of the Ledger to open.
 * @param bookKeeper A references to the BookKeeper client to use.
 * @param config     Configuration to use.
 * @return A ReadHandle for the newly opened ledger.
 * @throws DurableDataLogException If an exception occurred. The causing exception is wrapped inside it.
 */
static ReadHandle openRead(long ledgerId, BookKeeper bookKeeper, BookKeeperConfig config) throws DurableDataLogException {
    try {
        return Exceptions.handleInterruptedCall(
                () -> FutureUtils.result(bookKeeper
                        .newOpenLedgerOp()
                .withLedgerId(ledgerId)
                .withPassword(config.getBKPassword())
                .withRecovery(false)
                .execute(), BK_EXCEPTION_HANDLER));
    } catch (BKException bkEx) {
        throw new DurableDataLogException(String.format("Unable to open-read ledger %d.", ledgerId), bkEx);
    }
}
 
Example #23
Source File: Ledgers.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Opens a ledger. This operation also fences out the ledger in case anyone else was writing to it.
 *
 * @param ledgerId   The Id of the Ledger to open.
 * @param bookKeeper A references to the BookKeeper client to use.
 * @param config     Configuration to use.
 * @return A ReadHandle for the newly opened ledger.
 * @throws DurableDataLogException If an exception occurred. The causing exception is wrapped inside it.
 */
static ReadHandle openFence(long ledgerId, BookKeeper bookKeeper, BookKeeperConfig config) throws DurableDataLogException {
    try {
        return Exceptions.handleInterruptedCall(
                () -> FutureUtils.result(bookKeeper
                        .newOpenLedgerOp()
                .withLedgerId(ledgerId)                    
                .withPassword(config.getBKPassword())
                .withRecovery(true)
                .execute(), BK_EXCEPTION_HANDLER));
    } catch (BKException bkEx) {
        throw new DurableDataLogException(String.format("Unable to open-fence ledger %d.", ledgerId), bkEx);
    }
}
 
Example #24
Source File: DLOutputStream.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public void flush() throws IOException {
    try {
        FutureUtils.result(writeControlRecord());
    } catch (IOException ioe) {
        throw ioe;
    } catch (Exception e) {
        log.error("Unexpected exception in DLOutputStream", e);
        throw new UnexpectedException("unexpected exception in DLOutputStream#flush()", e);
    }
}
 
Example #25
Source File: TableSpaceManager.java    From herddb with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<StatementExecutionResult> executeTableAwareStatement(Statement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
    long lockStamp = context.getTableSpaceLock();
    boolean lockAcquired = false;
    if (lockStamp == 0) {
        lockStamp = acquireReadLock(statement);
        context.setTableSpaceLock(lockStamp);
        lockAcquired = true;
    }
    TableAwareStatement st = (TableAwareStatement) statement;
    String table = st.getTable();
    AbstractTableManager manager = tables.get(table);
    CompletableFuture<StatementExecutionResult> res;
    if (manager == null) {
        res = FutureUtils.exception(new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName));
    } else if (manager.getCreatedInTransaction() > 0
            && (transaction == null || transaction.transactionId != manager.getCreatedInTransaction())) {
        res = FutureUtils.exception(new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName + ". created temporary in transaction " + manager.getCreatedInTransaction()));
    } else {
        res = manager.executeStatementAsync(statement, transaction, context);
    }
    if (lockAcquired) {
        res = releaseReadLock(res, lockStamp, statement)
                .whenComplete((s, err) -> {
                    context.setTableSpaceLock(0);
                });
    }
    return res;

}
 
Example #26
Source File: TopicReaderTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "variationsForExpectedPos")
public void testReaderStartMessageIdAtExpectedPos(boolean batching, boolean startInclusive, int numOfMessages)
        throws Exception {
    final String topicName = "persistent://my-property/my-ns/ReaderStartMessageIdAtExpectedPos";
    final int resetIndex = new Random().nextInt(numOfMessages); // Choose some random index to reset
    final int firstMessage = startInclusive ? resetIndex : resetIndex + 1; // First message of reset

    Producer<byte[]> producer = pulsarClient.newProducer()
            .topic(topicName)
            .enableBatching(batching)
            .create();

    CountDownLatch latch = new CountDownLatch(numOfMessages);

    final AtomicReference<MessageId> resetPos = new AtomicReference<>();

    for (int i = 0; i < numOfMessages; i++) {

        final int j = i;

        producer.sendAsync(String.format("msg num %d", i).getBytes())
                .thenCompose(messageId -> FutureUtils.value(Pair.of(j, messageId)))
                .whenComplete((p, e) -> {
                    if (e != null) {
                        fail("send msg failed due to " + e.getMessage());
                    } else {
                        if (p.getLeft() == resetIndex) {
                            resetPos.set(p.getRight());
                        }
                    }
                    latch.countDown();
                });
    }

    latch.await();

    ReaderBuilder<byte[]> readerBuilder = pulsarClient.newReader()
            .topic(topicName)
            .startMessageId(resetPos.get());

    if (startInclusive) {
        readerBuilder.startMessageIdInclusive();
    }

    Reader<byte[]> reader = readerBuilder.create();
    Set<String> messageSet = Sets.newHashSet();
    for (int i = firstMessage; i < numOfMessages; i++) {
        Message<byte[]> message = reader.readNext();
        String receivedMessage = new String(message.getData());
        String expectedMessage = String.format("msg num %d", i);
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
    }

    assertTrue(reader.isConnected());
    assertEquals(((ReaderImpl) reader).getConsumer().numMessagesInQueue(), 0);

    // Processed messages should be the number of messages in the range: [FirstResetMessage..TotalNumOfMessages]
    assertEquals(messageSet.size(), numOfMessages - firstMessage);

    reader.close();
    producer.close();
}
 
Example #27
Source File: SimpleProducerConsumerTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "variationsForExpectedPos")
public void testConsumerStartMessageIdAtExpectedPos(boolean batching, boolean startInclusive, int numOfMessages)
        throws Exception {
    final String topicName = "persistent://my-property/my-ns/ConsumerStartMessageIdAtExpectedPos";
    final int resetIndex = new Random().nextInt(numOfMessages); // Choose some random index to reset
    final int firstMessage = startInclusive ? resetIndex : resetIndex + 1; // First message of reset

    Producer<byte[]> producer = pulsarClient.newProducer()
            .topic(topicName)
            .enableBatching(batching)
            .create();

    CountDownLatch latch = new CountDownLatch(numOfMessages);

    final AtomicReference<MessageId> resetPos = new AtomicReference<>();

    for (int i = 0; i < numOfMessages; i++) {

        final int j = i;

        producer.sendAsync(String.format("msg num %d", i).getBytes())
                .thenCompose(messageId -> FutureUtils.value(Pair.of(j, messageId)))
                .whenComplete((p, e) -> {
                    if (e != null) {
                        fail("send msg failed due to " + e.getMessage());
                    } else {
                        log.info("send msg with id {}", p.getRight());
                        if (p.getLeft() == resetIndex) {
                            resetPos.set(p.getRight());
                        }
                    }
                    latch.countDown();
                });
    }

    latch.await();

    ConsumerBuilder<byte[]> consumerBuilder = pulsarClient.newConsumer()
            .topic(topicName);

    if (startInclusive) {
        consumerBuilder.startMessageIdInclusive();
    }

    Consumer<byte[]> consumer = consumerBuilder.subscriptionName("my-subscriber-name").subscribe();
    consumer.seek(resetPos.get());
    log.info("reset cursor to {}", resetPos.get());
    Set<String> messageSet = Sets.newHashSet();
    for (int i = firstMessage; i < numOfMessages; i++) {
        Message<byte[]> message = consumer.receive();
        String receivedMessage = new String(message.getData());
        String expectedMessage = String.format("msg num %d", i);
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
    }

    assertEquals(((ConsumerImpl) consumer).numMessagesInQueue(), 0);

    // Processed messages should be the number of messages in the range: [FirstResetMessage..TotalNumOfMessages]
    assertEquals(messageSet.size(), numOfMessages - firstMessage);

    consumer.close();
    producer.close();
}
 
Example #28
Source File: MockExecutorController.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public DeferredTask(Runnable runnable,
                    long delayTimeMs) {
    this.runnable = runnable;
    this.scheduledAtMillis = delayTimeMs + clock.millis();
    this.future = FutureUtils.createFuture();
}
 
Example #29
Source File: MockExecutorController.java    From pulsar with Apache License 2.0 4 votes vote down vote up
void run() {
    runnable.run();
    FutureUtils.complete(future, null);
}
 
Example #30
Source File: BookKeeperCommitLogTest.java    From herddb with Apache License 2.0 4 votes vote down vote up
@Test
public void testSimpleFence() throws Exception {
    final String tableSpaceUUID = UUID.randomUUID().toString();
    final String name = TableSpace.DEFAULT;
    final String nodeid = "nodeid";
    ServerConfiguration serverConfiguration = new ServerConfiguration();
    try (ZookeeperMetadataStorageManager man = new ZookeeperMetadataStorageManager(testEnv.getAddress(),
            testEnv.getTimeout(), testEnv.getPath());
            BookkeeperCommitLogManager logManager = new BookkeeperCommitLogManager(man, serverConfiguration, NullStatsLogger.INSTANCE)) {
        man.start();
        logManager.start();

        LogSequenceNumber lsn1;
        LogSequenceNumber lsn2;
        LogSequenceNumber lsn3;
        try (BookkeeperCommitLog writer = logManager.createCommitLog(tableSpaceUUID, name, nodeid);) {
            writer.startWriting();
            lsn1 = writer.log(LogEntryFactory.beginTransaction(1), true).getLogSequenceNumber();
            lsn2 = writer.log(LogEntryFactory.beginTransaction(2), true).getLogSequenceNumber();

            // a new leader starts, from START_OF_TIME
            try (BookkeeperCommitLog writer2 = logManager.createCommitLog(tableSpaceUUID, name, nodeid);) {
                writer2.recovery(LogSequenceNumber.START_OF_TIME, (a, b) -> {}, true);
                writer2.startWriting();
                lsn3 = writer2.log(LogEntryFactory.beginTransaction(3), true).getLogSequenceNumber();
            }

            TestUtils.assertThrows(LogNotAvailableException.class, ()
                    -> FutureUtils.result(writer.log(LogEntryFactory.beginTransaction(3), true).logSequenceNumber));

            assertTrue(writer.isFailed());

            assertTrue(lsn1.after(LogSequenceNumber.START_OF_TIME));
            assertTrue(lsn2.after(lsn1));

            // written by second writer
            assertTrue(lsn3.after(lsn2));
        }

        try (BookkeeperCommitLog reader = logManager.createCommitLog(tableSpaceUUID, name, nodeid);) {
            List<Map.Entry<LogSequenceNumber, LogEntry>> list = new ArrayList<>();
            reader.recovery(LogSequenceNumber.START_OF_TIME, (lsn, entry) -> {
                if (entry.type != LogEntryType.NOOP) {
                    list.add(new AbstractMap.SimpleImmutableEntry<>(lsn, entry));
                }
            }, false);
            assertEquals(3, list.size());
            assertEquals(lsn1, list.get(0).getKey());
            assertEquals(lsn2, list.get(1).getKey());
            assertEquals(lsn3, list.get(2).getKey());
        }

    }
}