|
12 | 12 | import org.apache.lucene.search.Sort;
|
13 | 13 | import org.apache.lucene.store.Directory;
|
14 | 14 | import org.apache.lucene.store.IOContext;
|
| 15 | +import org.apache.lucene.store.IndexInput; |
| 16 | +import org.apache.lucene.store.IndexOutput; |
15 | 17 | import org.apache.lucene.util.InfoStream;
|
16 | 18 | import org.apache.lucene.util.Version;
|
17 | 19 | import org.junit.Before;
|
|
42 | 44 | import org.opensearch.repositories.RepositoryMissingException;
|
43 | 45 | import org.opensearch.repositories.blobstore.BlobStoreRepository;
|
44 | 46 |
|
| 47 | +import java.io.ByteArrayInputStream; |
45 | 48 | import java.io.IOException;
|
46 | 49 | import java.io.InputStream;
|
47 | 50 | import java.nio.file.Path;
|
48 | 51 | import java.util.List;
|
49 | 52 | import java.util.Map;
|
| 53 | +import java.util.Random; |
50 | 54 |
|
51 | 55 | import static org.mockito.ArgumentMatchers.any;
|
52 | 56 | import static org.mockito.Mockito.mock;
|
@@ -208,4 +212,56 @@ public void testRepositoryInteraction() throws IOException {
|
208 | 212 | verify(mockBlobStore).blobContainer(any());
|
209 | 213 | verify(mockRepository).basePath();
|
210 | 214 | }
|
| 215 | + |
| 216 | + /** |
| 217 | + * Verify the buffered read method in {@link RemoteIndexBuildStrategy#readFromRepository} produces the correct result |
| 218 | + */ |
| 219 | + public void testRepositoryRead() throws IOException { |
| 220 | + // Create an InputStream with random values |
| 221 | + int TEST_ARRAY_SIZE = 64 * 1024 * 10; |
| 222 | + byte[] byteArray = new byte[TEST_ARRAY_SIZE]; |
| 223 | + Random random = new Random(); |
| 224 | + random.nextBytes(byteArray); |
| 225 | + InputStream randomStream = new ByteArrayInputStream(byteArray); |
| 226 | + |
| 227 | + // Create a test segment that we will read/write from |
| 228 | + Directory directory; |
| 229 | + directory = newFSDirectory(createTempDir()); |
| 230 | + String TEST_SEGMENT_NAME = "test-segment-name"; |
| 231 | + IndexOutput testIndexOutput = directory.createOutput(TEST_SEGMENT_NAME, IOContext.DEFAULT); |
| 232 | + IndexOutputWithBuffer testIndexOutputWithBuffer = new IndexOutputWithBuffer(testIndexOutput); |
| 233 | + |
| 234 | + // Set up RemoteIndexBuildStrategy and write to IndexOutput |
| 235 | + RepositoriesService repositoriesService = mock(RepositoriesService.class); |
| 236 | + BlobStoreRepository mockRepository = mock(BlobStoreRepository.class); |
| 237 | + BlobPath testBasePath = new BlobPath().add("testBasePath"); |
| 238 | + BlobStore mockBlobStore = mock(BlobStore.class); |
| 239 | + AsyncMultiStreamBlobContainer mockBlobContainer = mock(AsyncMultiStreamBlobContainer.class); |
| 240 | + |
| 241 | + when(repositoriesService.repository(any())).thenReturn(mockRepository); |
| 242 | + when(mockRepository.basePath()).thenReturn(testBasePath); |
| 243 | + when(mockRepository.blobStore()).thenReturn(mockBlobStore); |
| 244 | + when(mockBlobStore.blobContainer(any())).thenReturn(mockBlobContainer); |
| 245 | + when(mockBlobContainer.readBlob("testFile")).thenReturn(randomStream); |
| 246 | + |
| 247 | + RemoteIndexBuildStrategy objectUnderTest = new RemoteIndexBuildStrategy( |
| 248 | + () -> repositoriesService, |
| 249 | + mock(NativeIndexBuildStrategy.class), |
| 250 | + mock(IndexSettings.class) |
| 251 | + ); |
| 252 | + // This should read from randomStream into testIndexOutput |
| 253 | + BlobPath testPath = new BlobPath().add("testBasePath").add("testDirectory").add("testFile"); |
| 254 | + objectUnderTest.readFromRepository(testPath, testIndexOutputWithBuffer); |
| 255 | + testIndexOutput.close(); |
| 256 | + |
| 257 | + // Now try to read from the IndexOutput |
| 258 | + IndexInput testIndexInput = directory.openInput(TEST_SEGMENT_NAME, IOContext.DEFAULT); |
| 259 | + byte[] resultByteArray = new byte[TEST_ARRAY_SIZE]; |
| 260 | + testIndexInput.readBytes(resultByteArray, 0, TEST_ARRAY_SIZE); |
| 261 | + assertArrayEquals(byteArray, resultByteArray); |
| 262 | + |
| 263 | + // Test Cleanup |
| 264 | + testIndexInput.close(); |
| 265 | + directory.close(); |
| 266 | + } |
211 | 267 | }
|
0 commit comments