1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.process.cluster.health;
import com.google.common.collect.ImmutableSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.stream.IntStream;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.event.Level;
import org.sonar.process.LoggingRule;
import org.sonar.process.cluster.hz.HazelcastMember;
import static java.util.Collections.singleton;
import static org.apache.commons.lang3.RandomStringUtils.secure;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.process.cluster.health.NodeDetails.newNodeDetailsBuilder;
import static org.sonar.process.cluster.health.NodeHealth.newNodeHealthBuilder;
public class SharedHealthStateImplTest {
private static final String MAP_SQ_HEALTH_STATE = "sq_health_state";
@Rule
public LoggingRule logging = new LoggingRule(SharedHealthStateImpl.class);
private final Random random = new Random();
private long clusterTime = 99 + Math.abs(random.nextInt(9621));
private HazelcastMember hazelcastMember = mock(HazelcastMember.class);
private SharedHealthStateImpl underTest = new SharedHealthStateImpl(hazelcastMember);
@Test
public void write_fails_with_NPE_if_arg_is_null() {
assertThatThrownBy(() -> underTest.writeMine(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("nodeHealth can't be null");
}
@Test
public void write_put_arg_into_map_sq_health_state_under_current_client_uuid() {
NodeHealth nodeHealth = randomNodeHealth();
Map<UUID, TimestampedNodeHealth> map = new HashMap<>();
doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE);
long clusterTime = random.nextLong();
UUID uuid = UUID.randomUUID();
when(hazelcastMember.getUuid()).thenReturn(uuid);
when(hazelcastMember.getClusterTime()).thenReturn(clusterTime);
underTest.writeMine(nodeHealth);
assertThat(map).hasSize(1);
assertThat(map.get(uuid)).isEqualTo(new TimestampedNodeHealth(nodeHealth, clusterTime));
assertThat(logging.getLogs()).isEmpty();
}
@Test
public void write_logs_map_sq_health_state_content_and_NodeHealth_to_be_added_if_TRACE() {
logging.setLevel(Level.TRACE);
NodeHealth newNodeHealth = randomNodeHealth();
Map<String, TimestampedNodeHealth> map = new HashMap<>();
map.put(secure().nextAlphanumeric(4), new TimestampedNodeHealth(randomNodeHealth(), random.nextLong()));
doReturn(new HashMap<>(map)).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE);
UUID uuid = UUID.randomUUID();
when(hazelcastMember.getUuid()).thenReturn(uuid);
underTest.writeMine(newNodeHealth);
assertThat(logging.getLogs()).hasSize(1);
assertThat(logging.hasLog(Level.TRACE, "Reading " + map + " and adding " + newNodeHealth)).isTrue();
}
@Test
public void readAll_returns_all_NodeHealth_in_map_sq_health_state_for_existing_client_uuids_aged_less_than_30_seconds() {
NodeHealth[] nodeHealths = IntStream.range(0, 1 + random.nextInt(6)).mapToObj(i -> randomNodeHealth()).toArray(NodeHealth[]::new);
Map<UUID, TimestampedNodeHealth> allNodeHealths = new HashMap<>();
Map<UUID, NodeHealth> expected = new HashMap<>();
for (NodeHealth nodeHealth : nodeHealths) {
UUID memberUuid = UUID.randomUUID();
TimestampedNodeHealth timestampedNodeHealth = new TimestampedNodeHealth(nodeHealth, clusterTime - random.nextInt(30 * 1000));
allNodeHealths.put(memberUuid, timestampedNodeHealth);
if (random.nextBoolean()) {
expected.put(memberUuid, nodeHealth);
}
}
doReturn(allNodeHealths).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE);
when(hazelcastMember.getMemberUuids()).thenReturn(expected.keySet());
when(hazelcastMember.getClusterTime()).thenReturn(clusterTime);
assertThat(underTest.readAll())
.containsOnly(expected.values().toArray(new NodeHealth[0]));
assertThat(logging.getLogs()).isEmpty();
}
@Test
public void readAll_ignores_NodeHealth_of_30_seconds_before_cluster_time() {
NodeHealth nodeHealth = randomNodeHealth();
Map<UUID, TimestampedNodeHealth> map = new HashMap<>();
UUID memberUuid = UUID.randomUUID();
TimestampedNodeHealth timestampedNodeHealth = new TimestampedNodeHealth(nodeHealth, clusterTime - 30 * 1000);
map.put(memberUuid, timestampedNodeHealth);
doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE);
when(hazelcastMember.getMemberUuids()).thenReturn(map.keySet());
when(hazelcastMember.getClusterTime()).thenReturn(clusterTime);
assertThat(underTest.readAll()).isEmpty();
}
@Test
public void readAll_ignores_NodeHealth_of_more_than_30_seconds_before_cluster_time() {
NodeHealth nodeHealth = randomNodeHealth();
Map<UUID, TimestampedNodeHealth> map = new HashMap<>();
UUID memberUuid = UUID.randomUUID();
TimestampedNodeHealth timestampedNodeHealth = new TimestampedNodeHealth(nodeHealth, clusterTime - 30 * 1000 - random.nextInt(99));
map.put(memberUuid, timestampedNodeHealth);
doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE);
when(hazelcastMember.getMemberUuids()).thenReturn(map.keySet());
when(hazelcastMember.getClusterTime()).thenReturn(clusterTime);
assertThat(underTest.readAll()).isEmpty();
}
@Test
public void readAll_logs_map_sq_health_state_content_and_the_content_effectively_returned_if_TRACE() {
logging.setLevel(Level.TRACE);
Map<UUID, TimestampedNodeHealth> map = new HashMap<>();
UUID uuid = UUID.randomUUID();
NodeHealth nodeHealth = randomNodeHealth();
map.put(uuid, new TimestampedNodeHealth(nodeHealth, clusterTime - 1));
when(hazelcastMember.getClusterTime()).thenReturn(clusterTime);
when(hazelcastMember.getMemberUuids()).thenReturn(singleton(uuid));
doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE);
underTest.readAll();
assertThat(logging.getLogs()).hasSize(1);
assertThat(logging.hasLog(Level.TRACE, "Reading " + new HashMap<>(map) + " and keeping " + singleton(nodeHealth))).isTrue();
}
@Test
public void readAll_logs_message_for_each_non_existing_member_ignored_if_TRACE() {
logging.setLevel(Level.TRACE);
Map<String, TimestampedNodeHealth> map = new HashMap<>();
String memberUuid1 = secure().nextAlphanumeric(44);
String memberUuid2 = secure().nextAlphanumeric(44);
map.put(memberUuid1, new TimestampedNodeHealth(randomNodeHealth(), clusterTime - 1));
map.put(memberUuid2, new TimestampedNodeHealth(randomNodeHealth(), clusterTime - 1));
when(hazelcastMember.getClusterTime()).thenReturn(clusterTime);
doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE);
underTest.readAll();
assertThat(logging.getLogs()).hasSize(3);
assertThat(logging.getLogs(Level.TRACE))
.containsOnly(
"Reading " + new HashMap<>(map) + " and keeping []",
"Ignoring NodeHealth of member " + memberUuid1 + " because it is not part of the cluster at the moment",
"Ignoring NodeHealth of member " + memberUuid2 + " because it is not part of the cluster at the moment");
}
@Test
public void readAll_logs_message_for_each_timed_out_NodeHealth_ignored_if_TRACE() {
logging.setLevel(Level.TRACE);
Map<UUID, TimestampedNodeHealth> map = new HashMap<>();
UUID memberUuid1 = UUID.randomUUID();
UUID memberUuid2 = UUID.randomUUID();
map.put(memberUuid1, new TimestampedNodeHealth(randomNodeHealth(), clusterTime - 30 * 1000));
map.put(memberUuid2, new TimestampedNodeHealth(randomNodeHealth(), clusterTime - 30 * 1000));
doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE);
when(hazelcastMember.getMemberUuids()).thenReturn(ImmutableSet.of(memberUuid1, memberUuid2));
when(hazelcastMember.getClusterTime()).thenReturn(clusterTime);
underTest.readAll();
assertThat(logging.getLogs()).hasSize(3);
assertThat(logging.getLogs(Level.TRACE))
.containsOnly(
"Reading " + new HashMap<>(map) + " and keeping []",
"Ignoring NodeHealth of member " + memberUuid1 + " because it is too old",
"Ignoring NodeHealth of member " + memberUuid2 + " because it is too old");
}
@Test
public void clearMine_clears_entry_into_map_sq_health_state_under_current_client_uuid() {
Map<UUID, TimestampedNodeHealth> map = new HashMap<>();
UUID uuid = UUID.randomUUID();
map.put(uuid, new TimestampedNodeHealth());
doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE);
when(hazelcastMember.getUuid()).thenReturn(uuid);
underTest.clearMine();
assertThat(map).doesNotContainKey(uuid);
assertThat(logging.getLogs()).isEmpty();
}
@Test
public void clearMine_logs_map_sq_health_state_and_current_client_uuid_if_TRACE() {
logging.setLevel(Level.TRACE);
Map<UUID, TimestampedNodeHealth> map = new HashMap<>();
map.put(UUID.randomUUID(), new TimestampedNodeHealth(randomNodeHealth(), random.nextLong()));
doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE);
UUID uuid = UUID.randomUUID();
when(hazelcastMember.getUuid()).thenReturn(uuid);
underTest.clearMine();
assertThat(logging.getLogs()).hasSize(1);
assertThat(logging.hasLog(Level.TRACE, "Reading " + map + " and clearing for " + uuid)).isTrue();
}
private NodeHealth randomNodeHealth() {
return newNodeHealthBuilder()
.setStatus(NodeHealth.Status.values()[random.nextInt(NodeHealth.Status.values().length)])
.setDetails(newNodeDetailsBuilder()
.setType(random.nextBoolean() ? NodeDetails.Type.SEARCH : NodeDetails.Type.APPLICATION)
.setName(secure().nextAlphanumeric(30))
.setHost(secure().nextAlphanumeric(10))
.setPort(1 + random.nextInt(666))
.setStartedAt(1 + random.nextInt(852))
.build())
.build();
}
}
|