In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Redis setnx command
Redis setnx Command Properti
Set only when the specified key does not exist. In other words, if you return 1, your command is executed successfully, and the key in the redis server is the value you set earlier. If 0 is returned, the key you set already exists in the redis server.
Status = jedis.setnx (lockKey, redisIdentityKey); / * * set lock key.*/ if (status > 0) {expire = jedis.expire (lockKey, lockKeyExpireSecond); / * * set redis key expire time.*/}
If the setting is successful, set the expiration time to prevent your retry lock from setting the expiration time repeatedly, resulting in never expiration.
Java object condition queue conditional queue
Here is a tip to maximize cpu utilization and solve fairness problems.
When you retry frequently, either while (true), and then add a Thread.sleep, or CAS. The former has a certain thread context switching overhead (Thread.sleep will not release the current built-in lock), while CAS will waste a lot of CPU computing cycles when it is not clear how long the remote lock is occupied. It is possible that a task can be calculated for more than ten minutes, and CPU cannot be idle for so long.
Here I try to use the condition queue conditional queuing feature (there are certainly other better ways to do it).
If (isWait & & retryCounts
< RetryCount) { retryCounts++; synchronized (this) {//借助object condition queue 来提高CPU利用率 logger.info(String. format("t:%s,当前节点:%s,尝试等待获取锁:%s", Thread.currentThread().getId(), getRedisIdentityKey(), lockKey)); this.wait(WaitLockTimeSecond); //未能获取到lock,进行指定时间的wait再重试. } } else if (retryCounts == RetryCount) { logger.info(String. format("t:%s,当前节点:%s,指定时间内获取锁失败:%s", Thread.currentThread().getId(), getRedisIdentityKey(), lockKey)); return false; } else { return false;//不需要等待,直接退出。 } 使用条件队列的好处就是,它虽然释放出了CPU但是也不会持有当前synchronized,这样就可以让其他并发进来的线程也可以获取到当前内置锁,然后形成队列。当wait时间到了被调度唤醒之后才会重新来申请synchronized锁。 简单讲就是不会再锁上等待而是在队列里等待。java object每一个对象都持有一个条件队列,与当前内置锁配合使用。 retrycount 带有重试次数限制 等待远程redis lock肯定是需要一定重试机制,但是这种重试是需要一定的限制。 /** * 重试获取锁的次数,可以根据当前任务的执行时间来设置。 * 需要时间=RetryCount*(WaitLockTimeSecond/1000) */ private static final int RetryCount = 10; 这种等待是需要用户指定的, if (isWait && retryCounts < RetryCount) ,当isWait为true才会进行重试。 object wait time 带有超时时间的wait object.wait(timeout),条件队列中的方法wait是需要一个waittime。 /** * 等待获取锁的时间,可以根据当前任务的执行时间来设置。 * 设置的太短,浪费CPU,设置的太长锁就不太公平。 */ private static final long WaitLockTimeSecond = 2000; 默认2000毫秒。 this.wait(WaitLockTimeSecond); //未能获取到lock,进行指定时间的wait再重试. 注意:this.wait虽然会blocking住,但是这里的内置锁是会立即释放出来的。所以,有时候我们可以借助这种特性来优化特殊场景。 delete lock 删除远程锁 释放redis lock比较简单,直接del key就好了 long status = jedis.del(lockKey); if (status >0) {logger.info (String. Format ("tRV% s, current node:% s, release lock:% s successful." , Thread.currentThread () .getId (), getRedisIdentityKey (), lockKey); return true;}
Once the delete is completed, the thread awakened by the first wait will acquire the lock.
Acquire lock applies for lock/** * redis lock with timeout. * * @ param lockKeyExpireSecond Lock the past time of key in redis * @ param lockKey lock key * @ param isWait whether you need to wait for * @ throws Exception lockKey is empty throw exception when the lock is not available. * / public Boolean acquireLockWithTimeout (int lockKeyExpireSecond, String lockKey, Boolean isWait) throws Exception {if (StringUtils.isEmpty (lockKey)) throw new Exception ("lockKey is empty."); int retryCounts = 0; while (true) {Long status, expire = 0L; status = jedis.setnx (lockKey, redisIdentityKey) / * * set lock key.*/ if (status > 0) {expire = jedis.expire (lockKey, lockKeyExpireSecond); / * * set redis key expire time.*/} if (status > 0 & & expire > 0) {logger.info (String. Format ("tvv% s, current node:% s, get lock:% s", Thread.currentThread (). GetId (), getRedisIdentityKey (), lockKey); return true;/** gets lock*/} try {if (isWait & & retryCounts)
< RetryCount) { retryCounts++; synchronized (this) {//借助object condition queue 来提高CPU利用率 logger.info(String. format("t:%s,当前节点:%s,尝试等待获取锁:%s", Thread.currentThread().getId(), getRedisIdentityKey(), lockKey)); this.wait(WaitLockTimeSecond); //未能获取到lock,进行指定时间的wait再重试. } } else if (retryCounts == RetryCount) { logger.info(String. format("t:%s,当前节点:%s,指定时间内获取锁失败:%s", Thread.currentThread().getId(), getRedisIdentityKey(), lockKey)); return false; } else { return false;//不需要等待,直接退出。 } } catch (InterruptedException e) { e.printStackTrace(); } } }## release lock 释放lock/** * 释放redis lock。 * * @param lockKey lock key * @throws Exception lockKey is empty throw exception. */ public Boolean releaseLockWithTimeout(String lockKey) throws Exception { if (StringUtils.isEmpty(lockKey)) throw new Exception("lockKey is empty."); long status = jedis.del(lockKey); if (status >0) {logger.info ("current node:% s, release lock:% s successful." , getRedisIdentityKey (), lockKey); return true;} logger.info (String.format ("current node:% s, release lock:% s failed." , getRedisIdentityKey (), lockKey); return false;} demo demonstration
2017-06-18 13 c.plen.opensource.implement.RedisLocker 57 c.plen.opensource.implement.RedisLocker 43.867 INFO 1444-[c.plen.opensource.implement.RedisLocker] TRV 23, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, get lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 57 c.plen.opensource.implement.RedisLocker 47.062 INFO 1444-[c.plen.opensource.implement.RedisLocker] TRV 25, current node: 5f81f482-lock, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 57 c.plen.opensource.implement.RedisLocker 49.063 INFO 1444-[c.plen.opensource.implement.RedisLocker] TRV 25, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait to acquire lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 57 INFO 51.064 1444-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 57 c.plen.opensource.implement.RedisLocker 53.066 INFO 1444-[c.plen.opensource.implement.RedisLocker] TRV 25, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 55.068 INFO 1444-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 57 c.plen.opensource.implement.RedisLocker 57.069 INFO 1444-[c.plen.opensource.implement.RedisLocker] TRV 25, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 57 c.plen.opensource.implement.RedisLocker 59.070 INFO 1444-[c.plen.opensource.implement.RedisLocker] TVV 25, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 58 c.plen.opensource.implement.RedisLocker 01.071 INFO 1444-[c.plen.opensource.implement.RedisLocker] TRV 25, current node: 5f81f482-lock, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 58 c.plen.opensource.implement.RedisLocker 03.072 INFO 1444-[c.plen.opensource.implement.RedisLocker] TRV 25, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 58 c.plen.opensource.implement.RedisLocker 05.073 INFO 1444-[c.plen.opensource.implement.RedisLocker] TRV 25, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 58 c.plen.opensource.implement.RedisLocker 07.074 INFO 1444-[c.plen.opensource.implement.RedisLocker] TRV 25, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e. Failed to acquire lock within the specified time: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 58 c.plen.opensource.implement.RedisLocker 23.768 INFO 1444-[c.plen.opensource.implement.RedisLocker] tRV 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 INFO 5825.769 INFO 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 58 INFO 27.770 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 58 INFO 29.772 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for lock acquisition: product:10100101:shopping
2017-06-18 13 INFO 5815 31.773 INFO 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: TRV 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for lock acquisition: product:10100101:shopping
2017-06-18 13 INFO 5833.774 INFO 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for lock acquisition: product:10100101:shopping
2017-06-18 13 5f81f482 58 INFO 35.774 INFO 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, get lock: product:10100101:shopping
Thread 23 gives priority to the modification of the item ID 10100101, so lock the current item first.
TRV 23, current node: 843d3ec0-9c22-4d8a-bcaa-745dba35b8a4, acquire lock: product:10100101:shopping
Then, thread 25 also modifies the current commodity 10100101, so it is trying to acquire the lock.
2017-06-18 13 29f3 50 INFO 11.021 INFO 4616-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 946b7250-29f3-459b-8320-62d31e6f1fc4, try to wait for the lock: product:10100101:shopping
2017-06-18 13 29f3 50 INFO 13.023 INFO 4616-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 946b7250-29f3-459b-8320-62d31e6f1fc4, try to wait for the lock: product:10100101:shopping
2017-06-18 13 29f3 50 INFO 15.026 INFO 4616-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 946b7250-29f3-459b-8320-62d31e6f1fc4, try to wait for the lock: product:10100101:shopping
2017-06-18 13 29f3 50 INFO 17.028 INFO 4616-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 946b7250-29f3-459b-8320-62d31e6f1fc4, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 50 946b7250 19.030 INFO 4616-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 946b7250-29f3-459b-8320-62d31e6f1fc4, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 50 INFO 21.031 4616-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 946b7250-29f3-459b-8320-62d31e6f1fc4, try to wait for the lock: product:10100101:shopping
2017-06-18 13 29f3 50 INFO 23.035 INFO 4616-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 946b7250-29f3-459b-8320-62d31e6f1fc4, try to wait for the lock: product:10100101:shopping
2017-06-18 13 29f3 50 INFO 25.037 INFO 4616-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 946b7250-29f3-459b-8320-62d31e6f1fc4, try to wait for the lock: product:10100101:shopping
2017-06-18 13 29f3 50 INFO 27.041 INFO 4616-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 946b7250-29f3-459b-8320-62d31e6f1fc4, try to wait for the lock: product:10100101:shopping
2017-06-18 13 29f3 50 INFO 29.042 INFO 4616-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 25, current node: 946b7250-29f3-459b-8320-62d31e6f1fc4, try to wait for the lock: product:10100101:shopping
2017-06-18 13 29f3 50 INFO 35.289 INFO 4616-[nio-8080-exec-3] c.plen.opensource.implement.RedisLocker: TRV 25, current node: 946b7250-29f3-459b-8320-62d31e6f1fc4. Failed to acquire lock within specified time: product:10100101:shopping
After retry10 (2000 milliseconds, 2 seconds), the fetch fails, returns directly, and waits for the next task schedule to start.
2017-06-18 13 c.plen.opensource.implement.RedisLocker 58 c.plen.opensource.implement.RedisLocker 07.074 INFO 1444-[c.plen.opensource.implement.RedisLocker] TRV 25, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e. Failed to acquire lock within the specified time: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 58 c.plen.opensource.implement.RedisLocker 23.768 INFO 1444-[c.plen.opensource.implement.RedisLocker] tRV 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 INFO 5825.769 INFO 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 58 INFO 27.770 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for the lock: product:10100101:shopping
2017-06-18 13 c.plen.opensource.implement.RedisLocker 58 INFO 29.772 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for lock acquisition: product:10100101:shopping
2017-06-18 13 INFO 5815 31.773 INFO 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: TRV 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for lock acquisition: product:10100101:shopping
2017-06-18 13 INFO 5833.774 INFO 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, try to wait for lock acquisition: product:10100101:shopping
2017-06-18 13 5f81f482 58 INFO 35.774 INFO 1444-[nio-8080-exec-6] c.plen.opensource.implement.RedisLocker: t c.plen.opensource.implement.RedisLocker 28, current node: 5f81f482-295a-4394-b8cb-d7282e51dd6e, get lock: product:10100101:shopping
Thread 28 initiates a modification to the commodity 10100101, and the lock is obtained after retry6 times.
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.