|
36 | 36 | import static org.junit.jupiter.api.Assertions.assertTrue; |
37 | 37 | import static org.junit.jupiter.api.Assertions.fail; |
38 | 38 | import static org.mockito.ArgumentMatchers.anyCollection; |
39 | | -import static org.mockito.ArgumentMatchers.anyInt; |
40 | | -import static org.mockito.ArgumentMatchers.anyMap; |
41 | 39 | import static org.mockito.ArgumentMatchers.anySet; |
| 40 | +import static org.mockito.ArgumentMatchers.anyString; |
42 | 41 | import static org.mockito.Mockito.any; |
43 | 42 | import static org.mockito.Mockito.anyBoolean; |
44 | 43 | import static org.mockito.Mockito.doAnswer; |
|
84 | 83 | import javax.servlet.WriteListener; |
85 | 84 | import javax.servlet.http.HttpServletRequest; |
86 | 85 | import javax.servlet.http.HttpServletResponse; |
87 | | -import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
88 | | -import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; |
89 | 86 | import org.apache.commons.lang3.RandomStringUtils; |
90 | 87 | import org.apache.hadoop.fs.FileUtil; |
91 | 88 | import org.apache.hadoop.hdds.client.ReplicationConfig; |
92 | 89 | import org.apache.hadoop.hdds.client.ReplicationFactor; |
93 | 90 | import org.apache.hadoop.hdds.client.ReplicationType; |
94 | 91 | import org.apache.hadoop.hdds.client.StandaloneReplicationConfig; |
95 | 92 | import org.apache.hadoop.hdds.conf.OzoneConfiguration; |
96 | | -import org.apache.hadoop.hdds.utils.Archiver; |
97 | 93 | import org.apache.hadoop.hdds.utils.IOUtils; |
98 | 94 | import org.apache.hadoop.hdds.utils.db.DBCheckpoint; |
99 | 95 | import org.apache.hadoop.hdds.utils.db.DBStore; |
@@ -239,19 +235,19 @@ public void write(int b) throws IOException { |
239 | 235 | doCallRealMethod().when(omDbCheckpointServletMock) |
240 | 236 | .writeDbDataToStream(any(), any(), any(), any(), any()); |
241 | 237 | doCallRealMethod().when(omDbCheckpointServletMock) |
242 | | - .writeDBToArchive(any(), any(), any(), any(), any(), any(), anyBoolean()); |
| 238 | + .collectFilesFromDir(any(), any(), any(), anyBoolean(), any()); |
| 239 | + doCallRealMethod().when(omDbCheckpointServletMock).collectDbDataToTransfer(any(), any(), any()); |
243 | 240 |
|
244 | 241 | when(omDbCheckpointServletMock.getBootstrapStateLock()) |
245 | 242 | .thenReturn(lock); |
246 | 243 | doCallRealMethod().when(omDbCheckpointServletMock).getCheckpoint(any(), anyBoolean()); |
247 | 244 | assertNull(doCallRealMethod().when(omDbCheckpointServletMock).getBootstrapTempData()); |
248 | 245 | doCallRealMethod().when(omDbCheckpointServletMock). |
249 | 246 | processMetadataSnapshotRequest(any(), any(), anyBoolean(), anyBoolean()); |
250 | | - doCallRealMethod().when(omDbCheckpointServletMock).writeDbDataToStream(any(), any(), any(), any()); |
251 | 247 | doCallRealMethod().when(omDbCheckpointServletMock).getCompactionLogDir(); |
252 | 248 | doCallRealMethod().when(omDbCheckpointServletMock).getSstBackupDir(); |
253 | 249 | doCallRealMethod().when(omDbCheckpointServletMock) |
254 | | - .transferSnapshotData(anySet(), any(), anyCollection(), anyCollection(), any(), any(), anyMap()); |
| 250 | + .collectSnapshotData(anySet(), anyCollection(), anyCollection(), any(), any()); |
255 | 251 | doCallRealMethod().when(omDbCheckpointServletMock).createAndPrepareCheckpoint(anyBoolean()); |
256 | 252 | doCallRealMethod().when(omDbCheckpointServletMock).getSnapshotDirsFromDB(any(), any(), any()); |
257 | 253 | } |
@@ -298,14 +294,14 @@ public void testWriteDBToArchiveClosesFilesListStream() throws Exception { |
298 | 294 | // Do not use CALLS_REAL_METHODS for java.nio.file.Files: internal/private static |
299 | 295 | // methods (eg Files.provider()) get intercepted too and Mockito will try to invoke |
300 | 296 | // them reflectively, which fails on JDK9+ without --add-opens. |
301 | | - try (MockedStatic<Files> files = mockStatic(Files.class); |
302 | | - TarArchiveOutputStream tar = new TarArchiveOutputStream(new java.io.ByteArrayOutputStream())) { |
| 297 | + try (MockedStatic<Files> files = mockStatic(Files.class)) { |
303 | 298 | files.when(() -> Files.exists(dbDir)).thenReturn(true); |
304 | 299 | files.when(() -> Files.list(dbDir)).thenReturn(stream); |
305 | 300 |
|
306 | | - boolean result = servlet.writeDBToArchive( |
307 | | - new HashSet<>(), dbDir, new AtomicLong(Long.MAX_VALUE), |
308 | | - tar, folder, null, true); |
| 301 | + OMDBArchiver archiver = new OMDBArchiver(); |
| 302 | + archiver.setTmpDir(folder); |
| 303 | + boolean result = servlet.collectFilesFromDir(new HashSet<>(), dbDir, |
| 304 | + new AtomicLong(Long.MAX_VALUE), true, archiver); |
309 | 305 | assertTrue(result); |
310 | 306 | } |
311 | 307 |
|
@@ -446,49 +442,38 @@ public void testWriteDBToArchive(boolean expectOnlySstFiles) throws Exception { |
446 | 442 | // Create dummy files: one SST, one non-SST |
447 | 443 | Path sstFile = dbDir.resolve("test.sst"); |
448 | 444 | Files.write(sstFile, "sst content".getBytes(StandardCharsets.UTF_8)); // Write some content to make it non-empty |
449 | | - |
450 | 445 | Path nonSstFile = dbDir.resolve("test.log"); |
451 | 446 | Files.write(nonSstFile, "log content".getBytes(StandardCharsets.UTF_8)); |
452 | 447 | Set<String> sstFilesToExclude = new HashSet<>(); |
453 | 448 | AtomicLong maxTotalSstSize = new AtomicLong(1000000); // Sufficient size |
454 | | - Map<String, String> hardLinkFileMap = new java.util.HashMap<>(); |
| 449 | + OMDBArchiver omDbArchiver = new OMDBArchiver(); |
455 | 450 | Path tmpDir = folder.resolve("tmp"); |
456 | 451 | Files.createDirectories(tmpDir); |
457 | | - TarArchiveOutputStream mockArchiveOutputStream = mock(TarArchiveOutputStream.class); |
| 452 | + omDbArchiver.setTmpDir(tmpDir); |
| 453 | + OMDBArchiver omDbArchiverSpy = spy(omDbArchiver); |
458 | 454 | List<String> fileNames = new ArrayList<>(); |
459 | | - try (MockedStatic<Archiver> archiverMock = mockStatic(Archiver.class)) { |
460 | | - archiverMock.when(() -> Archiver.linkAndIncludeFile(any(), any(), any(), any())).thenAnswer(invocation -> { |
461 | | - // Get the actual mockArchiveOutputStream passed from writeDBToArchive |
462 | | - TarArchiveOutputStream aos = invocation.getArgument(2); |
463 | | - File sourceFile = invocation.getArgument(0); |
464 | | - String fileId = invocation.getArgument(1); |
465 | | - fileNames.add(sourceFile.getName()); |
466 | | - aos.putArchiveEntry(new TarArchiveEntry(sourceFile, fileId)); |
467 | | - aos.write(new byte[100], 0, 100); // Simulate writing |
468 | | - aos.closeArchiveEntry(); |
469 | | - return 100L; |
470 | | - }); |
471 | | - boolean success = omDbCheckpointServletMock.writeDBToArchive( |
472 | | - sstFilesToExclude, dbDir, maxTotalSstSize, mockArchiveOutputStream, |
473 | | - tmpDir, hardLinkFileMap, expectOnlySstFiles); |
474 | | - assertTrue(success); |
475 | | - verify(mockArchiveOutputStream, times(fileNames.size())).putArchiveEntry(any()); |
476 | | - verify(mockArchiveOutputStream, times(fileNames.size())).closeArchiveEntry(); |
477 | | - verify(mockArchiveOutputStream, times(fileNames.size())).write(any(byte[].class), anyInt(), |
478 | | - anyInt()); // verify write was called once |
479 | | - |
480 | | - boolean containsNonSstFile = false; |
481 | | - for (String fileName : fileNames) { |
482 | | - if (expectOnlySstFiles) { |
483 | | - assertTrue(fileName.endsWith(".sst"), "File is not an SST File"); |
484 | | - } else { |
485 | | - containsNonSstFile = true; |
486 | | - } |
| 455 | + doAnswer((invocation) -> { |
| 456 | + File sourceFile = invocation.getArgument(0); |
| 457 | + fileNames.add(sourceFile.getName()); |
| 458 | + omDbArchiver.recordFileEntry(sourceFile, invocation.getArgument(1)); |
| 459 | + return null; |
| 460 | + }).when(omDbArchiverSpy).recordFileEntry(any(), anyString()); |
| 461 | + boolean success = |
| 462 | + omDbCheckpointServletMock.collectFilesFromDir(sstFilesToExclude, dbDir, maxTotalSstSize, expectOnlySstFiles, |
| 463 | + omDbArchiverSpy); |
| 464 | + assertTrue(success); |
| 465 | + verify(omDbArchiverSpy, times(fileNames.size())).recordFileEntry(any(), anyString()); |
| 466 | + boolean containsNonSstFile = false; |
| 467 | + for (String fileName : fileNames) { |
| 468 | + if (expectOnlySstFiles) { |
| 469 | + assertTrue(fileName.endsWith(".sst"), "File is not an SST File"); |
| 470 | + } else { |
| 471 | + containsNonSstFile = true; |
487 | 472 | } |
| 473 | + } |
488 | 474 |
|
489 | | - if (!expectOnlySstFiles) { |
490 | | - assertTrue(containsNonSstFile, "SST File is not expected"); |
491 | | - } |
| 475 | + if (!expectOnlySstFiles) { |
| 476 | + assertTrue(containsNonSstFile, "SST File is not expected"); |
492 | 477 | } |
493 | 478 | } |
494 | 479 |
|
@@ -905,6 +890,7 @@ private void setupClusterAndMocks(String volumeName, String bucketName, |
905 | 890 | doCallRealMethod().when(omDbCheckpointServletMock).initialize(any(), any(), |
906 | 891 | eq(false), any(), any(), eq(false)); |
907 | 892 | doCallRealMethod().when(omDbCheckpointServletMock).getSnapshotDirsFromDB(any(), any(), any()); |
| 893 | + doCallRealMethod().when(omDbCheckpointServletMock).collectDbDataToTransfer(any(), any(), any()); |
908 | 894 | omDbCheckpointServletMock.initialize(spyDbStore, om.getMetrics().getDBCheckpointMetrics(), |
909 | 895 | false, |
910 | 896 | om.getOmAdminUsernames(), om.getOmAdminGroups(), false); |
|
0 commit comments