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;
}
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());
}
}
}
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);
}
}
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();
}
}
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);
}
}
Aggregations