rich7420 commented on code in PR #11843:
URL: https://github.com/apache/ozone/pull/11843#discussion_r4704699097


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/SCMClientProtocolServer.java:
##########
@@ -1772,6 +1774,66 @@ public List<Long> suppressContainers(List<Long> 
containerIds, boolean suppress)
     return failedContainerIDs;
   }
 
+  @Override
+  public String submitContainerIdExport(ContainerID startContainerId, 
HddsProtos.LifeCycleState lifeCycleState,
+      ContainerHealthState healthState, long maxRows, int batchSize, int 
partSize, String requestId)
+      throws IOException {
+    getScm().checkAdminAccess(getRemoteUser(), false);
+    final Map<String, String> auditMap = Maps.newHashMap();
+    auditMap.put("startContainerID", String.valueOf(startContainerId));
+    auditMap.put("lifeCycleState", String.valueOf(lifeCycleState));
+    auditMap.put("healthState", String.valueOf(healthState));
+    auditMap.put("maxRows", String.valueOf(maxRows));
+    auditMap.put("batchSize", String.valueOf(batchSize));
+    auditMap.put("partSize", String.valueOf(partSize));
+    auditMap.put("requestId", String.valueOf(requestId));
+    try {
+      final String jobId = scm.getContainerExportManager().submitJob(

Review Comment:
   Rare, but could `containerExportManager.start()` run before the client RPC 
server starts? A submit in between lost its job directory to startup cleanup 
and failed with `No part files found`.



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/export/ExportLimits.java:
##########
@@ -39,6 +39,8 @@ public final class ExportLimits {
   public static final int MAX_BATCH_SIZE = 1_000_000;
   /** Maximum allowed {@code partSize} override for a single export job. */
   public static final int MAX_PART_SIZE = 5_000_000;
+  /** Maximum number of part files a single export job may write, which bounds 
its temporary files on the SCM disk. */
+  public static final int MAX_PARTS = 10_000;

Review Comment:
   Part names use `part%03d` and the archive sorts by name, so 1,001 parts gave 
me `part100, part1000, part1001, part101`. Could the cap be 999, or the pad 
wider?



##########
hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/server/TestSCMClientProtocolServer.java:
##########
@@ -252,6 +265,164 @@ public void 
testGetExistingContainersInBatchChecksAdminAccess() throws Exception
     }
   }
 
+  @Test
+  public void testSubmitContainerIdExportChecksWriteAccessAndDelegates() 
throws Exception {
+    final ContainerExportManager exportManager = 
mock(ContainerExportManager.class);
+    final ExportJob job = mock(ExportJob.class);
+    when(job.getId()).thenReturn(ExportJob.Id.of("job-1"));
+    when(exportManager.submitJob(any(), any(), any(), anyLong(), anyInt(), 
anyInt(), any())).thenReturn(job);
+    final StorageContainerManager mockScm = 
scmWithExportManager(exportManager);
+    final SCMClientProtocolServer scmServer = new SCMClientProtocolServer(new 
OzoneConfiguration(),
+        mockScm, mock(ReconfigurationHandler.class));
+    try {
+      assertEquals("job-1", scmServer.submitContainerIdExport(
+          ContainerID.valueOf(5), LifeCycleState.OPEN, 
ContainerHealthState.MISSING, 10, 20, 30, "req-1"));
+      // Submitting starts a job, so it needs write access (readOnly=false).
+      verify(mockScm).checkAdminAccess(any(), eq(false));
+      verify(exportManager).submitJob(
+          ContainerID.valueOf(5), LifeCycleState.OPEN, 
ContainerHealthState.MISSING, 10, 20, 30, "req-1");
+    } finally {
+      scmServer.stop();
+    }
+  }
+
+  @Test
+  public void testSubmitContainerIdExportDeniedDoesNotStartAJob() throws 
Exception {
+    final ContainerExportManager exportManager = 
mock(ContainerExportManager.class);
+    final StorageContainerManager mockScm = 
scmWithExportManager(exportManager);
+    doThrow(new AccessControlException("Access 
denied")).when(mockScm).checkAdminAccess(any(), eq(false));
+    final SCMClientProtocolServer scmServer = new SCMClientProtocolServer(new 
OzoneConfiguration(),
+        mockScm, mock(ReconfigurationHandler.class));
+    try {
+      assertThrows(AccessControlException.class, () -> 
scmServer.submitContainerIdExport(
+          null, LifeCycleState.OPEN, null, 0, 0, 0, null));
+      verifyNoInteractions(exportManager);
+    } finally {
+      scmServer.stop();
+    }
+  }
+
+  @Test
+  public void testSubmitContainerIdExportRejectsBadSizingWithoutRetry() throws 
Exception {
+    final ContainerExportManager exportManager = 
mock(ContainerExportManager.class);
+    when(exportManager.submitJob(any(), any(), any(), anyLong(), anyInt(), 
anyInt(), any()))
+        .thenThrow(new IllegalArgumentException("batchSize must be between 0 
and 1000000: -1"));
+    final SCMClientProtocolServer scmServer = new SCMClientProtocolServer(new 
OzoneConfiguration(),
+        scmWithExportManager(exportManager), 
mock(ReconfigurationHandler.class));
+    try {
+      assertThatThrownBy(() -> scmServer.submitContainerIdExport(null, 
LifeCycleState.OPEN, null, 0, -1, 0, null))
+          .isInstanceOf(NonRetriableException.class)
+          .hasMessageContaining("batchSize");
+    } finally {
+      scmServer.stop();
+    }
+  }
+
+  @Test
+  public void 
testSubmitContainerIdExportRaisesRetryableNotLeaderWhenLeadershipIsLost() 
throws Exception {
+    final ContainerExportManager exportManager = 
mock(ContainerExportManager.class);
+    when(exportManager.submitJob(any(), any(), any(), anyLong(), anyInt(), 
anyInt(), any()))
+        .thenThrow(new SCMException("not leader", 
SCMException.ResultCodes.SCM_NOT_LEADER));
+    final StorageContainerManager mockScm = 
scmWithExportManager(exportManager);
+    final NotLeaderException notLeader = new NotLeaderException(
+        RaftGroupMemberId.valueOf(RaftPeerId.valueOf("peer"), 
RaftGroupId.randomId()), null, new ArrayList<>());
+    final SCMHAManager haManager = mock(SCMHAManager.class);
+    final SCMRatisServer ratisServer = mock(SCMRatisServer.class);
+    when(mockScm.getScmHAManager()).thenReturn(haManager);
+    when(haManager.getRatisServer()).thenReturn(ratisServer);
+    when(ratisServer.triggerNotLeaderException()).thenReturn(notLeader);
+    final SCMClientProtocolServer scmServer = new SCMClientProtocolServer(new 
OzoneConfiguration(),
+        mockScm, mock(ReconfigurationHandler.class));
+    try {
+      // A plain SCMException is never retried by the client; the Ratis error 
makes it fail over and resubmit.
+      assertThatThrownBy(() -> scmServer.submitContainerIdExport(null, 
LifeCycleState.OPEN, null, 0, 0, 0, "req"))
+          .isSameAs(notLeader);
+    } finally {
+      scmServer.stop();
+    }
+  }
+
+  @Test
+  public void testGetContainerIdExportStatusNamesTheScmThatHoldsTheArchive() 
throws Exception {
+    final ContainerExportManager exportManager = 
mock(ContainerExportManager.class);
+    when(exportManager.getContainerExportStatus("job-1")).thenReturn(
+        GetContainerIdExportStatusResponseProto.newBuilder().setJobId("job-1")
+            
.setState(GetContainerIdExportStatusResponseProto.ExecutionState.SUCCEEDED)
+            .setTarPath("/data/exports/job-1.tar.gz").build());
+    final StorageContainerManager mockScm = 
scmWithExportManager(exportManager);
+    when(mockScm.getHostname()).thenReturn("scm1.example.com");
+    final SCMClientProtocolServer scmServer = new SCMClientProtocolServer(new 
OzoneConfiguration(),
+        mockScm, mock(ReconfigurationHandler.class));
+    try {
+      final GetContainerIdExportStatusResponseProto status = 
scmServer.getContainerIdExportStatus("job-1");
+      assertEquals("scm1.example.com", status.getScmHostname());
+      assertEquals("/data/exports/job-1.tar.gz", status.getTarPath());
+    } finally {
+      scmServer.stop();
+    }
+  }
+
+  @Test
+  public void testGetContainerIdExportStatusOfUnknownJobExplainsLeaderChange() 
throws Exception {

Review Comment:
   This looks the same as 
`testGetContainerIdExportStatusOfUnknownJobIsNotRetried` plus 
`hasMessageContaining("leader")`. Could that one assertion move there instead?



##########
hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/export/TestContainerExportManager.java:
##########
@@ -475,6 +480,223 @@ public void 
testCompletedArchivesTrimmedAndOthersKeptOnRestart() throws Exceptio
     assertEquals(1, Stream.of(first, second).filter(job -> 
archiveFile(job).exists()).count());
   }
 
+  @Test
+  public void testContainerExportStatusOfSucceededJob() throws Exception {
+    final ExportJob job = exportOneContainer(ContainerHealthState.MISSING, 1);
+
+    final GetContainerIdExportStatusResponseProto status = 
statusOf(job.getId());
+    assertEquals(ExecutionState.SUCCEEDED, status.getState());
+    assertTrue(isDone(status));
+    assertEquals(job.getId().toString(), status.getJobId());
+    assertEquals(ContainerHealthState.MISSING.name(), status.getHealthState());
+    assertFalse(status.hasLifecycleState());
+    assertEquals(1, status.getIdsWritten());
+    // The sizing the job runs with, defaults applied.
+    assertEquals(0, status.getMaxRows());
+    assertEquals(TEST_BATCH_SIZE, status.getBatchSize());
+    assertEquals(TEST_PART_SIZE, status.getPartSize());
+    assertEquals(archiveFile(job).getAbsolutePath(), status.getTarPath());
+    assertFalse(status.hasErrorMessage());
+    // Elapsed time stops once the job is done.
+    final long elapsed = status.getElapsedMs();
+    Thread.sleep(50);
+    assertEquals(elapsed, statusOf(job.getId()).getElapsedMs());
+  }
+
+  @Test
+  public void testContainerExportStatusOfJobWithoutMatchesHasNoTarPath() 
throws Exception {
+    when(containerManager.getContainerIDs(
+        eq(ContainerID.valueOf(0)), anyInt(), eq(LifeCycleState.OPEN), 
isNull()))
+        .thenReturn(Collections.emptyList());
+    final ExportJob job = submit(ContainerID.valueOf(0), LifeCycleState.OPEN, 
null);
+    waitForDone(job.getId());
+
+    final GetContainerIdExportStatusResponseProto status = 
statusOf(job.getId());
+    assertEquals(ExecutionState.SUCCEEDED, status.getState());
+    assertEquals(LifeCycleState.OPEN, status.getLifecycleState());
+    assertFalse(status.hasHealthState());
+    assertEquals(0, status.getIdsWritten());
+    assertFalse(status.hasTarPath());
+  }
+
+  @Test
+  public void testContainerExportStatusOfFailedJob() throws Exception {
+    when(containerManager.getContainerIDs(
+        eq(ContainerID.valueOf(0)), anyInt(), isNull(), 
eq(ContainerHealthState.MISSING)))
+        .thenThrow(new RuntimeException("container listing failed"));
+    final ExportJob job = submit(ContainerID.valueOf(0), null, 
ContainerHealthState.MISSING);
+    waitForDone(job.getId());
+
+    final GetContainerIdExportStatusResponseProto status = 
statusOf(job.getId());
+    assertEquals(ExecutionState.FAILED, status.getState());
+    assertTrue(isDone(status));
+    assertTrue(status.getErrorMessage().contains("container listing failed"));
+    assertEquals(0, status.getIdsWritten());
+    assertFalse(status.hasTarPath());
+  }
+
+  @Test
+  public void 
testContainerExportStatusOfFailureWithoutMessageNamesTheException() throws 
Exception {
+    when(containerManager.getContainerIDs(
+        eq(ContainerID.valueOf(0)), anyInt(), isNull(), 
eq(ContainerHealthState.MISSING)))
+        .thenThrow(new IllegalStateException());
+    final ExportJob job = submit(ContainerID.valueOf(0), null, 
ContainerHealthState.MISSING);
+    waitForDone(job.getId());
+
+    final GetContainerIdExportStatusResponseProto status = 
statusOf(job.getId());
+    assertEquals(ExecutionState.FAILED, status.getState());
+    assertTrue(status.getErrorMessage().contains("IllegalStateException"), 
status.getErrorMessage());
+  }
+
+  @Test
+  public void testRepeatedSubmitWithSameRequestIdReturnsTheSameJob() throws 
Exception {
+    when(containerManager.getContainerIDs(
+        eq(ContainerID.valueOf(0)), anyInt(), isNull(), 
eq(ContainerHealthState.MISSING)))
+        .thenReturn(Collections.emptyList());
+
+    final ExportJob first = exportManager.submitJob(
+        ContainerID.valueOf(0), null, ContainerHealthState.MISSING, 0, 0, 0, 
"req-1");
+    waitForDone(first.getId());
+    // The client lost the reply and sends the same request again, after the 
first export has finished.
+    final ExportJob again = exportManager.submitJob(
+        ContainerID.valueOf(0), null, ContainerHealthState.MISSING, 0, 0, 0, 
"req-1");
+    assertEquals(first.getId(), again.getId());
+
+    // A different request id, or none, is a new export.
+    final ExportJob other = exportManager.submitJob(
+        ContainerID.valueOf(0), null, ContainerHealthState.MISSING, 0, 0, 0, 
"req-2");
+    assertNotEquals(first.getId(), other.getId());
+    waitForDone(other.getId());
+    assertNotEquals(other.getId(), submit(ContainerID.valueOf(0), null, 
ContainerHealthState.MISSING).getId());
+  }
+
+  @Test
+  public void testRepeatedSubmitWhileRunningReturnsTheRunningJobNotAnError() 
throws Exception {
+    final CountDownLatch listing = new CountDownLatch(1);
+    final CountDownLatch release = new CountDownLatch(1);
+    when(containerManager.getContainerIDs(
+        eq(ContainerID.valueOf(0)), anyInt(), isNull(), 
eq(ContainerHealthState.MISSING)))
+        .thenAnswer(invocation -> {
+          listing.countDown();
+          release.await();
+          return Collections.emptyList();
+        });
+    final ExportJob first = exportManager.submitJob(
+        ContainerID.valueOf(0), null, ContainerHealthState.MISSING, 0, 0, 0, 
"req-1");
+    assertTrue(listing.await(30, TimeUnit.SECONDS));
+
+    assertEquals(first.getId(), exportManager.submitJob(
+        ContainerID.valueOf(0), null, ContainerHealthState.MISSING, 0, 0, 0, 
"req-1").getId());
+    release.countDown();
+    waitForDone(first.getId());
+  }
+
+  @Test
+  public void testRequestIdIsForgottenWhenItsJobIsEvicted() throws Exception {
+    when(containerManager.getContainerIDs(
+        eq(ContainerID.valueOf(0)), anyInt(), isNull(), 
eq(ContainerHealthState.MISSING)))
+        .thenReturn(Collections.emptyList());
+    final ExportJob first = exportManager.submitJob(
+        ContainerID.valueOf(0), null, ContainerHealthState.MISSING, 0, 0, 0, 
"req-1");
+    waitForDone(first.getId());
+    // TEST_MAX_RETAINED is 2, so two more finished jobs push the first one 
out.
+    waitForDone(submit(ContainerID.valueOf(0), null, 
ContainerHealthState.MISSING).getId());
+    waitForDone(submit(ContainerID.valueOf(0), null, 
ContainerHealthState.MISSING).getId());
+    assertNull(statusOf(first.getId()));
+
+    final ExportJob again = exportManager.submitJob(
+        ContainerID.valueOf(0), null, ContainerHealthState.MISSING, 0, 0, 0, 
"req-1");
+    assertNotEquals(first.getId(), again.getId());
+    waitForDone(again.getId());
+  }
+
+  @Test
+  public void testExportFailsWhenItWouldWriteTooManyParts() throws Exception {
+    // partSize 1 and no maxRows: more matching containers than the 3 part 
files this manager allows.
+    final int maxParts = 3;

Review Comment:
   Could this also cover exactly `maxParts` parts succeeding? With `>=` instead 
of `>` in the cap check, this test still passes.



##########
hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/export/TestContainerExportManager.java:
##########
@@ -475,6 +480,223 @@ public void 
testCompletedArchivesTrimmedAndOthersKeptOnRestart() throws Exceptio
     assertEquals(1, Stream.of(first, second).filter(job -> 
archiveFile(job).exists()).count());
   }
 
+  @Test
+  public void testContainerExportStatusOfSucceededJob() throws Exception {
+    final ExportJob job = exportOneContainer(ContainerHealthState.MISSING, 1);
+
+    final GetContainerIdExportStatusResponseProto status = 
statusOf(job.getId());
+    assertEquals(ExecutionState.SUCCEEDED, status.getState());
+    assertTrue(isDone(status));
+    assertEquals(job.getId().toString(), status.getJobId());
+    assertEquals(ContainerHealthState.MISSING.name(), status.getHealthState());
+    assertFalse(status.hasLifecycleState());
+    assertEquals(1, status.getIdsWritten());
+    // The sizing the job runs with, defaults applied.
+    assertEquals(0, status.getMaxRows());
+    assertEquals(TEST_BATCH_SIZE, status.getBatchSize());
+    assertEquals(TEST_PART_SIZE, status.getPartSize());
+    assertEquals(archiveFile(job).getAbsolutePath(), status.getTarPath());
+    assertFalse(status.hasErrorMessage());
+    // Elapsed time stops once the job is done.
+    final long elapsed = status.getElapsedMs();
+    Thread.sleep(50);
+    assertEquals(elapsed, statusOf(job.getId()).getElapsedMs());
+  }
+
+  @Test
+  public void testContainerExportStatusOfJobWithoutMatchesHasNoTarPath() 
throws Exception {
+    when(containerManager.getContainerIDs(
+        eq(ContainerID.valueOf(0)), anyInt(), eq(LifeCycleState.OPEN), 
isNull()))
+        .thenReturn(Collections.emptyList());
+    final ExportJob job = submit(ContainerID.valueOf(0), LifeCycleState.OPEN, 
null);
+    waitForDone(job.getId());
+
+    final GetContainerIdExportStatusResponseProto status = 
statusOf(job.getId());
+    assertEquals(ExecutionState.SUCCEEDED, status.getState());
+    assertEquals(LifeCycleState.OPEN, status.getLifecycleState());
+    assertFalse(status.hasHealthState());
+    assertEquals(0, status.getIdsWritten());
+    assertFalse(status.hasTarPath());
+  }
+
+  @Test
+  public void testContainerExportStatusOfFailedJob() throws Exception {
+    when(containerManager.getContainerIDs(
+        eq(ContainerID.valueOf(0)), anyInt(), isNull(), 
eq(ContainerHealthState.MISSING)))
+        .thenThrow(new RuntimeException("container listing failed"));
+    final ExportJob job = submit(ContainerID.valueOf(0), null, 
ContainerHealthState.MISSING);
+    waitForDone(job.getId());
+
+    final GetContainerIdExportStatusResponseProto status = 
statusOf(job.getId());
+    assertEquals(ExecutionState.FAILED, status.getState());
+    assertTrue(isDone(status));
+    assertTrue(status.getErrorMessage().contains("container listing failed"));
+    assertEquals(0, status.getIdsWritten());
+    assertFalse(status.hasTarPath());
+  }
+
+  @Test
+  public void 
testContainerExportStatusOfFailureWithoutMessageNamesTheException() throws 
Exception {
+    when(containerManager.getContainerIDs(
+        eq(ContainerID.valueOf(0)), anyInt(), isNull(), 
eq(ContainerHealthState.MISSING)))
+        .thenThrow(new IllegalStateException());
+    final ExportJob job = submit(ContainerID.valueOf(0), null, 
ContainerHealthState.MISSING);
+    waitForDone(job.getId());
+
+    final GetContainerIdExportStatusResponseProto status = 
statusOf(job.getId());
+    assertEquals(ExecutionState.FAILED, status.getState());
+    assertTrue(status.getErrorMessage().contains("IllegalStateException"), 
status.getErrorMessage());
+  }
+
+  @Test
+  public void testRepeatedSubmitWithSameRequestIdReturnsTheSameJob() throws 
Exception {
+    when(containerManager.getContainerIDs(
+        eq(ContainerID.valueOf(0)), anyInt(), isNull(), 
eq(ContainerHealthState.MISSING)))
+        .thenReturn(Collections.emptyList());
+
+    final ExportJob first = exportManager.submitJob(
+        ContainerID.valueOf(0), null, ContainerHealthState.MISSING, 0, 0, 0, 
"req-1");
+    waitForDone(first.getId());
+    // The client lost the reply and sends the same request again, after the 
first export has finished.
+    final ExportJob again = exportManager.submitJob(
+        ContainerID.valueOf(0), null, ContainerHealthState.MISSING, 0, 0, 0, 
"req-1");
+    assertEquals(first.getId(), again.getId());
+
+    // A different request id, or none, is a new export.
+    final ExportJob other = exportManager.submitJob(
+        ContainerID.valueOf(0), null, ContainerHealthState.MISSING, 0, 0, 0, 
"req-2");
+    assertNotEquals(first.getId(), other.getId());
+    waitForDone(other.getId());
+    assertNotEquals(other.getId(), submit(ContainerID.valueOf(0), null, 
ContainerHealthState.MISSING).getId());

Review Comment:
   An unset request ID reaches the manager as `""`, not `null`. Could this 
submit `""` twice, waiting in between, and expect two jobs? Dropping `|| 
requestId.isEmpty()` didn't fail any existing test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to