Reprinted from http://blog.fens.me/zookeeper-queue/
Let Hadoop run in the Cloud series on How to integrate virtualization and Hadoop, let Hadoop clusters run on VPS virtual hosting, and provide storage and computing services to users through the cloud.
Now hardware is getting cheaper, a non-brand server, 2 24-core CPU, with 48G of memory, 2T of hard disk, has fallen below 20,000 yuan. This configuration is obviously a waste of luxury if you simply put a few web applications in place. Even if it is used to achieve single node Hadoop, the waste of computing resources is very high. For such a high-performance computer, how to effectively use computing resources, it becomes an important issue of cost control.
With virtualization technology, we can split a server into 12 VPS, each 2-core cpu,4g memory, 40G hard disk, and support resource reallocation. What a great technology! Now we have a 12-node Hadoop cluster that lets Hadoop run in the cloud and accelerate the world.
About
- Zhang Dan (Conan), programmer Java,r,php,javascript
- Weibo: @Conan_Z
- Blog:http://blog.fens.me
- Email: [Email protected]
Reprint please specify the source:
Http://blog.fens.me/zookeeper-queue
Objective
Zookeeper is a collaborative, collaborative system, which zookeeper value and how to embody it. Through the case of distributed queues in this article, you will learn about the power of zookeeper. For basic use of zookeeper, please refer to: Zookeeper Pseudo-step cluster installation and use
Directory
- Distributed queues
- Design ideas
- Program implementation
1. Distributed queues
Queues have many kinds of products, most of which are implemented by messaging systems, such as ACTIVEMQ,JBOSSMQ,RABBITMQ,IBM-MQ. There are not too many steps in the queued product, like BEANSTALKD.
The distributed pair of columns implemented in this paper is a synchronized, zookeeper-based, synchronous queue that is available only when a member of a queue is NAND, otherwise it waits for all members to arrive.
2. Design Ideas
Create a parent directory/queue, each member of the Monitor (WATCH) flag bit directory/queue/start exists, and then each member joins the queue, the way to join the queue is to create a temporary directory node/queue/x (i), and then each member gets/queue All directory nodes of the directory, that is, X (i). Determine if the value of I is already the number of members, and if it is less than the number of members waiting for/queue/start to appear, create/queue/start if it is already equal.
Product flowchart
Application examples
Icon Explanation
- APP1,APP2,APP3,APP4 is a 4 independent business system.
- ZK1,ZK2,ZK3 is the 3 connection point of the zookeeper cluster
- /queue, is the Znode queue, assuming that the queue length is 3
- /queue/x1, is the Znode queue, row 1th, submitted by APP1, synchronous request, App1 mount wait
- /QUEUE/X2, is the Znode queue, number 2nd pairs, submitted by APP2, synchronous request, app2 hang wait
- /queue/x3, is the Znode queue, number 3rd pairs, submitted by APP3, synchronous request, app3 hang wait
- /queue/start, when the Znode queue is full, triggers the creation of the start node
- When/qeueu/start is created, APP4 is started, all ZK connections are notified to the Synchronizer (red line), the queue is complete, all programs end
Note:
- 1). Create/queue/x1,/queue/x2,/queue/x3 There is no back-and-forth order, and the program will hang synchronously after submission.
- 2). App1 can be submitted via ZK2, APP2 can also be submitted via ZK3
- 3). App1 can submit 3 requests, generate X1,X2,X3 use queue full
- 4). After/queue/start is created, ZK1 will listen to this event and tell App1 that the queue is complete!
3. Program implementation
1). Single-node simulation experiment
Analog App1, submit 3 requests via ZK1
public static void doOne() throws Exception { String host1 = "192.168.1.201:2181"; ZooKeeper zk = connection(host1); initQueue(zk); joinQueue(zk, 1); joinQueue(zk, 2); joinQueue(zk, 3); zk.close(); }
Create a connection to the server
public static ZooKeeper connection(String host) throws IOException { ZooKeeper zk = new ZooKeeper(host, 60000, new Watcher() { // 监听/queue/start创建的事件 public void process(WatchedEvent event) { if (event.getPath() != null && event.getPath().equals("/queue/start") && event.getType() == Event.EventType.NodeCreated) { System.out.println("Queue has Completed.Finish testing!!!"); } } }); return zk; }
Out of the beginning queue
public static void initQueue(ZooKeeper zk) throws KeeperException, InterruptedException { System.out.println("WATCH => /queue/start"); zk.exists("/queue/start", true); if (zk.exists("/queue", false) == null) { System.out.println("create /queue task-queue"); zk.create("/queue", "task-queue".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } else { System.out.println("/queue is exist!"); } }
Increase the queue node
public static void joinQueue(ZooKeeper zk, int x) throws KeeperException, InterruptedException { System.out.println("create /queue/x" + x + " x" + x); zk.create("/queue/x" + x, ("x" + x).getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); isCompleted(zk); }
Check if the queue is complete
public static void isCompleted(ZooKeeper zk) throws KeeperException, InterruptedException { int size = 3; int length = zk.getChildren("/queue", true).size(); System.out.println("Queue Complete:" + length + "/" + size); if (length >= size) { System.out.println("create /queue/start start"); zk.create("/queue/start", "start".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } }
Start function Main
public static void main(String[] args) throws Exception { doOne();}
Operation Result:
WATCH => /queue/start/queue is exist!create /queue/x1 x1Queue Complete:1/3create /queue/x2 x2Queue Complete:2/3create /queue/x3 x3Queue Complete:3/3create /queue/start startQueue has Completed.Finish testing!!!
Perfectly in line with what I expected. Next we look at the distributed environment
2). Distributed simulation Experiment
Analog App1 submitted via ZK1 X1,app2 via Zk2 commit x2,app3 via ZK3 commit x3
public static void doAction(int client) throws Exception { String host1 = "192.168.1.201:2181"; String host2 = "192.168.1.201:2182"; String host3 = "192.168.1.201:2183"; ZooKeeper zk = null; switch (client) { case 1: zk = connection(host1); initQueue(zk); joinQueue(zk, 1); break; case 2: zk = connection(host2); initQueue(zk); joinQueue(zk, 2); break; case 3: zk = connection(host3); initQueue(zk); joinQueue(zk, 3); break; } }
Note:
- 1). For the sake of simplicity, we do not have the mechanism to add complex multithreaded controls.
- 2). The Zk.close () method is not called, that is, App1 executes a separate commit, App1 ends, but Zk1 still exists, so/queue/x1 exists in the queue.
- 3). Program Start method, 3 start, command line to pass different parameters, respectively, is the three-way
Executive App1–>zk1
#日志输出WATCH => /queue/start/queue is exist!create /queue/x1 x1Queue Complete:1/3#zookeeper控制台[zk: 192.168.1.201:2181(CONNECTED) 4] ls /queue[x10000000011]
Executive APP2–>ZK2
#日志输出WATCH => /queue/start/queue is exist!create /queue/x2 x2Queue Complete:2/3#zookeeper控制台[zk: 192.168.1.201:2181(CONNECTED) 5] ls /queue[x20000000012, x10000000011]
Executive App3–>zk3
#日志输出WATCH => /queue/start/queue is exist!create /queue/x3 x3Queue Complete:3/3create /queue/start startQueue has Completed.Finish testing!!!#zookeeper控制台[zk: 192.168.1.201:2181(CONNECTED) 6] ls /queue[x30000000016, x10000000014, start, x20000000015]
/queue/stats was built to print out "queue has completed.finish testing!!!", representing the call APP4 completed!
We completed the distributed queue experiment due to the time rush. Text description and code inevitably have some problems, please find the problem of classmates help correct.
The complete code is posted below:
Package Org.conan.zookeeper.demo;import Java.io.ioexception;import Org.apache.zookeeper.createmode;import Org.apache.zookeeper.keeperexception;import Org.apache.zookeeper.watchedevent;import Org.apache.zookeeper.watcher;import Org.apache.zookeeper.zookeeper;import Org.apache.zookeeper.ZooDefs.Ids; public class Queuezookeeper {public static void main (string[] args) throws Exception {if (args.length = = 0) { Doone (); } else {doAction (Integer.parseint (args[0])); }} public static void Doone () throws Exception {String host1 = "192.168.1.201:2181"; ZooKeeper ZK = connection (host1); Initqueue (ZK); Joinqueue (ZK, 1); Joinqueue (ZK, 2); Joinqueue (ZK, 3); Zk.close (); public static void DoAction (int client) throws Exception {String host1 = "192.168.1.201:2181"; String host2 = "192.168.1.201:2182"; String host3 = "192.168.1.201:2183"; ZooKeeper ZK = null; Switch (client) {Case 1:ZK = connection (host1); Initqueue (ZK); Joinqueue (ZK, 1); Break Case 2:ZK = connection (HOST2); Initqueue (ZK); Joinqueue (ZK, 2); Break Case 3:ZK = connection (HOST3); Initqueue (ZK); Joinqueue (ZK, 3); Break }}//Create a connection to the server public static ZooKeeper connection (String host) throws IOException {ZooKeeper ZK = new ZooKeeper (host, 60000, new Watcher () {//monitor all triggered events public void process (Watchedevent event) { if (event.gettype () = = Event.EventType.NodeCreated && event.getpath (). Equals ("/queue/start")) { System.out.println ("Queue has completed.finish testing!!!"); } } }); return ZK; } public static void Initqueue (ZooKeeper zk) throws Keeperexception, interruptedexception {SYstem.out.println ("WATCH =/queue/start"); Zk.exists ("/queue/start", true); if (zk.exists ("/queue", false) = = null) {System.out.println ("Create/queue task-queue"); Zk.create ("/queue", "Task-queue". GetBytes (), Ids.open_acl_unsafe, createmode.persistent); } else {System.out.println ("/queue is exist!"); }} public static void Joinqueue (ZooKeeper zk, int x) throws Keeperexception, interruptedexception {SYSTEM.O Ut.println ("create/queue/x" + x + "X" + x); Zk.create ("/queue/x" + x, ("X" + x). GetBytes (), Ids.open_acl_unsafe, createmode.ephemeral_sequential); IsCompleted (ZK); public static void IsCompleted (ZooKeeper zk) throws Keeperexception, interruptedexception {int size = 3; int length = Zk.getchildren ("/queue", true). Size (); System.out.println ("Queue Complete:" + length + "/" + size); if (length >= size) {System.out.println ("Create/queue/staRT start "); Zk.create ("/queue/start", "Start". GetBytes (), Ids.open_acl_unsafe, createmode.ephemeral); } }}
[reprint] Zookeeper implement distributed queue queues