use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.
the class ForceReadOnlyBookieTest method testBookieForceStartAsReadOnly.
/**
* Check force start readonly bookie.
*/
@Test
public void testBookieForceStartAsReadOnly() throws Exception {
// create ledger, add entries
LedgerHandle ledger = bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
for (int i = 0; i < 10; i++) {
ledger.addEntry("data".getBytes());
}
ledger.close();
LOG.info("successed prepare");
// start bookie 1 as readonly
bsConfs.get(1).setReadOnlyModeEnabled(true);
bsConfs.get(1).setForceReadOnlyBookie(true);
restartBookies();
Bookie bookie = bs.get(1).getBookie();
assertTrue("Bookie should be running and in readonly mode", bookie.isRunning() && bookie.isReadOnly());
LOG.info("successed force start ReadOnlyBookie");
// Check new bookie with readonly mode enabled.
File[] ledgerDirs = bsConfs.get(1).getLedgerDirs();
assertEquals("Only one ledger dir should be present", 1, ledgerDirs.length);
// kill the writable bookie
killBookie(0);
// read entry from read only bookie
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()));
}
LOG.info("successed read entry from ReadOnlyBookie");
// test will not transfer to Writable mode.
LedgerDirsManager ledgerDirsManager = bookie.getLedgerDirsManager();
ledgerDirsManager.addToWritableDirs(new File(ledgerDirs[0], "current"), true);
assertTrue("Bookie should be running and in readonly mode", bookie.isRunning() && bookie.isReadOnly());
LOG.info("successed: bookie still readonly");
}
use of org.apache.bookkeeper.client.LedgerEntry 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.LedgerEntry in project bookkeeper by apache.
the class TestAutoRecoveryAlongWithBookieServers method testAutoRecoveryAlongWithBookieServers.
/**
* Tests that the auto recovery service along with Bookie servers itself.
*/
@Test
public void testAutoRecoveryAlongWithBookieServers() throws Exception {
LedgerHandle lh = bkc.createLedger(3, 3, BookKeeper.DigestType.CRC32, "testpasswd".getBytes());
byte[] testData = "testBuiltAutoRecovery".getBytes();
for (int i = 0; i < 10; i++) {
lh.addEntry(testData);
}
lh.close();
BookieSocketAddress replicaToKill = LedgerHandleAdapter.getLedgerMetadata(lh).getEnsembles().get(0L).get(0);
killBookie(replicaToKill);
BookieSocketAddress newBkAddr = startNewBookieAndReturnAddress();
while (ReplicationTestUtil.isLedgerInUnderReplication(zkc, lh.getId(), basePath)) {
Thread.sleep(100);
}
// Killing all bookies except newly replicated bookie
Set<Entry<Long, ArrayList<BookieSocketAddress>>> entrySet = LedgerHandleAdapter.getLedgerMetadata(lh).getEnsembles().entrySet();
for (Entry<Long, ArrayList<BookieSocketAddress>> entry : entrySet) {
ArrayList<BookieSocketAddress> bookies = entry.getValue();
for (BookieSocketAddress bookie : bookies) {
if (bookie.equals(newBkAddr)) {
continue;
}
killBookie(bookie);
}
}
// Should be able to read the entries from 0-9
LedgerHandle lhs = bkc.openLedgerNoRecovery(lh.getId(), BookKeeper.DigestType.CRC32, "testpasswd".getBytes());
Enumeration<LedgerEntry> entries = lhs.readEntries(0, 9);
assertTrue("Should have the elements", entries.hasMoreElements());
while (entries.hasMoreElements()) {
LedgerEntry entry = entries.nextElement();
assertEquals("testBuiltAutoRecovery", new String(entry.getEntry()));
}
}
use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.
the class MD5DigestBookKeeperTest 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) {
bookieConf.setBookieAuthProviderFactoryClass(SASLBookieAuthProviderFactory.class.getName());
bookieConf.setProperty(JAAS_CLIENT_ALLOWED_IDS, ".*hd.*");
}
clientConf.setClientAuthProviderFactoryClass(SASLClientProviderFactory.class.getName());
restartBookies();
try (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.LedgerEntry in project bookkeeper by apache.
the class ReadOnlyBookieTest method testBookieShouldServeAsReadOnly.
/**
* Check readonly bookie.
*/
@Test
public void testBookieShouldServeAsReadOnly() 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());
}
// Now add the current ledger dir to filled dirs list
ledgerDirsManager.addToFilledDirs(new File(ledgerDirs[0], "current"));
try {
ledger.addEntry("data".getBytes());
fail("Should fail to add entry since there isn't enough bookies alive.");
} catch (BKException.BKNotEnoughBookiesException e) {
// Expected
}
assertTrue("Bookie should be running and 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()));
}
}
Aggregations