
” 写作ZooKeeper留学生作业、Python程序 辅导Assignment 2DescriptionYou will implement a replicated key-value data store maintained by N servers. Each server willmaintain a copy of the data store and expose two functionsRead(key): will read the value associated with the key.Add_Update(key, value): will add/update the value associated with the key.A client may contact any of the server to read/add/update the data store. All Add/Update(key,value) requests will be routed to the leader server, who will be responsible for any writerequests to the key-value. The leader will also be responsible for propagating the updates to otherreplicas. You Will use ZooKeeper (a coordination service) to elect the leader (see moreinformation below on how to use ZooKeeper for leader lection).All Read(key) request will be served as follows. If the key exists in the local data store, it willsend the value even if it is stale. Else, it will return empty.ImplementationYou will create a server program (e.g., server.py) that spawns the server process and connects tothe ZooKeeper service.python server.py host hostip port xxxx -zookeeper zkip -zookeeper_portzkportThis will spawn a server that will connect to the zookeeper service, create a /election/ znodeand register itself in the /election/ znode. Servers under the /election/ znode willparticipate in the election, and a server will be identified as a leader.Use Znode type SEQUENTIAL, so when the server registers under /election/ it is assigned avalue server_id. e.g., /election/server_000000001.For leader election, query all the children nodes under election and select the znode with thesmallest sequence number.You may use REST protocols or RPCs for implementing the add/update and read functions.TestingYou will provide a driver-test program that will spawn the servers and connect to the zookeeperservice. The test Driver program will also be responsible for sending / receiving key value storeupdates to one of the servers. Use the Docker Zookeeper service script to spawn zookeeperservers and test the following scenarios:- Add and Read: Start 3 servers. The servers elect a leader and all add requests are routedto the leader. Subsequent Read requests of the key can be fetched from any of the server.- Leader election: Same as above but kill the leader node. A new leader is elected and allsubsequent requests is routed to the new leader.- Stale Read: The killed leader is back online but is not the leader and may have stale data(if main data store Was updated). Since the key value is in memory, it will return emptyuntil the key was updated. Once key value is updated, the server will output the updatedvalue.Please put appropriate print statements.Extra Credit (10%)Start the Zookeeper service on Googles cloud platform and compare the leader election latencywith a local zookeeper service. Report the latency over multiple runs.SubmissionPlease upload your code and your report on Gradescope. Your submission should contain all thecode including the test cases and log files of your execution.Grading CriteriaComponent %Implementation 60Testing 30Code documentation 10Extra credit 10Zookeeper DocumentationLeader selection via ZookeeperZooKeeper is a Distributed, open-source coordination service for distributed applications. Itexposes a simple set of primitives that distributed applications can build upon to implementhigher level services for synchronization, configuration maintenance, and groups and naming. Itis designed to be easy to program to, and uses a data model styled after the familiar directory treestructure of file systems.Installation:We are going to use docker to install Zookeeper.Go get docker on: httpss://docs.docker.com/desktop/Once the docker is successfully installed, use your cmd/terminal to check via command:$ docker –versionGet Zookeeper from docker on: httpss://hub.docker.com/_/zookeeperThen we can use docker to get ZooKeeper Service setup on our machine via command:$ docker pull zookeeperCheck your installation with$ docker image lsTo lunch a Zookeeper server,$ docker run –name my-zookeeper –restart always -d zookeeperWhere the name sets the name of the container you started and -d sets the image the containerutilized. You can check running containers with$ docker container lsBy default, your Zookeeper will use following portsClient port::2181Follower port::2888Election port::3888AdminServer port::8080When a server is on, you can connect via commands,$ docker run -it –rm –link my-zookeeper:zookeeper zookeeper zkCli.sh -serverzookeeperWhere use your containers name to replace my-zookeeperProgramming in ZooKeeperOnline resources: httpss://www.tutorialspoint.com/zookeeper/index.htm)Example Python implementation : KAZOOIf you are using python. Kazoo is a good choice as an API for zookeeper.Documentation: httpss://kazoo.readthedocs.io/en/latest/basic_usage.htmlUsage:from kazoo.client import KazooClientzk = KazooClient(hosts=localhost:2181)zk.start()It supports all UI required for leader election such as zk.create(), zk.set(), zk.delete(). Etc.You can check znode under current znode with: zk.get_children(dir). The returned object will bea list of childrens names.Docker script to Run your Zookeeper server on replicate modeTo build a distributed file system we need at least 3 zookeeper servers, this requires us to run 3zookeeper servers as a service in docker. To do that:1. Initialize your docker to swarm mode to simulate distributed environment: dockerswarm init2. Use docker stack deploy to launch 3 servers (in following example we use zookeeper asour service name, you can replace that with any name you what when practicing):a. Use a config file which specifies server configs in a *.yml file (such aszookeeper.yml): an example on Appendixb. Then you can deploy stack via: docker stack deploy –compose-filezookeeper.yml zookeeperc. You can check the running service by: docker stack services zookeeper3. To gracefully exit (in following example we use zookeeper as our service name, you canreplace that with any name you what when practicing):a. Bring stack down via: docker stack rm zookeeperb. (optional)Bring service down via: docker service rm zookeeperc. Quit swam mode By: docker swarm leave forceTo run your leader election, first make sure you have a znode created for such purpose(such as /leader, /election)Appendixversion: 3.1services:zoo1:image: zookeeperrestart: alwayshostname: zoo1ports:- 2181:2181environment:ZOO_MY_ID: 1ZOO_SERVERS: server.1=0.0.0.0:2888:3888;2181server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181zoo2:image: zookeeperrestart: Alwayshostname: zoo2ports:- 2182:2181environment:ZOO_MY_ID: 2ZOO_SERVERS: server.1=zoo1:2888:3888;2181server.2=0.0.0.0:2888:3888;2181 server.3=zoo3:2888:3888;2181zoo3:image: zookeeperrestart: Alwayshostname: zoo3ports:- 2183:2181environment:ZOO_MY_ID: 3ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181server.3=0.0.0.0:2888:3888;2181如有需要,请加QQ:99515681 或邮箱:99515681@qq.com
“
添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导。






