zookeeper (13) source analysis - request processing chain (3)

FinalRequestProcessor request processing chain is the last one processor.

public class FinalRequestProcessor implements RequestProcessor {
    ZooKeeperServer zks;
}

FinalRequestProcessor only achieved RequestProcessor interfaces, process Request method and shutdown methods need to achieve.

The core properties for zks, expressed Zookeeper server, you can access the database memory Zookeeper by zks.

We look at the core method process Request Code:

Sync block

synchronized (zks.outstandingChanges) {
            // Need to process local session requests
            // 当前节点,处理请求,若为事务性请求,则提交到ZooKeeper内存数据库中。
            // 对于processTxn函数而言,其最终会调用DataTree的processTxn
            rc = zks.processTxn(request);

            // request.hdr is set for write requests, which are the only ones
            // that add to outstandingChanges.
            //只有写请求才会有消息头
            if (request.getHdr() != null) {
                TxnHeader hdr = request.getHdr();
                Record txn = request.getTxn();
                long zxid = hdr.getZxid();
                //当outstandingChanges不为空且其首元素的zxid小于等于请求的zxid时,
                // 就会一直从outstandingChanges中取出首元素,并且对outstandingChangesForPath做相应的操作
                while (!zks.outstandingChanges.isEmpty()
                       && zks.outstandingChanges.peek().zxid <= zxid) {
                    ChangeRecord cr = zks.outstandingChanges.remove();
                    if (cr.zxid < zxid) {
                        LOG.warn("Zxid outstanding " + cr.zxid
                                 + " is less than current " + zxid);
                    }
                    if (zks.outstandingChangesForPath.get(cr.path) == cr) {
                        zks.outstandingChangesForPath.remove(cr.path);
                    }
                }
            }

            // do not add non quorum packets to the queue.
            //判断是否为事务性请求则是通过调用isQuorum函数
            //只将quorum包(事务性请求)添加进队列
            //addCommittedProposal函数将请求添加至ZKDatabase的committedLog结构中
            if (request.isQuorum()) {
                zks.getZKDatabase().addCommittedProposal(request);
            }
        }

If the request is ping

Zookeeper server to update the delay time in accordance with the request of creation, updateLatency function will record the maximum delay, minimum delay, the overall delay and delay times.
Then updates the status response, such as a request to create a response to the request of the total time it takes, the last type of operation. Then after setting response returned

case OpCode.ping: {
                //更新延迟
                zks.serverStats().updateLatency(request.createTime);

                lastOp = "PING";
                // 更新响应的状态
                cnxn.updateStatsForResponse(request.cxid, request.zxid, lastOp,
                        request.createTime, Time.currentElapsedTime());
                // 设置响应
                cnxn.sendResponse(new ReplyHeader(-2,
                        zks.getZKDatabase().getDataTreeLastProcessedZxid(), 0), null, "response");
                return;
            }

Similarly other requests,
eventually delayed update server is provided in response to the request based on other state again, etc.

// 获取最后处理的zxid
long lastZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid();
// 响应头
        ReplyHeader hdr =
            new ReplyHeader(request.cxid, lastZxid, err.intValue());
// 更新服务器延迟
        zks.serverStats().updateLatency(request.createTime);
                // 更新状态
        cnxn.updateStatsForResponse(request.cxid, lastZxid, lastOp,
                    request.createTime, Time.currentElapsedTime());

Finally, the function sends a response using sendResponse to the requester.

try {
            //返回相应
            cnxn.sendResponse(hdr, rsp, "response");
            if (request.type == OpCode.closeSession) {
                //关闭会话
                cnxn.sendCloseSession();
            }
        } catch (IOException e) {
            LOG.error("FIXMSG",e);
        }

Guess you like

Origin blog.51cto.com/janephp/2458874