@@ -343,12 +343,12 @@ public long getLockedAt() {
343
343
/**
344
344
* Unlock the lock using the unlink method in redis.
345
345
*/
346
- protected abstract void removeLockKeyInnerUnlink ();
346
+ protected abstract Boolean removeLockKeyInnerUnlink ();
347
347
348
348
/**
349
349
* Unlock the lock using the delete method in redis.
350
350
*/
351
- protected abstract void removeLockKeyInnerDelete ();
351
+ protected abstract Boolean removeLockKeyInnerDelete ();
352
352
353
353
@ Override
354
354
public final void lock () {
@@ -454,11 +454,6 @@ public final void unlock() {
454
454
return ;
455
455
}
456
456
try {
457
- if (!isAcquiredInThisProcess ()) {
458
- throw new IllegalStateException ("Lock was released in the store due to expiration. " +
459
- "The integrity of data protected by this lock may have been compromised." );
460
- }
461
-
462
457
if (Thread .currentThread ().isInterrupted ()) {
463
458
RedisLockRegistry .this .executor .execute (this ::removeLockKey );
464
459
}
@@ -481,7 +476,10 @@ public final void unlock() {
481
476
private void removeLockKey () {
482
477
if (RedisLockRegistry .this .unlinkAvailable ) {
483
478
try {
484
- removeLockKeyInnerUnlink ();
479
+ if (!removeLockKeyInnerUnlink ()) {
480
+ throw new IllegalStateException ("Lock was released in the store due to expiration. " +
481
+ "The integrity of data protected by this lock may have been compromised." );
482
+ }
485
483
return ;
486
484
}
487
485
catch (Exception ex ) {
@@ -496,7 +494,10 @@ private void removeLockKey() {
496
494
}
497
495
}
498
496
}
499
- removeLockKeyInnerDelete ();
497
+ if (!removeLockKeyInnerDelete ()) {
498
+ throw new IllegalStateException ("Lock was released in the store due to expiration. " +
499
+ "The integrity of data protected by this lock may have been compromised." );
500
+ }
500
501
}
501
502
502
503
@ Override
@@ -559,19 +560,23 @@ private RedisLockRegistry getOuterType() {
559
560
560
561
private final class RedisPubSubLock extends RedisLock {
561
562
562
- private static final String UNLINK_UNLOCK_SCRIPT =
563
- "if (redis.call('unlink', KEYS[1]) == 1) then " +
564
- "redis.call('publish', ARGV[1], KEYS[1]) " +
565
- "return true " +
566
- "end " +
567
- "return false" ;
563
+ private static final String UNLINK_UNLOCK_SCRIPT = """
564
+ local lockClientId = redis.call('GET', KEYS[1])
565
+ if (lockClientId == ARGV[1] and redis.call('UNLINK', KEYS[1]) == 1) then
566
+ redis.call('PUBLISH', ARGV[2], KEYS[1])
567
+ return true
568
+ end
569
+ return false
570
+ """ ;
568
571
569
- private static final String DELETE_UNLOCK_SCRIPT =
570
- "if (redis.call('del', KEYS[1]) == 1) then " +
571
- "redis.call('publish', ARGV[1], KEYS[1]) " +
572
- "return true " +
573
- "end " +
574
- "return false" ;
572
+ private static final String DELETE_UNLOCK_SCRIPT = """
573
+ local lockClientId = redis.call('GET', KEYS[1])
574
+ if (lockClientId == ARGV[1] and redis.call('DEL', KEYS[1]) == 1) then
575
+ redis.call('PUBLISH', ARGV[2], KEYS[1])
576
+ return true
577
+ end
578
+ return false
579
+ """ ;
575
580
576
581
private static final RedisScript <Boolean >
577
582
UNLINK_UNLOCK_REDIS_SCRIPT = new DefaultRedisScript <>(UNLINK_UNLOCK_SCRIPT , Boolean .class );
@@ -589,17 +594,17 @@ protected boolean tryRedisLockInner(long time) throws ExecutionException, Interr
589
594
}
590
595
591
596
@ Override
592
- protected void removeLockKeyInnerUnlink () {
593
- RedisLockRegistry .this .redisTemplate .execute (
597
+ protected Boolean removeLockKeyInnerUnlink () {
598
+ return RedisLockRegistry .this .redisTemplate .execute (
594
599
UNLINK_UNLOCK_REDIS_SCRIPT , Collections .singletonList (this .lockKey ),
595
- RedisLockRegistry .this .unLockChannelKey );
600
+ RedisLockRegistry .this .clientId , RedisLockRegistry . this . unLockChannelKey );
596
601
}
597
602
598
603
@ Override
599
- protected void removeLockKeyInnerDelete () {
600
- RedisLockRegistry .this .redisTemplate .execute (
604
+ protected Boolean removeLockKeyInnerDelete () {
605
+ return RedisLockRegistry .this .redisTemplate .execute (
601
606
DELETE_UNLOCK_REDIS_SCRIPT , Collections .singletonList (this .lockKey ),
602
- RedisLockRegistry .this .unLockChannelKey );
607
+ RedisLockRegistry .this .clientId , RedisLockRegistry . this . unLockChannelKey );
603
608
604
609
}
605
610
@@ -694,6 +699,30 @@ private void unlockNotify(String lockKey) {
694
699
695
700
private final class RedisSpinLock extends RedisLock {
696
701
702
+ private static final String UNLINK_UNLOCK_SCRIPT = """
703
+ local lockClientId = redis.call('GET', KEYS[1])
704
+ if lockClientId == ARGV[1] then
705
+ redis.call('UNLINK', KEYS[1])
706
+ return true
707
+ end
708
+ return false
709
+ """ ;
710
+
711
+ private static final String DELETE_UNLOCK_SCRIPT = """
712
+ local lockClientId = redis.call('GET', KEYS[1])
713
+ if lockClientId == ARGV[1] then
714
+ redis.call('DEL', KEYS[1])
715
+ return true
716
+ end
717
+ return false
718
+ """ ;
719
+
720
+ private static final RedisScript <Boolean >
721
+ UNLINK_UNLOCK_REDIS_SCRIPT = new DefaultRedisScript <>(UNLINK_UNLOCK_SCRIPT , Boolean .class );
722
+
723
+ private static final RedisScript <Boolean >
724
+ DELETE_UNLOCK_REDIS_SCRIPT = new DefaultRedisScript <>(DELETE_UNLOCK_SCRIPT , Boolean .class );
725
+
697
726
private RedisSpinLock (String path ) {
698
727
super (path );
699
728
}
@@ -718,13 +747,17 @@ protected boolean tryRedisLockInner(long time) throws InterruptedException {
718
747
}
719
748
720
749
@ Override
721
- protected void removeLockKeyInnerUnlink () {
722
- RedisLockRegistry .this .redisTemplate .unlink (this .lockKey );
750
+ protected Boolean removeLockKeyInnerUnlink () {
751
+ return RedisLockRegistry .this .redisTemplate .execute (
752
+ UNLINK_UNLOCK_REDIS_SCRIPT , Collections .singletonList (this .lockKey ),
753
+ RedisLockRegistry .this .clientId );
723
754
}
724
755
725
756
@ Override
726
- protected void removeLockKeyInnerDelete () {
727
- RedisLockRegistry .this .redisTemplate .delete (this .lockKey );
757
+ protected Boolean removeLockKeyInnerDelete () {
758
+ return RedisLockRegistry .this .redisTemplate .execute (
759
+ DELETE_UNLOCK_REDIS_SCRIPT , Collections .singletonList (this .lockKey ),
760
+ RedisLockRegistry .this .clientId );
728
761
}
729
762
730
763
}
0 commit comments