sumitagrawl commented on code in PR #8797:
URL: https://github.com/apache/ozone/pull/8797#discussion_r2254719395


##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/NSSummaryTaskDbEventHandler.java:
##########
@@ -157,35 +175,59 @@ protected void handleDeleteKeyEvent(OmKeyInfo keyInfo,
 
     int binIndex = ReconUtils.getFileSizeBinIndex(keyInfo.getDataSize());
 
-    // decrement count, data size, and bucket count
-    // even if there's no direct key, we still keep the entry because
-    // we still need children dir IDs info
+    // Decrement immediate parent's totals (these fields now represent totals)
     nsSummary.setNumOfFiles(nsSummary.getNumOfFiles() - 1);
     nsSummary.setSizeOfFiles(nsSummary.getSizeOfFiles() - 
keyInfo.getDataSize());
     --fileBucket[binIndex];
     nsSummary.setFileSizeBucket(fileBucket);
     nsSummaryMap.put(parentObjectId, nsSummary);
+
+    // Propagate upwards to all parents in the parent chain
+    propagateSizeUpwards(parentObjectId, -keyInfo.getDataSize(), -1, 
nsSummaryMap);
   }
 
   protected void handleDeleteDirEvent(OmDirectoryInfo directoryInfo,
                                       Map<Long, NSSummary> nsSummaryMap)
       throws IOException {
+    long deletedDirObjectId = directoryInfo.getObjectID();
     long parentObjectId = directoryInfo.getParentObjectID();
-    // Try to get the NSSummary from our local map that maps NSSummaries to IDs
-    NSSummary nsSummary = nsSummaryMap.get(parentObjectId);
-    if (nsSummary == null) {
-      // If we don't have it in this batch we try to get it from the DB
-      nsSummary = reconNamespaceSummaryManager.getNSSummary(parentObjectId);
+    
+    // Get the deleted directory's NSSummary to extract its totals
+    NSSummary deletedDirSummary = nsSummaryMap.get(deletedDirObjectId);
+    if (deletedDirSummary == null) {
+      deletedDirSummary = 
reconNamespaceSummaryManager.getNSSummary(deletedDirObjectId);
+    }
+    
+    // Get the parent directory's NSSummary
+    NSSummary parentNsSummary = nsSummaryMap.get(parentObjectId);
+    if (parentNsSummary == null) {
+      parentNsSummary = 
reconNamespaceSummaryManager.getNSSummary(parentObjectId);
     }
 
     // Just in case the OmDirectoryInfo isn't correctly written.
-    if (nsSummary == null) {
+    if (parentNsSummary == null) {
       LOG.error("The namespace table is not correctly populated.");
       return;
     }
 
-    nsSummary.removeChildDir(directoryInfo.getObjectID());
-    nsSummaryMap.put(parentObjectId, nsSummary);
+    // If deleted directory exists, decrement its totals from parent and 
propagate
+    if (deletedDirSummary != null) {
+      // Decrement parent's totals by the deleted directory's totals
+      parentNsSummary.setNumOfFiles(parentNsSummary.getNumOfFiles() - 
deletedDirSummary.getNumOfFiles());
+      parentNsSummary.setSizeOfFiles(parentNsSummary.getSizeOfFiles() - 
deletedDirSummary.getSizeOfFiles());
+      
+      // Propagate the decrements upwards to all ancestors
+      propagateSizeUpwards(parentObjectId, 
-deletedDirSummary.getSizeOfFiles(), 
+                          -deletedDirSummary.getNumOfFiles(), nsSummaryMap);
+      
+      // Set the deleted directory's parentId to 0 (unlink it)
+      deletedDirSummary.setParentId(0);

Review Comment:
   handle update dir event will be issue as parent is reset, but no set when 
same is added again. update is -- delete and add.



##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/NSSummaryTaskDbEventHandler.java:
##########
@@ -199,4 +241,42 @@ protected boolean flushAndCommitNSToDB(Map<Long, 
NSSummary> nsSummaryMap) {
     }
     return true;
   }
+
+  /**
+   * Propagates size and count changes upwards through the parent chain.
+   * This ensures that when files are added/deleted, all ancestor directories
+   * reflect the total changes in their sizeOfFiles and numOfFiles fields.
+   */
+  protected void propagateSizeUpwards(long objectId, long sizeChange, 
+                                       long countChange, Map<Long, NSSummary> 
nsSummaryMap) 
+                                       throws IOException {
+    // Get the current directory's NSSummary
+    NSSummary nsSummary = nsSummaryMap.get(objectId);
+    if (nsSummary == null) {
+      nsSummary = reconNamespaceSummaryManager.getNSSummary(objectId);
+    }
+    if (nsSummary == null) {
+      return; // No more parents to update
+    }
+
+    // Continue propagating to parent
+    long parentId = nsSummary.getParentId();
+    if (parentId != 0) {
+      // Get parent's NSSummary
+      NSSummary parentSummary = nsSummaryMap.get(parentId);
+      if (parentSummary == null) {
+        parentSummary = reconNamespaceSummaryManager.getNSSummary(parentId);
+      }
+      if (parentSummary != null) {
+        // Update parent's totals
+        parentSummary.setSizeOfFiles(parentSummary.getSizeOfFiles() + 
sizeChange);
+        parentSummary.setNumOfFiles(parentSummary.getNumOfFiles() + 
(int)countChange);

Review Comment:
   we can use int all places while declaring as value type is int only



##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/NSSummaryTaskDbEventHandler.java:
##########
@@ -199,4 +241,42 @@ protected boolean flushAndCommitNSToDB(Map<Long, 
NSSummary> nsSummaryMap) {
     }
     return true;
   }
+
+  /**
+   * Propagates size and count changes upwards through the parent chain.
+   * This ensures that when files are added/deleted, all ancestor directories
+   * reflect the total changes in their sizeOfFiles and numOfFiles fields.
+   */
+  protected void propagateSizeUpwards(long objectId, long sizeChange, 
+                                       long countChange, Map<Long, NSSummary> 
nsSummaryMap) 
+                                       throws IOException {
+    // Get the current directory's NSSummary
+    NSSummary nsSummary = nsSummaryMap.get(objectId);
+    if (nsSummary == null) {
+      nsSummary = reconNamespaceSummaryManager.getNSSummary(objectId);
+    }
+    if (nsSummary == null) {
+      return; // No more parents to update
+    }
+
+    // Continue propagating to parent
+    long parentId = nsSummary.getParentId();
+    if (parentId != 0) {

Review Comment:
   better to use >= 0, as -1 being used for compatibility earlier



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