public void testReadWriteZero() throws IOException { try { // Create a BookKeeper client and a ledger bkc = new BookKeeper("127.0.0.1"); lh = bkc.createLedger(ledgerPassword); bkc.initMessageDigest("SHA1"); ledgerId = lh.getId(); LOG.info("Ledger ID: " + lh.getId()); for (int i = 0; i < numEntriesToWrite; i++) { bkc.addEntry(lh, new byte[0]); } /* * Write a non-zero entry */ ByteBuffer entry = ByteBuffer.allocate(4); entry.putInt(rng.nextInt(maxInt)); entry.position(0); entries.add(entry.array()); bkc.addEntry(lh, entry.array()); bkc.closeLedger(lh); lh = bkc.openLedger(ledgerId, ledgerPassword); LOG.debug("Number of entries written: " + lh.getLast()); assertTrue("Verifying number of entries written", lh.getLast() == (numEntriesToWrite + 1)); ls = bkc.readEntries(lh, 0, numEntriesToWrite - 1); int i = 0; while (ls.hasMoreElements()) { ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry()); LOG.debug("Length of result: " + result.capacity()); assertTrue("Checking if entry " + i + " has zero bytes", result.capacity() == 0); } bkc.closeLedger(lh); } catch (KeeperException e) { e.printStackTrace(); } catch (BKException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } }
public void testReadWriteSyncSingleClient() throws IOException { try { // Create a BookKeeper client and a ledger bkc = new BookKeeper("127.0.0.1"); lh = bkc.createLedger(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()); bkc.addEntry(lh, entry.array()); } bkc.closeLedger(lh); lh = bkc.openLedger(ledgerId, ledgerPassword); LOG.debug("Number of entries written: " + lh.getLast()); assertTrue("Verifying number of entries written", lh.getLast() == numEntriesToWrite); ls = bkc.readEntries(lh, 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)); } bkc.closeLedger(lh); } catch (KeeperException e) { e.printStackTrace(); } catch (BKException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } }
/* * (non-Javadoc) * * @see * org.apache.bookkeeper.client.AsyncCallback.OpenCallback#openComplete(int, * org.apache.bookkeeper.client.LedgerHandle, java.lang.Object) */ @Override public void openComplete(int rc, LedgerHandle ledger, Object ctx) { OpReadEntry opReadEntry = (OpReadEntry) ctx; if (rc != BKException.Code.OK) { opReadEntry.failed(new ManagedLedgerException(BKException.create(rc))); return; } log.debug("[{}] Successfully opened ledger {} for reading", name, ledger.getId()); internalReadFromLedger(ledger, opReadEntry); }
/* * (non-Javadoc) * * @see * org.apache.bookkeeper.client.AsyncCallback.CreateCallback#createComplete * (int, org.apache.bookkeeper.client.LedgerHandle, java.lang.Object) */ @Override public synchronized void createComplete(int rc, LedgerHandle lh, Object ctx) { log.debug("[{}] createComplete rc={} ledger={}", va(name, rc, lh != null ? lh.getId() : -1)); if (rc != BKException.Code.OK) { state = State.ClosedLedger; log.error("[{}] Error creating ledger rc={} {}", va(name, rc, BKException.getMessage(rc))); ManagedLedgerException status = new ManagedLedgerException(BKException.create(rc)); // Empty the list of pending requests and make all of them fail while (!pendingAddEntries.isEmpty()) { pendingAddEntries.poll().failed(status); } } else { log.debug("[{}] Successfully created new ledger {}", name, lh.getId()); ledgers.put(lh.getId(), new LedgerStat(lh.getId(), 0, 0)); currentLedger = lh; currentLedgerEntries = 0; currentLedgerSize = 0; MetaStoreCallback<Void> cb = new MetaStoreCallback<Void>() { public void operationComplete(Void v, Version version) { updateLedgersIdsComplete(version); } public void operationFailed(MetaStoreException e) { log.warn("Error updating meta data with the new list of ledgers"); while (!pendingAddEntries.isEmpty()) { pendingAddEntries.poll().failed(e); } } }; store.asyncUpdateLedgerIds(name, ledgers.values(), ledgersVersion, cb); } }
public void force() throws IOException { SyncCounter counter = lastCounter.get(); if (counter != null) { try { counter.block(0); if (counter.getrc() != BKException.Code.OK) { throw new IOException("Force append failed", BKException.create(counter.getrc())); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } finally { lastCounter.remove(); } } }
/* * (non-Javadoc) * * @see * org.apache.bookkeeper.client.AsyncCallback.ReadCallback#readComplete(int, * org.apache.bookkeeper.client.LedgerHandle, java.util.Enumeration, * java.lang.Object) */ @Override public void readComplete( int rc, LedgerHandle lh, Enumeration<LedgerEntry> entriesEnum, Object ctx) { OpReadEntry opReadEntry = (OpReadEntry) ctx; if (rc != BKException.Code.OK) { log.warn( "[{}] read failed from ledger {} at position:{}", va(name, lh.getId(), opReadEntry.readPosition)); opReadEntry.failed(new ManagedLedgerException(BKException.create(rc))); return; } List<Entry> entries = opReadEntry.entries; while (entriesEnum.hasMoreElements()) entries.add(new EntryImpl(entriesEnum.nextElement())); long lastEntry = entries.get(entries.size() - 1).getPosition().getEntryId(); // Get the "next read position", we need to advance the position taking // care of ledgers boundaries Position nextReadPosition; if (lastEntry < lh.getLastAddConfirmed()) { nextReadPosition = new Position(lh.getId(), lastEntry + 1); } else { // Move to next ledger Long nextLedgerId = ledgers.ceilingKey(lh.getId() + 1); if (nextLedgerId == null) { // We are already in the last ledger nextReadPosition = new Position(lh.getId(), lastEntry + 1); } else { nextReadPosition = new Position(nextLedgerId, 0); } } opReadEntry.nextReadPosition = nextReadPosition; opReadEntry.succeeded(); }
@Test public void testSyncReadAsyncWriteStringsSingleClient() throws IOException { LOG.info("TEST READ WRITE STRINGS MIXED SINGLE CLIENT"); String charset = "utf-8"; LOG.debug("Default charset: " + Charset.defaultCharset()); try { // Create a BookKeeper client and a ledger bkc = new BookKeeper("127.0.0.1"); lh = bkc.createLedger(ledgerPassword); bkc.initMessageDigest("SHA1"); ledgerId = lh.getId(); LOG.info("Ledger ID: " + lh.getId()); for (int i = 0; i < numEntriesToWrite; i++) { int randomInt = rng.nextInt(maxInt); byte[] entry = new String(Integer.toString(randomInt)).getBytes(charset); entries.add(entry); bkc.asyncAddEntry(lh, entry, this, sync); } // wait for all entries to be acknowledged synchronized (sync) { if (sync.counter < numEntriesToWrite) { LOG.debug("Entries counter = " + sync.counter); sync.wait(); } } LOG.debug("*** ASYNC WRITE COMPLETED ***"); // close ledger bkc.closeLedger(lh); // *** WRITING PART COMPLETED // READ PART BEGINS *** // open ledger lh = bkc.openLedger(ledgerId, ledgerPassword); LOG.debug("Number of entries written: " + lh.getLast()); assertTrue("Verifying number of entries written", lh.getLast() == numEntriesToWrite); // read entries ls = bkc.readEntries(lh, 0, numEntriesToWrite - 1); assertTrue("Checking number of read entries", ls.size() == numEntriesToWrite); LOG.debug("*** SYNC READ COMPLETED ***"); // at this point, LedgerSequence ls is filled with the returned values int i = 0; while (ls.hasMoreElements()) { byte[] origEntryBytes = entries.get(i++); byte[] retrEntryBytes = ls.nextElement().getEntry(); LOG.debug("Original byte entry size: " + origEntryBytes.length); LOG.debug("Saved byte entry size: " + retrEntryBytes.length); String origEntry = new String(origEntryBytes, charset); String retrEntry = new String(retrEntryBytes, charset); LOG.debug("Original entry: " + origEntry); LOG.debug("Retrieved entry: " + retrEntry); assertTrue("Checking entry " + i + " for equality", origEntry.equals(retrEntry)); } bkc.closeLedger(lh); } catch (KeeperException e) { e.printStackTrace(); } catch (BKException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } }
@Test public void testReadWriteAsyncSingleClient() throws IOException { try { // Create a BookKeeper client and a ledger bkc = new BookKeeper("127.0.0.1"); lh = bkc.createLedger(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()); entriesSize.add(entry.array().length); bkc.asyncAddEntry(lh, entry.array(), this, sync); } // wait for all entries to be acknowledged synchronized (sync) { if (sync.counter < numEntriesToWrite) { LOG.debug("Entries counter = " + sync.counter); sync.wait(); } } LOG.debug("*** WRITE COMPLETED ***"); // close ledger bkc.closeLedger(lh); // *** WRITING PART COMPLETED // READ PART BEGINS *** // open ledger lh = bkc.openLedger(ledgerId, ledgerPassword); LOG.debug("Number of entries written: " + lh.getLast()); assertTrue("Verifying number of entries written", lh.getLast() == numEntriesToWrite); // read entries bkc.asyncReadEntries(lh, 0, numEntriesToWrite - 1, this, (Object) sync); synchronized (sync) { while (sync.value == false) { sync.wait(); } } assertTrue("Checking number of read entries", ls.size() == numEntriesToWrite); LOG.debug("*** READ COMPLETED ***"); // at this point, LedgerSequence ls is filled with the returned values int i = 0; while (ls.hasMoreElements()) { ByteBuffer origbb = ByteBuffer.wrap(entries.get(i)); Integer origEntry = origbb.getInt(); byte[] entry = ls.nextElement().getEntry(); ByteBuffer result = ByteBuffer.wrap(entry); 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)); assertTrue( "Checking entry " + i + " for size", entry.length == entriesSize.get(i).intValue()); i++; } bkc.closeLedger(lh); } catch (KeeperException e) { e.printStackTrace(); } catch (BKException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } }
void auxTestReadWriteAsyncSingleClient(BookieServer bs) throws IOException { SyncObj sync = new SyncObj(); try { // Create a ledger lh = bkc.createLedger(3, 2, digestType, ledgerPassword); 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()); entriesSize.add(entry.array().length); lh.asyncAddEntry(entry.array(), this, sync); } LOG.info("Wrote " + numEntriesToWrite + " and now going to fail bookie."); // Bookie fail bs.shutdown(); // wait for all entries to be acknowledged synchronized (sync) { while (sync.counter < numEntriesToWrite) { LOG.debug("Entries counter = " + sync.counter); sync.wait(10000); assertFalse("Failure occurred during write", sync.failureOccurred); } } LOG.debug("*** WRITE COMPLETE ***"); // close ledger lh.close(); // *** WRITING PART COMPLETE // READ PART BEGINS *** // open ledger bkc.close(); bkc = new BookKeeperTestClient(baseClientConf); lh = bkc.openLedger(ledgerId, digestType, ledgerPassword); LOG.debug("Number of entries written: " + (lh.getLastAddConfirmed() + 1)); assertTrue( "Verifying number of entries written", lh.getLastAddConfirmed() == (numEntriesToWrite - 1)); // read entries lh.asyncReadEntries(0, numEntriesToWrite - 1, this, sync); synchronized (sync) { int i = 0; sync.wait(10000); assertFalse("Failure occurred during read", sync.failureOccurred); assertTrue("Haven't received entries", sync.value); } LOG.debug("*** READ COMPLETE ***"); // at this point, Enumeration<LedgerEntry> ls is filled with the returned // values int i = 0; while (sync.ls.hasMoreElements()) { ByteBuffer origbb = ByteBuffer.wrap(entries.get(i)); Integer origEntry = origbb.getInt(); byte[] entry = sync.ls.nextElement().getEntry(); ByteBuffer result = ByteBuffer.wrap(entry); Integer retrEntry = result.getInt(); LOG.debug("Retrieved entry: " + i); assertTrue("Checking entry " + i + " for equality", origEntry.equals(retrEntry)); assertTrue( "Checking entry " + i + " for size", entry.length == entriesSize.get(i).intValue()); i++; } assertTrue("Checking number of read entries", i == numEntriesToWrite); LOG.info("Verified that entries are ok, and now closing ledger"); lh.close(); } catch (KeeperException e) { LOG.error("Caught KeeperException", e); fail(e.toString()); } catch (BKException e) { LOG.error("Caught BKException", e); fail(e.toString()); } catch (InterruptedException e) { LOG.error("Caught InterruptedException", e); fail(e.toString()); } }