[
https://issues.apache.org/jira/browse/GEODE-3594?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16246301#comment-16246301
]
ASF GitHub Bot commented on GEODE-3594:
---------------------------------------
dschneider-pivotal closed pull request #1029: GEODE-3594: remove calls of
getInstance
URL: https://github.com/apache/geode/pull/1029
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/SearchLoadAndWriteProcessor.java
b/geode-core/src/main/java/org/apache/geode/internal/cache/SearchLoadAndWriteProcessor.java
index 552048ca6d..f7e8506d6c 100755
---
a/geode-core/src/main/java/org/apache/geode/internal/cache/SearchLoadAndWriteProcessor.java
+++
b/geode-core/src/main/java/org/apache/geode/internal/cache/SearchLoadAndWriteProcessor.java
@@ -1271,7 +1271,7 @@ private synchronized void waitForObject2(final int
timeoutMs) throws TimeoutExce
}
private int getSearchTimeout() {
- return region.getCache().getSearchTimeout(); //
CacheFactory.getInstance(((DistributedRegion)this.region).getSystem()).getSearchTimeout();
+ return region.getCache().getSearchTimeout();
}
private void resetResults() {
@@ -1504,7 +1504,7 @@ private void doGet(DistributionManager dm) {
try {
// check to see if we would have to wait on initialization latch (if
global)
// if so abort and reply with null
- InternalCache cache = (InternalCache)
CacheFactory.getInstance(dm.getSystem());
+ InternalCache cache = dm.getExistingCache();
if (cache.isGlobalRegionInitializing(this.regionName)) {
replyWithNull(dm);
if (logger.isDebugEnabled()) {
@@ -1513,8 +1513,7 @@ private void doGet(DistributionManager dm) {
return;
}
- LocalRegion region =
- (LocalRegion)
CacheFactory.getInstance(dm.getSystem()).getRegion(this.regionName);
+ LocalRegion region = (LocalRegion)
dm.getExistingCache().getRegion(this.regionName);
Object o = null;
if (region != null) {
@@ -1850,8 +1849,7 @@ private void doGet(DistributionManager dm) {
int oldLevel =
LocalRegion.setThreadInitLevelRequirement(LocalRegion.BEFORE_INITIAL_IMAGE);
try {
- LocalRegion region =
- (LocalRegion)
CacheFactory.getInstance(dm.getSystem()).getRegion(this.regionName);
+ LocalRegion region = (LocalRegion)
dm.getExistingCache().getRegion(this.regionName);
if (region != null) {
setClearCountReference(region);
try {
@@ -2210,7 +2208,7 @@ private void doLoad(DistributionManager dm) {
long startTime = dm.cacheTimeMillis();
int oldLevel =
LocalRegion.setThreadInitLevelRequirement(LocalRegion.BEFORE_INITIAL_IMAGE);
try {
- InternalCache gfc = (InternalCache)
CacheFactory.getInstance(dm.getSystem());
+ InternalCache gfc = dm.getExistingCache();
LocalRegion region = (LocalRegion) gfc.getRegion(this.regionName);
if (region != null && region.isInitialized()
&& (dm.cacheTimeMillis() - startTime < timeoutMs)) {
@@ -2467,7 +2465,7 @@ protected void process(DistributionManager dm) {
long startTime = dm.cacheTimeMillis();
int oldLevel =
LocalRegion.setThreadInitLevelRequirement(LocalRegion.BEFORE_INITIAL_IMAGE);
try {
- InternalCache gfc = (InternalCache)
CacheFactory.getInstance(dm.getSystem());
+ InternalCache gfc = dm.getExistingCache();
LocalRegion region = (LocalRegion) gfc.getRegion(this.regionName);
if (region != null && region.isInitialized()
&& (dm.cacheTimeMillis() - startTime < timeoutMs)) {
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 SearchLoadAndWriteProcessor to not use CacheFactory.getInstance
> ------------------------------------------------------------------------
>
> Key: GEODE-3594
> URL: https://issues.apache.org/jira/browse/GEODE-3594
> Project: Geode
> Issue Type: Sub-task
> Components: regions
> Reporter: Darrel Schneider
> Assignee: Darrel Schneider
> Fix For: 1.4.0
>
>
> The following methods all call CacheFactory.getInstance and could instead
> call "dm.getCache":
> org.apache.geode.internal.cache.SearchLoadAndWriteProcessor.NetSearchRequestMessage.doGet(DistributionManager)
> org.apache.geode.internal.cache.SearchLoadAndWriteProcessor.QueryMessage.doGet(DistributionManager)
> twice
> org.apache.geode.internal.cache.SearchLoadAndWriteProcessor.NetLoadRequestMessage.doLoad(DistributionManager)
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)