Skip to content

Commit 5f33d22

Browse files
committed
GEODE-10106: Use local ref to queueConnection. (#7740)
* Using queueConnection local ref for multiple if checks * As it is a volatile variable, the value may become null mid checks. (cherry picked from commit b16dafa)
1 parent f05ef0d commit 5f33d22

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

geode-core/src/main/java/org/apache/geode/cache/client/internal/QueueManagerImpl.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,7 @@ public void recoverPrimary(Set<ServerLocation> excludedServers) {
862862
return;
863863
}
864864
final boolean isDebugEnabled = logger.isDebugEnabled();
865-
if (queueConnections != null && queueConnections.getPrimary() != null
866-
&& !queueConnections.getPrimary().isDestroyed()) {
865+
if (!isPrimaryRecoveryNeeded(queueConnections)) {
867866
if (isDebugEnabled) {
868867
logger.debug("Primary recovery not needed");
869868
}
@@ -966,6 +965,16 @@ public void recoverPrimary(Set<ServerLocation> excludedServers) {
966965
}
967966
}
968967

968+
static boolean isPrimaryRecoveryNeeded(final ConnectionList queueConnectionList) {
969+
if (queueConnectionList != null) {
970+
final Connection primaryConnection = queueConnectionList.getPrimary();
971+
if (primaryConnection != null) {
972+
return primaryConnection.isDestroyed();
973+
}
974+
}
975+
return true;
976+
}
977+
969978
private QueueConnectionImpl initializeQueueConnection(Connection connection, boolean isPrimary,
970979
ClientUpdater failedUpdater) {
971980
QueueConnectionImpl queueConnection = null;

geode-core/src/test/java/org/apache/geode/cache/client/internal/QueueManagerImplTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@
2323
import static org.mockito.Mockito.mock;
2424
import static org.mockito.Mockito.never;
2525
import static org.mockito.Mockito.spy;
26+
import static org.mockito.Mockito.times;
2627
import static org.mockito.Mockito.verify;
2728
import static org.mockito.Mockito.when;
2829

30+
import java.util.HashSet;
31+
import java.util.Set;
32+
2933
import org.junit.Before;
3034
import org.junit.Test;
3135
import org.mockito.InOrder;
3236

37+
import org.apache.geode.distributed.internal.ServerLocation;
38+
3339
public class QueueManagerImplTest {
3440
private final InternalPool pool = mock(InternalPool.class, RETURNS_DEEP_STUBS);
3541
private final Endpoint endpoint = mock(Endpoint.class);
@@ -39,6 +45,9 @@ public class QueueManagerImplTest {
3945
private final ClientUpdater clientUpdater = mock(ClientUpdater.class);
4046
private QueueManagerImpl queueManager;
4147

48+
private final QueueManagerImpl.ConnectionList queueConnections = mock(
49+
QueueManagerImpl.ConnectionList.class);
50+
4251
@Before
4352
public void setup() {
4453
queueManager = new QueueManagerImpl(pool, null, null, null, 1, 1, null, null);
@@ -53,6 +62,34 @@ public void setup() {
5362
when(backupEndpoint.isClosed()).thenReturn(false);
5463
}
5564

65+
@Test
66+
public void whenPrimaryIsNotDestroyedThenPrimaryRecoveryIsNotNeeded() {
67+
assertThat(queueManager.addToConnectionList((primary), true)).isTrue();
68+
when(primary.isDestroyed()).thenReturn(false);
69+
when(pool.getPoolOrCacheCancelInProgress()).thenReturn(null);
70+
Set<ServerLocation> excludedServers = new HashSet<>();
71+
queueManager.recoverPrimary(excludedServers);
72+
verify(pool, times(2)).getPoolOrCacheCancelInProgress();
73+
}
74+
75+
@Test
76+
public void whenPrimaryIsNullThenPrimaryRecoveryIsNeeded() {
77+
assertThat(QueueManagerImpl.isPrimaryRecoveryNeeded(null)).isTrue();
78+
}
79+
80+
@Test
81+
public void whenConnectionListIsNullThenPrimaryRecoveryIsNeeded() {
82+
when(queueConnections.getPrimary()).thenReturn(null);
83+
assertThat(QueueManagerImpl.isPrimaryRecoveryNeeded(queueConnections)).isTrue();
84+
}
85+
86+
@Test
87+
public void whenPrimaryIsDestroyedThenPrimaryRecoveryIsNeeded() {
88+
when(queueConnections.getPrimary()).thenReturn(primary);
89+
when(primary.isDestroyed()).thenReturn(true);
90+
assertThat(QueueManagerImpl.isPrimaryRecoveryNeeded(queueConnections)).isTrue();
91+
}
92+
5693
@Test
5794
public void addNoClientUpdaterConnectionToConnectionListReturnsFalse() {
5895
when(primary.getUpdater()).thenReturn(null);

0 commit comments

Comments
 (0)