Search in sources :

Example 1 with TxnDigest

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

the class Observer method processPacket.

/**
 * Controls the response of an observer to the receipt of a quorumpacket
 * @param qp
 * @throws Exception
 */
protected void processPacket(QuorumPacket qp) throws Exception {
    TxnLogEntry logEntry;
    TxnHeader hdr;
    TxnDigest digest;
    Record txn;
    switch(qp.getType()) {
        case Leader.PING:
            ping(qp);
            break;
        case Leader.PROPOSAL:
            LOG.warn("Ignoring proposal");
            break;
        case Leader.COMMIT:
            LOG.warn("Ignoring commit");
            break;
        case Leader.UPTODATE:
            LOG.error("Received an UPTODATE message after Observer started");
            break;
        case Leader.REVALIDATE:
            revalidate(qp);
            break;
        case Leader.SYNC:
            ((ObserverZooKeeperServer) zk).sync();
            break;
        case Leader.INFORM:
            ServerMetrics.getMetrics().LEARNER_COMMIT_RECEIVED_COUNT.add(1);
            logEntry = SerializeUtils.deserializeTxn(qp.getData());
            hdr = logEntry.getHeader();
            txn = logEntry.getTxn();
            digest = logEntry.getDigest();
            Request request = new Request(hdr.getClientId(), hdr.getCxid(), hdr.getType(), hdr, txn, 0);
            request.logLatency(ServerMetrics.getMetrics().COMMIT_PROPAGATION_LATENCY);
            request.setTxnDigest(digest);
            ObserverZooKeeperServer obs = (ObserverZooKeeperServer) zk;
            obs.commitRequest(request);
            break;
        case Leader.INFORMANDACTIVATE:
            // get new designated leader from (current) leader's message
            ByteBuffer buffer = ByteBuffer.wrap(qp.getData());
            long suggestedLeaderId = buffer.getLong();
            byte[] remainingdata = new byte[buffer.remaining()];
            buffer.get(remainingdata);
            logEntry = SerializeUtils.deserializeTxn(remainingdata);
            hdr = logEntry.getHeader();
            txn = logEntry.getTxn();
            digest = logEntry.getDigest();
            QuorumVerifier qv = self.configFromString(new String(((SetDataTxn) txn).getData(), UTF_8));
            request = new Request(hdr.getClientId(), hdr.getCxid(), hdr.getType(), hdr, txn, 0);
            request.setTxnDigest(digest);
            obs = (ObserverZooKeeperServer) zk;
            boolean majorChange = self.processReconfig(qv, suggestedLeaderId, qp.getZxid(), true);
            obs.commitRequest(request);
            if (majorChange) {
                throw new Exception("changes proposed in reconfig");
            }
            break;
        default:
            LOG.warn("Unknown packet type: {}", LearnerHandler.packetToString(qp));
            break;
    }
}
Also used : Request(org.apache.zookeeper.server.Request) SetDataTxn(org.apache.zookeeper.txn.SetDataTxn) ByteBuffer(java.nio.ByteBuffer) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) TxnLogEntry(org.apache.zookeeper.server.TxnLogEntry) Record(org.apache.jute.Record) TxnDigest(org.apache.zookeeper.txn.TxnDigest) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 2 with TxnDigest

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

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

the class TxnLogDigestTest method digestFromTxnLogsMatchesTree.

/**
 * Check that the digest stored in the txn matches the digest calculated
 * from DataTree.
 */
@Test
public void digestFromTxnLogsMatchesTree() throws Exception {
    // reset the mismatch metrics
    SimpleCounter digestMistachesCount = (SimpleCounter) ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT;
    digestMistachesCount.reset();
    // trigger some write ops
    performOperations(createClient(), "/digestFromTxnLogsMatchesTree");
    // make sure there is no digest mismatch
    assertEquals(0, digestMistachesCount.get());
    // verify that the digest is wrote to disk with txn
    TxnDigest lastDigest = getLastTxnLogDigest();
    assertNotNull(lastDigest);
    assertEquals(server.getZKDatabase().getDataTree().getTreeDigest(), lastDigest.getTreeDigest());
}
Also used : SimpleCounter(org.apache.zookeeper.server.metric.SimpleCounter) TxnDigest(org.apache.zookeeper.txn.TxnDigest) QuorumPeerMainTest(org.apache.zookeeper.server.quorum.QuorumPeerMainTest) Test(org.junit.jupiter.api.Test)

Example 4 with TxnDigest

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

the class TxnLogDigestTest method getLastTxnLogDigest.

private TxnDigest getLastTxnLogDigest() throws IOException {
    TxnIterator itr = new FileTxnLog(new File(tmpDir, "version-2")).read(1);
    TxnDigest lastDigest = null;
    while (itr.next()) {
        lastDigest = itr.getDigest();
    }
    return lastDigest;
}
Also used : FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) TxnIterator(org.apache.zookeeper.server.persistence.TxnLog.TxnIterator) File(java.io.File) TxnDigest(org.apache.zookeeper.txn.TxnDigest)

Example 5 with TxnDigest

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

the class Follower method processPacket.

/**
 * Examine the packet received in qp and dispatch based on its contents.
 * @param qp
 * @throws IOException
 */
protected void processPacket(QuorumPacket qp) throws Exception {
    switch(qp.getType()) {
        case Leader.PING:
            ping(qp);
            break;
        case Leader.PROPOSAL:
            ServerMetrics.getMetrics().LEARNER_PROPOSAL_RECEIVED_COUNT.add(1);
            TxnLogEntry logEntry = SerializeUtils.deserializeTxn(qp.getData());
            TxnHeader hdr = logEntry.getHeader();
            Record txn = logEntry.getTxn();
            TxnDigest digest = logEntry.getDigest();
            if (hdr.getZxid() != lastQueued + 1) {
                LOG.warn("Got zxid 0x{} expected 0x{}", Long.toHexString(hdr.getZxid()), Long.toHexString(lastQueued + 1));
            }
            lastQueued = hdr.getZxid();
            if (hdr.getType() == OpCode.reconfig) {
                SetDataTxn setDataTxn = (SetDataTxn) txn;
                QuorumVerifier qv = self.configFromString(new String(setDataTxn.getData(), UTF_8));
                self.setLastSeenQuorumVerifier(qv, true);
            }
            fzk.logRequest(hdr, txn, digest);
            if (hdr != null) {
                /*
                 * Request header is created only by the leader, so this is only set
                 * for quorum packets. If there is a clock drift, the latency may be
                 * negative. Headers use wall time, not CLOCK_MONOTONIC.
                 */
                long now = Time.currentWallTime();
                long latency = now - hdr.getTime();
                if (latency >= 0) {
                    ServerMetrics.getMetrics().PROPOSAL_LATENCY.add(latency);
                }
            }
            if (om != null) {
                final long startTime = Time.currentElapsedTime();
                om.proposalReceived(qp);
                ServerMetrics.getMetrics().OM_PROPOSAL_PROCESS_TIME.add(Time.currentElapsedTime() - startTime);
            }
            break;
        case Leader.COMMIT:
            ServerMetrics.getMetrics().LEARNER_COMMIT_RECEIVED_COUNT.add(1);
            fzk.commit(qp.getZxid());
            if (om != null) {
                final long startTime = Time.currentElapsedTime();
                om.proposalCommitted(qp.getZxid());
                ServerMetrics.getMetrics().OM_COMMIT_PROCESS_TIME.add(Time.currentElapsedTime() - startTime);
            }
            break;
        case Leader.COMMITANDACTIVATE:
            // get the new configuration from the request
            Request request = fzk.pendingTxns.element();
            SetDataTxn setDataTxn = (SetDataTxn) request.getTxn();
            QuorumVerifier qv = self.configFromString(new String(setDataTxn.getData(), UTF_8));
            // get new designated leader from (current) leader's message
            ByteBuffer buffer = ByteBuffer.wrap(qp.getData());
            long suggestedLeaderId = buffer.getLong();
            final long zxid = qp.getZxid();
            boolean majorChange = self.processReconfig(qv, suggestedLeaderId, zxid, true);
            // commit (writes the new config to ZK tree (/zookeeper/config)
            fzk.commit(zxid);
            if (om != null) {
                om.informAndActivate(zxid, suggestedLeaderId);
            }
            if (majorChange) {
                throw new Exception("changes proposed in reconfig");
            }
            break;
        case Leader.UPTODATE:
            LOG.error("Received an UPTODATE message after Follower started");
            break;
        case Leader.REVALIDATE:
            if (om == null || !om.revalidateLearnerSession(qp)) {
                revalidate(qp);
            }
            break;
        case Leader.SYNC:
            fzk.sync();
            break;
        default:
            LOG.warn("Unknown packet type: {}", LearnerHandler.packetToString(qp));
            break;
    }
}
Also used : TxnLogEntry(org.apache.zookeeper.server.TxnLogEntry) Request(org.apache.zookeeper.server.Request) Record(org.apache.jute.Record) SetDataTxn(org.apache.zookeeper.txn.SetDataTxn) ByteBuffer(java.nio.ByteBuffer) TxnDigest(org.apache.zookeeper.txn.TxnDigest) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) IOException(java.io.IOException) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Aggregations

TxnDigest (org.apache.zookeeper.txn.TxnDigest)6 Record (org.apache.jute.Record)4 TxnHeader (org.apache.zookeeper.txn.TxnHeader)4 TxnLogEntry (org.apache.zookeeper.server.TxnLogEntry)3 SetDataTxn (org.apache.zookeeper.txn.SetDataTxn)3 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Request (org.apache.zookeeper.server.Request)2 QuorumVerifier (org.apache.zookeeper.server.quorum.flexible.QuorumVerifier)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 EOFException (java.io.EOFException)1 File (java.io.File)1 BinaryInputArchive (org.apache.jute.BinaryInputArchive)1 InputArchive (org.apache.jute.InputArchive)1 DataTree (org.apache.zookeeper.server.DataTree)1 SimpleCounter (org.apache.zookeeper.server.metric.SimpleCounter)1 FileTxnLog (org.apache.zookeeper.server.persistence.FileTxnLog)1 TxnIterator (org.apache.zookeeper.server.persistence.TxnLog.TxnIterator)1 QuorumPeerMainTest (org.apache.zookeeper.server.quorum.QuorumPeerMainTest)1 CloseSessionTxn (org.apache.zookeeper.txn.CloseSessionTxn)1