|
| 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.common; |
| 10 | + |
| 11 | +import org.apache.lucene.index.IndexCommit; |
| 12 | +import org.apache.lucene.index.IndexFileNames; |
| 13 | +import org.apache.lucene.index.SegmentInfos; |
| 14 | +import org.apache.lucene.util.Version; |
| 15 | +import org.opensearch.common.collect.Map; |
| 16 | +import org.opensearch.common.concurrent.GatedCloseable; |
| 17 | +import org.opensearch.index.shard.IndexShard; |
| 18 | +import org.opensearch.index.shard.IndexShardTestCase; |
| 19 | +import org.opensearch.index.shard.ShardId; |
| 20 | +import org.opensearch.index.store.Store; |
| 21 | +import org.opensearch.index.store.StoreFileMetadata; |
| 22 | +import org.opensearch.indices.replication.checkpoint.ReplicationCheckpoint; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | + |
| 30 | +public class CopyStateTests extends IndexShardTestCase { |
| 31 | + |
| 32 | + public void testCopyStateCreation() throws IOException { |
| 33 | + // dummy objects setup |
| 34 | + final long expectedLongValue = 1L; |
| 35 | + final ShardId testShardId = new ShardId("testIndex", "testUUID", 0); |
| 36 | + final StoreFileMetadata segmentsFile = new StoreFileMetadata(IndexFileNames.SEGMENTS, 1L, "0", Version.LATEST); |
| 37 | + final StoreFileMetadata pendingDeleteFile = new StoreFileMetadata("pendingDelete.del", 1L, "1", Version.LATEST); |
| 38 | + final Store.MetadataSnapshot commitMetadataSnapshot = new Store.MetadataSnapshot( |
| 39 | + Map.of("segmentsFile", segmentsFile, "pendingDeleteFile", pendingDeleteFile), |
| 40 | + null, |
| 41 | + 0 |
| 42 | + ); |
| 43 | + final Store.MetadataSnapshot segmentInfosMetadataSnapshot = new Store.MetadataSnapshot( |
| 44 | + Map.of("segmentsFile", segmentsFile), |
| 45 | + null, |
| 46 | + 0 |
| 47 | + ); |
| 48 | + |
| 49 | + // Mock objects setup |
| 50 | + IndexShard mockShard = mock(IndexShard.class); |
| 51 | + when(mockShard.shardId()).thenReturn(testShardId); |
| 52 | + when(mockShard.getOperationPrimaryTerm()).thenReturn(expectedLongValue); |
| 53 | + when(mockShard.getProcessedLocalCheckpoint()).thenReturn(expectedLongValue); |
| 54 | + |
| 55 | + Store mockStore = mock(Store.class); |
| 56 | + when(mockShard.store()).thenReturn(mockStore); |
| 57 | + |
| 58 | + SegmentInfos testSegmentInfos = new SegmentInfos(Version.LATEST.major); |
| 59 | + when(mockShard.getSegmentInfosSnapshot()).thenReturn(new GatedCloseable<>(testSegmentInfos, () -> {})); |
| 60 | + |
| 61 | + when(mockStore.getMetadata(testSegmentInfos)).thenReturn(segmentInfosMetadataSnapshot); |
| 62 | + |
| 63 | + IndexCommit mockIndexCommit = mock(IndexCommit.class); |
| 64 | + when(mockShard.acquireLastIndexCommit(false)).thenReturn(new GatedCloseable<>(mockIndexCommit, () -> {})); |
| 65 | + when(mockStore.getMetadata(mockIndexCommit)).thenReturn(commitMetadataSnapshot); |
| 66 | + |
| 67 | + // unit test |
| 68 | + CopyState copyState = new CopyState(mockShard); |
| 69 | + ReplicationCheckpoint checkpoint = copyState.getCheckpoint(); |
| 70 | + assertEquals(testShardId, checkpoint.getShardId()); |
| 71 | + // version was never set so this should be zero |
| 72 | + assertEquals(0, checkpoint.getSegmentInfosVersion()); |
| 73 | + assertEquals(expectedLongValue, checkpoint.getPrimaryTerm()); |
| 74 | + |
| 75 | + Set<StoreFileMetadata> pendingDeleteFiles = copyState.getPendingDeleteFiles(); |
| 76 | + assertEquals(1, pendingDeleteFiles.size()); |
| 77 | + assertTrue(pendingDeleteFiles.contains(pendingDeleteFile)); |
| 78 | + } |
| 79 | +} |
0 commit comments