Search in sources :

Example 36 with Record

use of org.apache.jute.Record in project zookeeper by apache.

the class TxnLogToolkit method printTxn.

private void printTxn(byte[] bytes, String prefix) throws IOException {
    TxnLogEntry logEntry = SerializeUtils.deserializeTxn(bytes);
    TxnHeader hdr = logEntry.getHeader();
    Record txn = logEntry.getTxn();
    String txnStr = getFormattedTxnStr(txn);
    String txns = String.format("%s session 0x%s cxid 0x%s zxid 0x%s %s %s", DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(new Date(hdr.getTime())), Long.toHexString(hdr.getClientId()), Long.toHexString(hdr.getCxid()), Long.toHexString(hdr.getZxid()), Request.op2String(hdr.getType()), txnStr);
    if (prefix != null && !"".equals(prefix.trim())) {
        System.out.print(prefix + " - ");
    }
    if (txns.endsWith("\n")) {
        System.out.print(txns);
    } else {
        System.out.println(txns);
    }
}
Also used : TxnLogEntry(org.apache.zookeeper.server.TxnLogEntry) Record(org.apache.jute.Record) Date(java.util.Date) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 37 with Record

use of org.apache.jute.Record 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)

Example 38 with Record

use of org.apache.jute.Record in project zookeeper by apache.

the class PrepRequestProcessorTest method process.

private void process(List<Op> ops) throws Exception {
    pLatch = new CountDownLatch(1);
    processor = new PrepRequestProcessor(zks, new MyRequestProcessor());
    Record record = new MultiOperationRecord(ops);
    Request req = createRequest(record, OpCode.multi, false);
    processor.pRequest(req);
    assertTrue(pLatch.await(5, TimeUnit.SECONDS), "request hasn't been processed in chain");
}
Also used : MultiOperationRecord(org.apache.zookeeper.MultiOperationRecord) CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) ReconfigRequest(org.apache.zookeeper.proto.ReconfigRequest) Record(org.apache.jute.Record) ChangeRecord(org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord) MultiOperationRecord(org.apache.zookeeper.MultiOperationRecord) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 39 with Record

use of org.apache.jute.Record in project zookeeper by apache.

the class PrepRequestProcessorTest method testReconfigWithAnotherOutstandingChange.

@Test
public void testReconfigWithAnotherOutstandingChange() throws Exception {
    QuorumPeerConfig.setReconfigEnabled(true);
    QuorumPeerConfig.setStandaloneEnabled(false);
    QuorumPeer qp = new QuorumPeer();
    QuorumVerifier quorumVerifierMock = mock(QuorumVerifier.class);
    when(quorumVerifierMock.getAllMembers()).thenReturn(LeaderBeanTest.getMockedPeerViews(qp.getId()));
    qp.setQuorumVerifier(quorumVerifierMock, false);
    FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDir, tmpDir);
    LeaderZooKeeperServer lzks = new LeaderZooKeeperServer(snapLog, qp, new ZKDatabase(snapLog));
    qp.leader = new Leader(qp, lzks);
    lzks.sessionTracker = new MySessionTracker();
    ZooKeeperServer.setDigestEnabled(true);
    processor = new PrepRequestProcessor(lzks, new MyRequestProcessor());
    Record record = new CreateRequest("/foo", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT.toFlag());
    pLatch = new CountDownLatch(1);
    processor.pRequest(createRequest(record, OpCode.create, false));
    assertTrue(pLatch.await(5, TimeUnit.SECONDS), "request hasn't been processed in chain");
    String newMember = "server.0=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant";
    record = new ReconfigRequest(null, null, newMember, 0);
    pLatch = new CountDownLatch(1);
    processor.pRequest(createRequest(record, OpCode.reconfig, true));
    assertTrue(pLatch.await(5, TimeUnit.SECONDS), "request hasn't been processed in chain");
    // Verifies that there was no error.
    assertEquals(outcome.getHdr().getType(), OpCode.reconfig);
}
Also used : Leader(org.apache.zookeeper.server.quorum.Leader) CreateRequest(org.apache.zookeeper.proto.CreateRequest) CountDownLatch(java.util.concurrent.CountDownLatch) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) ReconfigRequest(org.apache.zookeeper.proto.ReconfigRequest) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) Record(org.apache.jute.Record) ChangeRecord(org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord) MultiOperationRecord(org.apache.zookeeper.MultiOperationRecord) LeaderZooKeeperServer(org.apache.zookeeper.server.quorum.LeaderZooKeeperServer) LeaderBeanTest(org.apache.zookeeper.server.quorum.LeaderBeanTest) Test(org.junit.jupiter.api.Test)

Example 40 with Record

use of org.apache.jute.Record in project zookeeper by apache.

the class FileTxnSnapLogTest method testACLCreatedDuringFuzzySnapshotSync.

/**
 * Make sure the ACL is exist in the ACL map after SNAP syncing.
 *
 * ZooKeeper uses ACL reference id and count to save the space in snapshot.
 * During fuzzy snapshot sync, the reference count may not be updated
 * correctly in case like the znode is already exist.
 *
 * When ACL reference count reaches 0, it will be deleted from the cache,
 * but actually there might be other nodes still using it. When visiting
 * a node with the deleted ACL id, it will be rejected because it doesn't
 * exist anymore.
 *
 * Here is the detailed flow for one of the scenario here:
 *   1. Server A starts to have snap sync with leader
 *   2. After serializing the ACL map to Server A, there is a txn T1 to
 *      create a node N1 with new ACL_1 which was not exist in ACL map
 *   3. On leader, after this txn, the ACL map will be ID1 -&gt; (ACL_1, COUNT: 1),
 *      and data tree N1 -&gt; ID1
 *   4. On server A, it will be empty ACL map, and N1 -&gt; ID1 in fuzzy snapshot
 *   5. When replaying the txn T1, it will skip at the beginning since the
 *      node is already exist, which leaves an empty ACL map, and N1 is
 *      referencing to a non-exist ACL ID1
 *   6. Node N1 will be not accessible because the ACL not exist, and if it
 *      became leader later then all the write requests will be rejected as
 *      well with marshalling error.
 */
@Test
public void testACLCreatedDuringFuzzySnapshotSync() throws IOException {
    DataTree leaderDataTree = new DataTree();
    // Start the simulated snap-sync by serializing ACL cache.
    File file = File.createTempFile("snapshot", "zk");
    FileOutputStream os = new FileOutputStream(file);
    OutputArchive oa = BinaryOutputArchive.getArchive(os);
    leaderDataTree.serializeAcls(oa);
    // Add couple of transaction in-between.
    TxnHeader hdr1 = new TxnHeader(1, 2, 2, 2, ZooDefs.OpCode.create);
    Record txn1 = new CreateTxn("/a1", "foo".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, false, -1);
    leaderDataTree.processTxn(hdr1, txn1);
    // Finish the snapshot.
    leaderDataTree.serializeNodes(oa);
    os.close();
    // Simulate restore on follower and replay.
    FileInputStream is = new FileInputStream(file);
    InputArchive ia = BinaryInputArchive.getArchive(is);
    DataTree followerDataTree = new DataTree();
    followerDataTree.deserialize(ia, "tree");
    followerDataTree.processTxn(hdr1, txn1);
    DataNode a1 = leaderDataTree.getNode("/a1");
    assertNotNull(a1);
    assertEquals(ZooDefs.Ids.CREATOR_ALL_ACL, leaderDataTree.getACL(a1));
    assertEquals(ZooDefs.Ids.CREATOR_ALL_ACL, followerDataTree.getACL(a1));
}
Also used : CreateTxn(org.apache.zookeeper.txn.CreateTxn) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) DataTree(org.apache.zookeeper.server.DataTree) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) DataNode(org.apache.zookeeper.server.DataNode) FileOutputStream(java.io.FileOutputStream) Record(org.apache.jute.Record) File(java.io.File) FileInputStream(java.io.FileInputStream) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Aggregations

Record (org.apache.jute.Record)42 TxnHeader (org.apache.zookeeper.txn.TxnHeader)28 IOException (java.io.IOException)13 SetDataTxn (org.apache.zookeeper.txn.SetDataTxn)13 CreateTxn (org.apache.zookeeper.txn.CreateTxn)12 ByteBuffer (java.nio.ByteBuffer)10 BinaryInputArchive (org.apache.jute.BinaryInputArchive)9 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)9 DeleteTxn (org.apache.zookeeper.txn.DeleteTxn)9 MultiTxn (org.apache.zookeeper.txn.MultiTxn)9 ErrorTxn (org.apache.zookeeper.txn.ErrorTxn)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ArrayList (java.util.ArrayList)7 SetACLTxn (org.apache.zookeeper.txn.SetACLTxn)7 Txn (org.apache.zookeeper.txn.Txn)7 Test (org.junit.jupiter.api.Test)7 EOFException (java.io.EOFException)6 File (java.io.File)6 KeeperException (org.apache.zookeeper.KeeperException)6 CreateRequest (org.apache.zookeeper.proto.CreateRequest)6