You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by nn...@apache.org on 2021/06/08 23:59:57 UTC

[geode] branch support/1.13 updated: GEODE-9146: have idle expiration ignore timestamp on removed remote entries

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

nnag pushed a commit to branch support/1.13
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/support/1.13 by this push:
     new 01a69eb  GEODE-9146: have idle expiration ignore timestamp on removed remote entries
01a69eb is described below

commit 01a69ebdbbc8e04ee3f2239756934850753bee82
Author: Darrel Schneider <da...@vmware.com>
AuthorDate: Wed Jun 2 16:28:06 2021 -0700

    GEODE-9146: have idle expiration ignore timestamp on removed remote entries
    
    LatestLastAccessTimeMessage now ignores the last access time
    of both invalid and removed entries.
    
    (cherry picked from commit e4e10ace74a8a74868690d0c78f97814380a566d)
---
 .../PREntryIdleExpirationDistributedTest.java      |  18 +++-
 .../cache/LatestLastAccessTimeMessage.java         |  32 ++++--
 .../cache/LatestLastAccessTimeMessageTest.java     | 110 ++++++++++++++++++---
 3 files changed, 138 insertions(+), 22 deletions(-)

diff --git a/geode-core/src/distributedTest/java/org/apache/geode/internal/cache/PREntryIdleExpirationDistributedTest.java b/geode-core/src/distributedTest/java/org/apache/geode/internal/cache/PREntryIdleExpirationDistributedTest.java
index 02b865f..f78fccf 100644
--- a/geode-core/src/distributedTest/java/org/apache/geode/internal/cache/PREntryIdleExpirationDistributedTest.java
+++ b/geode-core/src/distributedTest/java/org/apache/geode/internal/cache/PREntryIdleExpirationDistributedTest.java
@@ -16,7 +16,7 @@ package org.apache.geode.internal.cache;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
 import static org.apache.geode.cache.ExpirationAction.DESTROY;
-import static org.apache.geode.cache.RegionShortcut.PARTITION;
+import static org.apache.geode.cache.RegionShortcut.PARTITION_HEAP_LRU;
 import static org.apache.geode.test.awaitility.GeodeAwaitility.await;
 import static org.apache.geode.test.dunit.VM.getVM;
 import static org.assertj.core.api.Assertions.assertThat;
@@ -94,11 +94,22 @@ public class PREntryIdleExpirationDistributedTest implements Serializable {
     }
   }
 
+  private void waitForExpirationInEachMember() {
+    VM[] vms = new VM[] {member1, member2, member3};
+    for (VM vm : vms) {
+      vm.invoke(() -> {
+        Region<String, String> region = cacheRule.getCache().getRegion(regionName);
+        await().until(() -> !region.containsKey(KEY));
+      });
+    }
+  }
+
   @Test
   public void readsInOtherMemberShouldPreventExpiration() throws Exception {
     AsyncInvocation<?> memberReading = member3.invokeAsync(() -> {
       Region<String, String> region = cacheRule.getCache().getRegion(regionName);
       region.put(KEY, VALUE);
+      ExpiryTask.permitExpiration();
       while (KEEP_READING.get()) {
         region.get(KEY);
         Thread.sleep(10);
@@ -108,6 +119,7 @@ public class PREntryIdleExpirationDistributedTest implements Serializable {
     member2.invoke(() -> {
       Region<String, String> region = cacheRule.getCache().getRegion(regionName);
       await().until(() -> region.containsKey(KEY));
+      ExpiryTask.permitExpiration();
       assertThat(region.containsKey(KEY)).isTrue();
     });
 
@@ -127,10 +139,12 @@ public class PREntryIdleExpirationDistributedTest implements Serializable {
     member3.invoke(() -> KEEP_READING.set(false));
 
     memberReading.await();
+    waitForExpirationInEachMember();
   }
 
   private void createRegion() {
-    RegionFactory<String, String> factory = cacheRule.getCache().createRegionFactory(PARTITION);
+    RegionFactory<String, String> factory =
+        cacheRule.getCache().createRegionFactory(PARTITION_HEAP_LRU);
     factory.setPartitionAttributes(
         new PartitionAttributesFactory<String, String>().setRedundantCopies(2).create());
     factory.setEntryIdleTimeout(new ExpirationAttributes(1, DESTROY));
diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/LatestLastAccessTimeMessage.java b/geode-core/src/main/java/org/apache/geode/internal/cache/LatestLastAccessTimeMessage.java
index f012085..46bb749 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/LatestLastAccessTimeMessage.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/LatestLastAccessTimeMessage.java
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.util.Set;
 
 import org.apache.geode.DataSerializer;
+import org.apache.geode.annotations.VisibleForTesting;
 import org.apache.geode.distributed.internal.ClusterDistributionManager;
 import org.apache.geode.distributed.internal.MessageWithReply;
 import org.apache.geode.distributed.internal.PooledDistributionMessage;
@@ -61,26 +62,39 @@ public class LatestLastAccessTimeMessage<K> extends PooledDistributionMessage
 
   @Override
   protected void process(ClusterDistributionManager dm) {
-    long latestLastAccessTime = 0L;
-    InternalCache cache = dm.getCache();
+    final InternalCache cache = dm.getCache();
     if (cache == null) {
+      sendReply(dm, 0);
       return;
     }
-    InternalDistributedRegion region =
+    final InternalDistributedRegion region =
         (InternalDistributedRegion) cache.getRegion(this.regionName);
     if (region == null) {
+      sendReply(dm, 0);
       return;
     }
-    RegionEntry entry = region.getRegionEntry(this.key);
+    final RegionEntry entry = region.getRegionEntry(this.key);
     if (entry == null) {
+      sendReply(dm, 0);
       return;
     }
-    try {
-      latestLastAccessTime = entry.getLastAccessed();
-    } catch (InternalStatisticsDisabledException ignored) {
-      // last access time is not available
+    long lastAccessed = 0L;
+    // noinspection SynchronizationOnLocalVariableOrMethodParameter
+    synchronized (entry) {
+      if (!entry.isInvalidOrRemoved()) {
+        try {
+          lastAccessed = entry.getLastAccessed();
+        } catch (InternalStatisticsDisabledException ignored) {
+          // last access time is not available
+        }
+      }
     }
-    ReplyMessage.send(getSender(), this.processorId, latestLastAccessTime, dm);
+    sendReply(dm, lastAccessed);
+  }
+
+  @VisibleForTesting
+  void sendReply(ClusterDistributionManager dm, long lastAccessTime) {
+    ReplyMessage.send(getSender(), this.processorId, lastAccessTime, dm);
   }
 
   @Override
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/LatestLastAccessTimeMessageTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/LatestLastAccessTimeMessageTest.java
index 1ddd31e..380006e 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/cache/LatestLastAccessTimeMessageTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/LatestLastAccessTimeMessageTest.java
@@ -14,7 +14,11 @@
  */
 package org.apache.geode.internal.cache;
 
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 import java.util.Collections;
 import java.util.Set;
@@ -22,22 +26,106 @@ import java.util.Set;
 import org.junit.Test;
 
 import org.apache.geode.distributed.internal.ClusterDistributionManager;
-import org.apache.geode.distributed.internal.DistributionManager;
 import org.apache.geode.distributed.internal.membership.InternalDistributedMember;
-import org.apache.geode.internal.inet.LocalHostUtil;
+import org.apache.geode.internal.InternalStatisticsDisabledException;
 
 public class LatestLastAccessTimeMessageTest {
+  private final ClusterDistributionManager dm = mock(ClusterDistributionManager.class);
+  private final InternalCache cache = mock(InternalCache.class);
+  private final InternalDistributedRegion region = mock(InternalDistributedRegion.class);
+  private final RegionEntry regionEntry = mock(RegionEntry.class);
+  private final long LAST_ACCESSED = 47;
 
-  @Test
-  public void processMessageShouldLookForNullCache() throws Exception {
-    final DistributionManager distributionManager = mock(DistributionManager.class);
+  private LatestLastAccessTimeMessage<String> lastAccessTimeMessage;
+
+  private void setupMessage() {
     final LatestLastAccessTimeReplyProcessor replyProcessor =
         mock(LatestLastAccessTimeReplyProcessor.class);
-    final InternalDistributedRegion region = mock(InternalDistributedRegion.class);
-    Set<InternalDistributedMember> recipients = Collections.singleton(new InternalDistributedMember(
-        LocalHostUtil.getLocalHost(), 1234));
-    final LatestLastAccessTimeMessage<String> lastAccessTimeMessage =
-        new LatestLastAccessTimeMessage<>(replyProcessor, recipients, region, "foo");
-    lastAccessTimeMessage.process(mock(ClusterDistributionManager.class));
+    final Set<InternalDistributedMember> recipients =
+        Collections.singleton(mock(InternalDistributedMember.class));
+    lastAccessTimeMessage =
+        spy(new LatestLastAccessTimeMessage<>(replyProcessor, recipients, region, "foo"));
+    lastAccessTimeMessage.setSender(mock(InternalDistributedMember.class));
+  }
+
+  private void setupRegion(boolean hasRegion, boolean hasEntry) {
+    when(dm.getCache()).thenReturn(cache);
+    if (hasRegion) {
+      when(cache.getRegion(any())).thenReturn(region);
+    } else {
+      when(cache.getRegion(any())).thenReturn(null);
+    }
+    if (hasEntry) {
+      when(region.getRegionEntry(any())).thenReturn(regionEntry);
+      try {
+        when(regionEntry.getLastAccessed()).thenReturn(LAST_ACCESSED);
+      } catch (InternalStatisticsDisabledException e) {
+        throw new RuntimeException("should never happen when mocking", e);
+      }
+    } else {
+      when(region.getRegionEntry(any())).thenReturn(null);
+    }
+  }
+
+  @Test
+  public void processWithNullCacheRepliesZero() {
+    setupMessage();
+    when(dm.getCache()).thenReturn(null);
+
+    lastAccessTimeMessage.process(dm);
+
+    verify(lastAccessTimeMessage).sendReply(dm, 0);
+  }
+
+  @Test
+  public void processWithNullRegionRepliesZero() {
+    setupMessage();
+    setupRegion(false, false);
+
+    lastAccessTimeMessage.process(dm);
+
+    verify(lastAccessTimeMessage).sendReply(dm, 0);
+  }
+
+  @Test
+  public void processWithNullEntryRepliesZero() {
+    setupMessage();
+    setupRegion(true, false);
+
+    lastAccessTimeMessage.process(dm);
+
+    verify(lastAccessTimeMessage).sendReply(dm, 0);
+  }
+
+  @Test
+  public void processWithEntryStatsDisabledRepliesZero() throws Exception {
+    setupMessage();
+    setupRegion(true, true);
+    when(regionEntry.getLastAccessed()).thenThrow(new InternalStatisticsDisabledException());
+
+    lastAccessTimeMessage.process(dm);
+
+    verify(lastAccessTimeMessage).sendReply(dm, 0);
+  }
+
+  @Test
+  public void processWithRegionEntryRepliesWithLastAccessed() {
+    setupMessage();
+    setupRegion(true, true);
+
+    lastAccessTimeMessage.process(dm);
+
+    verify(lastAccessTimeMessage).sendReply(dm, LAST_ACCESSED);
+  }
+
+  @Test
+  public void processWithRemovedRegionEntryRepliesZero() {
+    setupMessage();
+    setupRegion(true, true);
+    when(regionEntry.isInvalidOrRemoved()).thenReturn(true);
+
+    lastAccessTimeMessage.process(dm);
+
+    verify(lastAccessTimeMessage).sendReply(dm, 0);
   }
 }