ashishkumar50 commented on code in PR #4764:
URL: https://github.com/apache/ozone/pull/4764#discussion_r1217805539


##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/OmTableInsightTask.java:
##########
@@ -0,0 +1,381 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.ozone.recon.tasks;
+
+import com.google.inject.Inject;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.commons.lang3.tuple.Triple;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.ozone.om.OMMetadataManager;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.recon.recovery.ReconOMMetadataManager;
+import org.hadoop.ozone.recon.schema.tables.daos.GlobalStatsDao;
+import org.hadoop.ozone.recon.schema.tables.pojos.GlobalStats;
+import org.jooq.Configuration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import java.util.Map.Entry;
+
+import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.OPEN_KEY_TABLE;
+import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.OPEN_FILE_TABLE;
+import static org.jooq.impl.DSL.currentTimestamp;
+import static org.jooq.impl.DSL.select;
+import static org.jooq.impl.DSL.using;
+
+/**
+ * Class to iterate over the OM DB and store the total counts of volumes,
+ * buckets, keys, open keys, deleted keys, etc.
+ */
+public class OmTableInsightTask implements ReconOmTask {
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OmTableInsightTask.class);
+
+  private GlobalStatsDao globalStatsDao;
+  private Configuration sqlConfiguration;
+  private ReconOMMetadataManager reconOMMetadataManager;
+
+  @Inject
+  public OmTableInsightTask(GlobalStatsDao globalStatsDao,
+                            Configuration sqlConfiguration,
+                            ReconOMMetadataManager reconOMMetadataManager) {
+    this.globalStatsDao = globalStatsDao;
+    this.sqlConfiguration = sqlConfiguration;
+    this.reconOMMetadataManager = reconOMMetadataManager;
+  }
+
+  /**
+   * Iterates the rows of each table in the OM snapshot DB and calculates the
+   * counts and sizes for table data.
+   *
+   * For tables that require data size calculation
+   * (as returned by getTablesToCalculateSize), both the number of
+   * records (count) and total data size of the records are calculated.
+   * For all other tables, only the count of records is calculated.
+   *
+   * @param omMetadataManager OM Metadata instance.
+   * @return Pair
+   */
+  @Override
+  public Pair<String, Boolean> reprocess(OMMetadataManager omMetadataManager) {
+    HashMap<String, Long> objectCountMap = initializeCountMap();
+    HashMap<String, Long> unReplicatedSizeCountMap =
+        initializeUnReplicatedSizeMap();
+    HashMap<String, Long> replicatedSizeCountMap =
+        initializeReplicatedSizeMap();
+
+    for (String tableName : getTaskTables()) {
+      Table table = omMetadataManager.getTable(tableName);
+      if (table == null) {
+        LOG.error("Table " + tableName + " not found in OM Metadata.");
+        return new ImmutablePair<>(getTaskName(), false);
+      }
+
+      try {
+        if (getTablesToCalculateSize().contains(tableName)) {
+          Triple<Long, Long, Long> details = getTableSizeAndCount(table);
+          objectCountMap.put(
+              getTableCountKeyFromTable(tableName), details.getLeft());
+          unReplicatedSizeCountMap.put(
+              getUnReplicatedSizeKeyFromTable(tableName), details.getMiddle());
+          replicatedSizeCountMap.put(
+              getReplicatedSizeKeyFromTable(tableName), details.getRight());
+        } else {
+          long count = getCount(table.iterator());
+          objectCountMap.put(getTableCountKeyFromTable(tableName), count);
+        }
+      } catch (IOException ioEx) {
+        LOG.error("Unable to populate Table Count in Recon DB.", ioEx);
+        return new ImmutablePair<>(getTaskName(), false);
+      }
+    }
+
+    // Write count data to DB if the sizeCountMap is not empty
+        if (!objectCountMap.isEmpty()) {
+          writeDataToDB(objectCountMap);
+        }
+
+    // Write unreplicated size data to DB if the sizeCountMap is not empty
+        if (!unReplicatedSizeCountMap.isEmpty()) {
+          writeDataToDB(unReplicatedSizeCountMap);
+        }
+
+    // Write replicated size data to DB if the sizeCountMap is not empty
+        if (!replicatedSizeCountMap.isEmpty()) {
+          writeDataToDB(replicatedSizeCountMap);
+        }
+
+    LOG.info("Completed a 'reprocess' run of OmTableInsightTask.");
+    return new ImmutablePair<>(getTaskName(), true);
+  }
+
+  /**
+   * Returns a pair with the total count of records (left), total unReplicated
+   * size (middle), and total replicated size (right) in the given table.
+   * Increments count for each record and adds the dataSize if a record's value
+   * is an instance of OmKeyInfo. If the table is null, returns (0, 0, 0).
+   *
+   * @param table The table from which to get the count and data size.
+   * @return Three Long values representing the count, unReplicated size,
+   *         and replicated size.
+   * @throws IOException If an I/O error occurs during the table iteration.
+   */
+  private Triple<Long, Long, Long> getTableSizeAndCount(Table table)
+      throws IOException {
+    long count = 0;
+    long unReplicatedSize = 0;
+    long replicatedSize = 0;
+
+    TableIterator<String, ? extends Table.KeyValue<String, ?>> iterator =
+        table.iterator();
+    while (iterator.hasNext()) {
+      Table.KeyValue<String, ?> kv = iterator.next();
+
+      if (kv != null && kv.getValue() != null) {
+        if (kv.getValue() instanceof OmKeyInfo) {
+          OmKeyInfo omKeyInfo = (OmKeyInfo) kv.getValue();
+          unReplicatedSize += omKeyInfo.getDataSize();
+          replicatedSize += omKeyInfo.getReplicatedSize();
+        }
+      }
+      count++;  // Increment count for each row
+    }
+    return Triple.of(count, unReplicatedSize, replicatedSize);
+  }
+
+  /**
+   * Returns a collection of table names that require data size calculation.
+   */
+  public Collection<String> getTablesToCalculateSize() {
+    List<String> taskTables = new ArrayList<>();
+    taskTables.add(OPEN_KEY_TABLE);
+    taskTables.add(OPEN_FILE_TABLE);
+    return taskTables;
+  }
+
+  private long getCount(Iterator iterator) {

Review Comment:
   No need of this method as we already have built-in method Iterators.size().



##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/OmTableInsightTask.java:
##########
@@ -0,0 +1,381 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.ozone.recon.tasks;
+
+import com.google.inject.Inject;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.commons.lang3.tuple.Triple;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.ozone.om.OMMetadataManager;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.recon.recovery.ReconOMMetadataManager;
+import org.hadoop.ozone.recon.schema.tables.daos.GlobalStatsDao;
+import org.hadoop.ozone.recon.schema.tables.pojos.GlobalStats;
+import org.jooq.Configuration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import java.util.Map.Entry;
+
+import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.OPEN_KEY_TABLE;
+import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.OPEN_FILE_TABLE;
+import static org.jooq.impl.DSL.currentTimestamp;
+import static org.jooq.impl.DSL.select;
+import static org.jooq.impl.DSL.using;
+
+/**
+ * Class to iterate over the OM DB and store the total counts of volumes,
+ * buckets, keys, open keys, deleted keys, etc.
+ */
+public class OmTableInsightTask implements ReconOmTask {
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OmTableInsightTask.class);
+
+  private GlobalStatsDao globalStatsDao;
+  private Configuration sqlConfiguration;
+  private ReconOMMetadataManager reconOMMetadataManager;
+
+  @Inject
+  public OmTableInsightTask(GlobalStatsDao globalStatsDao,
+                            Configuration sqlConfiguration,
+                            ReconOMMetadataManager reconOMMetadataManager) {
+    this.globalStatsDao = globalStatsDao;
+    this.sqlConfiguration = sqlConfiguration;
+    this.reconOMMetadataManager = reconOMMetadataManager;
+  }
+
+  /**
+   * Iterates the rows of each table in the OM snapshot DB and calculates the
+   * counts and sizes for table data.
+   *
+   * For tables that require data size calculation
+   * (as returned by getTablesToCalculateSize), both the number of
+   * records (count) and total data size of the records are calculated.
+   * For all other tables, only the count of records is calculated.
+   *
+   * @param omMetadataManager OM Metadata instance.
+   * @return Pair
+   */
+  @Override
+  public Pair<String, Boolean> reprocess(OMMetadataManager omMetadataManager) {
+    HashMap<String, Long> objectCountMap = initializeCountMap();
+    HashMap<String, Long> unReplicatedSizeCountMap =
+        initializeUnReplicatedSizeMap();
+    HashMap<String, Long> replicatedSizeCountMap =
+        initializeReplicatedSizeMap();
+
+    for (String tableName : getTaskTables()) {
+      Table table = omMetadataManager.getTable(tableName);
+      if (table == null) {
+        LOG.error("Table " + tableName + " not found in OM Metadata.");
+        return new ImmutablePair<>(getTaskName(), false);
+      }
+
+      try {
+        if (getTablesToCalculateSize().contains(tableName)) {
+          Triple<Long, Long, Long> details = getTableSizeAndCount(table);
+          objectCountMap.put(
+              getTableCountKeyFromTable(tableName), details.getLeft());
+          unReplicatedSizeCountMap.put(
+              getUnReplicatedSizeKeyFromTable(tableName), details.getMiddle());
+          replicatedSizeCountMap.put(
+              getReplicatedSizeKeyFromTable(tableName), details.getRight());
+        } else {
+          long count = getCount(table.iterator());
+          objectCountMap.put(getTableCountKeyFromTable(tableName), count);
+        }
+      } catch (IOException ioEx) {
+        LOG.error("Unable to populate Table Count in Recon DB.", ioEx);
+        return new ImmutablePair<>(getTaskName(), false);
+      }
+    }
+
+    // Write count data to DB if the sizeCountMap is not empty
+        if (!objectCountMap.isEmpty()) {
+          writeDataToDB(objectCountMap);
+        }
+
+    // Write unreplicated size data to DB if the sizeCountMap is not empty
+        if (!unReplicatedSizeCountMap.isEmpty()) {
+          writeDataToDB(unReplicatedSizeCountMap);
+        }
+
+    // Write replicated size data to DB if the sizeCountMap is not empty
+        if (!replicatedSizeCountMap.isEmpty()) {
+          writeDataToDB(replicatedSizeCountMap);
+        }
+
+    LOG.info("Completed a 'reprocess' run of OmTableInsightTask.");
+    return new ImmutablePair<>(getTaskName(), true);
+  }
+
+  /**
+   * Returns a pair with the total count of records (left), total unReplicated
+   * size (middle), and total replicated size (right) in the given table.
+   * Increments count for each record and adds the dataSize if a record's value
+   * is an instance of OmKeyInfo. If the table is null, returns (0, 0, 0).
+   *
+   * @param table The table from which to get the count and data size.
+   * @return Three Long values representing the count, unReplicated size,
+   *         and replicated size.
+   * @throws IOException If an I/O error occurs during the table iteration.
+   */
+  private Triple<Long, Long, Long> getTableSizeAndCount(Table table)
+      throws IOException {
+    long count = 0;
+    long unReplicatedSize = 0;
+    long replicatedSize = 0;
+
+    TableIterator<String, ? extends Table.KeyValue<String, ?>> iterator =
+        table.iterator();
+    while (iterator.hasNext()) {
+      Table.KeyValue<String, ?> kv = iterator.next();
+
+      if (kv != null && kv.getValue() != null) {
+        if (kv.getValue() instanceof OmKeyInfo) {
+          OmKeyInfo omKeyInfo = (OmKeyInfo) kv.getValue();
+          unReplicatedSize += omKeyInfo.getDataSize();
+          replicatedSize += omKeyInfo.getReplicatedSize();
+        }
+      }
+      count++;  // Increment count for each row

Review Comment:
   Move count++ inside condition(if kv.getValue() is instance of OmKeyInfo) or 
else it might give wrong result?



##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/OmTableInsightTask.java:
##########
@@ -0,0 +1,381 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.ozone.recon.tasks;
+
+import com.google.inject.Inject;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.commons.lang3.tuple.Triple;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.ozone.om.OMMetadataManager;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.recon.recovery.ReconOMMetadataManager;
+import org.hadoop.ozone.recon.schema.tables.daos.GlobalStatsDao;
+import org.hadoop.ozone.recon.schema.tables.pojos.GlobalStats;
+import org.jooq.Configuration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import java.util.Map.Entry;
+
+import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.OPEN_KEY_TABLE;
+import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.OPEN_FILE_TABLE;
+import static org.jooq.impl.DSL.currentTimestamp;
+import static org.jooq.impl.DSL.select;
+import static org.jooq.impl.DSL.using;
+
+/**
+ * Class to iterate over the OM DB and store the total counts of volumes,
+ * buckets, keys, open keys, deleted keys, etc.
+ */
+public class OmTableInsightTask implements ReconOmTask {
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OmTableInsightTask.class);
+
+  private GlobalStatsDao globalStatsDao;
+  private Configuration sqlConfiguration;
+  private ReconOMMetadataManager reconOMMetadataManager;
+
+  @Inject
+  public OmTableInsightTask(GlobalStatsDao globalStatsDao,
+                            Configuration sqlConfiguration,
+                            ReconOMMetadataManager reconOMMetadataManager) {
+    this.globalStatsDao = globalStatsDao;
+    this.sqlConfiguration = sqlConfiguration;
+    this.reconOMMetadataManager = reconOMMetadataManager;
+  }
+
+  /**
+   * Iterates the rows of each table in the OM snapshot DB and calculates the
+   * counts and sizes for table data.
+   *
+   * For tables that require data size calculation
+   * (as returned by getTablesToCalculateSize), both the number of
+   * records (count) and total data size of the records are calculated.
+   * For all other tables, only the count of records is calculated.
+   *
+   * @param omMetadataManager OM Metadata instance.
+   * @return Pair
+   */
+  @Override
+  public Pair<String, Boolean> reprocess(OMMetadataManager omMetadataManager) {
+    HashMap<String, Long> objectCountMap = initializeCountMap();
+    HashMap<String, Long> unReplicatedSizeCountMap =
+        initializeUnReplicatedSizeMap();
+    HashMap<String, Long> replicatedSizeCountMap =
+        initializeReplicatedSizeMap();
+
+    for (String tableName : getTaskTables()) {
+      Table table = omMetadataManager.getTable(tableName);
+      if (table == null) {
+        LOG.error("Table " + tableName + " not found in OM Metadata.");
+        return new ImmutablePair<>(getTaskName(), false);
+      }
+
+      try {
+        if (getTablesToCalculateSize().contains(tableName)) {
+          Triple<Long, Long, Long> details = getTableSizeAndCount(table);
+          objectCountMap.put(
+              getTableCountKeyFromTable(tableName), details.getLeft());
+          unReplicatedSizeCountMap.put(
+              getUnReplicatedSizeKeyFromTable(tableName), details.getMiddle());
+          replicatedSizeCountMap.put(
+              getReplicatedSizeKeyFromTable(tableName), details.getRight());
+        } else {
+          long count = getCount(table.iterator());

Review Comment:
   You can use instead
   ```suggestion
             long count = Iterators.size(table.iterator);
   ```



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