devmadhuu commented on code in PR #10186:
URL: https://github.com/apache/ozone/pull/10186#discussion_r3200612620


##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/scm/ReconStorageContainerManagerFacade.java:
##########
@@ -550,23 +711,162 @@ private void initializeSCMDB() {
 
   public void updateReconSCMDBWithNewSnapshot() throws IOException {
     if (isSyncDataFromSCMRunning.compareAndSet(false, true)) {
-      DBCheckpoint dbSnapshot = scmServiceProvider.getSCMDBSnapshot();
-      if (dbSnapshot != null && dbSnapshot.getCheckpointLocation() != null) {
-        LOG.info("Got new checkpoint from SCM : " +
-            dbSnapshot.getCheckpointLocation());
-        try {
-          initializeNewRdbStore(dbSnapshot.getCheckpointLocation().toFile());
-        } catch (IOException e) {
-          LOG.error("Unable to refresh Recon SCM DB Snapshot. ", e);
-        }
-      } else {
-        LOG.error("Null snapshot location got from SCM.");
+      try {
+        updateReconSCMDBWithNewSnapshotWithoutGuard();
+      } finally {
+        isSyncDataFromSCMRunning.compareAndSet(true, false);
       }
     } else {
       LOG.warn("SCM DB sync is already running.");
     }
   }
 
+  private void updateReconSCMDBWithNewSnapshotWithoutGuard()
+      throws IOException {
+    DBCheckpoint dbSnapshot = scmServiceProvider.getSCMDBSnapshot();
+    if (dbSnapshot != null && dbSnapshot.getCheckpointLocation() != null) {
+      LOG.info("Got new checkpoint from SCM : {}",
+          dbSnapshot.getCheckpointLocation());
+      initializeNewRdbStore(dbSnapshot.getCheckpointLocation().toFile());
+    } else {
+      throw new IOException("Null snapshot location got from SCM.");
+    }
+  }
+
+  public ScmDbSnapshotTriggerResponse triggerScmDbSnapshotSync() {
+    synchronized (scmSnapshotLock) {
+      if (!isSyncDataFromSCMRunning.compareAndSet(false, true)) {
+        return new ScmDbSnapshotTriggerResponse(false, scmSnapshotStatus,
+            "SCM DB sync is already running.");
+      }
+      scmSnapshotStatus = ScmDbSnapshotSyncStatus.IN_PROGRESS;
+      scmSnapshotPhase = ScmDbSnapshotSyncPhase.DOWNLOADING_CHECKPOINT;
+      scmSnapshotStartedAt = System.currentTimeMillis();
+      scmSnapshotFinishedAt = 0;
+      scmSnapshotCancelAllowed = true;
+      scmSnapshotTaskStarted = false;
+      scmSnapshotLastError = null;
+      scmSnapshotFuture = scmSnapshotExecutor.submit(this::runScmSnapshotSync);
+      return new ScmDbSnapshotTriggerResponse(true, scmSnapshotStatus,
+          "SCM DB snapshot sync started.");
+    }
+  }
+
+  public ScmDbSnapshotStatusResponse getScmDbSnapshotSyncStatus() {
+    synchronized (scmSnapshotLock) {
+      return new ScmDbSnapshotStatusResponse(scmSnapshotStatus,
+          scmSnapshotPhase, scmSnapshotStartedAt, scmSnapshotFinishedAt,
+          scmSnapshotCancelAllowed, scmSnapshotLastError);
+    }
+  }
+
+  public ScmDbSnapshotCancelResponse cancelScmDbSnapshotSync() {
+    synchronized (scmSnapshotLock) {
+      if (scmSnapshotStatus != ScmDbSnapshotSyncStatus.IN_PROGRESS) {
+        return new ScmDbSnapshotCancelResponse(false, scmSnapshotStatus,
+            scmSnapshotPhase, "No SCM DB snapshot sync is running.");
+      }
+      if (!scmSnapshotCancelAllowed) {
+        return new ScmDbSnapshotCancelResponse(false, scmSnapshotStatus,
+            scmSnapshotPhase,
+            "Cancellation is not allowed after DB initialization has 
started.");
+      }
+      boolean cancelled = scmSnapshotFuture != null &&
+          scmSnapshotFuture.cancel(true);
+      if (cancelled) {
+        scmSnapshotStatus = ScmDbSnapshotSyncStatus.CANCELLED;
+        scmSnapshotPhase = ScmDbSnapshotSyncPhase.CANCELLED;
+        scmSnapshotFinishedAt = System.currentTimeMillis();
+        scmSnapshotCancelAllowed = false;
+        if (!scmSnapshotTaskStarted) {
+          isSyncDataFromSCMRunning.compareAndSet(true, false);
+        }
+      }
+      return new ScmDbSnapshotCancelResponse(cancelled, scmSnapshotStatus,
+          scmSnapshotPhase, cancelled ? "SCM DB snapshot sync cancelled." :
+          "Unable to cancel SCM DB snapshot sync.");
+    }
+  }
+
+  private void runScmSnapshotSync() {
+    File checkpointLocation = null;
+    boolean initialized = false;
+    try {
+      synchronized (scmSnapshotLock) {
+        scmSnapshotTaskStarted = true;
+      }
+      DBCheckpoint dbSnapshot = scmServiceProvider.getSCMDBSnapshot();

Review Comment:
   Ah, thanks for catching this race condition. Yes I fixed it now.



##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/scm/ReconStorageContainerManagerFacade.java:
##########
@@ -550,23 +711,162 @@ private void initializeSCMDB() {
 
   public void updateReconSCMDBWithNewSnapshot() throws IOException {
     if (isSyncDataFromSCMRunning.compareAndSet(false, true)) {
-      DBCheckpoint dbSnapshot = scmServiceProvider.getSCMDBSnapshot();
-      if (dbSnapshot != null && dbSnapshot.getCheckpointLocation() != null) {
-        LOG.info("Got new checkpoint from SCM : " +
-            dbSnapshot.getCheckpointLocation());
-        try {
-          initializeNewRdbStore(dbSnapshot.getCheckpointLocation().toFile());
-        } catch (IOException e) {
-          LOG.error("Unable to refresh Recon SCM DB Snapshot. ", e);
-        }
-      } else {
-        LOG.error("Null snapshot location got from SCM.");
+      try {
+        updateReconSCMDBWithNewSnapshotWithoutGuard();

Review Comment:
   Yes . fixed it now.



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