You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ag...@apache.org on 2019/01/10 00:48:52 UTC

[geode] branch develop updated: GEODE-92: Fix deadlock issue from previous commit (#3061)

This is an automated email from the ASF dual-hosted git repository.

agingade pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 56529bf  GEODE-92: Fix deadlock issue from previous commit (#3061)
56529bf is described below

commit 56529bf406e44bc6f1147e3204670f11c9aed7f0
Author: agingade <ag...@pivotal.io>
AuthorDate: Wed Jan 9 16:48:43 2019 -0800

    GEODE-92: Fix deadlock issue from previous commit (#3061)
    
    The commit f4b0643 introduced deadlock between concurrent updates with custom expiry which invoked getValue() from the region entry.
    
    * The issue was fixed by making sure lruUpdateCallback is deferred/called at the higher/caller level (before synchronizing region entry).
---
 .../RegionEntryEvictionIntegrationTest.java        | 74 ++++++++++++++++++++--
 .../internal/cache/map/AbstractRegionMapPut.java   |  7 +-
 .../geode/internal/cache/map/FocusedRegionMap.java |  7 +-
 .../internal/cache/map/RegionMapCommitPut.java     |  2 +-
 .../geode/internal/cache/map/RegionMapPut.java     | 21 ++++--
 .../cache/map/AbstractRegionMapPutTest.java        | 27 +++++++-
 .../geode/internal/cache/map/RegionMapPutTest.java | 38 ++++++++++-
 7 files changed, 154 insertions(+), 22 deletions(-)

diff --git a/geode-core/src/integrationTest/java/org/apache/geode/internal/cache/eviction/RegionEntryEvictionIntegrationTest.java b/geode-core/src/integrationTest/java/org/apache/geode/internal/cache/eviction/RegionEntryEvictionIntegrationTest.java
index 476fd39..a0bc0f0 100644
--- a/geode-core/src/integrationTest/java/org/apache/geode/internal/cache/eviction/RegionEntryEvictionIntegrationTest.java
+++ b/geode-core/src/integrationTest/java/org/apache/geode/internal/cache/eviction/RegionEntryEvictionIntegrationTest.java
@@ -16,6 +16,13 @@ package org.apache.geode.internal.cache.eviction;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
+import java.util.ArrayList;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -24,25 +31,38 @@ import org.junit.rules.TestName;
 
 import org.apache.geode.cache.Cache;
 import org.apache.geode.cache.CacheFactory;
+import org.apache.geode.cache.CustomExpiry;
 import org.apache.geode.cache.EvictionAttributes;
+import org.apache.geode.cache.ExpirationAttributes;
 import org.apache.geode.cache.Region;
 import org.apache.geode.cache.RegionShortcut;
 import org.apache.geode.test.junit.categories.EvictionTest;
 
 @Category({EvictionTest.class})
 public class RegionEntryEvictionIntegrationTest {
+
+  private Cache cache;
+
   private Region<String, String> region;
 
   @Rule
   public TestName testName = new TestName();
 
   @Before
-  public void setUp() throws Exception {
-    region = createRegion();
+  public void setup() {
+    cache = new CacheFactory().set("locators", "").set("mcast-port", "0").create();
+  }
+
+  @After
+  public void cleaup() {
+    if (cache != null && !cache.isClosed()) {
+      cache.close();
+    }
   }
 
   @Test
   public void verifyMostRecentEntryIsNotEvicted() {
+    region = createRegion();
     region.create("one", "one");
     region.create("twoo", "twoo");
     region.put("one", "one");
@@ -53,11 +73,57 @@ public class RegionEntryEvictionIntegrationTest {
     assertThat(region.containsKey("threee")).isTrue();
   }
 
-  private Region<String, String> createRegion() throws Exception {
-    Cache cache = new CacheFactory().set("locators", "").set("mcast-port", "0").create();
+  @Test
+  public void evictionWithCustomExpiryCallingGetValueDoesNotDeadLock() throws Exception {
+    int numThreads = 2;
+    int entries = 1000;
+    int evictionCount = 1;
+    region = createRegionWithCustomExpiration(evictionCount);
+
+    ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
+    ArrayList<Future> futures = new ArrayList<>();
+
+    for (int i = 0; i < numThreads; i++) {
+      futures.add(executorService.submit(() -> doCreates(region, entries)));
+    }
+
+    for (int i = 0; i < numThreads; i++) {
+      int ops = (Integer) futures.get(i).get(2, TimeUnit.MINUTES);
+      assertThat(ops).isEqualTo(entries);
+    }
+
+    executorService.shutdown();
+  }
+
+  private int doCreates(Region region, int entries) {
+    String key = Thread.currentThread().getName();
+    for (int i = 1; i < entries; i++) {
+      region.create(key + i, "value " + i);
+    }
+    return entries;
+  }
+
+
+  private Region<String, String> createRegion() {
     return cache.<String, String>createRegionFactory(RegionShortcut.REPLICATE)
         .setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(2))
         .create(testName.getMethodName());
   }
 
+  private Region<String, String> createRegionWithCustomExpiration(int evictionCount) {
+    return cache.<String, String>createRegionFactory(RegionShortcut.REPLICATE)
+        .setCustomEntryIdleTimeout(new CustomExpiry<String, String>() {
+          @Override
+          public ExpirationAttributes getExpiry(Region.Entry<String, String> entry) {
+            entry.getValue();
+            return null;
+          }
+
+          @Override
+          public void close() {
+
+        }
+        }).setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(evictionCount))
+        .create(testName.getMethodName());
+  }
 }
diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/map/AbstractRegionMapPut.java b/geode-core/src/main/java/org/apache/geode/internal/cache/map/AbstractRegionMapPut.java
index b4aaae2..0efc2d2 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/map/AbstractRegionMapPut.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/map/AbstractRegionMapPut.java
@@ -156,8 +156,10 @@ public abstract class AbstractRegionMapPut {
    * Always called, even if the put failed.
    * Note that the RegionEntry that was modified by the put
    * is no longer synchronized when this is called.
+   *
+   * @param disabledEviction true if caller previously disabled eviction
    */
-  protected abstract void doAfterCompletionActions();
+  protected abstract void doAfterCompletionActions(boolean disabledEviction);
 
   /**
    * @return regionEntry if put completed, otherwise null.
@@ -173,13 +175,14 @@ public abstract class AbstractRegionMapPut {
   }
 
   private void doPut() {
+    final boolean disabledEviction = getRegionMap().disableLruUpdateCallback();
     try {
       doWithIndexInUpdateMode(this::doPutRetryingIfNeeded);
     } catch (DiskAccessException dae) {
       getOwner().handleDiskAccessException(dae);
       throw dae;
     } finally {
-      doAfterCompletionActions();
+      doAfterCompletionActions(disabledEviction);
     }
   }
 
diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/map/FocusedRegionMap.java b/geode-core/src/main/java/org/apache/geode/internal/cache/map/FocusedRegionMap.java
index 328e943..70ca505 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/map/FocusedRegionMap.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/map/FocusedRegionMap.java
@@ -22,8 +22,9 @@ import org.apache.geode.internal.cache.InternalRegion;
 import org.apache.geode.internal.cache.RegionEntry;
 import org.apache.geode.internal.cache.RegionEntryFactory;
 import org.apache.geode.internal.cache.TXEntryState;
+import org.apache.geode.internal.cache.eviction.EvictableMap;
 
-public interface FocusedRegionMap {
+public interface FocusedRegionMap extends EvictableMap {
 
   RegionEntry getEntry(EntryEventImpl event);
 
@@ -52,10 +53,6 @@ public interface FocusedRegionMap {
 
   void runWhileEvictionDisabled(Runnable runnable);
 
-  void lruUpdateCallback();
-
-  void resetThreadLocals();
-
   void txRemoveOldIndexEntry(Operation putOp, RegionEntry regionEntry);
 
   void processAndGenerateTXVersionTag(EntryEventImpl callbackEvent, RegionEntry regionEntry,
diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/map/RegionMapCommitPut.java b/geode-core/src/main/java/org/apache/geode/internal/cache/map/RegionMapCommitPut.java
index a88efae..48a629c 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/map/RegionMapCommitPut.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/map/RegionMapCommitPut.java
@@ -270,7 +270,7 @@ public class RegionMapCommitPut extends AbstractRegionMapPut {
   }
 
   @Override
-  protected void doAfterCompletionActions() {
+  protected void doAfterCompletionActions(boolean disabledEviction) {
     if (isOnlyExisting() && !isCompleted()) {
       if (didDestroy) {
         getOwner().txApplyPutHandleDidDestroy(getEvent().getKey());
diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/map/RegionMapPut.java b/geode-core/src/main/java/org/apache/geode/internal/cache/map/RegionMapPut.java
index f224188..1b419f0 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/map/RegionMapPut.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/map/RegionMapPut.java
@@ -286,18 +286,27 @@ public class RegionMapPut extends AbstractRegionMapPut {
   }
 
   @Override
-  protected void doAfterCompletionActions() {
-    if (isCompleted()) {
-      try {
+  protected void doAfterCompletionActions(boolean disabledEviction) {
+    try {
+      if (isCompleted()) {
         final boolean invokeListeners = getEvent().basicGetNewValue() != Token.TOMBSTONE;
         getOwner().basicPutPart3(getEvent(), getRegionEntry(), isOwnerInitialized(),
             getLastModifiedTime(), invokeListeners, isIfNew(), isIfOld(), getExpectedOldValue(),
             isRequireOldValue());
-      } finally {
+      }
+    } finally {
+      finishEviction(disabledEviction);
+    }
+  }
+
+  private void finishEviction(boolean disabledEviction) {
+    if (disabledEviction) {
+      getRegionMap().enableLruUpdateCallback();
+      if (isCompleted()) {
         lruUpdateCallbackIfNotCleared();
+      } else {
+        getRegionMap().resetThreadLocals();
       }
-    } else {
-      getRegionMap().resetThreadLocals();
     }
   }
 
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/map/AbstractRegionMapPutTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/map/AbstractRegionMapPutTest.java
index a02c35a..5506db7 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/cache/map/AbstractRegionMapPutTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/map/AbstractRegionMapPutTest.java
@@ -19,6 +19,7 @@ package org.apache.geode.internal.cache.map;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.inOrder;
@@ -114,7 +115,27 @@ public class AbstractRegionMapPutTest {
     verify(instance, never()).createOrUpdateEntry();
     verify(instance, times(1)).shouldCreatedEntryBeRemoved();
     verify(instance, never()).doBeforeCompletionActions();
-    verify(instance, times(1)).doAfterCompletionActions();
+    verify(instance, times(1)).doAfterCompletionActions(anyBoolean());
+  }
+
+  @Test
+  public void putWithDisableLruUpdateCallbackTrueCallsDoAfterCompletionActionsWithTrue() {
+    instance.checkPreconditions = false;
+    when(focusedRegionMap.disableLruUpdateCallback()).thenReturn(true);
+
+    instance.put();
+
+    verify(instance, times(1)).doAfterCompletionActions(true);
+  }
+
+  @Test
+  public void putWithDisableLruUpdateCallbackFalseCallsDoAfterCompletionActionsWithFalse() {
+    instance.checkPreconditions = false;
+    when(focusedRegionMap.disableLruUpdateCallback()).thenReturn(false);
+
+    instance.put();
+
+    verify(instance, times(1)).doAfterCompletionActions(false);
   }
 
   @Test
@@ -283,7 +304,7 @@ public class AbstractRegionMapPutTest {
       verify(instance, never()).doBeforeCompletionActions();
       verify(instance, never()).shouldCreatedEntryBeRemoved();
     }
-    verify(instance, times(1)).doAfterCompletionActions();
+    verify(instance, times(1)).doAfterCompletionActions(anyBoolean());
   }
 
   private class TestableRegionMapPut extends AbstractRegionMapPut {
@@ -343,7 +364,7 @@ public class AbstractRegionMapPutTest {
     }
 
     @Override
-    protected void doAfterCompletionActions() {}
+    protected void doAfterCompletionActions(boolean disabledEviction) {}
 
   }
 }
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/map/RegionMapPutTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/map/RegionMapPutTest.java
index 7f518dd..0aab7c5 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/cache/map/RegionMapPutTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/map/RegionMapPutTest.java
@@ -683,8 +683,9 @@ public class RegionMapPutTest {
   }
 
   @Test
-  public void lruUpdateCallbackCalled_ifPutDoneWithoutAClear() throws Exception {
+  public void lruUpdateCallbackCalled_ifPutDoneWithoutAClear() {
     ifNew = true;
+    when(focusedRegionMap.disableLruUpdateCallback()).thenReturn(true);
     when(event.getOperation()).thenReturn(Operation.CREATE);
 
     doPut();
@@ -693,9 +694,43 @@ public class RegionMapPutTest {
   }
 
   @Test
+  public void lruUpdateCallbackNotCalled_ifCallbacksNotDisabled() {
+    ifNew = true;
+    when(focusedRegionMap.disableLruUpdateCallback()).thenReturn(false);
+    when(event.getOperation()).thenReturn(Operation.CREATE);
+
+    doPut();
+
+    verify(focusedRegionMap, never()).lruUpdateCallback();
+  }
+
+  @Test
+  public void enableLruUpdateCallbackCalled_ifCallbacksDisabled() {
+    ifNew = true;
+    when(focusedRegionMap.disableLruUpdateCallback()).thenReturn(true);
+    when(event.getOperation()).thenReturn(Operation.CREATE);
+
+    doPut();
+
+    verify(focusedRegionMap, times(1)).enableLruUpdateCallback();
+  }
+
+  @Test
+  public void enableLruUpdateCallbackNotCalled_ifCallbacksNotDisabled() {
+    ifNew = true;
+    when(focusedRegionMap.disableLruUpdateCallback()).thenReturn(false);
+    when(event.getOperation()).thenReturn(Operation.CREATE);
+
+    doPut();
+
+    verify(focusedRegionMap, never()).enableLruUpdateCallback();
+  }
+
+  @Test
   public void lruUpdateCallbackNotCalled_ifPutDoneWithAClear() throws Exception {
     ifNew = true;
     when(event.getOperation()).thenReturn(Operation.CREATE);
+    when(focusedRegionMap.disableLruUpdateCallback()).thenReturn(true);
     doThrow(RegionClearedException.class).when(event).putNewEntry(any(), any());
 
     doPut();
@@ -778,6 +813,7 @@ public class RegionMapPutTest {
   public void putThrows_ifLruUpdateCallbackThrowsDiskAccessException() throws Exception {
     ifNew = true;
     when(event.getOperation()).thenReturn(Operation.CREATE);
+    when(focusedRegionMap.disableLruUpdateCallback()).thenReturn(true);
     doThrow(DiskAccessException.class).when(focusedRegionMap).lruUpdateCallback();
 
     assertThatThrownBy(() -> doPut()).isInstanceOf(DiskAccessException.class);