ZK master-slaver mechanism

Source: Internet
Author: User

1. Basic Concepts

>>zookeeper handler (ZK handle) is a bit like a file handle, and opening a file preserves a file handle! The same is true: the establishment of a session to ZK server will have a ZK handler (can be used in any programming language).

>> move session:client can connect to any and only one server, but if it times out (not hearded from the server for a long time) This session can be migrated to another server, called Moving session

>> a client can have more than one session

2. Master-worker Example

2.1, Master: watch for new workers and Tasks,and assign the tasks to workers;//actually do 3 things here, and there is a sequence: first warch for a Vailabe worker then watch for tasks then assign

>> a system with only one process can become master, and in order to become master, a ephemeral znode must be created Call/master

1 // - E represents Znode mode for ephemeral  here the data host:port purpose is to allow others to access him outside of ZK ephemeral indicates if session fails Znode delete 2 ls/                                          //list The root of ZK tree3 get/master                            // Get Metadata

Watch is accompanied by znode, and if znode changes, there will be events.
Stat effect two x 1 get the attributes of a znode;2 only exist Znode set watch on
4 Stat/master True //parameter is true for immediately after path sets the watch

Then master needs to create some znode of the parent, and then master needs to watch which worker is available? Those tasks to be assigned? Then do the assignment.

Is persistent znode does not contain data, the function is to tell which worker is available? When will a task be assigned? Then assign to workersCreate/worker ""   create/tasks ""create/assigns ""ls///master need watch for changes in th E childer of/workers and/tasks, parameter true = Watchls/worker true for Znode created     ls/tasks True 

2.2, worker: register themselves to the system, the master indicates that he is available, so that the master can watch for new tasks

// The first step of the worker is to notify Master that it exists to perform taskle // A, in/workers create a ephemeral znodecreate-e/worker/worker1 "Host:port"// because Master is concerned/ Workers so once the Znode is created in Worker1, Master will observe the notification watcher::watchedevent state:syncconneted type: Nodechildrechanged paht:/workders//B, then create a parent znode/assign/worker1 to receive Assignments,and Watch it for new tasks by LS truecreate-e/assign/true to this worker ready to accept Assig N.

2.3. Client:create new tasks and wait for system reaction

This simplifies the task that the client requires to execute the cmd command

1, first to add a taskzkcli>create-s/tasks/task-  "cmd"   //mode is sequential, The user waits for the task to be executed for true3///assign/ worker1/task-000000 ""

1-s/tasks/task-  "cmd"   //mode is sequential, the user waits for the task to be executed

ZK master-slaver mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.