|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.indices.replication; |
| 10 | + |
| 11 | +import org.opensearch.Version; |
| 12 | +import org.opensearch.action.ActionListener; |
| 13 | +import org.opensearch.cluster.node.DiscoveryNode; |
| 14 | +import org.opensearch.cluster.node.DiscoveryNodeRole; |
| 15 | +import org.opensearch.cluster.service.ClusterService; |
| 16 | +import org.opensearch.common.settings.ClusterSettings; |
| 17 | +import org.opensearch.common.settings.Settings; |
| 18 | +import org.opensearch.core.internal.io.IOUtils; |
| 19 | +import org.opensearch.index.shard.IndexShard; |
| 20 | +import org.opensearch.index.shard.IndexShardTestCase; |
| 21 | +import org.opensearch.index.store.Store; |
| 22 | +import org.opensearch.indices.recovery.RecoverySettings; |
| 23 | +import org.opensearch.indices.replication.checkpoint.ReplicationCheckpoint; |
| 24 | +import org.opensearch.test.transport.CapturingTransport; |
| 25 | +import org.opensearch.transport.TransportService; |
| 26 | + |
| 27 | +import java.util.Collections; |
| 28 | + |
| 29 | +import static java.util.Collections.emptyMap; |
| 30 | +import static org.hamcrest.Matchers.equalTo; |
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | +import static org.opensearch.test.ClusterServiceUtils.createClusterService; |
| 33 | + |
| 34 | +public class PeerReplicationSourceTests extends IndexShardTestCase { |
| 35 | + |
| 36 | + private CapturingTransport transport; |
| 37 | + private ClusterService clusterService; |
| 38 | + private TransportService transportService; |
| 39 | + private PeerReplicationSource peerReplicationSource; |
| 40 | + private IndexShard indexShard; |
| 41 | + private DiscoveryNode localNode; |
| 42 | + private DiscoveryNode sourceNode; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void setUp() throws Exception { |
| 46 | + super.setUp(); |
| 47 | + final Settings settings = Settings.builder().put("node.name", SegmentReplicationTargetServiceTests.class.getSimpleName()).build(); |
| 48 | + final ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); |
| 49 | + final RecoverySettings recoverySettings = new RecoverySettings(settings, clusterSettings); |
| 50 | + transport = new CapturingTransport(); |
| 51 | + localNode = newDiscoveryNode("localNode"); |
| 52 | + sourceNode = newDiscoveryNode("sourceNode"); |
| 53 | + clusterService = createClusterService(threadPool, localNode); |
| 54 | + transportService = transport.createTransportService( |
| 55 | + clusterService.getSettings(), |
| 56 | + threadPool, |
| 57 | + TransportService.NOOP_TRANSPORT_INTERCEPTOR, |
| 58 | + boundAddress -> clusterService.localNode(), |
| 59 | + null, |
| 60 | + Collections.emptySet() |
| 61 | + ); |
| 62 | + transportService.start(); |
| 63 | + transportService.acceptIncomingRequests(); |
| 64 | + |
| 65 | + indexShard = newStartedShard(true); |
| 66 | + |
| 67 | + peerReplicationSource = new PeerReplicationSource( |
| 68 | + transportService, |
| 69 | + recoverySettings, |
| 70 | + sourceNode, |
| 71 | + localNode, |
| 72 | + indexShard.routingEntry().allocationId().toString() |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void tearDown() throws Exception { |
| 78 | + IOUtils.close(transportService, clusterService, transport); |
| 79 | + closeShards(indexShard); |
| 80 | + super.tearDown(); |
| 81 | + } |
| 82 | + |
| 83 | + public void testGetCheckpointMetadata_invokesTransportService() { |
| 84 | + final ReplicationCheckpoint checkpoint = new ReplicationCheckpoint(indexShard.shardId(), 0L, 0L, 0L, 0L); |
| 85 | + final long replicationId = 1L; |
| 86 | + peerReplicationSource.getCheckpointMetadata(replicationId, checkpoint, mock(ActionListener.class)); |
| 87 | + CapturingTransport.CapturedRequest[] capturedRequests1 = transport.getCapturedRequestsAndClear(); |
| 88 | + assertThat(capturedRequests1.length, equalTo(1)); |
| 89 | + CapturingTransport.CapturedRequest request = capturedRequests1[0]; |
| 90 | + assertEquals(sourceNode, request.node); |
| 91 | + assertEquals(SegmentReplicationSourceService.Actions.GET_CHECKPOINT_INFO, request.action); |
| 92 | + } |
| 93 | + |
| 94 | + public void testGetFiles_invokesTransportService() { |
| 95 | + final ReplicationCheckpoint checkpoint = new ReplicationCheckpoint(indexShard.shardId(), 0L, 0L, 0L, 0L); |
| 96 | + final long replicationId = 1L; |
| 97 | + peerReplicationSource.getSegmentFiles( |
| 98 | + replicationId, |
| 99 | + checkpoint, |
| 100 | + Collections.emptyList(), |
| 101 | + mock(Store.class), |
| 102 | + mock(ActionListener.class) |
| 103 | + ); |
| 104 | + CapturingTransport.CapturedRequest[] capturedRequests1 = transport.getCapturedRequestsAndClear(); |
| 105 | + assertThat(capturedRequests1.length, equalTo(1)); |
| 106 | + CapturingTransport.CapturedRequest request = capturedRequests1[0]; |
| 107 | + assertEquals(sourceNode, request.node); |
| 108 | + assertEquals(SegmentReplicationSourceService.Actions.GET_SEGMENT_FILES, request.action); |
| 109 | + } |
| 110 | + |
| 111 | + private DiscoveryNode newDiscoveryNode(String nodeName) { |
| 112 | + return new DiscoveryNode( |
| 113 | + nodeName, |
| 114 | + randomAlphaOfLength(10), |
| 115 | + buildNewFakeTransportAddress(), |
| 116 | + emptyMap(), |
| 117 | + Collections.singleton(DiscoveryNodeRole.CLUSTER_MANAGER_ROLE), |
| 118 | + Version.CURRENT |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments