Park layout test blog

Course Outline:

Start source code analysis process
storage structure snapshot transaction log of
a start process
knowledge points:

Engineering structure introduced
to start the process of macro view of
the cluster starts a detailed flow
netty service mechanism
1. The introduction of engineering structures
Project Address: https://github.com/apache/zookeeper.git

Branch tag: 3.5.5

zookeeper-recipes: Example Source
zookeeper-client: C language client
zookeeper-server: the main source
2. Start SIPOC:
Pictures

[] Start example shows:
** server: ** ZooKeeperServerMain

** Client: ** ZooKeeperMain

3. Start the cluster in detail the process
load configuration:

zookeeper start the process stack

QuorumPeerMain # initializeAndRun // start the project
QuorumPeerConfig # parse // load config configuration
QuorumPeerConfig # parseProperties // parse config configuration
new DatadirCleanupManager // construct a data-cleaner
DatadirCleanupManager # start // start a scheduled task to clear snapshots expired
Code stack:

QuorumPeerMain # main // start main method
QuorumPeerConfig # parse // load zoo.cfg file
QuorumPeerConfig # parseProperties // resolution configuration
DatadirCleanupManager # start // start a scheduled task to clear the log
QuorumPeerConfig # isDistributed // determine whether the cluster model
ServerCnxnFactory # createFactory () // create a service defaults to NIO, recommended Netty
// create an initialization cluster manager /
QuorumPeerMain # getQuorumPeer
QuorumPeer # setTxnFactory
new new FileTxnSnapLog // data file management for detecting the snapshot and the log file
/
* initialize the database * /
new new ZKDatabase
ZKDatabase # createDataTree // create a data tree, all nodes are stored in this
// start the cluster: start a thread while
QuorumPeer # start //
QuorumPeer # loadDataBase // from the snapshot files and log files to load node and filled dataTree go
QuorumPeer # startServerCnxnFactory / / start netty or java nio service, open port 2181
AdminServer # start // start management services, netty http service default port is 8080
QuorumPeer # startLeaderElection // begin the election process
quorumPeer.join () // prevent the main process to exit
process descriptions:

main method to start
loading zoo.cfg configuration file
parsing configuration
create a service factory
to create threads cluster management
set the database file manager
set up a database
.... settings
start start the cluster management thread
to load data into memory node
start netty service, the client open port
start administrator Http service, the default port 8080
to start the election process
join management thread to prevent main process exits
4.netty service startup process:
service UML class diagram

image

Setting startup parameters netty

-Dzookeeper.serverCnxnFactory = org.apache.zookeeper.server.NettyServerCnxnFactory
initialization:

Key Code:

Initialization pipeline flow

channelHandler class is an internal message processor is concrete.

protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (secure) {
initSSL(pipeline);
}
pipeline.addLast("servercnxnfactory", channelHandler);
}
channelHandler 类结构图片

The execution stack:

NettyServerCnxnFactory # NettyServerCnxnFactory // initialize netty service factory

NettyUtils.newNioOrEpollEventLoopGroup // create IO thread group
NettyUtils # newNioOrEpollEventLoopGroup () // create a worker thread group
ServerBootstrap # childHandler (io.netty.channel.ChannelHandler) // add pipeline flow
NettyServerCnxnFactory # start // Bind port, and start the service netty
create a connection:

Whenever a new client connection comes in, it goes into this method to create NettyServerCnxn object. And added to the embodiment of cnxns

The execution stack

CnxnChannelHandler#channelActive

Construction of new NettyServerCnxn // connector
NettyServerCnxnFactory # addCnxn // added to the connector, and grouped based on the client IP
ipMap.get (addr) // IP-based group
read a message: the execution stack

CnxnChannelHandler#channelRead

NettyServerCnxn # processMessage // message processing
NettyServerCnxn # receiveMessage // received message
ZooKeeperServer # processPacket // message packet processing
org.apache.zookeeper.server.Request // package request object
org.apache.zookeeper.server.ZooKeeperServer # submitRequest // Submit request
org.apache.zookeeper.server.RequestProcessor the processRequest // # processing request
two transaction logs snapshot storage structure,
outline:
ZK all data are stored in memory, i.e. in zkDataBase. But all changes are for ZK data record to the transaction log, and the snapshot is generated when a certain number of times will be written to. It has pledged to back up data. Suffix is ZXID (the only thing that ID).

Transaction log: each CRUD, the log file which are stored in the
snapshot log: all the data stored at the specified time of the node
storage structure:
zkDdataBase database zk is the base class for all nodes which are stored in the class, and Zk data to make any changes will be based on the class. zk stored data is performed by DataTree the object, which was a map for storage.

image

UML 类图:

image

Read snapshot log:

org.apache.zookeeper.server.SnapshotFormatter
reading the transaction log:

org.apache.zookeeper.server.LogFormatter
snapshot configuration:
dataLogDir transaction log directory
zookeeper.preAllocSize previously open disk space for subsequent written to the transaction log, the default 64M
after zookeeper.snapCount be snapCount times each transaction log output, triggering a snapshot the default is 100,000
the number of snapshots retained automatically cleared when autopurge.snapRetainCount
autopurge.purgeInterval cleanup interval, in hours -1 means not automatically cleared.
Snapshot loading process:

ZooKeeperServer # loadData // load data
FileTxnSnapLog # restore // restore data
FileSnap # deserialize () // deserialize data
FileSnap # findNValidSnapshots // find a valid snapshot
Util # sortDataDir // sort files based on the suffix
persistence.Util # isValidSnapshot // verify valid snapshot file

Guess you like

Origin www.cnblogs.com/sunxinxin/p/12555948.html