Search in sources :

Example 11 with LedgerDirsManager

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()));
    }
}
Also used : LedgerDirsManager(org.apache.bookkeeper.bookie.LedgerDirsManager) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) Bookie(org.apache.bookkeeper.bookie.Bookie) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException) File(java.io.File) Test(org.junit.Test)

Example 12 with LedgerDirsManager

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()));
    }
}
Also used : LedgerDirsManager(org.apache.bookkeeper.bookie.LedgerDirsManager) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) Bookie(org.apache.bookkeeper.bookie.Bookie) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) File(java.io.File)

Example 13 with LedgerDirsManager

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();
    }
}
Also used : LedgerDirsManager(org.apache.bookkeeper.bookie.LedgerDirsManager) DiskChecker(org.apache.bookkeeper.util.DiskChecker) ByteString(com.google.protobuf.ByteString)

Aggregations

LedgerDirsManager (org.apache.bookkeeper.bookie.LedgerDirsManager)13 File (java.io.File)11 Test (org.junit.Test)8 DiskChecker (org.apache.bookkeeper.util.DiskChecker)7 Bookie (org.apache.bookkeeper.bookie.Bookie)6 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)6 ByteBuf (io.netty.buffer.ByteBuf)4 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)4 BookieShell (org.apache.bookkeeper.bookie.BookieShell)3 Checkpoint (org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint)3 BKException (org.apache.bookkeeper.client.BKException)3 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)3 InterleavedLedgerStorage (org.apache.bookkeeper.bookie.InterleavedLedgerStorage)2 Journal (org.apache.bookkeeper.bookie.Journal)2 LogMark (org.apache.bookkeeper.bookie.LogMark)2 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1