sureshanaparti commented on code in PR #8843:
URL: https://github.com/apache/cloudstack/pull/8843#discussion_r1650115706


##########
plugins/storage/volume/storpool/src/main/java/org/apache/cloudstack/storage/datastore/driver/StorPoolStatsCollector.java:
##########
@@ -185,4 +205,90 @@ private void getClusterStats(JsonArray data, Map<String, 
Pair<Long, Long>> map)
             }
         }
     }
+
+    class StorPoolSnapshotsWithDelayDelete implements Runnable {
+
+        @Override
+        public void run() {
+            List<StoragePoolVO> spPools = 
storagePoolDao.findPoolsByProvider(StorPoolUtil.SP_PROVIDER_NAME);
+            if (CollectionUtils.isNotEmpty(spPools)) {
+                Map<Long, StoragePoolVO> onePoolforZone = new HashMap<>();
+                for (StoragePoolVO storagePoolVO : spPools) {
+                    onePoolforZone.put(storagePoolVO.getDataCenterId(), 
storagePoolVO);
+                }
+                for (StoragePoolVO storagePool : onePoolforZone.values()) {
+                    List<SnapshotDetailsVO> snapshotsDetails = 
snapshotDetailsDao.findDetailsByZoneAndKey(storagePool.getDataCenterId(), 
StorPoolUtil.SP_DELAY_DELETE);
+                    if (CollectionUtils.isEmpty(snapshotsDetails)) {
+                        return;
+                    }
+                    Map<String, String> snapshotsWithDelayDelete = new 
HashMap<>();
+
+                    try {
+                        log.debug(String.format("Collecting snapshots marked 
to be deleted for zone [%s]", storagePool.getDataCenterId()));
+                        JsonArray arr = 
StorPoolUtil.snapshotsListAllClusters(StorPoolUtil.getSpConnection(storagePool.getUuid(),
+                                storagePool.getId(), storagePoolDetailsDao, 
storagePoolDao));
+                         
snapshotsWithDelayDelete.putAll(getSnapshotsMarkedForDeletion(arr));
+                         log.debug(String.format("Found snapshot details [%s] 
and snapshots on StorPool with delay delete flag [%s]", snapshotsDetails, 
snapshotsWithDelayDelete));
+                         syncSnapshots(snapshotsDetails, 
snapshotsWithDelayDelete);
+                    } catch (Exception e) {
+                        log.debug("Could not fetch the snapshots with delay 
delete flag " + e.getMessage());
+                    }
+                }
+            }
+        }
+
+        private void syncSnapshots(List<SnapshotDetailsVO> snapshotsDetails,
+                Map<String, String> snapshotsWithDelayDelete) {
+            for (SnapshotDetailsVO snapshotDetailsVO : snapshotsDetails) {
+                 if 
(!snapshotsWithDelayDelete.containsKey(snapshotDetailsVO.getValue())) {
+                     StorPoolUtil.spLog("The snapshot [%s] with delayDelete 
flag is no longer on StorPool. Removing it from CloudStack", 
snapshotDetailsVO.getValue());
+                     SnapshotDataStoreVO ss = snapshotDataStoreDao
+                             
.findBySourceSnapshot(snapshotDetailsVO.getResourceId(), DataStoreRole.Primary);
+                     if (ss != null) {
+                         ss.setState(State.Destroyed);
+                         snapshotDataStoreDao.update(ss.getId(), ss);
+                     }
+                     SnapshotVO snap = 
snapshotDao.findById(snapshotDetailsVO.getResourceId());
+                     if (snap != null) {
+                         
snap.setState(com.cloud.storage.Snapshot.State.Destroyed);
+                         snapshotDao.update(snap.getId(), snap);
+                     }
+                     snapshotDetailsDao.remove(snapshotDetailsVO.getId());
+                 }
+             }
+        }
+
+        private  Map<String, String> getSnapshotsMarkedForDeletion(JsonArray 
arr) {
+            for (JsonElement jsonElement : arr) {
+                JsonObject error = 
jsonElement.getAsJsonObject().getAsJsonObject("error");
+                if (error != null) {
+                    throw new CloudRuntimeException(String.format("Could not 
collect the snapshots marked for deletion from all storage nodes due to: [%s]", 
error));
+                }
+            }
+            Map<String, String> snapshotsWithDelayDelete = new HashMap<>();
+            for (JsonElement jsonElement : arr) {
+                JsonObject response = 
jsonElement.getAsJsonObject().getAsJsonObject("response");
+                if (response == null) {
+                    return snapshotsWithDelayDelete;
+                }
+                collectSnapshots(snapshotsWithDelayDelete, response);
+            }
+            log.debug("Found snapshots on StorPool" + 
snapshotsWithDelayDelete);
+            return snapshotsWithDelayDelete;
+        }
+
+        private void collectSnapshots(Map<String, String> 
snapshotsWithDelayDelete, JsonObject response) {
+            JsonArray snapshots = 
response.getAsJsonObject().getAsJsonArray("data");
+            for (JsonElement snapshot : snapshots) {
+                String name = 
snapshot.getAsJsonObject().get("name").getAsString();
+                JsonObject tags = 
snapshot.getAsJsonObject().get("tags").getAsJsonObject();
+                if (!StringUtils.startsWith(name, "*") && 
StringUtils.containsNone(name, "@") && tags != null && 
!tags.entrySet().isEmpty()) {
+                    String tag = tags.getAsJsonPrimitive("cs").getAsString();
+                    if (tag != null && tag.equals("delayDelete")) {

Review Comment:
   Noticed this tag `delayDelete` used at other classes. Maybe, you can define 
such common tags in StorPoolUtil and use them across.



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

Reply via email to