|
@@ -21,13 +21,19 @@ package org.apache.zookeeper.server;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.EOFException;
|
|
|
import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
import java.text.DateFormat;
|
|
|
import java.util.Date;
|
|
|
-
|
|
|
-import org.apache.log4j.Logger;
|
|
|
+import java.util.zip.Adler32;
|
|
|
+import java.util.zip.Checksum;
|
|
|
|
|
|
import org.apache.jute.BinaryInputArchive;
|
|
|
import org.apache.jute.InputArchive;
|
|
|
+import org.apache.jute.Record;
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+import org.apache.zookeeper.server.persistence.FileHeader;
|
|
|
+import org.apache.zookeeper.server.persistence.FileTxnLog;
|
|
|
+import org.apache.zookeeper.server.util.SerializeUtils;
|
|
|
import org.apache.zookeeper.txn.TxnHeader;
|
|
|
|
|
|
public class LogFormatter {
|
|
@@ -43,23 +49,51 @@ public class LogFormatter {
|
|
|
}
|
|
|
FileInputStream fis = new FileInputStream(args[0]);
|
|
|
BinaryInputArchive logStream = BinaryInputArchive.getArchive(fis);
|
|
|
+ FileHeader fhdr = new FileHeader();
|
|
|
+ fhdr.deserialize(logStream, "fileheader");
|
|
|
+
|
|
|
+ if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
|
|
|
+ System.err.println("Invalid magic number for " + args[0]);
|
|
|
+ System.exit(2);
|
|
|
+ }
|
|
|
+ System.out.println("ZooKeeper Transactional Log File with dbid "
|
|
|
+ + fhdr.getDbid() + " txnlog format version "
|
|
|
+ + fhdr.getVersion());
|
|
|
+
|
|
|
+ int count = 0;
|
|
|
while (true) {
|
|
|
- byte[] bytes = logStream.readBuffer("txnEntry");
|
|
|
+ long crcValue;
|
|
|
+ byte[] bytes;
|
|
|
+ try {
|
|
|
+ crcValue = logStream.readLong("crcvalue");
|
|
|
+
|
|
|
+ bytes = logStream.readBuffer("txnEntry");
|
|
|
+ } catch (EOFException e) {
|
|
|
+ System.out.println("EOF reached after " + count + " txns.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
if (bytes.length == 0) {
|
|
|
// Since we preallocate, we define EOF to be an
|
|
|
// empty transaction
|
|
|
- throw new EOFException();
|
|
|
+ System.out.println("EOF reached after " + count + " txns.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Checksum crc = new Adler32();
|
|
|
+ crc.update(bytes, 0, bytes.length);
|
|
|
+ if (crcValue != crc.getValue()) {
|
|
|
+ throw new IOException("CRC doesn't match " + crcValue +
|
|
|
+ " vs " + crc.getValue());
|
|
|
}
|
|
|
- InputArchive ia = BinaryInputArchive
|
|
|
- .getArchive(new ByteArrayInputStream(bytes));
|
|
|
+ InputArchive iab = BinaryInputArchive
|
|
|
+ .getArchive(new ByteArrayInputStream(bytes));
|
|
|
TxnHeader hdr = new TxnHeader();
|
|
|
- hdr.deserialize(ia, "hdr");
|
|
|
+ SerializeUtils.deserializeTxn(iab, hdr);
|
|
|
System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT,
|
|
|
DateFormat.LONG).format(new Date(hdr.getTime()))
|
|
|
+ " session 0x"
|
|
|
+ Long.toHexString(hdr.getClientId())
|
|
|
- + ":"
|
|
|
- + hdr.getCxid()
|
|
|
+ + " cxid 0x"
|
|
|
+ + Long.toHexString(hdr.getCxid())
|
|
|
+ " zxid 0x"
|
|
|
+ Long.toHexString(hdr.getZxid())
|
|
|
+ " " + TraceFormatter.op2String(hdr.getType()));
|
|
@@ -67,6 +101,7 @@ public class LogFormatter {
|
|
|
LOG.error("Last transaction was partial.");
|
|
|
throw new EOFException("Last transaction was partial.");
|
|
|
}
|
|
|
+ count++;
|
|
|
}
|
|
|
}
|
|
|
}
|