一、前言

  在分析了PrepRequestProcessor处理器后,接着来分析SyncRequestProcessor,该处理器将请求存入磁盘,其将请求批量的存入磁盘以提高效率,请求在写入磁盘之前是不会被转发到下个处理器的。

二、SyncRequestProcessor源码分析

  2.1 类的继承关系   

public class SyncRequestProcessor extends Thread implements RequestProcessor {}

  说明:与PrepRequestProcessor一样,SyncRequestProcessor也继承了Thread类并实现了RequestProcessor接口,表示其可以作为线程使用。

  2.2 类的属性  

public class SyncRequestProcessor extends Thread implements RequestProcessor {
// 日志
private static final Logger LOG = LoggerFactory.getLogger(SyncRequestProcessor.class); // Zookeeper服务器
private final ZooKeeperServer zks; // 请求队列
private final LinkedBlockingQueue<Request> queuedRequests =
new LinkedBlockingQueue<Request>(); // 下个处理器
private final RequestProcessor nextProcessor; // 快照处理线程
private Thread snapInProcess = null; // 是否在运行中
volatile private boolean running; /**
* Transactions that have been written and are waiting to be flushed to
* disk. Basically this is the list of SyncItems whose callbacks will be
* invoked after flush returns successfully.
*/
// 等待被刷新到磁盘的请求队列
private final LinkedList<Request> toFlush = new LinkedList<Request>(); // 随机数生成器
private final Random r = new Random(System.nanoTime());
/**
* The number of log entries to log before starting a snapshot
*/
// 快照个数
private static int snapCount = ZooKeeperServer.getSnapCount(); /**
* The number of log entries before rolling the log, number
* is chosen randomly
*/
// 日志滚动之前记录的日志号,编号是随机选择的
private static int randRoll; // 结束请求标识
private final Request requestOfDeath = Request.requestOfDeath;
}

  说明:其中,SyncRequestProcessor维护了ZooKeeperServer实例,其用于获取ZooKeeper的数据库和其他信息;维护了一个处理请求的队列,其用于存放请求;维护了一个处理快照的线程,用于处理快照;维护了一个running标识,标识SyncRequestProcessor是否在运行;同时还维护了一个等待被刷新到磁盘的请求队列。

  2.3 类的构造函数  

    public SyncRequestProcessor(ZooKeeperServer zks,
RequestProcessor nextProcessor)
{
// 调用父类构造函数
super("SyncThread:" + zks.getServerId());
// 给字段赋值
this.zks = zks;
this.nextProcessor = nextProcessor;
running = true;
}

  说明:构造函数首先会调用Thread类的构造函数,然后根据构造函数参数给类的属性赋值,其中会确定下个处理器,并会设置该处理器正在运行的标识。

  2.4 类的核心函数分析

  1. run函数

    public void run() {
try {
// 写日志数量初始化为0
int logCount = 0; // we do this in an attempt to ensure that not all of the servers
// in the ensemble take a snapshot at the same time
// 确保所有的服务器在同一时间不是使用的同一个快照
setRandRoll(r.nextInt(snapCount/2));
while (true) { //
// 初始请求为null
Request si = null;
if (toFlush.isEmpty()) { // 没有需要刷新到磁盘的请求
// 从请求队列中取出一个请求,若队列为空会阻塞
si = queuedRequests.take();
} else { // 队列不为空,即有需要刷新到磁盘的请求
// 从请求队列中取出一个请求,若队列为空,则返回空,不会阻塞
si = queuedRequests.poll();
if (si == null) { // 取出的请求为空
// 刷新到磁盘
flush(toFlush);
// 跳过后面的处理
continue;
}
}
if (si == requestOfDeath) { // 在关闭处理器之后,会添加requestOfDeath,表示关闭后不再处理请求
break;
}
if (si != null) { // 请求不为空
// track the number of records written to the log
if (zks.getZKDatabase().append(si)) { // 将请求添加至日志文件,只有事务性请求才会返回true
// 写入一条日志,logCount加1
logCount++;
if (logCount > (snapCount / 2 + randRoll)) { // 满足roll the log的条件
randRoll = r.nextInt(snapCount/2);
// roll the log
zks.getZKDatabase().rollLog();
// take a snapshot
if (snapInProcess != null && snapInProcess.isAlive()) { // 正在进行快照
LOG.warn("Too busy to snap, skipping");
} else { // 未被处理
snapInProcess = new Thread("Snapshot Thread") { // 创建线程来处理快照
public void run() {
try {
// 进行快照
zks.takeSnapshot();
} catch(Exception e) {
LOG.warn("Unexpected exception", e);
}
}
};
// 开始处理
snapInProcess.start();
}
// 重置为0
logCount = 0;
}
} else if (toFlush.isEmpty()) { // 等待被刷新到磁盘的请求队列为空
// optimization for read heavy workloads
// iff this is a read, and there are no pending
// flushes (writes), then just pass this to the next
// processor
// 查看此时toFlush是否为空,如果为空,说明近段时间读多写少,直接响应
if (nextProcessor != null) { // 下个处理器不为空
// 下个处理器开始处理请求
nextProcessor.processRequest(si);
if (nextProcessor instanceof Flushable) { // 处理器是Flushable的
// 刷新到磁盘
((Flushable)nextProcessor).flush();
}
}
// 跳过后续处理
continue;
}
// 将请求添加至被刷新至磁盘队列
toFlush.add(si);
if (toFlush.size() > 1000) { // 队列大小大于1000,直接刷新到磁盘
flush(toFlush);
}
}
}
} catch (Throwable t) { // 出现异常
LOG.error("Severe unrecoverable error, exiting", t);
// 设置运行标识为false,表示该处理器不再运行
running = false;
// 退出程序
System.exit(11);
}
LOG.info("SyncRequestProcessor exited!");
}

  说明:该函数是整个处理器的核心,其逻辑大致如下

  (1) 设置randRoll大小,确保所有的服务器在同一时间不是使用的同一个快照。

  (2) 判断toFlush队列是否为空,若是,则表示没有需要刷新到磁盘的请求,进入(3),若否,进入(4)。

  (3) 从queuedRequests中取出一个请求,进入(6)。

  (4) 从queuedRequests中取出一个请求,判断该请求是否为null,若是,则进入(5),若否,则进入(6)。

  (5) 调用flush函数,将toFlush中的请求刷新到磁盘,跳过之后的处理部分。

  (6) 判断请求是否是结束请求(在调用shutdown之后,会在队列中添加一个requestOfDeath)。若是,则退出,否则,进入(7)。

  (7) 判断请求是否为null,若否,则进入(8),否则进入(2)。

  (8) 若写入日志成功,返回true(表示为事务性请求),进入(9),否则进入(18)。

  (9) logCount加1,并判断是否大于了阈值,若是,则进入(10),否则进入(18)。

  (10) 调用rollLog函数翻转日志文件。

  (11) 判断snapInProcess是否为空并且是否存活,若是,则输出日志,否则,进入(12)。

  (12) 创建snapInProcess线程并启动。

  (13) 重置logCount为0。

  (14) 判断toFlush队列是否为空,若是,进入(15)。

  (15) 判断nextProcessor是否为空,若否,则使用nextProcessor处理请求,否则进入(16)。

  (16) 判断nextProcessor是否是Flushable的,若是,则调用flush函数刷新请求至磁盘,否则进入(17)

  (17) 跳过之后的处理步骤。

  (18) 将请求添加至toFlush队列。

  (19) 若toFlush队列大小大于1000,则刷新至磁盘,进入(2)。

  其中会调用flush函数,其源码如下 

    // 刷新到磁盘
private void flush(LinkedList<Request> toFlush)
throws IOException, RequestProcessorException
{
if (toFlush.isEmpty()) // 队列为空,返回
return; // 提交至ZK数据库
zks.getZKDatabase().commit();
while (!toFlush.isEmpty()) { // 队列不为空
// 从队列移除请求
Request i = toFlush.remove();
if (nextProcessor != null) { // 下个处理器不为空
// 下个处理器开始处理请求
nextProcessor.processRequest(i);
}
}
if (nextProcessor != null && nextProcessor instanceof Flushable) { // 下个处理器不为空并且是Flushable的
// 刷新到磁盘
((Flushable)nextProcessor).flush();
}
}

  说明:该函数主要用于将toFlush队列中的请求刷新到磁盘中。

  2. shutdown函数 

    public void shutdown() {
LOG.info("Shutting down");
// 添加结束请求请求至队列
queuedRequests.add(requestOfDeath);
try {
if(running){ // 还在运行
// 等待该线程终止
this.join();
}
if (!toFlush.isEmpty()) { // 队列不为空
// 刷新到磁盘
flush(toFlush);
}
} catch(InterruptedException e) {
LOG.warn("Interrupted while wating for " + this + " to finish");
} catch (IOException e) {
LOG.warn("Got IO exception during shutdown");
} catch (RequestProcessorException e) {
LOG.warn("Got request processor exception during shutdown");
}
if (nextProcessor != null) {
nextProcessor.shutdown();
}
}

  说明:该函数用于关闭SyncRequestProcessor处理器,其首先会在queuedRequests队列中添加一个结束请求,然后再判断SyncRequestProcessor是否还在运行,若是,则会等待其结束;之后判断toFlush队列是否为空,若不为空,则刷新到磁盘中。

三、总结

  本篇讲解了SyncRequestProcessor的工作原理,其主要作用包含将事务性请求刷新到磁盘,并且对请求进行快照处理。也谢谢各位园友的观看~

参考链接:http://blog.csdn.net/pwlazy/article/details/8137121  

【Zookeeper】源码分析之请求处理链(三)的更多相关文章

  1. 【Zookeeper】源码分析之请求处理链(三)之SyncRequestProcessor

    一.前言 在分析了PrepRequestProcessor处理器后,接着来分析SyncRequestProcessor,该处理器将请求存入磁盘,其将请求批量的存入磁盘以提高效率,请求在写入磁盘之前是不 ...

  2. 【Zookeeper】源码分析之请求处理链(一)

    一.前言 前面已经分析了Watcher机制的主要代码,现在接着分析Zookeeper中的请求处理链,其是Zookeeper的主要特点之一. 二.总体框图 对于请求处理链而言,所有请求处理器的父接口为R ...

  3. 【Zookeeper】源码分析之请求处理链(二)

    一.前言 前面学习了请求处理链的RequestProcessor父类,接着学习PrepRequestProcessor,其通常是请求处理链的第一个处理器. 二.ZooKeeper源码分析 2.1 类的 ...

  4. 【Zookeeper】源码分析之请求处理链(四)

    一.前言 前面分析了SyncReqeustProcessor,接着分析请求处理链中最后的一个处理器FinalRequestProcessor. 二.FinalRequestProcessor源码分析 ...

  5. 【Zookeeper】源码分析之请求处理链(四)之FinalRequestProcessor

    一.前言 前面分析了SyncReqeustProcessor,接着分析请求处理链中最后的一个处理器FinalRequestProcessor. 二.FinalRequestProcessor源码分析 ...

  6. 【Zookeeper】源码分析之请求处理链(二)之PrepRequestProcessor

    一.前言 前面学习了请求处理链的RequestProcessor父类,接着学习PrepRequestProcessor,其通常是请求处理链的第一个处理器. 二.PrepRequestProcessor ...

  7. zookeeper源码分析之五服务端(集群leader)处理请求流程

    leader的实现类为LeaderZooKeeperServer,它间接继承自标准ZookeeperServer.它规定了请求到达leader时需要经历的路径: PrepRequestProcesso ...

  8. zookeeper源码分析之四服务端(单机)处理请求流程

    上文: zookeeper源码分析之一服务端启动过程 中,我们介绍了zookeeper服务器的启动过程,其中单机是ZookeeperServer启动,集群使用QuorumPeer启动,那么这次我们分析 ...

  9. Spring AOP 源码分析 - 拦截器链的执行过程

    1.简介 本篇文章是 AOP 源码分析系列文章的最后一篇文章,在前面的两篇文章中,我分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...

随机推荐

  1. SVN打基线

    分成trunk.tags.branches的话,那直接从trunk copy 到tags下面就可以或者按照你自己的目录,只要规定好就行 选择要打基线的项目的根目录,右击鼠标,在弹出的菜单中选择“分支/ ...

  2. Java 抽象类和接口与多态

    引入抽象类和接口的原因 即"针对接口编程",关键就在多态,即向上转型 当变量的的声明类型是超类型时,即抽象类或者接口,这样,只要是具体实现此超类型的类所产生的对象,都可以指定给这个 ...

  3. 破解&屏蔽防止嵌入框架代码 top.location != self.location

    <script type="text/javascript"> if (top.location != self.location) top.location = se ...

  4. 最多两次股票交易-Best Time to Buy and Sell Stock III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. 在官网下载了最新版的PHP,解压后的安装包里为什么没有php5isapi.dll这个dll文件?

    因为自PHP 5.3.1版本开始,PHP便已不在支持ISAPI模式,所以你在PHP5.3.1版本以上的php目录中看不到php5isapi.dll文件. 那么,IIS6下跑PHP 5.3.1以上版本时 ...

  6. awakeFromNib与initWithCoder

  7. Unity3D中切换场景可能导致材质变暗的问题

    Unity版本:5.1.1f1 做公司项目的时候,无意间发现了一个很奇葩的现象:场景A(纯UI,故没有光源)转场到场景B的时候,B中原有的光源似乎失效了,但在Hierarchy窗口中能够看到光源存在, ...

  8. Intent的属性及Intent-filter配置——指定Action、Category调用系统Activity

    Intent代表了启动某个程序组件的“意图”,实际上Intent对象不仅可以启动本应用内程序组件,也可启动Android系统的其他应用的程序组件,包括系统自带的程序组件——只要权限允许. 实际上And ...

  9. pureMVC简单示例及其原理讲解五(Facade)

    本节将讲述Facade,Proxy.Mediator.Command的统一管家.自定义Facade必须继承Facade,在本示例中自定义Facade名称为ApplicationFacade,这个名称也 ...

  10. Weex系列一、构建Weex工程

    Weex比React Native更简单,更容易学习,并且做到真正的跨平台,一套代码可以多个平台运行.所以建议大家都用Weex吧. 一.安装Node 已经安装Node的,请忽略过去. 检查Node是否 ...