[ 
https://issues.apache.org/jira/browse/GEODE-3595?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16246289#comment-16246289
 ] 

ASF GitHub Bot commented on GEODE-3595:
---------------------------------------

dschneider-pivotal closed pull request #1030: GEODE-3595: remove getInstance 
calls
URL: https://github.com/apache/geode/pull/1030
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/geode-core/src/main/java/org/apache/geode/distributed/internal/DM.java 
b/geode-core/src/main/java/org/apache/geode/distributed/internal/DM.java
index 1ce742188b..43de293c18 100644
--- a/geode-core/src/main/java/org/apache/geode/distributed/internal/DM.java
+++ b/geode-core/src/main/java/org/apache/geode/distributed/internal/DM.java
@@ -24,6 +24,8 @@
 import java.util.concurrent.ExecutorService;
 
 import org.apache.geode.CancelCriterion;
+import org.apache.geode.cache.Cache;
+import org.apache.geode.cache.CacheClosedException;
 import org.apache.geode.distributed.DistributedMember;
 import org.apache.geode.distributed.Role;
 import org.apache.geode.distributed.internal.locks.ElderState;
@@ -464,7 +466,22 @@ public void addHostedLocators(InternalDistributedMember 
member, Collection<Strin
 
   int getDMType();
 
+  /**
+   * The returned cache will be null if the cache does not yet exist. Note 
that the returned cache
+   * may be one that is already closed. Callers of 
GemFireCacheImpl.getInstance() should try to use
+   * this method.
+   */
   InternalCache getCache();
 
+  /**
+   * Returns an existing non-closed cache associated with this DM. Callers of
+   * CacheFactory.getAnyInstance(), 
CacheFactory.getInstance(DistributedSystem) or
+   * GemFireCacheImpl.getExisting() should try to use this method.
+   *
+   * @throws CacheClosedException if a cache has not yet been associated with 
this DM or it has been
+   *         {@link Cache#isClosed closed}.
+   */
+  InternalCache getExistingCache();
+
   void setCache(InternalCache instance);
 }
diff --git 
a/geode-core/src/main/java/org/apache/geode/distributed/internal/DistributionManager.java
 
b/geode-core/src/main/java/org/apache/geode/distributed/internal/DistributionManager.java
index 339d673605..6130e12f71 100644
--- 
a/geode-core/src/main/java/org/apache/geode/distributed/internal/DistributionManager.java
+++ 
b/geode-core/src/main/java/org/apache/geode/distributed/internal/DistributionManager.java
@@ -53,6 +53,7 @@
 import org.apache.geode.SystemFailure;
 import org.apache.geode.ToDataException;
 import org.apache.geode.admin.GemFireHealthConfig;
+import org.apache.geode.cache.CacheClosedException;
 import org.apache.geode.distributed.DistributedMember;
 import org.apache.geode.distributed.DistributedSystemDisconnectedException;
 import org.apache.geode.distributed.Locator;
@@ -3939,7 +3940,7 @@ public String getDistributionConfigDescription() {
   /* -----------------------------Health Monitor------------------------------ 
*/
   private final ConcurrentMap hmMap = new ConcurrentHashMap();
 
-  private InternalCache cache;
+  private volatile InternalCache cache;
 
   /**
    * Returns the health monitor for this distribution manager and owner.
@@ -4820,4 +4821,19 @@ public void setCache(InternalCache instance) {
   public InternalCache getCache() {
     return this.cache;
   }
+
+  @Override
+  public InternalCache getExistingCache() {
+    InternalCache result = this.cache;
+    if (result == null) {
+      throw new CacheClosedException(
+          
LocalizedStrings.CacheFactory_A_CACHE_HAS_NOT_YET_BEEN_CREATED.toLocalizedString());
+    }
+    result.getCancelCriterion().checkCancelInProgress(null);
+    if (result.isClosed()) {
+      throw result.getCacheClosedException(
+          
LocalizedStrings.CacheFactory_THE_CACHE_HAS_BEEN_CLOSED.toLocalizedString(), 
null);
+    }
+    return result;
+  }
 }
diff --git 
a/geode-core/src/main/java/org/apache/geode/distributed/internal/LonerDistributionManager.java
 
b/geode-core/src/main/java/org/apache/geode/distributed/internal/LonerDistributionManager.java
index 8bbe019889..ffe4054d16 100644
--- 
a/geode-core/src/main/java/org/apache/geode/distributed/internal/LonerDistributionManager.java
+++ 
b/geode-core/src/main/java/org/apache/geode/distributed/internal/LonerDistributionManager.java
@@ -22,6 +22,7 @@
 
 import org.apache.geode.CancelCriterion;
 import org.apache.geode.InternalGemFireError;
+import org.apache.geode.cache.CacheClosedException;
 import org.apache.geode.distributed.DistributedMember;
 import org.apache.geode.distributed.DurableClientAttributes;
 import org.apache.geode.distributed.Role;
@@ -1249,7 +1250,7 @@ public RuntimeException 
generateCancelledException(Throwable e) {
   }
 
   private final Stopper stopper = new Stopper();
-  private InternalCache cache;
+  private volatile InternalCache cache;
 
   public CancelCriterion getCancelCriterion() {
     return stopper;
@@ -1379,12 +1380,27 @@ public boolean 
isSharedConfigurationServiceEnabledForDS() {
   }
 
   @Override
+  public void setCache(InternalCache instance) {
+    this.cache = instance;
+  }
+
+  @Override
   public InternalCache getCache() {
     return this.cache;
   }
 
   @Override
-  public void setCache(InternalCache instance) {
-    this.cache = instance;
+  public InternalCache getExistingCache() {
+    InternalCache result = this.cache;
+    if (result == null) {
+      throw new CacheClosedException(
+          
LocalizedStrings.CacheFactory_A_CACHE_HAS_NOT_YET_BEEN_CREATED.toLocalizedString());
+    }
+    result.getCancelCriterion().checkCancelInProgress(null);
+    if (result.isClosed()) {
+      throw result.getCacheClosedException(
+          
LocalizedStrings.CacheFactory_THE_CACHE_HAS_BEEN_CLOSED.toLocalizedString(), 
null);
+    }
+    return result;
   }
 }
diff --git 
a/geode-core/src/main/java/org/apache/geode/internal/cache/GemFireCacheImpl.java
 
b/geode-core/src/main/java/org/apache/geode/internal/cache/GemFireCacheImpl.java
index e8aac768ac..36c07d1d09 100755
--- 
a/geode-core/src/main/java/org/apache/geode/internal/cache/GemFireCacheImpl.java
+++ 
b/geode-core/src/main/java/org/apache/geode/internal/cache/GemFireCacheImpl.java
@@ -676,7 +676,12 @@ public void write(int i) {
   /** Map of Futures used to track Regions that are being reinitialized */
   private final ConcurrentMap reinitializingRegions = new ConcurrentHashMap();
 
-  /** Returns the last created instance of GemFireCache */
+  /**
+   * Returns the last created instance of GemFireCache
+   *
+   * @deprecated: use DM.getCache instead
+   */
+  @Deprecated
   public static GemFireCacheImpl getInstance() {
     return instance;
   }
@@ -695,7 +700,9 @@ public static GemFireCacheImpl 
setInstanceForTests(GemFireCacheImpl cache) {
    *
    * @return the existing cache
    * @throws CacheClosedException if an existing cache can not be found.
+   * @deprecated use DM.getExistingCache instead.
    */
+  @Deprecated
   public static GemFireCacheImpl getExisting() {
     final GemFireCacheImpl result = instance;
     if (result != null && !result.isClosing) {
@@ -715,7 +722,9 @@ public static GemFireCacheImpl getExisting() {
    * @param reason the reason an existing cache is being requested.
    * @return the existing cache
    * @throws CacheClosedException if an existing cache can not be found.
+   * @deprecated use DM.getExistingCache instead.
    */
+  @Deprecated
   public static GemFireCacheImpl getExisting(String reason) {
     GemFireCacheImpl result = getInstance();
     if (result == null) {
diff --git 
a/geode-core/src/main/java/org/apache/geode/internal/cache/StateFlushOperation.java
 
b/geode-core/src/main/java/org/apache/geode/internal/cache/StateFlushOperation.java
index 6509deac1f..fc3f07555f 100755
--- 
a/geode-core/src/main/java/org/apache/geode/internal/cache/StateFlushOperation.java
+++ 
b/geode-core/src/main/java/org/apache/geode/internal/cache/StateFlushOperation.java
@@ -311,11 +311,9 @@ private DistributedRegion getRegion(DistributionManager 
dm) {
       if (region != null) {
         return region;
       }
-      // set the init level requirement so that we don't hang in 
CacheFactory.getInstance() (bug
-      // 36175)
       int oldLevel = 
LocalRegion.setThreadInitLevelRequirement(LocalRegion.BEFORE_INITIAL_IMAGE);
       try {
-        InternalCache gfc = (InternalCache) 
CacheFactory.getInstance(dm.getSystem());
+        InternalCache gfc = dm.getExistingCache();
         Region r = gfc.getRegionByPathForProcessing(this.regionPath);
         if (r instanceof DistributedRegion) {
           region = (DistributedRegion) r;
@@ -328,11 +326,9 @@ private DistributedRegion getRegion(DistributionManager 
dm) {
 
     /** returns a set of all DistributedRegions for allRegions processing */
     private Set<DistributedRegion> getAllRegions(DistributionManager dm) {
-      // set the init level requirement so that we don't hang in 
CacheFactory.getInstance() (bug
-      // 36175)
       int oldLevel = 
LocalRegion.setThreadInitLevelRequirement(LocalRegion.BEFORE_INITIAL_IMAGE);
       try {
-        InternalCache cache = (InternalCache) 
CacheFactory.getInstance(dm.getSystem());
+        InternalCache cache = dm.getExistingCache();
         Set<DistributedRegion> result = new HashSet();
         for (LocalRegion r : cache.getAllRegions()) {
           // it's important not to check if the cache is closing, so access
diff --git a/geode-core/src/test/java/org/apache/geode/test/fake/Fakes.java 
b/geode-core/src/test/java/org/apache/geode/test/fake/Fakes.java
index cd75a04ddb..5148f5fe11 100644
--- a/geode-core/src/test/java/org/apache/geode/test/fake/Fakes.java
+++ b/geode-core/src/test/java/org/apache/geode/test/fake/Fakes.java
@@ -98,6 +98,7 @@ public static GemFireCacheImpl cache() {
     when(distributionManager.getSystem()).thenReturn(system);
     
when(distributionManager.getCancelCriterion()).thenReturn(systemCancelCriterion);
     when(distributionManager.getCache()).thenReturn(cache);
+    when(distributionManager.getExistingCache()).thenReturn(cache);
 
     return cache;
   }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> refactor StateMarkerMessage to no call CacheFactory.getInstance
> ---------------------------------------------------------------
>
>                 Key: GEODE-3595
>                 URL: https://issues.apache.org/jira/browse/GEODE-3595
>             Project: Geode
>          Issue Type: Sub-task
>          Components: regions
>            Reporter: Darrel Schneider
>            Assignee: Darrel Schneider
>
> The following methods on StateMarkerMessage should be changed to call 
> "dm.getCache" instead of CacheFactory.getInstance:
> org.apache.geode.internal.cache.StateFlushOperation.StateMarkerMessage.getAllRegions(DistributionManager)
> org.apache.geode.internal.cache.StateFlushOperation.StateMarkerMessage.getRegion(DistributionManager)



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to