ArafatKhan2198 commented on code in PR #8002:
URL: https://github.com/apache/ozone/pull/8002#discussion_r1980496173


##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/ContainerKeyMapperHelper.java:
##########
@@ -0,0 +1,474 @@
+/*
+ * 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 java.io.IOException;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.hadoop.hdds.utils.db.RDBBatchOperation;
+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.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfoGroup;
+import org.apache.hadoop.ozone.recon.ReconConstants;
+import org.apache.hadoop.ozone.recon.api.types.ContainerKeyPrefix;
+import org.apache.hadoop.ozone.recon.api.types.KeyPrefixContainer;
+import org.apache.hadoop.ozone.recon.spi.ReconContainerMetadataManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Helper class that encapsulates the common logic for 
ContainerKeyMapperTaskFSO and ContainerKeyMapperTaskOBS.
+ */
+public abstract class ContainerKeyMapperHelper {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(ContainerKeyMapperHelper.class);
+
+  // Static lock to guard table truncation.
+  private static final Object TRUNCATE_LOCK = new Object();
+
+  /**
+   * Ensures that the container key tables are truncated only once before 
reprocessing.
+   * Uses an AtomicBoolean to track if truncation has already been performed.
+   *
+   * @param reconContainerMetadataManager The metadata manager instance 
responsible for DB operations.
+   */
+  public static void truncateTablesIfNeeded(ReconContainerMetadataManager 
reconContainerMetadataManager) {
+    synchronized (TRUNCATE_LOCK) {
+      if (ReconConstants.CONTAINER_KEY_TABLES_TRUNCATED.compareAndSet(false, 
true)) {
+        try {
+          // Perform table truncation
+          reconContainerMetadataManager.reinitWithNewContainerDataFromOm(new 
HashMap<>());
+          LOG.info("Successfully truncated container key tables.");
+        } catch (IOException e) {

Review Comment:
   Done



##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/ContainerKeyMapperHelper.java:
##########
@@ -0,0 +1,474 @@
+/*
+ * 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 java.io.IOException;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.hadoop.hdds.utils.db.RDBBatchOperation;
+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.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfoGroup;
+import org.apache.hadoop.ozone.recon.ReconConstants;
+import org.apache.hadoop.ozone.recon.api.types.ContainerKeyPrefix;
+import org.apache.hadoop.ozone.recon.api.types.KeyPrefixContainer;
+import org.apache.hadoop.ozone.recon.spi.ReconContainerMetadataManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Helper class that encapsulates the common logic for 
ContainerKeyMapperTaskFSO and ContainerKeyMapperTaskOBS.
+ */
+public abstract class ContainerKeyMapperHelper {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(ContainerKeyMapperHelper.class);
+
+  // Static lock to guard table truncation.
+  private static final Object TRUNCATE_LOCK = new Object();
+
+  /**
+   * Ensures that the container key tables are truncated only once before 
reprocessing.
+   * Uses an AtomicBoolean to track if truncation has already been performed.
+   *
+   * @param reconContainerMetadataManager The metadata manager instance 
responsible for DB operations.
+   */
+  public static void truncateTablesIfNeeded(ReconContainerMetadataManager 
reconContainerMetadataManager) {
+    synchronized (TRUNCATE_LOCK) {
+      if (ReconConstants.CONTAINER_KEY_TABLES_TRUNCATED.compareAndSet(false, 
true)) {
+        try {
+          // Perform table truncation
+          reconContainerMetadataManager.reinitWithNewContainerDataFromOm(new 
HashMap<>());
+          LOG.info("Successfully truncated container key tables.");
+        } catch (IOException e) {
+          // Reset the flag so truncation can be retried
+          ReconConstants.CONTAINER_KEY_TABLES_TRUNCATED.set(false);
+          LOG.error("Error while truncating container key tables. Resetting 
flag.", e);
+          throw new RuntimeException("Table truncation failed", e);
+        }
+      } else {
+        LOG.info("Container key tables already truncated by another task, 
waiting for truncation to complete.");

Review Comment:
   Done



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