Distributed collaboration is indispensable in distributed applications. It usually acts as a coordinator, or isolates the responsibilities of multi-machine collaboration from distributed applications to reduce system coupling and enhance scalability. Apache's Zookeeper and google's Chubby are both real-time users of distributed collaboration. Fourinone can actually be used as a Zookeeper. It uses the least code to implement all functions of Zookeeper, and tries to make it more functional but more concise.
I. Implementation Principle
Fourinone's implementation of distributed collaboration is achieved by establishing a domain, node information with two-layer structures, domain can be classification or package, and node can be specific attributes, domain and node are designed based on their own needs. For example, you can name domain as ". b. c... "indicates a tree category.
A domain can have multiple nodes, each of which only specifies one domain. All nodes under it can be returned through the domain.
The domain does not need to be created separately. Generally, it is automatically created if no domain exists when node is created.
If no node exists in the domain, the domain is automatically deleted.
If a domain is deleted, all nodes under the domain are also deleted.
Each node can store a value, which can be any object.
All node information is stored in parkserver. parkserver provides the collaborators function. As shown in:
650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1159132404-0.png "/>
As you can see, other distributed processes can use parkserver's user interface ParkLocal to add, modify, delete, specify heartbeat, and specify permissions on nodes, in addition, combined with parkserver, it provides features such as synchronous backup, leader election, and expiration time settings to achieve a wide range of distributed collaboration functions, such:
1. Distributed configuration: the application of multiple machines shares one configuration information and fails to be elected as a leader. For details, see the guide and demo.
2. Distributed locks: multiple machines compete for one lock. When a machine releases or fails, other machines can compete for the lock to continue. For details, see the guide and demo.
3. Cluster Management. machines in the cluster can perceive each other and elect leaders. For details, see the guide and demo.
Ii. Core APIs
ParkLocal core api description:
// Create a node. You can call different methods based on the required permissions and heartbeat attributes.
Public ObjectBean create (String domain, Serializable obj); // automatically create a node
Public ObjectBean create (String domain, String node, Serializable obj );
Public ObjectBean create (String domain, String node, Serializable obj, AuthPolicy auth );
Public ObjectBean create (String domain, String node, Serializable obj, boolean heartbeat );
Public ObjectBean create (String domain, String node, Serializable obj, AuthPolicy auth, boolean heartbeat );
// Update the node
Public ObjectBean update (String domain, String node, Serializable obj );
// Obtain the node
Public ObjectBean get (String domain, String node );
// Obtain the latest node and pass in the old node for comparison
Public ObjectBean getLastest (String domain, String node, ObjectBean ob );
// Obtain the latest domain
Public List <ObjectBean> get (String domain );
// Obtain all nodes in the latest domain. You need to input the old node set for comparison.
Public List <ObjectBean> getLastest (String domain, List <ObjectBean> oblist );
// Delete a node
Public ObjectBean delete (String domain, String node );
// Forcibly set domain to delete
Public boolean setDeletable (String domain );
// Delete the domain and all nodes
Public List <ObjectBean> delete (String domain );
// Add node event listening
Public void addLastestListener (String domain, String node, ObjectBean ob, LastestListener liser );
// Add domain event listening
Public void addLastestListener (String domain, List <ObjectBean> oblist, LastestListener liser );
Iii. Permission mechanism:
Public ObjectBean create (String domain, String node, Serializable obj, AuthPolicy auth );
When creating a node through the above method, you can specify a permission parameter with three attributes: Read-Only AuthPolicy. OP_READ), read and write AuthPolicy. OP_READ_WRITE, and all AuthPolicy. OP_ALL. The default value is AuthPolicy. OP_ALL.
Note: The permission attribute here refers to the permission constraints of the creation process on other processes used, excluding itself. That is, for the node creation process, it has all the operation permissions for the node and domain, as long as it does not exit or stop)
A node with the domain d and node n is created. For other processes, the Operation permissions are shown in the following table:
650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/115913F34-1.jpg "/>
From the table above, we can find that when the creation process specifies the node permission to AuthPolicy. OP_ALL, other processes can delete the node but cannot delete its domain. Why?
Because domain usually has other nodes, their permissions are not all AuthPolicy. OP_ALL. For example, the node permission of n1 is AuthPolicy. OP_READ: according to the normal operation, the process cannot delete n1. Assuming that it can delete domain, it finally indirectly deletes n1, so there is a paradox. Therefore, in order to avoid risks, all processes can only delete the node according to the permission, but cannot delete the domain.
However, you are allowed to take such a risk of deletion. You can also forcibly specify the domain to be deleted in the creation process. After the domain is created, you can call:
Public boolean setDeletable (String domain );
This method can only be called by the domain creation process. Other processes do not have the permission to call this method.
After the domain can be deleted by force, other processes can directly Delete the domain and its nodes and ignore the consequences.
Iv. advantages over zookeeper
Zookeeper is undoubtedly a successful open-source product with extensive trust and application scenarios. As in the past, foreigners published a product on the apache website, our engineers will learn modestly and defend faithfully immediately, while original products made in China will often be challenged in a hundred ways, because our originality is mostly plagiarism and abuse, our domestic products are mostly framework integration rather than architecture design, so this kind of sentiment trend cannot be changed in a day.
Product comparison and listing advantages often lead to heated debate. They are considered to be promoting and guiding the use of products. In fact, they can meet functional requirements, it is a political issue rather than a technical issue to choose which product to use. The leader's will and the familiarity and hobbies of engineers are all decisive factors.
Here we will only elaborate on several advantages from the technical point of view. Zookeeper, as a chubby and paxos imitation product, lacks innovative design improvements and still has the following Disadvantages:
1. Tree-based node configuration is complex and has low performance. To ensure this structure, Zookeeper needs to maintain the overhead of a virtual file structure, which affects the performance of Tree nodes with a deep directory structure. In fact, the configuration information structure usually does not necessarily require a tree structure.
2. Rigid Design of the "watch" mechanism: zookeeper does not support the method for obtaining the latest version of information. It can only register a watch when writing updates and other methods, after these methods are called, the callback does not consider whether the information content changes. For updates that do not change the information content, zookeeper still calls back and the callback of zookeeper is relatively dull, it can only be used once. If the information keeps changing, you must register watch again. The fourinone event processing can freely control whether to continuously respond to information changes.
3. The leader election mechanism is too limited. The cluster has only two nodes, and zookeeper cannot be elected as the leader. zookeeper's leader election must be subject to odd node odd restrictions. In addition, although ZooKeeper's Leader election implementation is simpler than the original Paxos, it still has Leader, followers Follower, observer, and Learner) and so on. Compared with the fourinone leader election, zookeeper is still not intuitive and concise, and it is difficult to use less configuration and code demonstration.
4. linux Shell installation is not supported on Windows systems and is recommended only for study. Fourinone supports hybrid use of windows and linux clusters.
Fourinone proposed a new distributed collaborative system design that satisfies all zookeeper functions and overcomes the above shortcomings, A new configuration structure, change event mechanism, and simplified leader election are proposed to better meet Distributed coordination requirements.
V. demo
The following is a demo of an operation node. Pay attention to the permission range of each node. program description:
1. ParkServerDemo: the IP port used to start parkserver is already specified in the SERVERS of the PARK section of the configuration file.
2. ParkSet: four nodes (d1n1, d2n2, d3n3, and d4n4) are created in parkserver, corresponding to read-only and read-write, respectively. All nodes have the permissions forcibly deleted.
3. ParkGet: read, write, delete, and delete domain operations on d1n1, d2n2, d3n3, and d4n4 in sequence. Observe the result output. If you do not have the permission to operate, parkserver will output information, the result object returned by the operation is null.
Startup command and sequence:
Javac-classpath fourinone. jar; *. java
Java-classpath fourinone. jar; ParkServerDemo
Java-classpath fourinone. jar; ParkSet
Java-classpath fourinone. jar; ParkGet
If you do not have fourinone. jar, you can download it at the following address:
Http://www.skycn.com/soft/68321.html
The attachment is the demo source code.
This article is from the "fourinone distributed computing" blog, please be sure to keep this source http://3503265.blog.51cto.com/3493265/1058623