===================================== Chopped Autumn |http://blog.csdn.net/quhongwei_zhanqiu=======================================
Snapshot is a datatree in memory at a certain moment of the image, zookeeper a certain mechanism will be timed to generate Datatree snapshot. FILESNAP implements the snapshot interface for writing data to a file, let's look at snap-related content.
2.1 Snapshot file format
Snapshot is in binary form exists in the file, we use UE to open a new snapshot file
The data in the snapshot file can be divided into two parts: header and body.
Header data Format:
fileheader{
int Magic//Magic number constant ZKSN represents zookeeper snapshot file
int version//version constant 2
Long dbid//dbid constant-1
}
It's strange here that both version and dbid are constants, and what does that mean, maybe the reserved fields are used for subsequent versions.
By the head field can calculate the head information occupies 4 + 4 + 8 =16bit fixed length
5 a 4 B 4E is the Magic ZKSN
00 00 00 02 is dbid number 2.
FF FF FF FF FF FF is hexadecimal-1
Body Data format:
Snapshot file header information, followed by the body part of the information, the body data size is dynamic is not fixed.
1) Map<long, integer> sessionwithtimeoutbody Information The previous section stores the live session in memory and the timeout period of the session
Oa.writeint (Sesssnap.size (), "count");
For (Entry<long, integer> entry:sessSnap.entrySet ()) {
Oa.writelong (Entry.getkey (). Longvalue (), "id");
Oa.writeint (Entry.getvalue (). Intvalue (), "timeout");
}
From the above sequence to the file code can be seen to write an int type field is used to store the number of sessionwithtimeout, and then in the traversal of the collection as a long an int in the form of writing
2) followed by the serialization of Datatree to the file.
Let's look at the serialization method of Datatree
Public Voidserialize (outputarchive OA, String tag) throws IOException {
Scount = 0;
Serializelist (Longkeymap, OA);
Serializenode (OA, Newstringbuilder (""));
if (root = null) {
Oa.writestring ("/", "path");
}
}
2.1) Serialization Longkeymap is a collection of ACL permissions stored in the Datatree
READINT ("map")//acl number of mappings?
while (Map > 0) {
Readlong ("Long")//This Long value Longkeymap key, function? Is the key to this set of ACLs
ReadInt ("ACLs")
while (ACLs) {
ReadInt ("perms")
ReadString ("scheme")
ReadString ("id")
}
}
2.2) storing data nodes in Datatree
ReadString ("path")//First Datanode ""
while (!path.equals ("/")) {//"/" represents the end of the path
Readrecord (node, "Node") includes:
Readbuffer ("Data")
Readlong ("ACL")
Deserialize (archive, "statpersisted") state storage includes:
Readlong ("Czxid")//createnode when the transaction number
Readlong ("Mzxid")//createnode when with Czxid, SetData when the transaction number
Readlong ("CTime")//Create node time
Readlong ("Mtime")//createnode is the same as CTime, SetData time
ReadInt ("version") data version number//createnode version 0,setdata
ReadInt ("Cversion")//createnode version 0, parent node +1 when adding/removing child nodes
ReadInt ("aversion") version number of the//createnode version for 0,setacl
Readlong ("Ephemeralowner")//Temporary node represents SessionID, non-temporary node this value is 0
Readlong ("Pzxid")//createnode when same as CZXID, child node transaction number when adding/removing child nodes
ReadString ("path")//Read the next path
}
3) file tail Check data
2F Snapshot file End 5-bit data to verify that the snapshot file is valid
00 00 00 11 int is the numeric value 1, which represents the next 11 character data
2F is Snapshot's terminator/
4) Serialization of snapshot
5)Snapshot deserialization
===================================== Chopped Autumn |http://blog.csdn.net/quhongwei_zhanqiu=======================================
2.zookeeper principle Analysis-snapshot of data storage