swamirishi commented on code in PR #7193:
URL: https://github.com/apache/ozone/pull/7193#discussion_r1772518552


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotUtils.java:
##########
@@ -282,4 +320,47 @@ private static boolean 
isSameAsLatestOmKeyInfo(List<OmKeyInfo> omKeyInfos,
     }
     return false;
   }
+
+  public static SnapshotInfo getLatestGlobalSnapshotInfo(OzoneManager 
ozoneManager,
+                                                         SnapshotChainManager 
snapshotChainManager) throws IOException {
+    Optional<UUID> latestGlobalSnapshot  = 
Optional.ofNullable(snapshotChainManager.getLatestGlobalSnapshotId());
+    return latestGlobalSnapshot.isPresent() ? getSnapshotInfo(ozoneManager, 
snapshotChainManager,
+        latestGlobalSnapshot.get()) : null;
+  }
+
+  public static SnapshotInfo getLatestSnapshotInfo(String volumeName, String 
bucketName,
+                                                   OzoneManager ozoneManager,
+                                                   SnapshotChainManager 
snapshotChainManager) throws IOException {
+    Optional<UUID> latestPathSnapshot = Optional.ofNullable(
+        getLatestSnapshotId(volumeName, bucketName, snapshotChainManager));
+    return latestPathSnapshot.isPresent() ?
+        getSnapshotInfo(ozoneManager, snapshotChainManager, 
latestPathSnapshot.get()) : null;
+  }
+
+  public static UUID getLatestSnapshotId(String volumeName, String bucketName,
+                                         SnapshotChainManager 
snapshotChainManager) throws IOException {
+    String snapshotPath = volumeName + OM_KEY_PREFIX + bucketName;
+    return snapshotChainManager.getLatestPathSnapshotId(snapshotPath);
+  }
+
+  // Validates previous snapshotId given a snapshotInfo or volumeName & 
bucketName. Incase snapshotInfo is null, this
+  // would be considered as AOS and previous snapshot becomes the latest 
snapshot in the global snapshot chain.
+  // Would throw OMException if validation fails otherwise function would pass.
+  public static void validatePreviousSnapshotId(SnapshotInfo snapshotInfo,
+                                                SnapshotChainManager 
snapshotChainManager,
+                                                UUID 
expectedPreviousSnapshotId) throws IOException {
+    try {
+      UUID previousSnapshotId = snapshotInfo == null ? 
snapshotChainManager.getLatestGlobalSnapshotId() :
+          SnapshotUtils.getPreviousSnapshotId(snapshotInfo, 
snapshotChainManager);
+      if (!Objects.equals(expectedPreviousSnapshotId, previousSnapshotId)) {
+        throw new OMException("Snapshot validation failed. Expected previous 
snapshotId : " +
+                expectedPreviousSnapshotId + " but was " + previousSnapshotId,
+                OMException.ResultCodes.INVALID_REQUEST);
+      }
+    } catch (IOException e) {
+      LOG.error("Error while validating previous snapshot for snapshot: {}",
+          snapshotInfo == null ? null : snapshotInfo.getName(), e);
+      throw e;
+    }
+  }

Review Comment:
   Not exactly, this IOException would be thrown while getting latest snapshot 
from snapshot chain as well. Anyways removed the redundant error log



-- 
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