use of org.apache.bookkeeper.bookie.LedgerDirsManager in project bookkeeper by apache.
the class ReadOnlyBookieTest method testBookieShouldTurnWritableFromReadOnly.
@Test
public void testBookieShouldTurnWritableFromReadOnly() throws Exception {
killBookie(0);
baseConf.setReadOnlyModeEnabled(true);
startNewBookie();
LedgerHandle ledger = bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
// Check new bookie with readonly mode enabled.
File[] ledgerDirs = bsConfs.get(1).getLedgerDirs();
assertEquals("Only one ledger dir should be present", 1, ledgerDirs.length);
Bookie bookie = bs.get(1).getBookie();
LedgerDirsManager ledgerDirsManager = bookie.getLedgerDirsManager();
for (int i = 0; i < 10; i++) {
ledger.addEntry("data".getBytes());
}
File testDir = new File(ledgerDirs[0], "current");
// Now add the current ledger dir to filled dirs list
ledgerDirsManager.addToFilledDirs(testDir);
try {
ledger.addEntry("data".getBytes());
fail("Should fail to add entry since there isn't enough bookies alive.");
} catch (BKException.BKNotEnoughBookiesException e) {
// Expected
}
bkc.waitForReadOnlyBookie(Bookie.getBookieAddress(bsConfs.get(1))).get(30, TimeUnit.SECONDS);
LOG.info("bookie is running {}, readonly {}.", bookie.isRunning(), bookie.isReadOnly());
assertTrue("Bookie should be running and converted to readonly mode", bookie.isRunning() && bookie.isReadOnly());
// should fail to create ledger
try {
bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
fail("Should fail to create a ledger since there isn't enough bookies alive.");
} catch (BKException.BKNotEnoughBookiesException bke) {
// Expected.
}
// Now add the current ledger dir back to writable dirs list
ledgerDirsManager.addToWritableDirs(testDir, true);
bkc.waitForWritableBookie(Bookie.getBookieAddress(bsConfs.get(1))).get(30, TimeUnit.SECONDS);
LOG.info("bookie is running {}, readonly {}.", bookie.isRunning(), bookie.isReadOnly());
assertTrue("Bookie should be running and converted back to writable mode", bookie.isRunning() && !bookie.isReadOnly());
LedgerHandle newLedger = bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
for (int i = 0; i < 10; i++) {
newLedger.addEntry("data".getBytes());
}
Enumeration<LedgerEntry> readEntries = newLedger.readEntries(0, 9);
while (readEntries.hasMoreElements()) {
LedgerEntry entry = readEntries.nextElement();
assertEquals("Entry should contain correct data", "data", new String(entry.getEntry()));
}
}
use of org.apache.bookkeeper.bookie.LedgerDirsManager in project bookkeeper by apache.
the class ReadOnlyBookieTest method testReadFromReadOnlyBookieShouldBeSuccess.
/**
* Try to read closed ledger from restarted ReadOnlyBookie.
*/
public void testReadFromReadOnlyBookieShouldBeSuccess() throws Exception {
LedgerHandle ledger = bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
for (int i = 0; i < 10; i++) {
ledger.addEntry("data".getBytes());
}
ledger.close();
bsConfs.get(1).setReadOnlyModeEnabled(true);
bsConfs.get(1).setDiskCheckInterval(500);
restartBookies();
// Check new bookie with readonly mode enabled.
File[] ledgerDirs = bsConfs.get(1).getLedgerDirs();
assertEquals("Only one ledger dir should be present", 1, ledgerDirs.length);
Bookie bookie = bs.get(1).getBookie();
LedgerDirsManager ledgerDirsManager = bookie.getLedgerDirsManager();
// Now add the current ledger dir to filled dirs list
ledgerDirsManager.addToFilledDirs(new File(ledgerDirs[0], "current"));
// Wait till Bookie converts to ReadOnly mode.
Thread.sleep(1000);
assertTrue("Bookie should be converted to readonly mode", bookie.isRunning() && bookie.isReadOnly());
// Now kill the other bookie and read entries from the readonly bookie
killBookie(0);
Enumeration<LedgerEntry> readEntries = ledger.readEntries(0, 9);
while (readEntries.hasMoreElements()) {
LedgerEntry entry = readEntries.nextElement();
assertEquals("Entry should contain correct data", "data", new String(entry.getEntry()));
}
}
use of org.apache.bookkeeper.bookie.LedgerDirsManager in project bookkeeper by apache.
the class DbLedgerStorage method readLedgerIndexEntries.
/**
* Reads ledger index entries to get list of entry-logger that contains given ledgerId.
*
* @param ledgerId
* @param serverConf
* @param processor
* @throws IOException
*/
public static void readLedgerIndexEntries(long ledgerId, ServerConfiguration serverConf, LedgerLoggerProcessor processor) throws IOException {
checkNotNull(serverConf, "ServerConfiguration can't be null");
checkNotNull(processor, "LedgerLoggger info processor can't null");
LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(serverConf, serverConf.getLedgerDirs(), new DiskChecker(serverConf.getDiskUsageThreshold(), serverConf.getDiskUsageWarnThreshold()));
String ledgerBasePath = ledgerDirsManager.getAllLedgerDirs().get(0).toString();
EntryLocationIndex entryLocationIndex = new EntryLocationIndex(serverConf, (path, dbConfigType, conf1) -> new KeyValueStorageRocksDB(path, DbConfigType.Small, conf1, true), ledgerBasePath, NullStatsLogger.INSTANCE);
try {
long lastEntryId = entryLocationIndex.getLastEntryInLedger(ledgerId);
for (long currentEntry = 0; currentEntry <= lastEntryId; currentEntry++) {
long offset = entryLocationIndex.getLocation(ledgerId, currentEntry);
if (offset <= 0) {
// entry not found in this bookie
continue;
}
long entryLogId = offset >> 32L;
long position = offset & 0xffffffffL;
processor.process(currentEntry, entryLogId, position);
}
} finally {
entryLocationIndex.close();
}
}
Aggregations