Search in sources :

Example 6 with TxnHeader

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);
    }
}
Also used : ErrorTxn(org.apache.zookeeper.txn.ErrorTxn) DataTree(org.apache.zookeeper.server.DataTree) Request(org.apache.zookeeper.server.Request) File(java.io.File) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.Test)

Example 7 with TxnHeader

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();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) FileTxnIterator(org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator) FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) TxnIterator(org.apache.zookeeper.server.persistence.TxnLog.TxnIterator) FileTxnIterator(org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.Test)

Example 8 with TxnHeader

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);
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) CreateTxn(org.apache.zookeeper.txn.CreateTxn) FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) Record(org.apache.jute.Record) File(java.io.File) FileHeader(org.apache.zookeeper.server.persistence.FileHeader) FileInputStream(java.io.FileInputStream) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.Test)

Example 9 with TxnHeader

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();
}
Also used : Request(org.apache.zookeeper.server.Request) Record(org.apache.jute.Record) SetDataTxn(org.apache.zookeeper.txn.SetDataTxn) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 10 with TxnHeader

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Checksum(java.util.zip.Checksum) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) EOFException(java.io.EOFException) Record(org.apache.jute.Record) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) Adler32(java.util.zip.Adler32) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Aggregations

TxnHeader (org.apache.zookeeper.txn.TxnHeader)24 Record (org.apache.jute.Record)15 IOException (java.io.IOException)13 File (java.io.File)9 SetDataTxn (org.apache.zookeeper.txn.SetDataTxn)8 ByteBuffer (java.nio.ByteBuffer)7 CreateTxn (org.apache.zookeeper.txn.CreateTxn)6 Test (org.junit.Test)6 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)5 KeeperException (org.apache.zookeeper.KeeperException)5 Request (org.apache.zookeeper.server.Request)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 BinaryInputArchive (org.apache.jute.BinaryInputArchive)4 Stat (org.apache.zookeeper.data.Stat)4 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)4 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)4 QuorumVerifier (org.apache.zookeeper.server.quorum.flexible.QuorumVerifier)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 EOFException (java.io.EOFException)3 ArrayList (java.util.ArrayList)3