hemantk-12 commented on code in PR #4824:
URL: https://github.com/apache/ozone/pull/4824#discussion_r1224859061
##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotDiffManager.java:
##########
@@ -371,6 +378,45 @@ private Set<String> getSSTFileListForSnapshot(OmSnapshot
snapshot,
.getPath(), tablesToLookUp);
}
+ public List<SnapshotDiffJob> getSnapshotDiffJobList(
+ String volumeName, String bucketName,
+ String jobStatus, boolean listAll) {
+ List<SnapshotDiffJob> jobList = new ArrayList<>();
+
+ try (ClosableIterator<Map.Entry<String, SnapshotDiffJob>> iterator =
+ snapDiffJobTable.iterator()) {
+ while (iterator.hasNext()) {
+ SnapshotDiffJob snapshotDiffJob = iterator.next().getValue();
+ if (Objects.equals(snapshotDiffJob.getVolume(), volumeName) &&
+ Objects.equals(snapshotDiffJob.getBucket(), bucketName)) {
+ if (listAll) {
+ jobList.add(snapshotDiffJob);
+ continue;
+ }
+
+ // First check if the provided JobStatus is valid,
+ // then check for matches with the provided JobStatus.
+ if (validateStringJobStatusExists(jobStatus) &&
Review Comment:
You don't need to do `validateStringJobStatusExists`. Enum.valueOf() will
throw an exception in case of invalid status. The same exception should be
transferred to client.
May be something like:
```suggestion
if (Objects.equals(snapshotDiffJob.getStatus(),
getJobStatus(jobStatus))) {
jobList.add(snapshotDiffJob);
}
```
```
private JobStatus getJobStatus(String jobStatus)
throws IOException {
try {
return JobStatus.valueOf(jobStatus.toUpperCase());
} catch (IllegalArgumentException exp) {
throw new IOException("Invalid job status: " + jobStatus);
}
}
```
##########
hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotDiffManager.java:
##########
@@ -0,0 +1,333 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package org.apache.hadoop.ozone.om.snapshot;
+
+import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.hadoop.fs.FileUtil;
+import org.apache.hadoop.hdds.client.StandaloneReplicationConfig;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.OzoneConfigKeys;
+import org.apache.hadoop.ozone.om.OMMetadataManager;
+import org.apache.hadoop.ozone.om.OmSnapshot;
+import org.apache.hadoop.ozone.om.OmTestManagers;
+import org.apache.hadoop.ozone.om.OzoneManager;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.OmVolumeArgs;
+import org.apache.hadoop.ozone.om.helpers.SnapshotDiffJob;
+import org.apache.hadoop.ozone.om.helpers.SnapshotInfo;
+import org.apache.hadoop.ozone.snapshot.SnapshotDiffResponse;
+import org.apache.hadoop.ozone.snapshot.SnapshotDiffResponse.JobStatus;
+import org.apache.hadoop.security.UserGroupInformation;
+import
org.apache.hadoop.security.authentication.client.AuthenticationException;
+import org.apache.hadoop.util.ClosableIterator;
+import org.apache.ozone.test.GenericTestUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.rocksdb.RocksDBException;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.UUID;
+import java.util.concurrent.ThreadLocalRandom;
+
+import static
org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor.ONE;
+import static org.apache.hadoop.ozone.om.OmSnapshotManager.DELIMITER;
+
+/**
+ * Tests for {@link SnapshotDiffManager}.
+ */
+public class TestSnapshotDiffManager {
+
+ private static final String VOLUME = "vol";
+ private static final String BUCKET = "bucket";
+
+ private static File metaDir;
+ private static OzoneManager ozoneManager;
+ private static OMMetadataManager omMetadataManager;
+ private static SnapshotDiffManager snapshotDiffManager;
+ private static PersistentMap<String, SnapshotDiffJob> snapDiffJobTable;
+
+ @BeforeAll
+ public static void init() throws AuthenticationException,
+ IOException, RocksDBException {
+ metaDir = GenericTestUtils.getRandomizedTestDir();
+ if (!metaDir.exists()) {
+ Assertions.assertTrue(metaDir.mkdirs());
+ }
+ OzoneConfiguration conf = new OzoneConfiguration();
+ conf.set(OzoneConfigKeys.OZONE_METADATA_DIRS,
+ metaDir.getAbsolutePath());
+
+ OmTestManagers omTestManagers = new OmTestManagers(conf);
+ ozoneManager = omTestManagers.getOzoneManager();
+ omMetadataManager = omTestManagers.getMetadataManager();
+
+ snapshotDiffManager = ozoneManager
+ .getOmSnapshotManager().getSnapshotDiffManager();
+ snapDiffJobTable = snapshotDiffManager.getSnapDiffJobTable();
+
+ createVolumeAndBucket();
+ }
+
+ @AfterAll
+ public static void cleanUp() {
+ FileUtil.fullyDelete(metaDir);
+ }
+
+ /**
+ * Clear the SnapshotDiffTable before every test run.
+ */
+ @BeforeEach
+ public void setUp() {
+ ClosableIterator<Map.Entry<String, SnapshotDiffJob>>
+ iterator = snapDiffJobTable.iterator();
+ while (iterator.hasNext()) {
+ String key = iterator.next().getKey();
+ snapDiffJobTable.remove(key);
+ }
+ }
+
+ @ParameterizedTest
Review Comment:
nit:
```suggestion
private static Stream<Arguments> listSnapshotDiffJobsScenarios() {
return Stream.of(
Arguments.of("queued", false, false),
Arguments.of("done", false, false),
Arguments.of("in_progress", false, true),
Arguments.of("invalid", false, false),
Arguments.of("", false, false),
Arguments.of("queued", true, true),
Arguments.of("done", true, true),
Arguments.of("in_progress", true, true),
Arguments.of("invalid", true, true),
Arguments.of("", true, true)
);
}
@ParameterizedTest
@MethodSource("listSnapshotDiffJobsScenarios")
public void testListSnapshotDiffJobs(String jobStatus,
boolean listAll,
boolean containsJob) throws
IOException {
String fromSnapshotName = "snap-" + RandomStringUtils.randomNumeric(5);
String toSnapshotName = "snap-" + RandomStringUtils.randomNumeric(5);
String fromSnapshotId = UUID.randomUUID().toString();
String toSnapshotId = UUID.randomUUID().toString();
String diffJobKey = fromSnapshotId + DELIMITER + toSnapshotId;
setUpKeysAndSnapshots(fromSnapshotName, toSnapshotName,
fromSnapshotId, toSnapshotId);
SnapshotDiffJob diffJob = snapDiffJobTable.get(diffJobKey);
Assertions.assertNull(diffJob);
// There are no jobs in the table, therefore
// the response list should be empty.
List<SnapshotDiffJob> jobList = snapshotDiffManager
.getSnapshotDiffJobList(VOLUME, BUCKET, jobStatus, listAll);
Assertions.assertTrue(jobList.isEmpty());
SnapshotDiffResponse snapshotDiffResponse = snapshotDiffManager
.getSnapshotDiffReport(VOLUME, BUCKET,
fromSnapshotName, toSnapshotName,
0, 0, false);
Assertions.assertEquals(JobStatus.IN_PROGRESS,
snapshotDiffResponse.getJobStatus());
diffJob = snapDiffJobTable.get(diffJobKey);
Assertions.assertNotNull(diffJob);
Assertions.assertEquals(JobStatus.IN_PROGRESS,
diffJob.getStatus());
jobList = snapshotDiffManager
.getSnapshotDiffJobList(VOLUME, BUCKET, jobStatus, listAll);
if (containsJob) {
Assertions.assertTrue(jobList.contains(diffJob));
} else {
Assertions.assertTrue(jobList.isEmpty());
}
}
```
On a side note, there is no need to add straight forward comments like `//
Submit a job.` or `// Response should be IN_PROGRESS.`
##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmSnapshotManager.java:
##########
@@ -681,6 +686,37 @@ public SnapshotDiffResponse getSnapshotDiffReport(final
String volume,
return snapshotDiffReport;
}
+ public List<SnapshotDiffJob> getSnapshotDiffList(final String volumeName,
+ final String bucketName,
+ final String jobStatus,
+ final boolean listAll)
+ throws IOException {
+ String volumeKey = ozoneManager.getMetadataManager()
+ .getVolumeKey(volumeName);
+ String bucketKey = ozoneManager.getMetadataManager()
+ .getBucketKey(volumeName, bucketName);
+
+ if (ozoneManager.getMetadataManager()
Review Comment:
nit: use `isExist` instead of `getIfExist`.
```suggestion
if (!ozoneManager.getMetadataManager()
.getVolumeTable().isExist(volumeKey) ||
!ozoneManager.getMetadataManager()
.getBucketTable().isExist(bucketKey)) {
throw new IOException("Provided volume name " + volumeName +
" or bucket name " + bucketName + " doesn't exist");
}
```
##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOmSnapshot.java:
##########
@@ -767,6 +767,29 @@ public void testSnapDiffMultipleBuckets() throws Exception
{
Assert.assertEquals(1, diff1.getDiffList().size());
}
+ @Test
+ public void testListSnapshotDiffWithInvalidParameters()
+ throws Exception {
+ String volume = "vol-" + RandomStringUtils.randomNumeric(5);
+ String bucket = "buck-" + RandomStringUtils.randomNumeric(5);
+
+ String errorMessage = "Provided volume name " + volume +
+ " or bucket name " + bucket + " doesn't exist";
+ // List SnapshotDiff jobs for non existent volume and bucket.
+ LambdaTestUtils.intercept(OMException.class,
+ errorMessage,
+ () -> store.listSnapshotDiffJobs(volume, bucket, "", true));
+
+ // Create the volume and the bucket.
+ store.createVolume(volume);
+ OzoneVolume ozVolume = store.getVolume(volume);
+ ozVolume.createBucket(bucket);
+
+ // List SnapshotDiff jobs using a path that has no snapshots.
+ LambdaTestUtils.intercept(OMException.class,
Review Comment:
```suggestion
omException = assertThrows(OMException.class,
() -> store.listSnapshotDiffJobs(volume, bucket, "", true));
// List SnapshotDiff jobs using a path that has no snapshots.
assertEquals("There are no snapshots", omException.getMessage());
```
##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOmSnapshot.java:
##########
@@ -767,6 +767,29 @@ public void testSnapDiffMultipleBuckets() throws Exception
{
Assert.assertEquals(1, diff1.getDiffList().size());
}
+ @Test
+ public void testListSnapshotDiffWithInvalidParameters()
+ throws Exception {
+ String volume = "vol-" + RandomStringUtils.randomNumeric(5);
+ String bucket = "buck-" + RandomStringUtils.randomNumeric(5);
+
+ String errorMessage = "Provided volume name " + volume +
+ " or bucket name " + bucket + " doesn't exist";
+ // List SnapshotDiff jobs for non existent volume and bucket.
+ LambdaTestUtils.intercept(OMException.class,
Review Comment:
nit: Use `Assert.assertThrows()` directly.
```suggestion
OMException omException = assertThrows(OMException.class,
() -> store.listSnapshotDiffJobs(volume, bucket, "", true));
assertEquals(errorMessage, omException.getMessage());
```
--
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]