Search in sources :

Example 1 with CreateTxn

use of org.apache.zookeeper.txn.CreateTxn in project zookeeper by apache.

the class LearnerTest method syncTest.

@Test
public void syncTest() throws Exception {
    File tmpFile = File.createTempFile("test", ".dir", testData);
    tmpFile.delete();
    try {
        FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpFile, tmpFile);
        SimpleLearner sl = new SimpleLearner(ftsl);
        long startZxid = sl.zk.getLastProcessedZxid();
        // Set up bogus streams
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos);
        sl.leaderOs = BinaryOutputArchive.getArchive(new ByteArrayOutputStream());
        // make streams and socket do something innocuous
        sl.bufferedOutput = new BufferedOutputStream(System.out);
        sl.sock = new Socket();
        // fake messages from the server
        QuorumPacket qp = new QuorumPacket(Leader.SNAP, 0, null, null);
        oa.writeRecord(qp, null);
        sl.zk.getZKDatabase().serializeSnapshot(oa);
        oa.writeString("BenWasHere", "signature");
        TxnHeader hdr = new TxnHeader(0, 0, 0, 0, ZooDefs.OpCode.create);
        CreateTxn txn = new CreateTxn("/foo", new byte[0], new ArrayList<ACL>(), false, sl.zk.getZKDatabase().getNode("/").stat.getCversion());
        ByteArrayOutputStream tbaos = new ByteArrayOutputStream();
        BinaryOutputArchive boa = BinaryOutputArchive.getArchive(tbaos);
        hdr.serialize(boa, "hdr");
        txn.serialize(boa, "txn");
        tbaos.close();
        qp = new QuorumPacket(Leader.PROPOSAL, 1, tbaos.toByteArray(), null);
        oa.writeRecord(qp, null);
        // setup the messages to be streamed to follower
        sl.leaderIs = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray()));
        try {
            sl.syncWithLeader(3);
        } catch (EOFException e) {
        }
        sl.zk.shutdown();
        sl = new SimpleLearner(ftsl);
        Assert.assertEquals(startZxid, sl.zk.getLastProcessedZxid());
    } finally {
        TestUtils.deleteFileRecursively(tmpFile);
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ACL(org.apache.zookeeper.data.ACL) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) CreateTxn(org.apache.zookeeper.txn.CreateTxn) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.Test)

Example 2 with CreateTxn

use of org.apache.zookeeper.txn.CreateTxn 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 3 with CreateTxn

use of org.apache.zookeeper.txn.CreateTxn in project exhibitor by soabase.

the class IndexBuilder method indexRecord.

private void indexRecord(TxnHeader header, Record record, AtomicInteger count, AtomicLong from, AtomicLong to) throws IOException {
    if (record instanceof CreateTxn) {
        CreateTxn createTxn = (CreateTxn) record;
        EntryTypes type = createTxn.getEphemeral() ? EntryTypes.CREATE_EPHEMERAL : EntryTypes.CREATE_PERSISTENT;
        Document document = makeDocument(header, type, count, from, to);
        addPath(document, createTxn.getPath());
        addData(document, createTxn.getData());
        writer.addDocument(document);
    } else if (record instanceof DeleteTxn) {
        DeleteTxn deleteTxn = (DeleteTxn) record;
        Document document = makeDocument(header, EntryTypes.DELETE, count, from, to);
        addPath(document, deleteTxn.getPath());
        writer.addDocument(document);
    } else if (record instanceof SetDataTxn) {
        SetDataTxn setDataTxn = (SetDataTxn) record;
        NumericField versionField = new NumericField(FieldNames.VERSION, Field.Store.YES, true);
        versionField.setIntValue(setDataTxn.getVersion());
        Document document = makeDocument(header, EntryTypes.SET_DATA, count, from, to);
        addPath(document, setDataTxn.getPath());
        addData(document, setDataTxn.getData());
        document.add(versionField);
        writer.addDocument(document);
    }
}
Also used : CreateTxn(org.apache.zookeeper.txn.CreateTxn) NumericField(org.apache.lucene.document.NumericField) SetDataTxn(org.apache.zookeeper.txn.SetDataTxn) Document(org.apache.lucene.document.Document) DeleteTxn(org.apache.zookeeper.txn.DeleteTxn)

Example 4 with CreateTxn

use of org.apache.zookeeper.txn.CreateTxn in project zookeeper by apache.

the class SerializeUtils method deserializeTxn.

public static Record deserializeTxn(byte[] txnBytes, TxnHeader hdr) throws IOException {
    final ByteArrayInputStream bais = new ByteArrayInputStream(txnBytes);
    InputArchive ia = BinaryInputArchive.getArchive(bais);
    hdr.deserialize(ia, "hdr");
    bais.mark(bais.available());
    Record txn = null;
    switch(hdr.getType()) {
        case OpCode.createSession:
            // This isn't really an error txn; it just has the same
            // format. The error represents the timeout
            txn = new CreateSessionTxn();
            break;
        case OpCode.closeSession:
            return null;
        case OpCode.create:
        case OpCode.create2:
            txn = new CreateTxn();
            break;
        case OpCode.createTTL:
            txn = new CreateTTLTxn();
            break;
        case OpCode.createContainer:
            txn = new CreateContainerTxn();
            break;
        case OpCode.delete:
        case OpCode.deleteContainer:
            txn = new DeleteTxn();
            break;
        case OpCode.reconfig:
        case OpCode.setData:
            txn = new SetDataTxn();
            break;
        case OpCode.setACL:
            txn = new SetACLTxn();
            break;
        case OpCode.error:
            txn = new ErrorTxn();
            break;
        case OpCode.multi:
            txn = new MultiTxn();
            break;
        default:
            throw new IOException("Unsupported Txn with type=%d" + hdr.getType());
    }
    if (txn != null) {
        try {
            txn.deserialize(ia, "txn");
        } catch (EOFException e) {
            // perhaps this is a V0 Create
            if (hdr.getType() == OpCode.create) {
                CreateTxn create = (CreateTxn) txn;
                bais.reset();
                CreateTxnV0 createv0 = new CreateTxnV0();
                createv0.deserialize(ia, "txn");
                // cool now make it V1. a -1 parentCVersion will
                // trigger fixup processing in processTxn
                create.setPath(createv0.getPath());
                create.setData(createv0.getData());
                create.setAcl(createv0.getAcl());
                create.setEphemeral(createv0.getEphemeral());
                create.setParentCVersion(-1);
            } else {
                throw e;
            }
        }
    }
    return txn;
}
Also used : CreateContainerTxn(org.apache.zookeeper.txn.CreateContainerTxn) MultiTxn(org.apache.zookeeper.txn.MultiTxn) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) SetDataTxn(org.apache.zookeeper.txn.SetDataTxn) IOException(java.io.IOException) CreateTxnV0(org.apache.zookeeper.txn.CreateTxnV0) CreateTTLTxn(org.apache.zookeeper.txn.CreateTTLTxn) DeleteTxn(org.apache.zookeeper.txn.DeleteTxn) CreateSessionTxn(org.apache.zookeeper.txn.CreateSessionTxn) CreateTxn(org.apache.zookeeper.txn.CreateTxn) ErrorTxn(org.apache.zookeeper.txn.ErrorTxn) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) Record(org.apache.jute.Record) SetACLTxn(org.apache.zookeeper.txn.SetACLTxn)

Example 5 with CreateTxn

use of org.apache.zookeeper.txn.CreateTxn in project zookeeper by apache.

the class LoadFromLogTest method doOp.

/*
     * Does create/delete depending on the type and verifies
     * if cversion before the operation is 1 less than cversion afer.
     */
private void doOp(FileTxnSnapLog logFile, int type, String path, DataTree dt, DataNode parent, int cversion) throws Exception {
    int lastSlash = path.lastIndexOf('/');
    String parentName = path.substring(0, lastSlash);
    int prevCversion = parent.stat.getCversion();
    long prevPzxid = parent.stat.getPzxid();
    List<String> child = dt.getChildren(parentName, null, null);
    StringBuilder childStr = new StringBuilder();
    for (String s : child) {
        childStr.append(s).append(" ");
    }
    LOG.info("Children: " + childStr + " for " + parentName);
    LOG.info("(cverions, pzxid): " + prevCversion + ", " + prevPzxid);
    Record txn = null;
    TxnHeader txnHeader = null;
    if (type == OpCode.delete) {
        txn = new DeleteTxn(path);
        txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), OpCode.delete);
    } else if (type == OpCode.create) {
        txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), OpCode.create);
        txn = new CreateTxn(path, new byte[0], null, false, cversion);
    } else if (type == OpCode.multi) {
        txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), OpCode.create);
        txn = new CreateTxn(path, new byte[0], null, false, cversion);
        List<Txn> txnList = new ArrayList<Txn>();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
        txn.serialize(boa, "request");
        ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
        Txn txact = new Txn(OpCode.create, bb.array());
        txnList.add(txact);
        txn = new MultiTxn(txnList);
        txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), OpCode.multi);
    }
    logFile.processTransaction(txnHeader, dt, null, txn);
    int newCversion = parent.stat.getCversion();
    long newPzxid = parent.stat.getPzxid();
    child = dt.getChildren(parentName, null, null);
    childStr = new StringBuilder();
    for (String s : child) {
        childStr.append(s).append(" ");
    }
    LOG.info("Children: " + childStr + " for " + parentName);
    LOG.info("(cverions, pzxid): " + newCversion + ", " + newPzxid);
    Assert.assertTrue(type + " <cversion, pzxid> verification failed. Expected: <" + (prevCversion + 1) + ", " + (prevPzxid + 1) + ">, found: <" + newCversion + ", " + newPzxid + ">", (newCversion == prevCversion + 1 && newPzxid == prevPzxid + 1));
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) MultiTxn(org.apache.zookeeper.txn.MultiTxn) ArrayList(java.util.ArrayList) CreateTxn(org.apache.zookeeper.txn.CreateTxn) Txn(org.apache.zookeeper.txn.Txn) MultiTxn(org.apache.zookeeper.txn.MultiTxn) DeleteTxn(org.apache.zookeeper.txn.DeleteTxn) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) DeleteTxn(org.apache.zookeeper.txn.DeleteTxn) CreateTxn(org.apache.zookeeper.txn.CreateTxn) Record(org.apache.jute.Record) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Aggregations

CreateTxn (org.apache.zookeeper.txn.CreateTxn)8 TxnHeader (org.apache.zookeeper.txn.TxnHeader)5 Record (org.apache.jute.Record)4 DeleteTxn (org.apache.zookeeper.txn.DeleteTxn)4 File (java.io.File)3 BinaryInputArchive (org.apache.jute.BinaryInputArchive)3 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)3 CreateContainerTxn (org.apache.zookeeper.txn.CreateContainerTxn)3 CreateTTLTxn (org.apache.zookeeper.txn.CreateTTLTxn)3 MultiTxn (org.apache.zookeeper.txn.MultiTxn)3 SetDataTxn (org.apache.zookeeper.txn.SetDataTxn)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 EOFException (java.io.EOFException)2 IOException (java.io.IOException)2 Socket (java.net.Socket)2 ByteBuffer (java.nio.ByteBuffer)2 InputArchive (org.apache.jute.InputArchive)2 KeeperException (org.apache.zookeeper.KeeperException)2 ACL (org.apache.zookeeper.data.ACL)2