INT-4126: Fix Redis lock to use SET NX EX

JIRA: https://jira.spring.io/browse/INT-4126

Looks like `WATCH` doesn't provide good isolation when the call is performed concurrently at the same time from different Threads.

* Change locking algorithm as it is recommended by Redis `SET` command: http://redis.io/commands/set:
```
SET resource-name anystring NX EX max-lock-time
```

* Add `@Repeat(10)` to the `AggregatorWithRedisLocksTests#testDistributedAggregator()` since that helped to reproduce the issue

**Cherry-pick to 4.3.x & 4.2.x**
This commit is contained in:
Artem Bilan
2016-09-27 21:44:55 -04:00
parent 129984ec0a
commit 8aa9befef6
2 changed files with 25 additions and 21 deletions

View File

@@ -46,11 +46,13 @@ import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.Repeat;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Gary Russell
* @author Artem Bilan
* @since 4.0
*
*/
@@ -125,6 +127,7 @@ public class AggregatorWithRedisLocksTests extends RedisAvailableTests {
@Test
@RedisAvailable
@Repeat(10)
public void testDistributedAggregator() throws Exception {
this.releaseStrategy.reset(1);
Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 1));
@@ -223,5 +226,7 @@ public class AggregatorWithRedisLocksTests extends RedisAvailableTests {
this.callers = new AtomicInteger();
this.maxCallers = new AtomicInteger();
}
}
}