hemantk-12 commented on code in PR #4819:
URL: https://github.com/apache/ozone/pull/4819#discussion_r1218438431
##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOmSnapshot.java:
##########
@@ -627,6 +628,47 @@ public void testSnapDiff() throws Exception {
}
+ @Test
+ public void testSnapDiffCancel() throws Exception {
+ // Create key1 and take snapshot.
+ String key1 = "key-1-" + RandomStringUtils.randomNumeric(5);
+ createFileKey(ozoneBucket, key1);
+ String fromSnapName = "snap-1-" + RandomStringUtils.randomNumeric(5);
+ createSnapshot(volumeName, bucketName, fromSnapName);
+
+ // Create key2 and take snapshot.
+ String key2 = "key-2-" + RandomStringUtils.randomNumeric(5);
+ createFileKey(ozoneBucket, key2);
+ String toSnapName = "snap-2-" + RandomStringUtils.randomNumeric(5);
+ createSnapshot(volumeName, bucketName, toSnapName);
+
+ // Cancel works only if the job is IN_PROGRESS, and
+ // it's ignored if the job has any other status.
Review Comment:
This comment is not needed.
##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOmSnapshot.java:
##########
@@ -627,6 +628,47 @@ public void testSnapDiff() throws Exception {
}
+ @Test
+ public void testSnapDiffCancel() throws Exception {
+ // Create key1 and take snapshot.
+ String key1 = "key-1-" + RandomStringUtils.randomNumeric(5);
+ createFileKey(ozoneBucket, key1);
+ String fromSnapName = "snap-1-" + RandomStringUtils.randomNumeric(5);
+ createSnapshot(volumeName, bucketName, fromSnapName);
+
+ // Create key2 and take snapshot.
+ String key2 = "key-2-" + RandomStringUtils.randomNumeric(5);
+ createFileKey(ozoneBucket, key2);
+ String toSnapName = "snap-2-" + RandomStringUtils.randomNumeric(5);
+ createSnapshot(volumeName, bucketName, toSnapName);
+
+ // Cancel works only if the job is IN_PROGRESS, and
+ // it's ignored if the job has any other status.
+
+ SnapshotDiffResponse response = store.snapshotDiff(
+ volumeName, bucketName, fromSnapName, toSnapName,
+ null, 0, false, true);
+
+ // Cancel here is ignored, job gets saved in the snapDiffJobTable
+ // as QUEUED and then transitions IN_PROGRESS.
+ assertEquals(IN_PROGRESS, response.getJobStatus());
+
+ response = store.snapshotDiff(volumeName,
+ bucketName, fromSnapName, toSnapName,
+ null, 0, false, true);
+
+ // Job status should be updated to CANCELED.
+ assertEquals(CANCELED, response.getJobStatus());
+
+ // Executing the command again should return CANCELED,
+ // until the job is picked up by the SnapshotDiffCleanupService
+ // and removed from the snapDiffJobTable.
+ response = store.snapshotDiff(volumeName,
Review Comment:
As a completeness for this test, can we call
[SnapshotDiffCleanupService.run()](https://github.com/apache/ozone/blob/master/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/service/SnapshotDiffCleanupService.java#L112),
and assert that it is moved to cleanup table?
##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotDiffManager.java:
##########
@@ -1010,6 +1066,11 @@ private long generateDiffReport(
try (ClosableIterator<byte[]>
objectIdsIterator = objectIDsToCheck.iterator()) {
while (objectIdsIterator.hasNext()) {
+ if (snapDiffJobTable.get(jobKey).getStatus()
Review Comment:
1. To avoid NPE.
```suggestion
if (snapDiffJobTable.get(jobKey) != null &&
CANCELED.equals(snapDiffJobTable.get(jobKey).getStatus())) {
return -1L;
}
```
2. I feel it would be too expensive to check job status for every entry.
Should we check after like every 100 entries?
##########
hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotDiffManager.java:
##########
@@ -0,0 +1,302 @@
+/**
+ * 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.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.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.Test;
+import org.rocksdb.RocksDBException;
+
+import java.io.File;
+import java.io.IOException;
+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();
Review Comment:
You can use @TempDir annotation to create temp dir for the test.
##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotDiffManager.java:
##########
@@ -718,36 +746,63 @@ private void generateSnapshotDiffReport(final String
jobKey,
Table<String, OmKeyInfo> tsKeyTable = toSnapshot.getMetadataManager()
.getKeyTable(bucketLayout);
- getDeltaFilesAndDiffKeysToObjectIdToKeyMap(fsKeyTable, tsKeyTable,
- fromSnapshot, toSnapshot, fsInfo, tsInfo, useFullDiff,
- tablePrefixes, objectIdToKeyNameMapForFromSnapshot,
- objectIdToKeyNameMapForToSnapshot, objectIDsToCheckMap,
- path.toString());
-
- if (bucketLayout.isFileSystemOptimized()) {
- validateSnapshotsAreActive(volumeName, bucketName, fromSnapshotName,
- toSnapshotName);
-
- Table<String, OmDirectoryInfo> fsDirTable =
- fromSnapshot.getMetadataManager().getDirectoryTable();
- Table<String, OmDirectoryInfo> tsDirTable =
- toSnapshot.getMetadataManager().getDirectoryTable();
-
- getDeltaFilesAndDiffKeysToObjectIdToKeyMap(fsDirTable, tsDirTable,
- fromSnapshot, toSnapshot, fsInfo, tsInfo, useFullDiff,
- tablePrefixes, objectIdToKeyNameMapForFromSnapshot,
- objectIdToKeyNameMapForToSnapshot, objectIDsToCheckMap,
- path.toString());
- }
-
- validateSnapshotsAreActive(volumeName, bucketName, fromSnapshotName,
- toSnapshotName);
- long totalDiffEntries = generateDiffReport(jobId,
- objectIDsToCheckMap,
- objectIdToKeyNameMapForFromSnapshot,
- objectIdToKeyNameMapForToSnapshot);
+ // These are the most time and resource consuming method calls.
+ // Split the calls into steps and store them in an array, to avoid
+ // repetition while constantly checking if the job is cancelled.
+ Callable<Void>[] methodCalls = new Callable[]{
+ () -> {
+ getDeltaFilesAndDiffKeysToObjectIdToKeyMap(fsKeyTable, tsKeyTable,
+ fromSnapshot, toSnapshot, fsInfo, tsInfo, useFullDiff,
+ tablePrefixes, objectIdToKeyNameMapForFromSnapshot,
+ objectIdToKeyNameMapForToSnapshot, objectIDsToCheckMap,
+ path.toString());
+ return null;
+ },
+ () -> {
+ if (bucketLayout.isFileSystemOptimized()) {
+ validateSnapshotsAreActive(volumeName, bucketName,
Review Comment:
You can move `validateSnapshotsAreActive` check from here and line 780 to
callable check in for loop 799-805.
--
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]