当前位置:网站首页>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
边栏推荐
- Est - il sûr d'ouvrir un compte avec de nouvelles dettes? Une faible Commission est - elle crédible?
- Popular understanding of deviation and variance in machine learning
- PAT B1061
- Clickhouse disables automatic clearing of tables / columns, that is, disables TTL
- Suddenly found that the screen adjustment button can not be used and the brightness can not be adjusted
- Understanding C language structure pointer
- C language PTA -- continuity factor
- Two types of attribute injection methods
- Teach you how to create and publish a packaged NPM component
- II Traits (extractors)
猜你喜欢

Arduino ide + esp8266+mqtt subscribe to publish temperature and humidity information

<C>. Rolling phase division

App battery historian master

Arduino read temperature

Short video is just the time. How can you quickly build your video creation ability in your app?

Intra domain information collection for intranet penetration

Teach you how to add custom controls to a map

PAT B1076

Use of serialize() and serializearray() methods for form data serialization

String since I can perform performance tuning, I can call an expert directly
随机推荐
Please do not call Page constructor in files
Arduino : No such file or directory
Li-rads lesion classification reading notes
Some pictures of real machine preview development and debugging are not shown
Wechat applet cloud function does not have dependency option installed
Profile path and name
How does pycharm create multiple console windows for debugging in different environments?
PAT B1091
Thymleaf template configuration analysis
Besides using hackbar, how can I make post requests
打新债网上开户安全吗,需要注意什么
H5 application conversion fast application
PAT B1071
How to view and explain robots protocol
PAT B1057
2020-11-14-Alexnet
K-fold cross validation
<C>. function
Jsonp processing non homologous
Curtain down and departure