Uploaded image for project: 'Spring Data Redis'
  1. Spring Data Redis
  2. DATAREDIS-843

RedisAtomic[*].compareAndSet is not atomic

    Details

      Description

      Bump. Test to reproduce the issue:

      @RunWith(SpringRunner.class)
      @SpringBootTest
      public class SpringBugTest {
      
      
          @Autowired
          private RedisTemplate<String, Object> template;
      
      
          @Test
          public void testSpringBug() throws Exception {
              RedisAtomicInteger rint = new RedisAtomicInteger("atomic-int", template.getConnectionFactory());
              rint.set(0);
      
              assertEquals(1, IntStream.range(10, 300).parallel().filter(i -> rint.compareAndSet(0, i)).count());
          }
      
      }

      As far as I can tell, this would be the fix:

      	public boolean compareAndSet(final int expect, final int update) {
      		return generalOps.execute(new SessionCallback<Boolean>() {
      
      			@SuppressWarnings("unchecked")
      			
      			public Boolean execute(RedisOperations operations) {
      				for (;;) {
      					operations.watch(Collections.singleton(key));
      					if (expect == get()) {
      						generalOps.multi();
      						set(update);
      						List exec = operations.exec();
      						if (exec != null && !exec.isEmpty()) {  // <=== FIX HERE
      							return true;
      						}
      					}
      					{
      						return false;
      					}
      				}
      			}
      		});
      	}

      operations.exec() returns an empty list when the set-Operation does nothing. In spring-data-redis 2.0.7.RELEASE a check for null is done.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                mp911de Mark Paluch
                Reporter:
                mp911de Mark Paluch
                Last updater:
                Mark Paluch
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: