Skip to content

Commit

Permalink
GEODE-8706: getConnection should get readLock to sync with destroyCon…
Browse files Browse the repository at this point in the history
…nection
  • Loading branch information
gesterzhou committed Nov 14, 2020
1 parent 99e7a13 commit a64ab2e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Expand Up @@ -25,6 +25,7 @@

import org.apache.geode.CancelException;
import org.apache.geode.GemFireIOException;
import org.apache.geode.annotations.VisibleForTesting;
import org.apache.geode.cache.RegionDestroyedException;
import org.apache.geode.cache.client.ServerConnectivityException;
import org.apache.geode.cache.client.ServerOperationException;
Expand Down Expand Up @@ -280,6 +281,11 @@ private boolean _dispatchBatch(List events, boolean isRetry) {
}
}

@VisibleForTesting
ReentrantReadWriteLock getConnectionLifeCycleLock() {
return this.connectionLifeCycleLock;
}

/**
* Acquires or adds a new <code>Connection</code> to the corresponding <code>Gateway</code>
*
Expand All @@ -295,9 +301,16 @@ public Connection getConnection(boolean startAckReaderThread) throws GatewaySend
// OR the connection's ServerLocation doesn't match with the one stored in sender
// THEN initialize the connection
if (!this.sender.isParallel()) {
if (this.connection == null || this.connection.isDestroyed()
|| this.connection.getServer() == null
|| !this.connection.getServer().equals(this.sender.getServerLocation())) {
boolean needToReconnect = false;
getConnectionLifeCycleLock().readLock().lock();
try {
needToReconnect = this.connection == null || this.connection.isDestroyed()
|| this.connection.getServer() == null
|| !this.connection.getServer().equals(this.sender.getServerLocation());
} finally {
getConnectionLifeCycleLock().readLock().unlock();
}
if (needToReconnect) {
if (logger.isDebugEnabled()) {
logger.debug(
"Initializing new connection as serverLocation of old connection is : {} and the serverLocation to connect is {}",
Expand Down
Expand Up @@ -75,5 +75,6 @@ public void getConnectionShouldCreateNewConnectionWhenServerIsNull() {
doNothing().when(dispatcher).initializeConnection();
Connection newConnection = dispatcher.getConnection(true);
verify(dispatcher, times(1)).initializeConnection();
verify(dispatcher, times(2)).getConnectionLifeCycleLock();
}
}

0 comments on commit a64ab2e

Please sign in to comment.