use of org.apache.bookkeeper.client.LedgerHandle in project bookkeeper by apache.
the class TestAuth method entryCount.
/**
* check if the entry exists. Restart the bookie to allow
* access
*/
private int entryCount(long ledgerId, ServerConfiguration bookieConf, ClientConfiguration clientConf) throws Exception {
LOG.info("Counting entries in {}", ledgerId);
for (ServerConfiguration conf : bsConfs) {
conf.setBookieAuthProviderFactoryClass(AlwaysSucceedBookieAuthProviderFactory.class.getName());
}
clientConf.setClientAuthProviderFactoryClass(SendUntilCompleteClientAuthProviderFactory.class.getName());
restartBookies();
BookKeeper bkc = new BookKeeper(clientConf, zkc);
LedgerHandle lh = bkc.openLedger(ledgerId, DigestType.CRC32, PASSWD);
if (lh.getLastAddConfirmed() < 0) {
return 0;
}
Enumeration<LedgerEntry> e = lh.readEntries(0, lh.getLastAddConfirmed());
int count = 0;
while (e.hasMoreElements()) {
count++;
assertTrue("Should match what we wrote", Arrays.equals(e.nextElement().getEntry(), ENTRY));
}
return count;
}
use of org.apache.bookkeeper.client.LedgerHandle in project bookkeeper by apache.
the class TestAuth method connectAndWriteToBookie.
// we pass in ledgerId because the method may throw exceptions
private void connectAndWriteToBookie(ClientConfiguration conf, AtomicLong ledgerWritten) throws Exception {
LOG.info("Connecting to bookie");
BookKeeper bkc = new BookKeeper(conf, zkc);
LedgerHandle l = bkc.createLedger(1, 1, DigestType.CRC32, PASSWD);
ledgerWritten.set(l.getId());
l.addEntry(ENTRY);
l.close();
bkc.close();
}
use of org.apache.bookkeeper.client.LedgerHandle in project bookkeeper by apache.
the class EnableZkSecurityBasicTest method testCreateLedgerAddEntryOnSecureZooKeepeer.
@Test
public void testCreateLedgerAddEntryOnSecureZooKeepeer() throws Exception {
startNewBookie();
ClientConfiguration conf = new ClientConfiguration();
conf.setZkServers(zkUtil.getZooKeeperConnectString());
conf.setZkTimeout(20000);
conf.setZkEnableSecurity(true);
try (BookKeeper bkc = new BookKeeper(conf)) {
try (LedgerHandle lh = bkc.createLedger(1, 1, 1, BookKeeper.DigestType.CRC32, "testPasswd".getBytes())) {
lh.addEntry("foo".getBytes(StandardCharsets.UTF_8));
}
}
checkAllAcls();
}
use of org.apache.bookkeeper.client.LedgerHandle in project bookkeeper by apache.
the class LedgerStorageCheckpointTest method testIfEntryLogPerLedgerEnabledCheckpointFlushesAllLogs.
/*
* in this method it checks if entryLogPerLedger is enabled, then
* InterLeavedLedgerStorage.checkpoint flushes current activelog and flushes
* all rotatedlogs and closes them.
*
*/
@Test
public void testIfEntryLogPerLedgerEnabledCheckpointFlushesAllLogs() throws Exception {
File tmpDir = createTempDir("DiskCheck", "test");
final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(zkUtil.getZooKeeperConnectString()).setZkTimeout(5000).setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new String[] { tmpDir.getPath() }).setAutoRecoveryDaemonEnabled(false).setFlushInterval(3000).setBookiePort(PortManager.nextFreePort()).setEntryLogPerLedgerEnabled(true).setLedgerStorageClass(InterleavedLedgerStorage.class.getName()).setFlushIntervalInBytes(10000000);
Assert.assertEquals("Number of JournalDirs", 1, conf.getJournalDirs().length);
// we know there is only one ledgerDir
File ledgerDir = Bookie.getCurrentDirectories(conf.getLedgerDirs())[0];
BookieServer server = new BookieServer(conf);
server.start();
ClientConfiguration clientConf = new ClientConfiguration();
clientConf.setZkServers(zkUtil.getZooKeeperConnectString());
BookKeeper bkClient = new BookKeeper(clientConf);
InterleavedLedgerStorage ledgerStorage = (InterleavedLedgerStorage) server.getBookie().ledgerStorage;
EntryLogger entryLogger = ledgerStorage.entryLogger;
int numOfEntries = 5;
byte[] dataBytes = "data".getBytes();
long ledgerId = 10;
LedgerHandle handle = bkClient.createLedgerAdv(ledgerId, 1, 1, 1, DigestType.CRC32, "passwd".getBytes(), null);
for (int j = 0; j < numOfEntries; j++) {
handle.addEntry(j, dataBytes);
}
handle.close();
// simulate rolling entrylog
ledgerStorage.entryLogger.rollLog();
ledgerId = 20;
handle = bkClient.createLedgerAdv(ledgerId, 1, 1, 1, DigestType.CRC32, "passwd".getBytes(), null);
for (int j = 0; j < numOfEntries; j++) {
handle.addEntry(j, dataBytes);
}
handle.close();
// simulate rolling entrylog
ledgerStorage.entryLogger.rollLog();
ledgerId = 30;
handle = bkClient.createLedgerAdv(ledgerId, 1, 1, 1, DigestType.CRC32, "passwd".getBytes(), null);
for (int j = 0; j < numOfEntries; j++) {
handle.addEntry(j, dataBytes);
}
handle.close();
Assert.assertNotEquals("bytesWrittenSinceLastFlush shouldn't be zero", 0, entryLogger.logChannel.getUnpersistedBytes());
Assert.assertNotEquals("There should be logChannelsToFlush", 0, entryLogger.logChannelsToFlush.size());
/*
* wait for atleast flushInterval period, so that checkpoint can happen.
*/
executorController.advance(Duration.ofMillis(conf.getFlushInterval()));
/*
* since checkpoint happenend, there shouldn't be any logChannelsToFlush
* and bytesWrittenSinceLastFlush should be zero.
*/
Assert.assertTrue("There shouldn't be logChannelsToFlush", ((entryLogger.logChannelsToFlush == null) || (entryLogger.logChannelsToFlush.size() == 0)));
Assert.assertEquals("bytesWrittenSinceLastFlush should be zero", 0, entryLogger.logChannel.getUnpersistedBytes());
}
use of org.apache.bookkeeper.client.LedgerHandle in project bookkeeper by apache.
the class LedgerStorageCheckpointTest method testCheckpointofILSWhenEntryLogIsRotated.
public void testCheckpointofILSWhenEntryLogIsRotated(boolean entryLogPerLedgerEnabled) throws Exception {
File tmpDir = createTempDir("DiskCheck", "test");
final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(zkUtil.getZooKeeperConnectString()).setZkTimeout(5000).setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new String[] { tmpDir.getPath() }).setAutoRecoveryDaemonEnabled(false).setFlushInterval(30000).setBookiePort(PortManager.nextFreePort()).setEntryLogPerLedgerEnabled(entryLogPerLedgerEnabled).setLedgerStorageClass(InterleavedLedgerStorage.class.getName());
Assert.assertEquals("Number of JournalDirs", 1, conf.getJournalDirs().length);
// we know there is only one ledgerDir
File ledgerDir = Bookie.getCurrentDirectories(conf.getLedgerDirs())[0];
BookieServer server = new BookieServer(conf);
server.start();
ClientConfiguration clientConf = new ClientConfiguration();
clientConf.setZkServers(zkUtil.getZooKeeperConnectString());
BookKeeper bkClient = new BookKeeper(clientConf);
InterleavedLedgerStorage ledgerStorage = (InterleavedLedgerStorage) server.getBookie().ledgerStorage;
int numOfEntries = 5;
byte[] dataBytes = "data".getBytes();
long ledgerId = 10;
LedgerHandle handle = bkClient.createLedgerAdv(ledgerId, 1, 1, 1, DigestType.CRC32, "passwd".getBytes(), null);
for (int j = 0; j < numOfEntries; j++) {
handle.addEntry(j, dataBytes);
}
handle.close();
// simulate rolling entrylog
ledgerStorage.entryLogger.rollLog();
// sleep for a bit for checkpoint to do its task
executorController.advance(Duration.ofMillis(500));
File lastMarkFile = new File(ledgerDir, "lastMark");
LogMark rolledLogMark = readLastMarkFile(lastMarkFile);
if (entryLogPerLedgerEnabled) {
Assert.assertEquals("rolledLogMark should be zero, since checkpoint" + "shouldn't have happened when entryLog is rotated", 0, rolledLogMark.compare(new LogMark()));
} else {
Assert.assertNotEquals("rolledLogMark shouldn't be zero, since checkpoint" + "should have happened when entryLog is rotated", 0, rolledLogMark.compare(new LogMark()));
}
bkClient.close();
server.shutdown();
}
Aggregations