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

klund pushed a commit to branch release/1.10.0
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/release/1.10.0 by this push:
     new 433f5e9  GEODE-7081: Prevent NPE in getLocalSize()
433f5e9 is described below

commit 433f5e93f918d3a237e021fe06ee1e123c04d552
Author: Aaron Lindsey <alind...@pivotal.io>
AuthorDate: Mon Aug 12 15:18:22 2019 -0700

    GEODE-7081: Prevent NPE in getLocalSize()
    
    Prevent PartitionedRegion's getLocalSize() from throwing a
    NullPointerException if called before its internal data store is
    initialized. The stats sampler uses this method to get the region's
    entry count, and stats sampling may start before the internal data store
    is initialized.
    
    (cherry picked from commit e7beb9290175405c1f285add7c538a0a18df674e)
---
 .../geode/internal/cache/PartitionedRegion.java      |  4 ++++
 .../geode/internal/cache/PartitionedRegionTest.java  | 20 ++++++++++++++++----
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git 
a/geode-core/src/main/java/org/apache/geode/internal/cache/PartitionedRegion.java
 
b/geode-core/src/main/java/org/apache/geode/internal/cache/PartitionedRegion.java
index 02e90d6..7bbec97 100755
--- 
a/geode-core/src/main/java/org/apache/geode/internal/cache/PartitionedRegion.java
+++ 
b/geode-core/src/main/java/org/apache/geode/internal/cache/PartitionedRegion.java
@@ -6602,6 +6602,10 @@ public class PartitionedRegion extends LocalRegion
 
   @Override
   public int getLocalSize() {
+    if (dataStore == null) {
+      return 0;
+    }
+
     return dataStore.getLocalBucket2RegionMap().values().stream()
         .mapToInt(BucketRegion::getLocalSize)
         .sum();
diff --git 
a/geode-core/src/test/java/org/apache/geode/internal/cache/PartitionedRegionTest.java
 
b/geode-core/src/test/java/org/apache/geode/internal/cache/PartitionedRegionTest.java
index 8c48a94..07cb811 100644
--- 
a/geode-core/src/test/java/org/apache/geode/internal/cache/PartitionedRegionTest.java
+++ 
b/geode-core/src/test/java/org/apache/geode/internal/cache/PartitionedRegionTest.java
@@ -16,6 +16,7 @@ package org.apache.geode.internal.cache;
 
 import static 
org.apache.geode.cache.asyncqueue.internal.AsyncEventQueueImpl.getSenderIdFromAsyncEventQueueId;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.eq;
@@ -42,6 +43,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import org.apache.geode.Statistics;
 import org.apache.geode.cache.AttributesFactory;
 import org.apache.geode.cache.CacheLoader;
 import org.apache.geode.cache.CacheWriter;
@@ -59,7 +61,8 @@ import org.apache.geode.test.fake.Fakes;
 public class PartitionedRegionTest {
   private InternalCache internalCache;
   private PartitionedRegion partitionedRegion;
-  private Properties gemfireProperties = new Properties();
+  @SuppressWarnings("deprecation")
+  private AttributesFactory attributesFactory;
 
   @Before
   public void setup() {
@@ -68,15 +71,16 @@ public class PartitionedRegionTest {
     InternalResourceManager resourceManager =
         mock(InternalResourceManager.class, RETURNS_DEEP_STUBS);
     
when(internalCache.getInternalResourceManager()).thenReturn(resourceManager);
-    @SuppressWarnings("deprecation")
-    AttributesFactory attributesFactory = new AttributesFactory();
+    attributesFactory = new AttributesFactory();
     attributesFactory.setPartitionAttributes(
         new 
PartitionAttributesFactory().setTotalNumBuckets(1).setRedundantCopies(1).create());
     partitionedRegion = new PartitionedRegion("prTestRegion", 
attributesFactory.create(), null,
         internalCache, mock(InternalRegionArguments.class));
     DistributedSystem mockDistributedSystem = mock(DistributedSystem.class);
     
when(internalCache.getDistributedSystem()).thenReturn(mockDistributedSystem);
-    when(mockDistributedSystem.getProperties()).thenReturn(gemfireProperties);
+    when(mockDistributedSystem.getProperties()).thenReturn(new Properties());
+    when(mockDistributedSystem.createAtomicStatistics(any(), any()))
+        .thenReturn(mock(Statistics.class));
   }
 
   @SuppressWarnings("unused")
@@ -302,4 +306,12 @@ public class PartitionedRegionTest {
         Stream.of("parallel", "serial", 
"anotherParallel").collect(Collectors.toSet())))
             .isNotEmpty().containsExactly("parallel", "anotherParallel");
   }
+
+  @Test
+  public void getLocalSizeDoesNotThrowIfRegionUninitialized() {
+    partitionedRegion = new PartitionedRegion("region", 
attributesFactory.create(), null,
+        internalCache, mock(InternalRegionArguments.class));
+
+    assertThatCode(partitionedRegion::getLocalSize).doesNotThrowAnyException();
+  }
 }

Reply via email to