Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the ZooKeeper exception

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to solve the ZooKeeper exception, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Every ZooKeeper operation in Java API declares two types of exceptions, InterruptedException and KeeperException, in its throws clause.

(1) InterruptedException exception

If the operation is interrupted, an InterruptedException exception is thrown. There is a standard mechanism for unblocking methods in the Java language, that is, calling interrupt () on threads that have blocked methods. A successful cancel will result in an InterruptedException exception.

ZooKeeper also follows this mechanism, so you can use this method to cancel a ZooKeeper operation. Classes or libraries that use ZooKeeper usually propagate InterruptedException exceptions, allowing clients to cancel their operations. The InterruptedException exception does not mean that there is a fault, but rather that the corresponding operation has been cancelled, so in the example of configuring the service, the application can be aborted by propagating the exception.

(2) KeeperException exception

(1) if the ZooKeeper server sends an error signal or has a communication problem with the server, a KeeperException exception is thrown.

① has different subclasses of KeeperException exceptions for different error situations.

For example, KeeperException.NoNodeException is a subclass of KeeperException, and if you try to perform an operation on a znode that does not exist, the exception is thrown.

② every subclass of a KeeperException exception corresponds to a code about the error type information.

For example, the code for the KeeperException.NoNodeException exception is KeeperException.Code.NONODE

(2) there are two ways to handle KeeperException exceptions:

① catches a KeeperException exception and determines what remediation to take by examining its code

The other kind of ② is to capture the equivalent KeeperException subclass and perform the corresponding operation in each capture code.

(3) KeeperException anomalies can be divided into three categories.

Abnormal status of ①

A state exception occurs when an operation fails because it cannot be applied to the znode tree. State exceptions are usually caused by another process modifying the znode at the same time. For example, if a znode is first updated by another process, the process that performs the setData operation based on the version number will fail and receive a KeeperException.BadVersionException exception because the version numbers do not match. Programmers usually know that such conflicts always exist and write code to deal with them.

Some state exceptions indicate errors in the program, such as a KeeperException.NoChildrenForEphemeralsException exception, which is thrown when an attempt is made to create a child node under a brief znode.

② recoverable exception

Recoverable exceptions are those that an application can recover in the same ZooKeeper session. A recoverable exception is represented by KeeperException.ConnectionLossException, which means that the connection to ZooKeeper has been lost. ZooKeeper will try to reconnect, and in most cases the reconnection will succeed and ensure that the session is complete.

However, ZooKeeper cannot determine whether the operation related to the KeeperException.ConnectionLossException exception was performed successfully. This situation is an example of partial failure. At this point, the programmer has the responsibility to resolve this uncertainty and take appropriate action according to the situation of the application. At this point, it is necessary to distinguish between "idempotent" operations and "Nonidempotent" operations. Idempotent operations are those that are executed one or more times and produce the same result, such as read requests or setData operations that are executed unconditionally. For idempotent operations, you simply need to retry. For non-idempotent operations, you can't retry blindly, because the result of multiple execution is completely different from that of one execution. The program can detect whether the update of the non-idempotent operation has been completed by encoding information in the path of the znode and its data.

③ unrecoverable exception

In some cases, the ZooKeeper session fails-- either because of a timeout or because the session is closed, in both cases a KeeperException.SessionExpiredException exception is received, or a KeeperException.AuthFailedException exception is received due to authentication failure. In either case, all transient znode associated with the session will be lost, so the application needs to rebuild its state before reconnecting to the ZooKeeper.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report