use of org.apache.zookeeper.server.persistence.FileHeader in project zookeeper by apache.
the class LoadFromLogTest method testPad.
/**
* Simulates ZOOKEEPER-1069 and verifies that flush() before padLogFile
* fixes it.
*/
@Test
public void testPad() throws Exception {
File tmpDir = ClientBase.createTmpDir();
FileTxnLog txnLog = new FileTxnLog(tmpDir);
TxnHeader txnHeader = new TxnHeader(0xabcd, 0x123, 0x123, Time.currentElapsedTime(), OpCode.create);
Record txn = new CreateTxn("/Test", new byte[0], null, false, 1);
txnLog.append(txnHeader, txn);
FileInputStream in = new FileInputStream(tmpDir.getPath() + "/log." + Long.toHexString(txnHeader.getZxid()));
BinaryInputArchive ia = BinaryInputArchive.getArchive(in);
FileHeader header = new FileHeader();
header.deserialize(ia, "fileheader");
LOG.info("Received magic : " + header.getMagic() + " Expected : " + FileTxnLog.TXNLOG_MAGIC);
Assert.assertTrue("Missing magic number ", header.getMagic() == FileTxnLog.TXNLOG_MAGIC);
}
use of org.apache.zookeeper.server.persistence.FileHeader in project zookeeper by apache.
the class LogChopper method chop.
public static boolean chop(InputStream is, OutputStream os, long zxid) throws IOException {
BinaryInputArchive logStream = BinaryInputArchive.getArchive(is);
BinaryOutputArchive choppedStream = BinaryOutputArchive.getArchive(os);
FileHeader fhdr = new FileHeader();
fhdr.deserialize(logStream, "fileheader");
if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
System.err.println("Invalid magic number in txn log file");
return false;
}
System.out.println("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid() + " txnlog format version " + fhdr.getVersion());
fhdr.serialize(choppedStream, "fileheader");
int count = 0;
boolean hasZxid = false;
long previousZxid = -1;
while (true) {
long crcValue;
byte[] bytes;
try {
crcValue = logStream.readLong("crcvalue");
bytes = logStream.readBuffer("txnEntry");
} catch (EOFException e) {
System.out.println("EOF reached after " + count + " txns.");
// returning false because nothing was chopped
return false;
}
if (bytes.length == 0) {
// Since we preallocate, we define EOF to be an
// empty transaction
System.out.println("EOF reached after " + count + " txns.");
// returning false because nothing was chopped
return false;
}
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());
}
TxnLogEntry entry = SerializeUtils.deserializeTxn(bytes);
TxnHeader hdr = entry.getHeader();
Record txn = entry.getTxn();
if (logStream.readByte("EOR") != 'B') {
System.out.println("Last transaction was partial.");
throw new EOFException("Last transaction was partial.");
}
final long txnZxid = hdr.getZxid();
if (txnZxid == zxid) {
hasZxid = true;
}
// logging the gap to make the inconsistency investigation easier
if (previousZxid != -1 && txnZxid != previousZxid + 1) {
long txnEpoch = ZxidUtils.getEpochFromZxid(txnZxid);
long txnCounter = ZxidUtils.getCounterFromZxid(txnZxid);
long previousEpoch = ZxidUtils.getEpochFromZxid(previousZxid);
if (txnEpoch == previousEpoch) {
System.out.println(String.format("There is intra-epoch gap between %x and %x", previousZxid, txnZxid));
} else if (txnCounter != 1) {
System.out.println(String.format("There is inter-epoch gap between %x and %x", previousZxid, txnZxid));
}
}
previousZxid = txnZxid;
if (txnZxid > zxid) {
if (count == 0 || !hasZxid) {
System.out.println(String.format("This log does not contain zxid %x", zxid));
return false;
}
System.out.println(String.format("Chopping at %x new log has %d records", zxid, count));
return true;
}
choppedStream.writeLong(crcValue, "crcvalue");
choppedStream.writeBuffer(bytes, "txnEntry");
choppedStream.writeByte((byte) 'B', "EOR");
count++;
}
}
use of org.apache.zookeeper.server.persistence.FileHeader in project zookeeper by apache.
the class PurgeTxnTest method makeValidSnapshot.
private void makeValidSnapshot(File snapFile) throws IOException {
SnapStream.setStreamMode(SnapStream.StreamMode.CHECKED);
CheckedOutputStream os = SnapStream.getOutputStream(snapFile, true);
OutputArchive oa = BinaryOutputArchive.getArchive(os);
FileHeader header = new FileHeader(FileSnap.SNAP_MAGIC, 2, 1);
header.serialize(oa, "fileheader");
SnapStream.sealStream(os, oa);
os.flush();
os.close();
assertTrue(SnapStream.isValidSnapshot(snapFile));
}
use of org.apache.zookeeper.server.persistence.FileHeader in project zookeeper by apache.
the class LogFormatter method main.
/**
* @param args
*/
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("USAGE: LogFormatter log_file");
System.exit(2);
}
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) {
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
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());
}
TxnHeader hdr = new TxnHeader();
Record txn = SerializeUtils.deserializeTxn(bytes, hdr);
System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(new Date(hdr.getTime())) + " session 0x" + Long.toHexString(hdr.getClientId()) + " cxid 0x" + Long.toHexString(hdr.getCxid()) + " zxid 0x" + Long.toHexString(hdr.getZxid()) + " " + TraceFormatter.op2String(hdr.getType()) + " " + txn);
if (logStream.readByte("EOR") != 'B') {
LOG.error("Last transaction was partial.");
throw new EOFException("Last transaction was partial.");
}
count++;
}
}
use of org.apache.zookeeper.server.persistence.FileHeader in project zookeeper by apache.
the class LoadFromLogNoServerTest method testPad.
/**
* Simulates ZOOKEEPER-1069 and verifies that flush() before padLogFile
* fixes it.
*/
@Test
public void testPad() throws Exception {
File tmpDir = ClientBase.createTmpDir();
FileTxnLog txnLog = new FileTxnLog(tmpDir);
TxnHeader txnHeader = new TxnHeader(0xabcd, 0x123, 0x123, Time.currentElapsedTime(), ZooDefs.OpCode.create);
Record txn = new CreateTxn("/Test", new byte[0], null, false, 1);
txnLog.append(txnHeader, txn);
FileInputStream in = new FileInputStream(tmpDir.getPath() + "/log." + Long.toHexString(txnHeader.getZxid()));
BinaryInputArchive ia = BinaryInputArchive.getArchive(in);
FileHeader header = new FileHeader();
header.deserialize(ia, "fileheader");
LOG.info("Received magic : {} Expected : {}", header.getMagic(), FileTxnLog.TXNLOG_MAGIC);
assertTrue(header.getMagic() == FileTxnLog.TXNLOG_MAGIC, "Missing magic number ");
}
Aggregations