In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "example analysis of Java consistent hash algorithm". In daily operation, I believe that many people have doubts in the example analysis of Java consistent hash algorithm. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Java consistent hash algorithm example analysis". Next, please follow the editor to study!
I. background
Google has a Jump consistent hash algorithm that can achieve the same balance as consistent hashing through mathematical operations.
Second, look at the code
Look at the code first. Here is the whole code.
Does it feel incredible? we understand all the syntax of this code, but together we can't understand it.
When I first saw this code, I looked confused.
This algorithm was created by John Lamping and Eric Veach of Google.
Third, the principle of algorithm
The consistent hash algorithm has two goals:
Balance. That is, the data is evenly distributed among all nodes.
Monotonicity. That is, when the number of nodes changes, only part of the data needs to be moved from the old node to the new node, and no other moves are needed.
We can infer some properties from this monotonicity.
Here the shilling f (key, n) is a consistent hash algorithm, and the output is the number between [0fline n), which represents the data on the corresponding node.
For any key, the output should be 0.
In order to keep it uniform, there should be a result of 1 stroke 2 with a result of 0 pencil 2 and a result output of 1 with a result of 1 hand 2.
When nagging 3, there should be a result of 1 pare 3 that remains 0, and the result of 1 Accord 3 remains 2.
Recursively, when the number of nodes changes from n to n = 1, the result of n / (n = 1) in f (key, n) should be unchanged, and the result of 1 / (n = 1) becomes n.
This is expressed by a probability formula, and this is the code.
With regard to this algorithm, you may not understand it directly.
So you need to use the actual data to simulate it, as shown in the following figure.
The key lies in the process from nasty 2 to nasty 3, where the probability of each number is transformed from 1 beat 2 to 1 prime 3.
After that, we can draw a rule: by adding a node, the probability that the data will not change is n / (nasty 1), and then multiplied by the probability of each previous number 1, we can get the latest probability 1 / (nasty 1) of each number.
From this, it can be easily calculated that the probability of each number of nasty 4 is 1 to 4. Since then, we can be sure that this algorithm is indeed effective.
The only disadvantage of this algorithm is that the complexity is too high, which is O (n).
So it needs to be optimized.
Fourth, algorithm optimization
In the previous section, we learned about the correctness of the f (key, n) algorithm.
In addition to the complexity of O (n), we can also determine that the later the loop, the lower the probability of changing the result.
Result change means that the result of a fixed key output changes with the addition of a node.
If we can quickly calculate under which nodes the fixed key has changed, we can quickly calculate the final answer.
Suppose that the result of a certain time is b, and after several probability tests, the next time it is changed to a, then the result of this key will not change no matter how the node changes from b to a murl.
According to the probability change formula of arrival in the previous section, the probability that the number of a new node will not change is n / (n = 1).
The probability that there will be no change from b to I is bgamble I (offset in the middle).
If we have a uniform random function r, when r
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.