当前位置:网站首页>ZK implementation of distributed global counter for cursor application scenario analysis
ZK implementation of distributed global counter for cursor application scenario analysis
2022-06-25 20:21:00 【Listen to the wind with your right ear】
Curator Implement distributed global counters
In a stand-alone environment , How to realize thread safety self increment among multiple threads ( Count ) Implementation method and principle of , What about in a distributed environment ? natural synchronized,lock,atomicInteger Based on Java Method cannot be satisfied , Because these can only be in the current JVM In the environment , In a distributed environment, there are multiple JVM Instance is a normal thing
Let's introduce Curator be based on Zookeeper Distributed counters implemented
Curator recipes The package implements DistributedAtomicInteger,DistributedAtomicLong Equally distributed atomic self increasing counter
Simple application
public class CuratorAtomicInteger {
/** zookeeper Address */
static final String CONNECT_ADDR = "172.16.158.11:2181,"
+ "172.16.158.12:2181,"
+ "172.16.158.13:2181";
/** session Timeout time */
static final int SESSION_OUTTIME = 5000;//ms
public static void main(String[] args) throws Exception {
//1 Retrying strategy : The time for the first test is 1s retry 10 Time
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
//2 Create connections through the factory
CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(CONNECT_ADDR)
.sessionTimeoutMs(SESSION_OUTTIME)
.retryPolicy(retryPolicy)
.build();
//3 Open the connection
cf.start();
//cf.delete().forPath("/super");
//4 Use DistributedAtomicInteger
DistributedAtomicInteger atomicIntger =
new DistributedAtomicInteger(cf, "/super", new RetryNTimes(3, 1000));
AtomicValue<Integer> value = atomicIntger.add(1);
System.out.println(value.succeeded());
System.out.println(value.postValue()); // Latest value
System.out.println(value.preValue()); // Original value
}
}
The results returned to normal , No problem
Advanced applications ( Multi-threaded environment )
So in the case of multithreading , Is it right ? To be verified
public class CuratorAtomicInteger2 {
static CountDownLatch countDownLatch = new CountDownLatch(10);
public static void main(String[] args) throws Exception {
CuratorFramework zkClient = getZkClient();
// Specify the counter storage path And retry policy
DistributedAtomicInteger distributedAtomicInteger = new DistributedAtomicInteger(zkClient, "/counter", new ExponentialBackoffRetry(1000, 3));
// Multithread autoincrement 10*100 Time
for (int i = 0; i < 10; i++) {
new Thread(() -> {
for (int j = 0; j < 100; j++) {
try {
// call add Methods increase by themselves
AtomicValue<Integer> result = distributedAtomicInteger.add(1);
} catch (Exception e) {
e.printStackTrace();
}
}
countDownLatch.countDown();
}).start();
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
// View results
System.out.println(" Multithread auto increment result " + distributedAtomicInteger.get().postValue());
}
private static CuratorFramework getZkClient() {
String zkServerAddress = "172.16.158.11:2181,"
+ "172.16.158.12:2181,"
+ "172.16.158.13:2181";
ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3, 5000);
CuratorFramework zkClient = CuratorFrameworkFactory.builder()
.connectString(zkServerAddress)
.sessionTimeoutMs(5000)
.connectionTimeoutMs(5000)
.retryPolicy(retryPolicy)
.build();
zkClient.start();
return zkClient;
}
}
According to the principle that , Since the end of the increase , Should the result be 10*100?, After running for several times, Taoist friends will find that the actual values are not all 1000. Why? ? This thing is fake ?
Let's take a look at AtomicValue result = distributedAtomicInteger.add(1) In this line of code ,add() Method source code
/**
* Add delta to the current value and return the new value information. Remember to always
* check {@link AtomicValue#succeeded()}.
*
* @param delta amount to add
* @return value info
* @throws Exception ZooKeeper errors
*/
@Override
public AtomicValue<Integer> add(Integer delta) throws Exception
{
return worker(delta);
}
It reads Remember to always check {@link AtomicValue#succeeded()}. in other words , The self - augmentation of this method is not necessarily successful , When initializing the distributed machine count object earlier , A retry policy was passed in , If concurrency and self increment occur in a distributed environment , Will try again and again , If the retry fails , The result returns failed
边栏推荐
- A necessary programming assistant for programmers! Smartcoder helps you easily integrate HMS core
- Redis is a loser. If you don't understand the usage specification, you will spoil it
- PAT B1081
- Share a billing system (website) I have developed
- Popular understanding of deviation and variance in machine learning
- Applet request interface encapsulation
- 2.14(Knight Moves)
- Uniapp waterfall flow, applet waterfall flow, very simple, suitable for the whole platform
- Case: count the most characters and times
- 在打新債開戶證券安全嗎?低傭金靠譜嗎
猜你喜欢

Popular understanding of deviation and variance in machine learning

Wechat applet cloud function does not have dependency option installed

Redis practice: smart use of data types to achieve 100 million level data statistics
![[harmonyos] [arkui] how can Hongmeng ETS call pa](/img/19/9d2c68be48417e0aaa0d27068a67ce.jpg)
[harmonyos] [arkui] how can Hongmeng ETS call pa

JS asynchronism (I. asynchronous concept, basic use of web worker)

Clickhouse disables automatic clearing of tables / columns, that is, disables TTL

<C>. tic-tac-toe

App battery historian master

Redis core article: the secret that can only be broken quickly

Arduino read temperature
随机推荐
2.2 step tariff
Teach you how to create and publish a packaged NPM component
PAT B1056
Recommend a free screen recording software
node. JS express connect mysql write webapi Foundation
PAT B1064
Determine whether it is a web page opened on wechat
Interface automation -md5 password encryption
Is it safe to open an online account for new bonds? What should be paid attention to
PAT B1067
Solution to big noise of OBS screen recording software
Measurement index SSMI
II Traits (extractors)
Wechat applet cloud function does not have dependency option installed
Usage Summary of str.format() function [not 'str****{}'.Format()]
Remember to deploy selenium crawler on the server
2021-08-25
C language PTA -- continuity factor
4.ypthon function foundation
A necessary programming assistant for programmers! Smartcoder helps you easily integrate HMS core