Search in sources :

Example 1 with BookKeeperAdmin

use of org.apache.bookkeeper.client.BookKeeperAdmin in project pulsar by yahoo.

the class ManagedLedgerOfflineBacklog method tryGetMDPosition.

private PositionImpl tryGetMDPosition(BookKeeper bookKeeper, long ledgerId, String cursorName) {
    BookKeeperAdmin bookKeeperAdmin = null;
    long lastEntry = LedgerHandle.INVALID_ENTRY_ID;
    PositionImpl lastAckedMessagePosition = null;
    try {
        bookKeeperAdmin = new BookKeeperAdmin(bookKeeper);
        Iterator<LedgerEntry> entries = bookKeeperAdmin.readEntries(ledgerId, 0, lastEntry).iterator();
        while (entries.hasNext()) {
            LedgerEntry ledgerEntry = entries.next();
            lastEntry = ledgerEntry.getEntryId();
            if (log.isDebugEnabled()) {
                log.debug(" Read entry {} from ledger {} for cursor {}", lastEntry, ledgerId, cursorName);
            }
            MLDataFormats.PositionInfo positionInfo = MLDataFormats.PositionInfo.parseFrom(ledgerEntry.getEntry());
            lastAckedMessagePosition = new PositionImpl(positionInfo);
            if (log.isDebugEnabled()) {
                log.debug("Cursor {} read position {}", cursorName, lastAckedMessagePosition);
            }
        }
    } catch (Exception e) {
        log.warn("Unable to determine LAC for ledgerId {} for cursor {}: {}", ledgerId, cursorName, e);
    } finally {
        if (bookKeeperAdmin != null) {
            try {
                bookKeeperAdmin.close();
            } catch (Exception e) {
                log.warn("Unable to close bk admin for ledgerId {} for cursor {}", ledgerId, cursorName, e);
            }
        }
    }
    return lastAckedMessagePosition;
}
Also used : MLDataFormats(org.apache.bookkeeper.mledger.proto.MLDataFormats) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Example 2 with BookKeeperAdmin

use of org.apache.bookkeeper.client.BookKeeperAdmin in project herddb by diennea.

the class BookkeeperFailuresTest method testBookieNotAvailableDuringTransaction.

@Test
public void testBookieNotAvailableDuringTransaction() throws Exception {
    ServerConfiguration serverconfig_1 = new ServerConfiguration(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_PORT, 7867);
    serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
    try (Server server = new Server(serverconfig_1)) {
        server.start();
        server.waitForStandaloneBoot();
        Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).primaryKey("c").build();
        // create table is done out of the transaction (this is very like autocommit=true)
        server.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        StatementExecutionResult executeStatement = server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
        long transactionId = executeStatement.transactionId;
        server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
        server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
        TableSpaceManager tableSpaceManager = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
        BookkeeperCommitLog log = (BookkeeperCommitLog) tableSpaceManager.getLog();
        long ledgerId = log.getLastSequenceNumber().ledgerId;
        assertTrue(ledgerId >= 1);
        Transaction transaction = tableSpaceManager.getTransactions().stream().filter(t -> t.transactionId == transactionId).findFirst().get();
        // Transaction will synch, so every addEntry will be acked, but will not be "confirmed" yet
        transaction.synch();
        try (DataScanner scan = scan(server.getManager(), "select * from t1", Collections.emptyList(), new TransactionContext(transactionId))) {
            assertEquals(3, scan.consume().size());
        }
        try (DataScanner scan = scan(server.getManager(), "select * from t1", Collections.emptyList(), TransactionContext.NO_TRANSACTION)) {
            // no record, but the table exists!
            assertEquals(0, scan.consume().size());
        }
        // we do not want auto-recovery
        server.getManager().setActivatorPauseStatus(true);
        testEnv.stopBookie();
        // transaction will continue and see the failure only the time of the commit
        try {
            server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
            // this will piggyback the LAC for the transaction
            System.out.println("Insert of c,4 OK");
        } catch (StatementExecutionException expected) {
            System.out.println("Insert of c,4 failed " + expected);
            // in can happen that the log gets closed
            assertEquals(herddb.log.LogNotAvailableException.class, expected.getCause().getClass());
        }
        try {
            server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 5)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
            // this will piggyback the LAC for the transaction
            System.out.println("Insert of c,5 OK");
        } catch (StatementExecutionException expected) {
            System.out.println("Insert of c,5 failed " + expected);
            // in can happen that the log gets closed
            assertEquals(herddb.log.LogNotAvailableException.class, expected.getCause().getClass());
        }
        try {
            server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 6)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
            // this will piggyback the LAC for the transaction
            System.out.println("Insert of c,6 OK");
        } catch (StatementExecutionException expected) {
            System.out.println("Insert of c,6 failed " + expected);
            // in can happen that the log gets closed
            assertEquals(herddb.log.LogNotAvailableException.class, expected.getCause().getClass());
        }
        try {
            server.getManager().executeStatement(new CommitTransactionStatement(TableSpace.DEFAULT, transactionId), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            // this will fail alweays
            fail();
        } catch (StatementExecutionException expected) {
            System.out.println("Commit failed as expected:" + expected);
        }
        testEnv.startBookie(false);
        while (true) {
            System.out.println("status leader:" + tableSpaceManager.isLeader() + " failed:" + tableSpaceManager.isFailed());
            if (tableSpaceManager.isFailed()) {
                break;
            }
            Thread.sleep(100);
        }
        try (BookKeeper bk = createBookKeeper();
            LedgerHandle handle = bk.openLedgerNoRecovery(ledgerId, BookKeeper.DigestType.CRC32, "herddb".getBytes(StandardCharsets.UTF_8))) {
            BookKeeperAdmin admin = new BookKeeperAdmin(bk);
            try {
                LedgerMetadata ledgerMetadata = admin.getLedgerMetadata(handle);
                System.out.println("current ledger metadata before recovery: " + ledgerMetadata);
            } finally {
                admin.close();
            }
        }
        server.getManager().setActivatorPauseStatus(false);
        server.getManager().triggerActivator(ActivatorRunRequest.TABLESPACEMANAGEMENT);
        while (true) {
            TableSpaceManager tableSpaceManager_after_failure = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
            System.out.println("tableSpaceManager_after_failure:" + tableSpaceManager_after_failure);
            System.out.println("tableSpaceManager:" + tableSpaceManager);
            if (tableSpaceManager_after_failure != null && tableSpaceManager_after_failure != tableSpaceManager) {
                break;
            }
            Thread.sleep(1000);
            server.getManager().triggerActivator(ActivatorRunRequest.TABLESPACEMANAGEMENT);
        }
        TableSpaceManager tableSpaceManager_after_failure = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
        Assert.assertNotNull(tableSpaceManager_after_failure);
        assertNotSame(tableSpaceManager_after_failure, tableSpaceManager);
        assertTrue(!tableSpaceManager_after_failure.isFailed());
        // the insert should succeed because the trasaction has been rolledback automatically
        server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        try (DataScanner scan = scan(server.getManager(), "select * from t1", Collections.emptyList())) {
            assertEquals(1, scan.consume().size());
        }
    }
}
Also used : Table(herddb.model.Table) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) CreateTableStatement(herddb.model.commands.CreateTableStatement) BookKeeper(org.apache.bookkeeper.client.BookKeeper) InsertStatement(herddb.model.commands.InsertStatement) StatementExecutionException(herddb.model.StatementExecutionException) DataScanner(herddb.model.DataScanner) Transaction(herddb.model.Transaction) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) TransactionContext(herddb.model.TransactionContext) StatementExecutionResult(herddb.model.StatementExecutionResult) TableSpaceManager(herddb.core.TableSpaceManager) BookkeeperCommitLog(herddb.cluster.BookkeeperCommitLog) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) Test(org.junit.Test)

Example 3 with BookKeeperAdmin

use of org.apache.bookkeeper.client.BookKeeperAdmin in project bookkeeper by apache.

the class TestTLS method testClient.

private LedgerMetadata testClient(BookKeeper client, int clusterSize) throws Exception {
    byte[] passwd = "testPassword".getBytes();
    int numEntries = 100;
    long lid;
    byte[] testEntry = "testEntry".getBytes();
    try (LedgerHandle lh = client.createLedger(clusterSize, clusterSize, DigestType.CRC32, passwd)) {
        for (int i = 0; i <= numEntries; i++) {
            lh.addEntry(testEntry);
        }
        lid = lh.getId();
    }
    try (LedgerHandle lh = client.openLedger(lid, DigestType.CRC32, passwd)) {
        Enumeration<LedgerEntry> entries = lh.readEntries(0, numEntries);
        while (entries.hasMoreElements()) {
            LedgerEntry e = entries.nextElement();
            assertTrue("Entry contents incorrect", Arrays.equals(e.getEntry(), testEntry));
        }
        BookKeeperAdmin admin = new BookKeeperAdmin(client);
        return admin.getLedgerMetadata(lh);
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin)

Example 4 with BookKeeperAdmin

use of org.apache.bookkeeper.client.BookKeeperAdmin in project bookkeeper by apache.

the class Auditor method checkAllLedgers.

/**
 * List all the ledgers and check them individually. This should not
 * be run very often.
 */
void checkAllLedgers() throws BKAuditException, BKException, IOException, InterruptedException, KeeperException {
    ZooKeeper newzk = ZooKeeperClient.newBuilder().connectString(conf.getZkServers()).sessionTimeoutMs(conf.getZkTimeout()).build();
    final BookKeeper client = new BookKeeper(new ClientConfiguration(conf), newzk);
    final BookKeeperAdmin admin = new BookKeeperAdmin(client, statsLogger);
    try {
        final LedgerChecker checker = new LedgerChecker(client);
        final AtomicInteger returnCode = new AtomicInteger(BKException.Code.OK);
        final CountDownLatch processDone = new CountDownLatch(1);
        Processor<Long> checkLedgersProcessor = new Processor<Long>() {

            @Override
            public void process(final Long ledgerId, final AsyncCallback.VoidCallback callback) {
                try {
                    if (!ledgerUnderreplicationManager.isLedgerReplicationEnabled()) {
                        LOG.info("Ledger rereplication has been disabled, aborting periodic check");
                        processDone.countDown();
                        return;
                    }
                } catch (ReplicationException.UnavailableException ue) {
                    LOG.error("Underreplication manager unavailable running periodic check", ue);
                    processDone.countDown();
                    return;
                }
                LedgerHandle lh = null;
                try {
                    lh = admin.openLedgerNoRecovery(ledgerId);
                    checker.checkLedger(lh, new ProcessLostFragmentsCb(lh, callback), conf.getAuditorLedgerVerificationPercentage());
                    // we collect the following stats to get a measure of the
                    // distribution of a single ledger within the bk cluster
                    // the higher the number of fragments/bookies, the more distributed it is
                    numFragmentsPerLedger.registerSuccessfulValue(lh.getNumFragments());
                    numBookiesPerLedger.registerSuccessfulValue(lh.getNumBookies());
                    numLedgersChecked.inc();
                } catch (BKException.BKNoSuchLedgerExistsException bknsle) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Ledger was deleted before we could check it", bknsle);
                    }
                    callback.processResult(BKException.Code.OK, null, null);
                    return;
                } catch (BKException bke) {
                    LOG.error("Couldn't open ledger " + ledgerId, bke);
                    callback.processResult(BKException.Code.BookieHandleNotAvailableException, null, null);
                    return;
                } catch (InterruptedException ie) {
                    LOG.error("Interrupted opening ledger", ie);
                    Thread.currentThread().interrupt();
                    callback.processResult(BKException.Code.InterruptedException, null, null);
                    return;
                } finally {
                    if (lh != null) {
                        try {
                            lh.close();
                        } catch (BKException bke) {
                            LOG.warn("Couldn't close ledger " + ledgerId, bke);
                        } catch (InterruptedException ie) {
                            LOG.warn("Interrupted closing ledger " + ledgerId, ie);
                            Thread.currentThread().interrupt();
                        }
                    }
                }
            }
        };
        ledgerManager.asyncProcessLedgers(checkLedgersProcessor, new AsyncCallback.VoidCallback() {

            @Override
            public void processResult(int rc, String s, Object obj) {
                returnCode.set(rc);
                processDone.countDown();
            }
        }, null, BKException.Code.OK, BKException.Code.ReadException);
        try {
            processDone.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new BKAuditException("Exception while checking ledgers", e);
        }
        if (returnCode.get() != BKException.Code.OK) {
            throw BKException.create(returnCode.get());
        }
    } finally {
        admin.close();
        client.close();
        newzk.close();
    }
}
Also used : Processor(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.Processor) AsyncCallback(org.apache.zookeeper.AsyncCallback) BKAuditException(org.apache.bookkeeper.replication.ReplicationException.BKAuditException) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookKeeper(org.apache.bookkeeper.client.BookKeeper) CountDownLatch(java.util.concurrent.CountDownLatch) ZooKeeper(org.apache.zookeeper.ZooKeeper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LedgerChecker(org.apache.bookkeeper.client.LedgerChecker) BKException(org.apache.bookkeeper.client.BKException) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) UnavailableException(org.apache.bookkeeper.replication.ReplicationException.UnavailableException)

Example 5 with BookKeeperAdmin

use of org.apache.bookkeeper.client.BookKeeperAdmin in project bookkeeper by apache.

the class Auditor method initialize.

private void initialize(ServerConfiguration conf, ZooKeeper zkc) throws UnavailableException {
    try {
        ClientConfiguration clientConfiguration = new ClientConfiguration(conf);
        clientConfiguration.setClientRole(ClientConfiguration.CLIENT_ROLE_SYSTEM);
        LOG.info("AuthProvider used by the Auditor is {}", clientConfiguration.getClientAuthProviderFactoryClass());
        this.bkc = new BookKeeper(clientConfiguration, zkc);
        LedgerManagerFactory ledgerManagerFactory = AbstractZkLedgerManagerFactory.newLedgerManagerFactory(conf, bkc.getMetadataClientDriver().getLayoutManager());
        ledgerManager = ledgerManagerFactory.newLedgerManager();
        this.bookieLedgerIndexer = new BookieLedgerIndexer(ledgerManager);
        this.ledgerUnderreplicationManager = ledgerManagerFactory.newLedgerUnderreplicationManager();
        this.admin = new BookKeeperAdmin(bkc, statsLogger);
        if (this.ledgerUnderreplicationManager.initializeLostBookieRecoveryDelay(conf.getLostBookieRecoveryDelay())) {
            LOG.info("Initializing lostBookieRecoveryDelay zNode to the conif value: {}", conf.getLostBookieRecoveryDelay());
        } else {
            LOG.info("Valid lostBookieRecoveryDelay zNode is available, so not creating " + "lostBookieRecoveryDelay zNode as part of Auditor initialization ");
        }
        lostBookieRecoveryDelayBeforeChange = this.ledgerUnderreplicationManager.getLostBookieRecoveryDelay();
    } catch (CompatibilityException ce) {
        throw new UnavailableException("CompatibilityException while initializing Auditor", ce);
    } catch (IOException | BKException | KeeperException ioe) {
        throw new UnavailableException("Exception while initializing Auditor", ioe);
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new UnavailableException("Interrupted while initializing Auditor", ie);
    }
}
Also used : CompatibilityException(org.apache.bookkeeper.replication.ReplicationException.CompatibilityException) UnavailableException(org.apache.bookkeeper.replication.ReplicationException.UnavailableException) BookKeeper(org.apache.bookkeeper.client.BookKeeper) BKException(org.apache.bookkeeper.client.BKException) IOException(java.io.IOException) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory) AbstractZkLedgerManagerFactory(org.apache.bookkeeper.meta.AbstractZkLedgerManagerFactory) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

BookKeeperAdmin (org.apache.bookkeeper.client.BookKeeperAdmin)10 BKException (org.apache.bookkeeper.client.BKException)4 lombok.val (lombok.val)3 BookKeeper (org.apache.bookkeeper.client.BookKeeper)3 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)3 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 DurableDataLogException (io.pravega.segmentstore.storage.DurableDataLogException)2 BookKeeperConfig (io.pravega.segmentstore.storage.impl.bookkeeper.BookKeeperConfig)2 BookKeeperLogFactory (io.pravega.segmentstore.storage.impl.bookkeeper.BookKeeperLogFactory)2 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)2 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 MLDataFormats (org.apache.bookkeeper.mledger.proto.MLDataFormats)2 UnavailableException (org.apache.bookkeeper.replication.ReplicationException.UnavailableException)2 Test (org.junit.Test)2 BookkeeperCommitLog (herddb.cluster.BookkeeperCommitLog)1 TableSpaceManager (herddb.core.TableSpaceManager)1 DataScanner (herddb.model.DataScanner)1 StatementExecutionException (herddb.model.StatementExecutionException)1 StatementExecutionResult (herddb.model.StatementExecutionResult)1