Search in sources :

Example 1 with CreateTxnV0

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

the class SerializeUtils method deserializeTxn.

public static TxnLogEntry deserializeTxn(byte[] txnBytes) throws IOException {
    TxnHeader hdr = new TxnHeader();
    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:
            txn = ZooKeeperServer.isCloseSessionTxnEnabled() ? new CloseSessionTxn() : null;
            break;
        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 if (hdr.getType() == OpCode.closeSession) {
                // perhaps this is before CloseSessionTxn was added,
                // ignore it and reset txn to null
                txn = null;
            } else {
                throw e;
            }
        }
    }
    TxnDigest digest = null;
    if (ZooKeeperServer.isDigestEnabled()) {
        digest = new TxnDigest();
        try {
            digest.deserialize(ia, "digest");
        } catch (EOFException exception) {
            // may not have digest in the txn
            digest = null;
        }
    }
    return new TxnLogEntry(txn, hdr, digest);
}
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) TxnLogEntry(org.apache.zookeeper.server.TxnLogEntry) EOFException(java.io.EOFException) CloseSessionTxn(org.apache.zookeeper.txn.CloseSessionTxn) Record(org.apache.jute.Record) SetACLTxn(org.apache.zookeeper.txn.SetACLTxn) TxnDigest(org.apache.zookeeper.txn.TxnDigest) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 2 with CreateTxnV0

use of org.apache.zookeeper.txn.CreateTxnV0 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)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 EOFException (java.io.EOFException)2 IOException (java.io.IOException)2 BinaryInputArchive (org.apache.jute.BinaryInputArchive)2 InputArchive (org.apache.jute.InputArchive)2 Record (org.apache.jute.Record)2 CreateContainerTxn (org.apache.zookeeper.txn.CreateContainerTxn)2 CreateSessionTxn (org.apache.zookeeper.txn.CreateSessionTxn)2 CreateTTLTxn (org.apache.zookeeper.txn.CreateTTLTxn)2 CreateTxn (org.apache.zookeeper.txn.CreateTxn)2 CreateTxnV0 (org.apache.zookeeper.txn.CreateTxnV0)2 DeleteTxn (org.apache.zookeeper.txn.DeleteTxn)2 ErrorTxn (org.apache.zookeeper.txn.ErrorTxn)2 MultiTxn (org.apache.zookeeper.txn.MultiTxn)2 SetACLTxn (org.apache.zookeeper.txn.SetACLTxn)2 SetDataTxn (org.apache.zookeeper.txn.SetDataTxn)2 TxnLogEntry (org.apache.zookeeper.server.TxnLogEntry)1 CloseSessionTxn (org.apache.zookeeper.txn.CloseSessionTxn)1 TxnDigest (org.apache.zookeeper.txn.TxnDigest)1 TxnHeader (org.apache.zookeeper.txn.TxnHeader)1