diff --git a/hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/FileSizeCountTask.java b/hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/FileSizeCountTask.java index 6906d4bbb82..1e83043da81 100644 --- a/hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/FileSizeCountTask.java +++ b/hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/FileSizeCountTask.java @@ -95,7 +95,7 @@ public Pair reprocess(OMMetadataManager omMetadataManager) { if (!statusFSO && !statusOBS) { return new ImmutablePair<>(getTaskName(), false); } - writeCountsToDB(true, fileSizeCountMap); + writeCountsToDB(fileSizeCountMap); LOG.debug("Completed a 'reprocess' run of FileSizeCountTask."); return new ImmutablePair<>(getTaskName(), true); } @@ -112,7 +112,7 @@ private boolean reprocessBucketLayout(BucketLayout bucketLayout, handlePutKeyEvent(kv.getValue(), fileSizeCountMap); // The time complexity of .size() method is constant time, O(1) if (fileSizeCountMap.size() >= 100000) { - writeCountsToDB(true, fileSizeCountMap); + writeCountsToDB(fileSizeCountMap); fileSizeCountMap.clear(); } } @@ -198,7 +198,7 @@ public Pair process(OMUpdateEventBatch events) { value.getClass().getName(), updatedKey); } } - writeCountsToDB(false, fileSizeCountMap); + writeCountsToDB(fileSizeCountMap); LOG.debug("{} successfully processed in {} milliseconds", getTaskName(), (System.currentTimeMillis() - startTime)); return new ImmutablePair<>(getTaskName(), true); @@ -209,10 +209,11 @@ public Pair process(OMUpdateEventBatch events) { * using the dao. * */ - private void writeCountsToDB(boolean isDbTruncated, - Map fileSizeCountMap) { + private void writeCountsToDB(Map fileSizeCountMap) { + List insertToDb = new ArrayList<>(); List updateInDb = new ArrayList<>(); + boolean isDbTruncated = isFileCountBySizeTableEmpty(); // Check if table is empty fileSizeCountMap.keySet().forEach((FileSizeCountKey key) -> { FileCountBySize newRecord = new FileCountBySize(); @@ -295,6 +296,15 @@ private void handleDeleteKeyEvent(String key, OmKeyInfo omKeyInfo, } } + /** + * Checks if the FILE_COUNT_BY_SIZE table is empty. + * + * @return true if the table is empty, false otherwise. + */ + private boolean isFileCountBySizeTableEmpty() { + return dslContext.fetchCount(FILE_COUNT_BY_SIZE) == 0; + } + private static class FileSizeCountKey { private String volume; private String bucket;