Skip to content

Commit f30870f

Browse files
HDDS-13841. Namespace summary API gives wrong count of directories and keys. (#9213)
1 parent 8a5c4e8 commit f30870f

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/NSSummaryTaskDbEventHandler.java

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ protected void handlePutDirEvent(OmDirectoryInfo directoryInfo,
127127
long objectId = directoryInfo.getObjectID();
128128
// write the dir name to the current directory
129129
String dirName = directoryInfo.getName();
130-
// Try to get the NSSummary from our local map that maps NSSummaries to IDs
130+
131+
// Get or create the directory's NSSummary
131132
NSSummary curNSSummary = nsSummaryMap.get(objectId);
132133
if (curNSSummary == null) {
133134
// If we don't have it in this batch we try to get it from the DB
@@ -140,34 +141,32 @@ protected void handlePutDirEvent(OmDirectoryInfo directoryInfo,
140141
int existingNumOfFiles = directoryAlreadyExists ? curNSSummary.getNumOfFiles() : 0;
141142
long existingReplicatedSizeOfFiles = directoryAlreadyExists ? curNSSummary.getReplicatedSizeOfFiles() : 0;
142143

143-
if (curNSSummary == null) {
144-
// If we don't have it locally and in the DB we create a new instance
145-
// as this is a new ID
144+
if (!directoryAlreadyExists) {
146145
curNSSummary = new NSSummary();
147146
}
148147
curNSSummary.setDirName(dirName);
149-
// Set the parent directory ID
150148
curNSSummary.setParentId(parentObjectId);
151149
nsSummaryMap.put(objectId, curNSSummary);
152150

153-
// Write the child dir list to the parent directory
154-
// Try to get the NSSummary from our local map that maps NSSummaries to IDs
155-
NSSummary nsSummary = nsSummaryMap.get(parentObjectId);
156-
if (nsSummary == null) {
157-
// If we don't have it in this batch we try to get it from the DB
158-
nsSummary = reconNamespaceSummaryManager.getNSSummary(parentObjectId);
151+
// Get or create the parent's NSSummary
152+
NSSummary parentNSSummary = nsSummaryMap.get(parentObjectId);
153+
if (parentNSSummary == null) {
154+
parentNSSummary = reconNamespaceSummaryManager.getNSSummary(parentObjectId);
159155
}
160-
if (nsSummary == null) {
156+
if (parentNSSummary == null) {
161157
// If we don't have it locally and in the DB we create a new instance
162158
// as this is a new ID
163-
nsSummary = new NSSummary();
159+
parentNSSummary = new NSSummary();
164160
}
165-
nsSummary.addChildDir(objectId);
166-
nsSummaryMap.put(parentObjectId, nsSummary);
161+
162+
// Add child directory to parent
163+
parentNSSummary.addChildDir(objectId);
164+
nsSummaryMap.put(parentObjectId, parentNSSummary);
167165

168166
// If the directory already existed with content, propagate its totals upward
167+
// propagateSizeUpwards will update parent, grandparent, etc.
169168
if (directoryAlreadyExists && (existingSizeOfFiles > 0 || existingNumOfFiles > 0)) {
170-
propagateSizeUpwards(parentObjectId, existingSizeOfFiles,
169+
propagateSizeUpwards(objectId, existingSizeOfFiles,
171170
existingReplicatedSizeOfFiles, existingNumOfFiles, nsSummaryMap);
172171
}
173172
}
@@ -233,32 +232,25 @@ protected void handleDeleteDirEvent(OmDirectoryInfo directoryInfo,
233232
return;
234233
}
235234

236-
// If deleted directory exists, decrement its totals from parent and propagate
235+
// Remove the deleted directory ID from parent's childDir set
236+
parentNsSummary.removeChildDir(deletedDirObjectId);
237+
nsSummaryMap.put(parentObjectId, parentNsSummary);
238+
239+
// If deleted directory exists, propagate its totals upward (as negative deltas)
240+
// propagateSizeUpwards will update parent, grandparent, etc.
237241
if (deletedDirSummary != null) {
238-
// Decrement parent's totals by the deleted directory's totals
239-
parentNsSummary.setNumOfFiles(parentNsSummary.getNumOfFiles() - deletedDirSummary.getNumOfFiles());
240-
parentNsSummary.setSizeOfFiles(parentNsSummary.getSizeOfFiles() - deletedDirSummary.getSizeOfFiles());
241-
long parentReplSize = parentNsSummary.getReplicatedSizeOfFiles();
242242
long deletedReplSize = deletedDirSummary.getReplicatedSizeOfFiles();
243-
if (parentReplSize >= 0 && deletedReplSize >= 0) {
244-
parentNsSummary.setReplicatedSizeOfFiles(parentReplSize - deletedReplSize);
245-
}
246-
247-
// Propagate the decrements upwards to all ancestors
248243
if (deletedReplSize < 0) {
249244
deletedReplSize = 0;
250245
}
251-
propagateSizeUpwards(parentObjectId, -deletedDirSummary.getSizeOfFiles(),
246+
247+
propagateSizeUpwards(deletedDirObjectId, -deletedDirSummary.getSizeOfFiles(),
252248
-deletedReplSize, -deletedDirSummary.getNumOfFiles(), nsSummaryMap);
253249

254250
// Set the deleted directory's parentId to 0 (unlink it)
255251
deletedDirSummary.setParentId(0);
256252
nsSummaryMap.put(deletedDirObjectId, deletedDirSummary);
257253
}
258-
259-
// Remove the deleted directory ID from parent's childDir set
260-
parentNsSummary.removeChildDir(deletedDirObjectId);
261-
nsSummaryMap.put(parentObjectId, parentNsSummary);
262254
}
263255

264256
protected boolean flushAndCommitNSToDB(Map<Long, NSSummary> nsSummaryMap) {

hadoop-ozone/recon/src/test/java/org/apache/hadoop/ozone/recon/tasks/TestNSSummaryTaskWithFSO.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ public class TestProcess {
234234

235235
@BeforeEach
236236
public void setUp() throws IOException {
237+
getReconNamespaceSummaryManager().clearNSSummaryTable();
237238
nSSummaryTaskWithFso.reprocessWithFSO(getReconOMMetadataManager());
238239
result = nSSummaryTaskWithFso.processWithFSO(processEventBatch(), 0);
239240
}
@@ -347,9 +348,9 @@ private OMUpdateEventBatch processEventBatch() throws IOException {
347348
public void testProcessUpdateFileSize() throws IOException {
348349
NSSummary nsSummaryForBucket1 =
349350
getReconNamespaceSummaryManager().getNSSummary(BUCKET_ONE_OBJECT_ID);
350-
// file 1 is gone, so bucket 1 is empty now
351+
351352
assertNotNull(nsSummaryForBucket1);
352-
assertEquals(0, nsSummaryForBucket1.getNumOfFiles());
353+
assertEquals(1, nsSummaryForBucket1.getNumOfFiles());
353354

354355
Set<Long> childDirBucket1 = nsSummaryForBucket1.getChildDir();
355356
// after put dir4, bucket1 now has two child dirs: dir1 and dir4

hadoop-ozone/recon/src/test/java/org/apache/hadoop/ozone/recon/tasks/TestNSSummaryTaskWithLegacy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ public class TestProcess {
185185

186186
@BeforeEach
187187
public void setUp() throws IOException {
188+
getReconNamespaceSummaryManager().clearNSSummaryTable();
188189
nSSummaryTaskWithLegacy.reprocessWithLegacy(getReconOMMetadataManager());
189190
nSSummaryTaskWithLegacy.processWithLegacy(processEventBatch(), 0);
190191

@@ -330,7 +331,7 @@ private OMUpdateEventBatch processEventBatch() throws IOException {
330331
public void testProcessUpdateFileSize() throws IOException {
331332
// file 1 is gone, so bucket 1 is empty now
332333
assertNotNull(nsSummaryForBucket1);
333-
assertEquals(6, nsSummaryForBucket1.getNumOfFiles());
334+
assertEquals(2, nsSummaryForBucket1.getNumOfFiles());
334335

335336
Set<Long> childDirBucket1 = nsSummaryForBucket1.getChildDir();
336337
// after put dir4, bucket1 now has two child dirs: dir1 and dir4

0 commit comments

Comments
 (0)