use of org.apache.zookeeper.server.Request in project zookeeper by apache.
the class FollowerZooKeeperServer method sync.
public synchronized void sync() {
if (pendingSyncs.size() == 0) {
LOG.warn("Not expecting a sync.");
return;
}
Request r = pendingSyncs.remove();
if (r instanceof LearnerSyncRequest) {
LearnerSyncRequest lsr = (LearnerSyncRequest) r;
lsr.fh.queuePacket(new QuorumPacket(Leader.SYNC, 0, null, null));
}
commitProcessor.commit(r);
}
use of org.apache.zookeeper.server.Request in project zookeeper by apache.
the class FollowerZooKeeperServer method logRequest.
public void logRequest(TxnHeader hdr, Record txn, TxnDigest digest) {
Request request = new Request(hdr.getClientId(), hdr.getCxid(), hdr.getType(), hdr, txn, hdr.getZxid());
request.setTxnDigest(digest);
if ((request.zxid & 0xffffffffL) != 0) {
pendingTxns.add(request);
}
syncProcessor.processRequest(request);
}
use of org.apache.zookeeper.server.Request 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;
}
}
use of org.apache.zookeeper.server.Request in project zookeeper by apache.
the class CommitProcessorMetricsTest method testRequestsInSessionQueue.
@Test
public void testRequestsInSessionQueue() throws Exception {
setupProcessors(0, 0);
Request req1 = createWriteRequest(1L, 1);
processRequestWithWait(req1);
checkMetrics("requests_in_session_queue", 1L, 1L, 1D, 1L, 1L);
// these two read requests will be stuck in the session queue because there is write in front of them
processRequestWithWait(createReadRequest(1L, 2));
processRequestWithWait(createReadRequest(1L, 3));
checkMetrics("requests_in_session_queue", 1L, 3L, 2D, 3L, 6);
commitWithWait(req1);
checkMetrics("requests_in_session_queue", 1L, 3L, 2.25D, 4L, 9);
}
use of org.apache.zookeeper.server.Request in project zookeeper by apache.
the class FileTxnSnapLogTest method testGetTxnLogSyncElapsedTime.
@Test
public void testGetTxnLogSyncElapsedTime() throws IOException {
FileTxnSnapLog fileTxnSnapLog = createFileTxnSnapLogWithAutoCreateDataDir(logDir, snapDir, "true");
TxnHeader hdr = new TxnHeader(1, 1, 1, 1, ZooDefs.OpCode.setData);
Record txn = new SetDataTxn("/foo", new byte[0], 1);
Request req = new Request(0, 0, 0, hdr, txn, 0);
try {
fileTxnSnapLog.append(req);
fileTxnSnapLog.commit();
long syncElapsedTime = fileTxnSnapLog.getTxnLogElapsedSyncTime();
assertNotEquals(-1L, syncElapsedTime, "Did not update syncElapsedTime!");
} finally {
fileTxnSnapLog.close();
}
}
Aggregations