Distributed cluster framework - zookeeper required interview questions②

15. Data synchronization

After the entire cluster completes the Leader election, the Learner (collectively referred to as Follower and Observer) returns to the Leader server to register. When the Learner server completes the registration with the Leader server, it enters the data synchronization link. Data synchronization process: (all in the form of message passing)

Learner registers with Learner

Data Synchronization Synchronization Confirmation

Zookeeper's data synchronization is usually divided into four categories:

  1. Direct Differential Sync (DIFF Sync)
  2. Rollback first and then differential synchronization (TRUNC+DIFF synchronization)
  3. Rollback-only synchronization (TRUNC synchronization)
  4. Full sync (SNAP sync)

Before data synchronization, the Leader server will complete data synchronization initialization:

peerLastZxid:

∙ Extract lastZxid (the last ZXID processed by the learner server) from the ACKEPOCH message sent when the learner server registered

minCommittedLog:

∙ The leader server Proposal cache queue committedLog is the smallest

ZXIDmaxCommittedLog:

∙ The largest ZXID in the leader server Proposal cache queue committedLog direct differential synchronization (DIFF synchronization)

∙ Scenario: peerLastZxid is between minCommittedLog and maxCommittedLog, first rollback and then differential synchronization (TRUNC+DIFF synchronization)

∙ Scenario: When the new leader server finds that a learner server contains a transaction record that it does not have, it needs to let the learner server roll back the transaction – roll back to the one that exists on the leader server and is also the closest to peerLastZxid ZXID rollback only sync (TRUNC sync)

∙ Scenario: peerLastZxid is greater than maxCommittedLog Full synchronization (SNAP synchronization)

∙ Scenario 1: peerLastZxid is less than minCommittedLog

∙ Scenario 2: There is no Proposal cache queue on the Leader server and peerLastZxid is not equal to lastProcessZxid

16. How does zookeeper ensure the sequential consistency of transactions?

Zookeeper uses a globally incremented transaction Id to identify, all proposals (proposals) are added with zxid when they are proposed, zxid is actually a 64-bit number, and the upper 32 bits are epoch (period; era; era; New era) is used to identify the leader cycle. If a new leader is generated, the epoch will increase automatically, and the lower 32 bits are used to count up. When a new proposal is generated, it will first send a transaction execution request to other servers according to the two-stage process of the database. If more than half of the machines can execute and succeed, then the execution will start.

17. Why is there a master node in a distributed cluster?

In a distributed environment, some business logic only needs to be executed by a certain machine in the cluster, and other machines can share the result, which can greatly reduce repeated calculations and improve performance, so leader election is required.

18. How to deal with zk node downtime?

Zookeeper itself is also a cluster, and it is recommended to configure no less than 3 servers. Zookeeper itself also ensures that when a node goes down, other nodes will continue to provide services.

If a Follower goes down, there are still two servers to provide access, because there are multiple copies of the data on Zookeeper, and the data will not be lost;

If a Leader goes down, Zookeeper will elect a new Leader.

The mechanism of the ZK cluster is that as long as more than half of the nodes are normal, the cluster can provide services normally. The cluster fails only when there are too many ZK nodes and only half or less than half of the nodes can work.

So a cluster with 3 nodes can hang up 1 node (leader can get 2 votes>1.5)

A cluster with 2 nodes cannot hang any 1 node (leader can get 1 vote<=1)

19. The difference between zookeeper load balancing and nginx load balancing

The load balancing of zk can be adjusted, nginx can only adjust the weight, and other controllable ones need to write their own plug-ins;

However, the throughput of nginx is much larger than that of zk. It should be said which method is used according to the business.

20. What are the deployment modes of Zookeeper?

Zookeeper has three deployment modes:

  1. Stand-alone deployment: run on a cluster;
  2. Cluster deployment: running multiple clusters;
  3. Pseudo-cluster deployment: A cluster starts multiple Zookeeper instances to run.

21. How many machines are required for the cluster at least, and what are the cluster rules? There are 3 servers in the cluster, and one of the nodes is down. Can Zookeeper still be used at this time?

The cluster rule is 2N+1 units, N>0, that is, 3 units. It can continue to be used, and the singular server can continue to be used as long as no more than half of the servers are down.

22. Does the cluster support dynamic addition of machines?

In fact, it is horizontal expansion, and Zookeeper is not very good in this regard. Two ways:

Restart all: Close all Zookeeper services and start them after modifying the configuration. Previous client sessions are not affected.

Restart one by one: Under the principle that more than half of the machines are alive and available, restarting a machine does not affect the external services provided by the entire cluster. This is the more common way. Version 3.5 began to support dynamic expansion.

23. Is Zookeeper's watch notification to the node permanent? Why not forever?

no. Official statement: A Watch event is a one-time trigger. When the data that is set to Watch changes, the server will send this change to the client that is set to Watch, so as to notify them.

Why is it not permanent? For example, if the server changes frequently, and there are many listening clients, all clients must be notified of each change, which puts a lot of pressure on the network and server.

Generally, the client executes getData("/node A", true), if node A is changed or deleted, the client will get its watch event, but after that node A changes again, and the client does not set watch event, it will no longer be sent to the client. In practical applications, in many cases, our client does not need to know every change of the server, I only need the latest data.

24. What are the Java clients of Zookeeper?

Java client: zkclient that comes with zk and Apache open source Curator.

25. What is chubby, and what do you think compared with zookeeper?

Chubby is owned by Google, which fully implements the Paxos algorithm and is not open source. zookeeper is an open source implementation of chubby, using the zab protocol, a variant of the paxos algorithm.

26. Talk about some commonly used commands of zookeeper

Common commands: ls get set create delete etc.

27. What is the connection and difference between ZAB and Paxos algorithm?

Same point:

  1. Both have a role similar to the Leader process, which is responsible for coordinating the operation of multiple Follower processes
  2. The Leader process will wait for more than half of the Followers to give correct feedback before submitting a proposal
  3. In the ZAB protocol, each Proposal contains an epoch value to represent the current

The Leader cycle, the name in Paxos is Ballot. Differences:

ZAB is used to build a highly available distributed data master and backup system (Zookeeper), and Paxos is used to build a distributed consistent state machine system.

28. Typical application scenarios of Zookeeper

Zookeeper is a typical publish/subscribe model distributed data management and coordination framework, developers can use it to publish and subscribe to distributed data.

Through the cross-use of the rich data nodes in Zookeeper and the Watcher event notification mechanism, it is very convenient to build a series of core functions that will be involved in distributed applications, such as:

  1. Data publish/subscribe
  2. load balancing
  3. naming service
  4. Distributed coordination/notification
  5. cluster management
  6. Master Election
  7. distributed lock
  8. Distributed Queue Data Publish/Subscribe Introduction The data publish/subscribe system, the so-called configuration center, as the name implies, is that the publisher publishes data for the subscriber to subscribe to.

Purpose Dynamically acquire data (configuration information) to achieve centralized management of data (configuration information) and dynamic update design mode of data

push mode

pull mode

Data (Configuration Information) Properties

  1. The amount of data is usually relatively small
  2. Data content is dynamically updated at runtime
  3. Each machine in the cluster is shared and the configuration is consistent

Such as: machine list information, runtime switch configuration, database configuration information, etc. based on Zookeeper

∙ Data storage: store data (configuration information) to a data node on Zookeeper

∙ Data acquisition: the application reads data from the Zookeeper data node at the startup initialization node, and registers a data change Watcher on the node

∙ Data change: When changing data, update the corresponding node data of Zookeeper, Zookeeper will send a data change notification to each client, and the client can re-read the changed data after receiving the notification.

load balancing

The naming service of zk The naming service refers to obtaining the address of a resource or service through a specified name, using zk to create a global path, and this path can be used as a name, pointing to the address of the service provided by the cluster in the cluster, or a Remote objects and so on.

Distributed notification and coordination

For system scheduling: the operator sends a notification to change the status of a node through the console, and then zk sends these changes to all clients registered with the watcher of this node.

For execution reporting: each worker process creates a temporary node in a certain directory. And carry the progress data of the work, so that the summarized process can monitor the changes of the sub-nodes of the directory to obtain the real-time global situation of the work progress.

zk's naming service (file system) naming service refers to obtaining the address of a resource or service through a specified name, using zk to create a global path, which is the only path, and this path can be used as a name to point to the A cluster, the address of a service provided, or a remote object, etc.

Zk's configuration management (file system, notification mechanism) program is distributed and deployed on different machines, and the configuration information of the program is placed under the znode of zk. When any configuration changes, that is, when the znode changes, you can pass Change the content of a directory node in zk, and use the watcher to notify each client to change the configuration.

Zookeeper cluster management (file system, notification mechanism)

The so-called cluster management does not care about two points: whether there are machines exiting and joining, and electing the master.

For the first point, all machines agree to create temporary directory nodes under the parent directory, and then monitor the child node change messages of the parent directory node. Once a machine hangs up, the connection between the machine and zookeeper is disconnected, the temporary directory node created by it is deleted, and all other machines are notified: a sibling directory is deleted, so everyone knows: it is on board up.

The addition of a new machine is similar. All machines receive a notification: a new brother directory is added, and highcount is available again. For the second point, we slightly change it. All machines create temporary sequential numbered directory nodes, and each time the machine with the smallest number is selected as the master just fine.

Zookeeper distributed lock (file system, notification mechanism)

With zookeeper's consistent file system, the problem of locking becomes easy. Lock services can be divided into two categories, one is to maintain exclusive, and the other is to control timing.

For the first category, we regard a znode on zookeeper as a lock, which is realized by createznode. All clients create a /distribute_lock node, and the client that is successfully created finally owns the lock. Release the lock after deleting the distribute_lock node created by yourself.

For the second category, /distribute_lock already exists in advance, and all clients create temporary sequence numbered directory nodes under it, which is the same as selecting a master, the one with the smallest number acquires the lock, and deletes it when it is used up, which is convenient in order.

Zookeeper queue management (file system, notification mechanism) has two types of queues:

  1. Synchronous queue, when all members of a queue are gathered, this queue is available, otherwise it has been waiting for all members to arrive.
  2. The queue performs enqueue and dequeue operations in FIFO mode.

The first category is to create a temporary directory node under the agreed directory, and monitor whether the number of nodes is the number we require.

The second category is consistent with the basic principle of the control timing scenario in the distributed lock service. Enqueries are numbered and dequeues are numbered. Create a PERSISTENT_SEQUENTIAL node in a specific directory. When the creation is successful, the Watcher will notify the waiting queue, and the queue will delete the node with the smallest serial number for consumption. In this scenario

Zookeeper's znode is used for message storage. The data stored in znode is the message content in the message queue. The SEQUENTIAL serial number is the number of the message, which can be taken out in order. Since the created nodes are persistent, there is no need to worry about the loss of queue messages.

29. What functions does Zookeeper have?

1) Cluster management: monitor node survival status, running requests, etc.;

2) Master node election: After the master node hangs up, a new round of master election can be started from the standby node. The master node election refers to the election process, and Zookeeper can assist in completing this process;

3) Distributed locks: Zookeeper provides two types of locks: exclusive locks and shared locks. Exclusive locks mean that only one thread can use resources at a time, shared locks are read locks shared, read and write mutual exclusion, that is, multiple threads can be used

Threads read the same resource at the same time. If you want to use a write lock, only one thread can use it.

Zookeeper can control distributed locks.

4) Naming service: In a distributed system, by using the naming service, the client application can obtain information such as the address of the resource or service, the provider, etc. according to the specified name.

30. Tell me about the notification mechanism of Zookeeper?

The client will create a watcher event for a certain znode. When the znode changes, these clients will receive the zk notification, and then the client can make business changes according to the change of the znode.

31. What is the relationship between Zookeeper and Dubbo?

The role of Zookeeper:

Zookeeper is used to register services and perform load balancing. Which service is provided by which machine must be known to the caller. Simply put, it is the correspondence between the IP address and the service name. Of course, this correspondence can also be implemented in the caller's business code by hardcoding, but if the machine that provides the service

The hanged caller cannot know, if the code is not changed, it will continue to request the hung machine to provide services.

Zookeeper can detect the hanging machine through the heartbeat mechanism and delete the corresponding relationship between the ip and the service of the hanging machine from the list. As for supporting high concurrency, simply speaking, it is horizontal expansion, and the computing power can be improved by adding machines without changing the code. By adding new machines to register services with zookeeper, more service providers can serve more customers.

dubbo:

It is a tool for managing the middle layer. There are a lot of service access and service providers need to be scheduled between the business layer and the data warehouse. Dubbo provides a framework to solve this problem.

Note that the dubbo here is just a frame, as for what you put on the shelf is completely up to you, just like a car skeleton, you need to match your wheel engine. To complete the scheduling in this framework, there must be a distributed registration center to store the metadata of all services. You can use zk or others, but everyone uses zk.

The relationship between zookeeper and dubbo:

Dubbo abstracts the registration center, and it can connect different storage media to provide services for the registration center, including ZooKeeper, Memcached, Redis, etc.

The introduction of ZooKeeper as a storage medium also introduces the characteristics of ZooKeeper. The first is load balancing. The carrying capacity of a single registration center is limited. When the traffic reaches a certain level, it needs to be distributed. Load balancing exists for the purpose of distribution. A ZooKeeper group can easily achieve load balancing with the corresponding web applications. ;Resource synchronization, load balancing alone is not enough, data and resources between nodes need to be synchronized, ZooKeeper clusters naturally have such a function; naming service, the tree structure is used to maintain the global service address list, service provider When starting up, write your own URL address to the specified node /dubbo/${serviceName}/providers directory on ZooKeeper, and this operation completes the release of the service. Other features include Mast elections, distributed locks, etc.

Guess you like

Origin blog.csdn.net/qq_53142796/article/details/132610566