Skip to content

Commit faaece8

Browse files
committed
Add UTs
Signed-off-by: Arpit Bandejiya <[email protected]>
1 parent cc0871e commit faaece8

File tree

5 files changed

+144
-9
lines changed

5 files changed

+144
-9
lines changed

server/src/main/java/org/opensearch/cluster/coordination/PublicationTransportHandler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private PublishWithJoinResponse handleIncomingPublishRequest(BytesTransportReque
233233
}
234234

235235
// package private for testing
236-
PublishWithJoinResponse handleIncomingRemotePublishRequest(RemotePublishRequest request) throws IOException, RuntimeException {
236+
PublishWithJoinResponse handleIncomingRemotePublishRequest(RemotePublishRequest request) throws IOException, IllegalStateException {
237237
try {
238238
if (transportService.getLocalNode().equals(request.getSourceNode())) {
239239
return acceptRemoteStateOnLocalNode(request);
@@ -302,8 +302,7 @@ PublishWithJoinResponse handleIncomingRemotePublishRequest(RemotePublishRequest
302302
}
303303
} catch (Exception e) {
304304
remoteClusterStateService.readMetadataFailed();
305-
if (e instanceof IOException) throw new IOException("IOException in reading remote cluster state", e);
306-
throw new RuntimeException("Runtime exception in reading remote cluster state", e);
305+
throw e;
307306
}
308307
}
309308

server/src/main/java/org/opensearch/gateway/remote/RemoteDownloadStats.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@ public void fullDownloadState() {
3333
fullDownloadCount.incrementAndGet();
3434
}
3535

36+
public long getDiffDownloadCount() {
37+
return diffDownloadCount.get();
38+
}
39+
40+
public long getFullDownloadCount() {
41+
return fullDownloadCount.get();
42+
}
3643
}

server/src/test/java/org/opensearch/cluster/coordination/PublicationTransportHandlerTests.java

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
import org.opensearch.common.settings.Settings;
4545
import org.opensearch.core.common.io.stream.StreamOutput;
4646
import org.opensearch.gateway.remote.ClusterMetadataManifest;
47+
import org.opensearch.gateway.remote.ClusterStateDiffManifest;
4748
import org.opensearch.gateway.remote.RemoteClusterStateService;
49+
import org.opensearch.gateway.remote.RemoteDownloadStats;
4850
import org.opensearch.node.Node;
4951
import org.opensearch.telemetry.tracing.noop.NoopTracer;
5052
import org.opensearch.test.OpenSearchTestCase;
@@ -62,8 +64,12 @@
6264
import static org.hamcrest.Matchers.containsString;
6365
import static org.hamcrest.Matchers.instanceOf;
6466
import static org.hamcrest.Matchers.is;
67+
import static org.mockito.ArgumentMatchers.any;
68+
import static org.mockito.Mockito.doAnswer;
6569
import static org.mockito.Mockito.mock;
6670
import static org.mockito.Mockito.times;
71+
import static org.mockito.Mockito.verify;
72+
import static org.mockito.Mockito.verifyNoMoreInteractions;
6773
import static org.mockito.Mockito.when;
6874

6975
public class PublicationTransportHandlerTests extends OpenSearchTestCase {
@@ -160,7 +166,8 @@ public void testHandleIncomingRemotePublishRequestWhenNoCurrentPublishRequest()
160166
() -> handler.handleIncomingRemotePublishRequest(remotePublishRequest)
161167
);
162168
assertThat(e.getMessage(), containsString("publication to self failed"));
163-
Mockito.verifyNoInteractions(remoteClusterStateService);
169+
verify(remoteClusterStateService, times(1)).readMetadataFailed();
170+
verifyNoMoreInteractions(remoteClusterStateService);
164171
}
165172

166173
public void testHandleIncomingRemotePublishRequestWhenTermMismatch() {
@@ -185,7 +192,8 @@ public void testHandleIncomingRemotePublishRequestWhenTermMismatch() {
185192
() -> handler.handleIncomingRemotePublishRequest(remotePublishRequest)
186193
);
187194
assertThat(e.getMessage(), containsString("publication to self failed"));
188-
Mockito.verifyNoInteractions(remoteClusterStateService);
195+
verify(remoteClusterStateService, times(1)).readMetadataFailed();
196+
verifyNoMoreInteractions(remoteClusterStateService);
189197
}
190198

191199
public void testHandleIncomingRemotePublishRequestWhenVersionMismatch() {
@@ -210,7 +218,8 @@ public void testHandleIncomingRemotePublishRequestWhenVersionMismatch() {
210218
() -> handler.handleIncomingRemotePublishRequest(remotePublishRequest)
211219
);
212220
assertThat(e.getMessage(), containsString("publication to self failed"));
213-
Mockito.verifyNoInteractions(remoteClusterStateService);
221+
verify(remoteClusterStateService, times(1)).readMetadataFailed();
222+
verifyNoMoreInteractions(remoteClusterStateService);
214223
}
215224

216225
public void testHandleIncomingRemotePublishRequestForLocalNode() throws IOException {
@@ -235,6 +244,119 @@ public void testHandleIncomingRemotePublishRequestForLocalNode() throws IOExcept
235244
Mockito.verifyNoInteractions(remoteClusterStateService);
236245
}
237246

247+
public void testDownloadRemotePersistedFailedStats() throws IOException {
248+
RemoteDownloadStats remoteDownloadStats = new RemoteDownloadStats();
249+
RemoteClusterStateService remoteClusterStateService = mock(RemoteClusterStateService.class);
250+
when(remoteClusterStateService.getDownloadStats()).thenReturn(remoteDownloadStats);
251+
252+
doAnswer((i) -> {
253+
remoteDownloadStats.stateFailed();
254+
return null;
255+
}).when(remoteClusterStateService).readMetadataFailed();
256+
257+
PublishWithJoinResponse expectedPublishResponse = new PublishWithJoinResponse(new PublishResponse(TERM, VERSION), Optional.empty());
258+
Function<PublishRequest, PublishWithJoinResponse> handlePublishRequest = p -> expectedPublishResponse;
259+
final PublicationTransportHandler handler = getPublicationTransportHandler(handlePublishRequest, remoteClusterStateService);
260+
RemotePublishRequest remotePublishRequest = new RemotePublishRequest(
261+
secondNode,
262+
TERM,
263+
VERSION,
264+
CLUSTER_NAME,
265+
CLUSTER_UUID,
266+
MANIFEST_FILE
267+
);
268+
ClusterState clusterState = buildClusterState(TERM, VERSION);
269+
PublishRequest publishRequest = new PublishRequest(clusterState);
270+
handler.setCurrentPublishRequestToSelf(publishRequest);
271+
272+
assertThrows(IllegalStateException.class, () -> handler.handleIncomingRemotePublishRequest(remotePublishRequest));
273+
assertEquals(1, remoteClusterStateService.getDownloadStats().getFailedCount());
274+
}
275+
276+
public void testDownloadRemotePersistedDiffStats() throws IOException {
277+
RemoteDownloadStats remoteDownloadStats = new RemoteDownloadStats();
278+
RemoteClusterStateService remoteClusterStateService = mock(RemoteClusterStateService.class);
279+
when(remoteClusterStateService.getDownloadStats()).thenReturn(remoteDownloadStats);
280+
ClusterMetadataManifest metadataManifest = new ClusterMetadataManifest.Builder().diffManifest(
281+
new ClusterStateDiffManifest.Builder().fromStateUUID("state-uuid").build()
282+
).build();
283+
when(remoteClusterStateService.getClusterMetadataManifestByFileName(any(), any())).thenReturn(metadataManifest);
284+
285+
doAnswer((i) -> {
286+
remoteDownloadStats.diffDownloadState();
287+
return null;
288+
}).when(remoteClusterStateService).diffDownloadState();
289+
290+
doAnswer((i) -> {
291+
remoteDownloadStats.fullDownloadState();
292+
return null;
293+
}).when(remoteClusterStateService).fullDownloadState();
294+
295+
PublishWithJoinResponse expectedPublishResponse = new PublishWithJoinResponse(new PublishResponse(TERM, VERSION), Optional.empty());
296+
Function<PublishRequest, PublishWithJoinResponse> handlePublishRequest = p -> expectedPublishResponse;
297+
final PublicationTransportHandler handler = getPublicationTransportHandler(handlePublishRequest, remoteClusterStateService);
298+
ClusterState clusterState = mock(ClusterState.class);
299+
handler.setLastSeenClusterState(clusterState);
300+
when(clusterState.stateUUID()).thenReturn("state-uuid");
301+
302+
RemotePublishRequest remotePublishRequest = new RemotePublishRequest(
303+
secondNode,
304+
TERM,
305+
VERSION,
306+
CLUSTER_NAME,
307+
CLUSTER_UUID,
308+
MANIFEST_FILE
309+
);
310+
clusterState = buildClusterState(TERM, VERSION);
311+
PublishRequest publishRequest = new PublishRequest(clusterState);
312+
handler.setCurrentPublishRequestToSelf(publishRequest);
313+
assertThrows(NullPointerException.class, () -> handler.handleIncomingRemotePublishRequest(remotePublishRequest));
314+
assertEquals(1, remoteDownloadStats.getDiffDownloadCount());
315+
assertEquals(0, remoteDownloadStats.getFullDownloadCount());
316+
}
317+
318+
public void testDownloadRemotePersistedFullStats() throws IOException {
319+
RemoteDownloadStats remoteDownloadStats = new RemoteDownloadStats();
320+
RemoteClusterStateService remoteClusterStateService = mock(RemoteClusterStateService.class);
321+
when(remoteClusterStateService.getDownloadStats()).thenReturn(remoteDownloadStats);
322+
ClusterMetadataManifest metadataManifest = new ClusterMetadataManifest.Builder().diffManifest(
323+
new ClusterStateDiffManifest.Builder().fromStateUUID("state-uuid2").build()
324+
).build();
325+
when(remoteClusterStateService.getClusterMetadataManifestByFileName(any(), any())).thenReturn(metadataManifest);
326+
327+
doAnswer((i) -> {
328+
remoteDownloadStats.diffDownloadState();
329+
return null;
330+
}).when(remoteClusterStateService).diffDownloadState();
331+
332+
doAnswer((i) -> {
333+
remoteDownloadStats.fullDownloadState();
334+
return null;
335+
}).when(remoteClusterStateService).fullDownloadState();
336+
337+
PublishWithJoinResponse expectedPublishResponse = new PublishWithJoinResponse(new PublishResponse(TERM, VERSION), Optional.empty());
338+
Function<PublishRequest, PublishWithJoinResponse> handlePublishRequest = p -> expectedPublishResponse;
339+
final PublicationTransportHandler handler = getPublicationTransportHandler(handlePublishRequest, remoteClusterStateService);
340+
ClusterState clusterState = mock(ClusterState.class);
341+
handler.setLastSeenClusterState(clusterState);
342+
when(clusterState.stateUUID()).thenReturn("state-uuid");
343+
344+
RemotePublishRequest remotePublishRequest = new RemotePublishRequest(
345+
secondNode,
346+
TERM,
347+
VERSION,
348+
CLUSTER_NAME,
349+
CLUSTER_UUID,
350+
MANIFEST_FILE
351+
);
352+
clusterState = buildClusterState(TERM, VERSION);
353+
PublishRequest publishRequest = new PublishRequest(clusterState);
354+
handler.setCurrentPublishRequestToSelf(publishRequest);
355+
assertThrows(NullPointerException.class, () -> handler.handleIncomingRemotePublishRequest(remotePublishRequest));
356+
assertEquals(0, remoteDownloadStats.getDiffDownloadCount());
357+
assertEquals(1, remoteDownloadStats.getFullDownloadCount());
358+
}
359+
238360
public void testHandleIncomingRemotePublishRequestWhenManifestNotFound() throws IOException {
239361
RemoteClusterStateService remoteClusterStateService = mock(RemoteClusterStateService.class);
240362

server/src/test/java/org/opensearch/gateway/GatewayMetaStatePersistedStateTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
import static org.hamcrest.Matchers.nullValue;
110110
import static org.mockito.ArgumentMatchers.any;
111111
import static org.mockito.ArgumentMatchers.anyBoolean;
112-
import static org.mockito.Mockito.doCallRealMethod;
112+
import static org.mockito.Mockito.doAnswer;
113113
import static org.mockito.Mockito.mock;
114114
import static org.mockito.Mockito.times;
115115
import static org.mockito.Mockito.verify;
@@ -837,7 +837,10 @@ public void testRemotePersistedStateFailureStats() throws IOException {
837837
final String previousClusterUUID = "prev-cluster-uuid";
838838
Mockito.doThrow(IOException.class).when(remoteClusterStateService).writeFullMetadata(Mockito.any(), Mockito.any());
839839
when(remoteClusterStateService.getUploadStats()).thenReturn(remoteStateStats);
840-
doCallRealMethod().when(remoteClusterStateService).writeMetadataFailed();
840+
doAnswer((i) -> {
841+
remoteStateStats.stateFailed();
842+
return null;
843+
}).when(remoteClusterStateService).writeMetadataFailed();
841844
CoordinationState.PersistedState remotePersistedState = new RemotePersistedState(remoteClusterStateService, previousClusterUUID);
842845

843846
final long clusterTerm = randomNonNegativeLong();

server/src/test/java/org/opensearch/gateway/remote/RemoteClusterStateServiceTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,10 @@ public void testGetClusterStateForManifest_IncludeEphemeral() throws IOException
851851
when(mockedResult.getComponent()).thenReturn(COORDINATION_METADATA);
852852
RemoteClusterStateService mockService = spy(remoteClusterStateService);
853853
mockService.getClusterStateForManifest(ClusterName.DEFAULT.value(), manifest, NODE_ID, true);
854+
855+
assertNotNull(remoteClusterStateService.getDownloadStats());
856+
assertEquals(1, remoteClusterStateService.getDownloadStats().getSuccessCount());
857+
assertEquals(0, remoteClusterStateService.getDownloadStats().getFailedCount());
854858
verify(mockService, times(1)).readClusterStateInParallel(
855859
any(),
856860
eq(manifest),
@@ -2568,7 +2572,7 @@ public void testGetValidPreviousClusterUUIDWhenLastUUIDUncommitted() throws IOEx
25682572
assertThat(previousClusterUUID, equalTo("cluster-uuid2"));
25692573
}
25702574

2571-
public void testRemoteStateStats() throws IOException {
2575+
public void testRemoteStateUploadStats() throws IOException {
25722576
final ClusterState clusterState = generateClusterStateWithOneIndex().nodes(nodesWithLocalNodeClusterManager()).build();
25732577
mockBlobStoreObjects();
25742578
remoteClusterStateService.start();

0 commit comments

Comments
 (0)