use of org.apache.bookkeeper.client.LedgerEntry 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.client.LedgerEntry 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.client.LedgerEntry in project bookkeeper by apache.
the class BookieFailureTest method testBookieRecovery.
@Test
public void testBookieRecovery() throws Exception {
// Shutdown all but 1 bookie
bs.get(0).shutdown();
bs.get(1).shutdown();
bs.get(2).shutdown();
byte[] passwd = "blah".getBytes();
LedgerHandle lh = bkc.createLedger(1, 1, digestType, passwd);
int numEntries = 100;
for (int i = 0; i < numEntries; i++) {
byte[] data = ("" + i).getBytes();
lh.addEntry(data);
}
bs.get(3).shutdown();
BookieServer server = new BookieServer(bsConfs.get(3));
server.start();
bs.set(3, server);
assertEquals(numEntries - 1, lh.getLastAddConfirmed());
Enumeration<LedgerEntry> entries = lh.readEntries(0, lh.getLastAddConfirmed());
int numScanned = 0;
while (entries.hasMoreElements()) {
assertEquals(("" + numScanned), new String(entries.nextElement().getEntry()));
numScanned++;
}
assertEquals(numEntries, numScanned);
}
use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.
the class BookieReadWriteTest method testReadFromOpenLedgerOpenOnce.
@Test
public void testReadFromOpenLedgerOpenOnce() throws Exception {
try {
// Create a ledger
lh = bkc.createLedger(digestType, ledgerPassword);
// bkc.initMessageDigest("SHA1");
ledgerId = lh.getId();
LOG.info("Ledger ID: " + lh.getId());
LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
writeNEntriesLastWriteSync(lh, numEntriesToWrite / 2);
ByteBuffer entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
// no recovery opened ledger 's last confirmed entry id is
// less than written
// and it just can read until (i-1)
int toRead = numEntriesToWrite / 2 - 2;
long readLastConfirmed = lhOpen.readLastConfirmed();
assertTrue(readLastConfirmed != 0);
Enumeration<LedgerEntry> readEntry = lhOpen.readEntries(toRead, toRead);
assertTrue("Enumeration of ledger entries has no element", readEntry.hasMoreElements());
LedgerEntry e = readEntry.nextElement();
assertEquals(toRead, e.getEntryId());
assertArrayEquals(entries.get(toRead), e.getEntry());
// should not written to a read only ledger
try {
lhOpen.addEntry(entry.array());
fail("Should have thrown an exception here");
} catch (BKException.BKIllegalOpException bkioe) {
// this is the correct response
} catch (Exception ex) {
LOG.error("Unexpected exception", ex);
fail("Unexpected exception");
}
writeNEntriesLastWriteSync(lh, numEntriesToWrite / 2);
long last = lh.readLastConfirmed();
assertTrue("Last confirmed add: " + last, last == (numEntriesToWrite - 2));
LOG.debug("*** WRITE COMPLETE ***");
// close ledger
lh.close();
// close read only ledger should not change metadata
lhOpen.close();
} catch (BKException e) {
LOG.error("Test failed", e);
fail("Test failed due to BookKeeper exception");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.error("Test failed", e);
fail("Test failed due to interruption");
}
}
use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.
the class BookieReadWriteTest method testReadWriteSyncSingleClient.
@Test
public void testReadWriteSyncSingleClient() throws IOException {
try {
// Create a ledger
lh = bkc.createLedger(digestType, ledgerPassword);
// bkc.initMessageDigest("SHA1");
ledgerId = lh.getId();
LOG.info("Ledger ID: " + lh.getId());
for (int i = 0; i < numEntriesToWrite; i++) {
ByteBuffer entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
entries.add(entry.array());
lh.addEntry(entry.array());
}
lh.close();
lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);
LOG.debug("Number of entries written: " + lh.getLastAddConfirmed());
assertTrue("Verifying number of entries written", lh.getLastAddConfirmed() == (numEntriesToWrite - 1));
Enumeration<LedgerEntry> ls = lh.readEntries(0, numEntriesToWrite - 1);
int i = 0;
while (ls.hasMoreElements()) {
ByteBuffer origbb = ByteBuffer.wrap(entries.get(i++));
Integer origEntry = origbb.getInt();
ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry());
LOG.debug("Length of result: " + result.capacity());
LOG.debug("Original entry: " + origEntry);
Integer retrEntry = result.getInt();
LOG.debug("Retrieved entry: " + retrEntry);
assertTrue("Checking entry " + i + " for equality", origEntry.equals(retrEntry));
}
lh.close();
} catch (BKException e) {
LOG.error("Test failed", e);
fail("Test failed due to BookKeeper exception");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.error("Test failed", e);
fail("Test failed due to interruption");
}
}
Aggregations