In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to realize the wait lock in RedissonLock in redis. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Preface
There is often such a requirement, that is, in a query interface, if the first query is not reached, the initialization method will be executed and the data will be initialized, and then the query can directly query the data in the database. The purpose of this design is that if the data that needs to be initialized is too large to be processed in the method again, or that not every piece of data needs to be initialized, in this case, the data of the priority query is initialized first.
problem
Such a plan will then cause a problem. The query interface is known to be naturally idempotent and there is no need for us to do additional idempotent processing. But in the scenario, this query is more than just a query. The initialization method is executed if there is no query, which is essentially an insertion logic. This requires us to be idempotent ourselves.
Scheme
For a single service, we can use the lock of Java to achieve idempotency, and the primary key id of each piece of data as the lock. But now it is basically distributed services, as mentioned in the previous article, we can use the distributed lock RedissonLock to implement.
When concurrently with the first request, competing RedissonLock, whoever gets the lock will execute the initialization method. There is no request contending to the lock, so you can set a waiting time to wait for the lock to be released. When the lock is released, you can first query whether the data has been initialized, and then check the library directly when it is finished. Here, I would like to mention how RedissonLock implements waiting.
TryLock
RedissonLock provides an api in the locking method and a parameter waitTime, the wait time.
Public boolean tryLock (long waitTime, long leaseTime, TimeUnit unit)
Messages are subscribed during waitTime time, which uses the publish and subscribe feature of redis itself.
RFuture subscribeFuture = subscribe (threadId); if (! subscribeFuture.await (time, TimeUnit.MILLISECONDS)) {if (! subscribeFuture.cancel (false)) {subscribeFuture.onComplete ((res, e)-> {if (e = = null) {unsubscribe (subscribeFuture, threadId);}});} acquireFailed (threadId); return false;}
In this way, when the lock is released, the message is released at the same time. All threads listening for the lock will be notified, and these threads will compete for locks again to achieve the idempotent functionality we need. We are looking at the logic of releasing the lock. Has the message been released?
UnlockInnerAsync
Protected RFuture unlockInnerAsync (long threadId) {return commandExecutor.evalWriteAsync (getName (), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "if (redis.call ('hexists', KEYS [1], ARGV [3]) = = 0) then" + "return nil;" + "end;" + "local counter = redis.call (' hincrby', KEYS [1], ARGV [3],-1) "+" if (counter > 0) then "+" redis.call ('pexpire', KEYS [1], ARGV [2]); "+" return 0; "+" else "+" redis.call (' del', KEYS [1]); "+" redis.call ('publish', KEYS [2], ARGV [1]) "+" return 1; "+" end; "+" return nil; ", Arrays.asList (getName (), getChannelName ()), LockPubSub.UNLOCK_MESSAGE, internalLockLeaseTime, getLockName (threadId);}
You can see that in the unlockInnerAsync method, the lua script is executed, and in the script, we can easily see that the publish command is executed.
Redisson skillfully uses the publish and subscribe function of redis to realize the waiting function of distributed lock.
After reading the above, do you have any further understanding of how RedissonLock implements wait locks in redis? If you want to know more knowledge or related content, 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.
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.