use of org.apache.zookeeper.txn.TxnHeader in project zookeeper by apache.
the class Zab1_0Test method testInitialAcceptedCurrent.
@Test
public void testInitialAcceptedCurrent() throws Exception {
File tmpDir = File.createTempFile("test", ".dir", testData);
tmpDir.delete();
tmpDir.mkdir();
try {
FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
File version2 = new File(tmpDir, "version-2");
version2.mkdir();
logFactory.save(new DataTree(), new ConcurrentHashMap<Long, Integer>());
long zxid = ZxidUtils.makeZxid(3, 3);
logFactory.append(new Request(1, 1, ZooDefs.OpCode.error, new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error), new ErrorTxn(1), zxid));
logFactory.commit();
ZKDatabase zkDb = new ZKDatabase(logFactory);
QuorumPeer peer = new QuorumPeer();
peer.setZKDatabase(zkDb);
peer.setTxnFactory(logFactory);
peer.getLastLoggedZxid();
Assert.assertEquals(3, peer.getAcceptedEpoch());
Assert.assertEquals(3, peer.getCurrentEpoch());
Assert.assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.CURRENT_EPOCH_FILENAME))));
Assert.assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.ACCEPTED_EPOCH_FILENAME))));
} finally {
TestUtils.deleteFileRecursively(tmpDir);
}
}
use of org.apache.zookeeper.txn.TxnHeader in project zookeeper by apache.
the class LoadFromLogTest method testLoad.
/**
* test that all transactions from the Log are loaded, and only once
* @throws Exception an exception might be thrown here
*/
@Test
public void testLoad() throws Exception {
final String hostPort = HOST + PortAssignment.unique();
// setup a single server cluster
File tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
SyncRequestProcessor.setSnapCount(100);
final int PORT = Integer.parseInt(hostPort.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(hostPort, CONNECTION_TIMEOUT));
ZooKeeper zk = ClientBase.createZKClient(hostPort);
// generate some transactions that will get logged
try {
for (int i = 0; i < NUM_MESSAGES; i++) {
zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} finally {
zk.close();
}
f.shutdown();
Assert.assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(hostPort, CONNECTION_TIMEOUT));
// now verify that the FileTxnLog reads every transaction only once
File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
FileTxnLog txnLog = new FileTxnLog(logDir);
TxnIterator itr = txnLog.read(0);
// Check that storage space return some value
FileTxnIterator fileItr = (FileTxnIterator) itr;
long storageSize = fileItr.getStorageSize();
LOG.info("Txnlog size: " + storageSize + " bytes");
Assert.assertTrue("Storage size is greater than zero ", (storageSize > 0));
long expectedZxid = 0;
long lastZxid = 0;
TxnHeader hdr;
do {
hdr = itr.getHeader();
expectedZxid++;
Assert.assertTrue("not the same transaction. lastZxid=" + lastZxid + ", zxid=" + hdr.getZxid(), lastZxid != hdr.getZxid());
Assert.assertTrue("excepting next transaction. expected=" + expectedZxid + ", retreived=" + hdr.getZxid(), (hdr.getZxid() == expectedZxid));
lastZxid = hdr.getZxid();
} while (itr.next());
Assert.assertTrue("processed all transactions. " + expectedZxid + " == " + TOTAL_TRANSACTIONS, (expectedZxid == TOTAL_TRANSACTIONS));
zks.shutdown();
}
use of org.apache.zookeeper.txn.TxnHeader 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.txn.TxnHeader in project zookeeper by apache.
the class TruncateTest method append.
private void append(ZKDatabase zkdb, int i) throws IOException {
TxnHeader hdr = new TxnHeader(1, 1, i, 1, ZooDefs.OpCode.setData);
Record txn = new SetDataTxn("/foo" + i, new byte[0], 1);
Request req = new Request(0, 0, 0, hdr, txn, 0);
zkdb.append(req);
zkdb.commit();
}
use of org.apache.zookeeper.txn.TxnHeader in project exhibitor by soabase.
the class ZooKeeperLogParser method parse.
public void parse(LogEntryReceiver receiver) throws Exception {
if (!validHeader) {
throw new Exception("Invalid magic number for");
}
while (true) {
long crcValue;
byte[] bytes;
try {
crcValue = logStream.readLong("crcvalue");
bytes = logStream.readBuffer("txnEntry");
} catch (EOFException e) {
break;
}
if (bytes.length == 0) {
// empty transaction
break;
}
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 iab = BinaryInputArchive.getArchive(new ByteArrayInputStream(bytes));
TxnHeader hdr = new TxnHeader();
Record record = useOldDeserializeMethod ? (Record) deserializeTxnMethod.invoke(null, iab, hdr) : (Record) deserializeTxnMethod.invoke(null, bytes, hdr);
if (logStream.readByte("EOR") != 'B') {
// partial transaction
break;
}
receiver.receiveEntry(hdr, record);
}
}
Aggregations