/** * Proposal returned by this iterator has request part set to null, since * it is not used for follower sync-up. */ @Override public Proposal next() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); Proposal p = new Proposal(); try { TxnHeader hdr = itr.getHeader(); Record txn = itr.getTxn(); hdr.serialize(boa, "hdr"); if (txn != null) { txn.serialize(boa, "txn"); } baos.close(); QuorumPacket pp = new QuorumPacket(Leader.PROPOSAL, itr.getHeader() .getZxid(), baos.toByteArray(), null); p.packet = pp; p.request = null; // This is the only place that can throw IO exception hasNext = itr.next(); } catch (IOException e) { LOG.error("Unable to read txnlog from disk", e); hasNext = false; } return p; }
/** * get the last zxid that was logged in the transaction logs * @return the last zxid logged in the transaction logs */ public long getLastLoggedZxid() { File[] files = getLogFiles(logDir.listFiles(), 0); long maxLog=files.length>0? Util.getZxidFromName(files[files.length-1].getName(),LOG_FILE_PREFIX):-1; // if a log file is more recent we must scan it to find // the highest zxid long zxid = maxLog; TxnIterator itr = null; try { FileTxnLog txn = new FileTxnLog(logDir); itr = txn.read(maxLog); while (true) { if(!itr.next()) break; TxnHeader hdr = itr.getHeader(); zxid = hdr.getZxid(); } } catch (IOException e) { LOG.warn("Unexpected exception", e); } finally { close(itr); } return zxid; }
hdr = itr.getHeader(); if (hdr == null) {
/** * get the last zxid that was logged in the transaction logs * @return the last zxid logged in the transaction logs */ public long getLastLoggedZxid() { File[] files = getLogFiles(logDir.listFiles(), 0); long maxLog=files.length>0? Util.getZxidFromName(files[files.length-1].getName(),"log"):-1; // if a log file is more recent we must scan it to find // the highest zxid long zxid = maxLog; try { FileTxnLog txn = new FileTxnLog(logDir); TxnIterator itr = txn.read(maxLog); while (true) { if(!itr.next()) break; TxnHeader hdr = itr.getHeader(); zxid = hdr.getZxid(); } } catch (IOException e) { LOG.warn("Unexpected exception", e); } return zxid; }
hdr = itr.getHeader(); if (hdr == null) {
/** * get the last zxid that was logged in the transaction logs * @return the last zxid logged in the transaction logs */ public long getLastLoggedZxid() { File[] files = getLogFiles(logDir.listFiles(), 0); long maxLog=files.length>0? Util.getZxidFromName(files[files.length-1].getName(),LOG_FILE_PREFIX):-1; // if a log file is more recent we must scan it to find // the highest zxid long zxid = maxLog; TxnIterator itr = null; try { FileTxnLog txn = new FileTxnLog(logDir); itr = txn.read(maxLog); while (true) { if(!itr.next()) break; TxnHeader hdr = itr.getHeader(); zxid = hdr.getZxid(); } } catch (IOException e) { LOG.warn("Unexpected exception", e); } finally { close(itr); } return zxid; }
Assert.assertEquals(secondStartZxid, itr.getHeader().getZxid()); Assert.assertEquals(secondStartZxid, itr.getHeader().getZxid()); Assert.assertTrue(itr.next()); long nextZxid = itr.getHeader().getZxid(); Assert.assertEquals(secondStartZxid, itr.getHeader().getZxid()); Assert.assertEquals(secondStartZxid, itr.getHeader().getZxid()); Assert.assertTrue(itr.next()); nextZxid = itr.getHeader().getZxid(); itr = txnLog.read(nextZxid, false); Assert.assertEquals(secondStartZxid, itr.getHeader().getZxid());
hdr = itr.getHeader(); if (hdr == null) {
@Test public void testTruncationStreamReset() throws Exception { File tmpdir = ClientBase.createTmpDir(); FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpdir, tmpdir); ZKDatabase zkdb = new ZKDatabase(snaplog); // make sure to snapshot, so that we have something there when // truncateLog reloads the db snaplog.save(zkdb.getDataTree(), zkdb.getSessionWithTimeOuts(), false); for (int i = 1; i <= 100; i++) { append(zkdb, i); } zkdb.truncateLog(1); append(zkdb, 200); zkdb.close(); // verify that the truncation and subsequent append were processed // correctly FileTxnLog txnlog = new FileTxnLog(new File(tmpdir, "version-2")); TxnIterator iter = txnlog.read(1); TxnHeader hdr = iter.getHeader(); Record txn = iter.getTxn(); Assert.assertEquals(1, hdr.getZxid()); Assert.assertTrue(txn instanceof SetDataTxn); iter.next(); hdr = iter.getHeader(); txn = iter.getTxn(); Assert.assertEquals(200, hdr.getZxid()); Assert.assertTrue(txn instanceof SetDataTxn); iter.close(); ClientBase.recursiveDelete(tmpdir); }
TxnHeader hdr; do { hdr = itr.getHeader(); expectedZxid++; Assert.assertTrue("not the same transaction. lastZxid=" + lastZxid + ", zxid=" + hdr.getZxid(), lastZxid != hdr.getZxid());
Pair<Long, Long> getFirstLastZxid(File logFile) throws IOException { File tmp = createTmpDir(); Files.copy(logFile.toPath(), new File(tmp, "log.0").toPath()); FileTxnLog txnLog = new FileTxnLog(tmp); TxnLog.TxnIterator it = txnLog.read(0); long firstZxid = it.getHeader().getZxid(); long lastZxid = firstZxid; while (it.next()) { lastZxid = it.getHeader().getZxid(); } txnLog.close(); rmr(tmp); return new Pair<Long, Long>(firstZxid, lastZxid); }
public TxnLogProposalIterator(TxnIterator itr) { if (itr != null) { this.itr = itr; hasNext = (itr.getHeader() != null); } }