aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-09-01 16:55:51 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-09-13 15:50:52 +0200
commit81b30756f2baecc347ce5cf97795ab861b8e4781 (patch)
tree314c1950ba5807022758e0734836666aaab74e2d /server
parentef16ed8f1b4f383734b8698300e94ff563197503 (diff)
downloadsonarqube-81b30756f2baecc347ce5cf97795ab861b8e4781.tar.gz
sonarqube-81b30756f2baecc347ce5cf97795ab861b8e4781.zip
SONAR-9741 remove health info from hazelcast when node shuts down
Diffstat (limited to 'server')
-rw-r--r--server/sonar-cluster/src/main/java/org/sonar/cluster/health/HealthStateRefresher.java2
-rw-r--r--server/sonar-cluster/src/main/java/org/sonar/cluster/health/SharedHealthState.java5
-rw-r--r--server/sonar-cluster/src/main/java/org/sonar/cluster/health/SharedHealthStateImpl.java10
-rw-r--r--server/sonar-cluster/src/test/java/org/sonar/cluster/health/HealthStateRefresherTest.java3
-rw-r--r--server/sonar-cluster/src/test/java/org/sonar/cluster/health/SharedHealthStateImplTest.java32
5 files changed, 50 insertions, 2 deletions
diff --git a/server/sonar-cluster/src/main/java/org/sonar/cluster/health/HealthStateRefresher.java b/server/sonar-cluster/src/main/java/org/sonar/cluster/health/HealthStateRefresher.java
index d37989dfa2d..5074095bf52 100644
--- a/server/sonar-cluster/src/main/java/org/sonar/cluster/health/HealthStateRefresher.java
+++ b/server/sonar-cluster/src/main/java/org/sonar/cluster/health/HealthStateRefresher.java
@@ -56,6 +56,6 @@ public class HealthStateRefresher implements Startable {
@Override
public void stop() {
- // nothing to do
+ sharedHealthState.clearMine();
}
}
diff --git a/server/sonar-cluster/src/main/java/org/sonar/cluster/health/SharedHealthState.java b/server/sonar-cluster/src/main/java/org/sonar/cluster/health/SharedHealthState.java
index a714487eb70..0e3515a6421 100644
--- a/server/sonar-cluster/src/main/java/org/sonar/cluster/health/SharedHealthState.java
+++ b/server/sonar-cluster/src/main/java/org/sonar/cluster/health/SharedHealthState.java
@@ -28,6 +28,11 @@ public interface SharedHealthState {
void writeMine(NodeHealth nodeHealth);
/**
+ * Clears the {@link NodeHealth} of the current node in the shared health state (if any).
+ */
+ void clearMine();
+
+ /**
* Reads the {@link NodeHealth} of all nodes which shared to the shared health state.
*/
Set<NodeHealth> readAll();
diff --git a/server/sonar-cluster/src/main/java/org/sonar/cluster/health/SharedHealthStateImpl.java b/server/sonar-cluster/src/main/java/org/sonar/cluster/health/SharedHealthStateImpl.java
index 32c178cab12..782af0dbc08 100644
--- a/server/sonar-cluster/src/main/java/org/sonar/cluster/health/SharedHealthStateImpl.java
+++ b/server/sonar-cluster/src/main/java/org/sonar/cluster/health/SharedHealthStateImpl.java
@@ -51,6 +51,16 @@ public class SharedHealthStateImpl implements SharedHealthState {
}
@Override
+ public void clearMine() {
+ Map<String, NodeHealth> sqHealthState = hazelcastClient.getReplicatedMap(SQ_HEALTH_STATE_REPLICATED_MAP_IDENTIFIER);
+ String clientUUID = hazelcastClient.getClientUUID();
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Reading {} and clear for {}", new HashMap<>(sqHealthState), clientUUID);
+ }
+ sqHealthState.remove(clientUUID);
+ }
+
+ @Override
public Set<NodeHealth> readAll() {
Map<String, NodeHealth> sqHealthState = hazelcastClient.getReplicatedMap(SQ_HEALTH_STATE_REPLICATED_MAP_IDENTIFIER);
if (LOG.isTraceEnabled()) {
diff --git a/server/sonar-cluster/src/test/java/org/sonar/cluster/health/HealthStateRefresherTest.java b/server/sonar-cluster/src/test/java/org/sonar/cluster/health/HealthStateRefresherTest.java
index edcc89af1fa..6f89ab04b7a 100644
--- a/server/sonar-cluster/src/test/java/org/sonar/cluster/health/HealthStateRefresherTest.java
+++ b/server/sonar-cluster/src/test/java/org/sonar/cluster/health/HealthStateRefresherTest.java
@@ -92,6 +92,7 @@ public class HealthStateRefresherTest {
public void stop_has_no_effect() {
underTest.stop();
- verifyZeroInteractions(executorService, nodeHealthProvider, sharedHealthState);
+ verify(sharedHealthState).clearMine();
+ verifyZeroInteractions(executorService, nodeHealthProvider);
}
}
diff --git a/server/sonar-cluster/src/test/java/org/sonar/cluster/health/SharedHealthStateImplTest.java b/server/sonar-cluster/src/test/java/org/sonar/cluster/health/SharedHealthStateImplTest.java
index 3e3afce53ea..778daeeab55 100644
--- a/server/sonar-cluster/src/test/java/org/sonar/cluster/health/SharedHealthStateImplTest.java
+++ b/server/sonar-cluster/src/test/java/org/sonar/cluster/health/SharedHealthStateImplTest.java
@@ -34,6 +34,9 @@ import org.sonar.cluster.localclient.HazelcastClient;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.sonar.cluster.health.NodeDetails.newNodeDetailsBuilder;
import static org.sonar.cluster.health.NodeHealth.newNodeHealthBuilder;
@@ -117,6 +120,35 @@ public class SharedHealthStateImplTest {
assertThat(logTester.logs(LoggerLevel.TRACE).iterator().next()).isEqualTo("Reading " + map);
}
+ @Test
+ public void clearMine_clears_entry_into_map_sq_health_state_under_current_client_uuid() {
+ Map<String, NodeHealth> map = mock(Map.class);
+ doReturn(map).when(hazelcastClient).getReplicatedMap(MAP_SQ_HEALTH_STATE);
+ String uuid = randomAlphanumeric(5);
+ when(hazelcastClient.getClientUUID()).thenReturn(uuid);
+
+ underTest.clearMine();
+
+ verify(map).remove(uuid);
+ verifyNoMoreInteractions(map);
+ assertThat(logTester.logs()).isEmpty();
+ }
+
+ @Test
+ public void clearMine_logs_map_sq_health_state_and_current_client_uuid_if_TRACE() {
+ logTester.setLevel(LoggerLevel.TRACE);
+ Map<String, NodeHealth> map = new HashMap<>();
+ map.put(randomAlphanumeric(4), randomNodeHealth());
+ doReturn(map).when(hazelcastClient).getReplicatedMap(MAP_SQ_HEALTH_STATE);
+ String uuid = randomAlphanumeric(5);
+ when(hazelcastClient.getClientUUID()).thenReturn(uuid);
+
+ underTest.clearMine();
+
+ assertThat(logTester.logs()).hasSize(1);
+ assertThat(logTester.logs(LoggerLevel.TRACE).iterator().next()).isEqualTo("Reading " + map + " and clearing for " + uuid);
+ }
+
private NodeHealth randomNodeHealth() {
return newNodeHealthBuilder()
.setStatus(NodeHealth.Status.values()[random.nextInt(NodeHealth.Status.values().length)])