devmadhuu commented on code in PR #6272:
URL: https://github.com/apache/ozone/pull/6272#discussion_r1541373345


##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/scm/ReconScmMetadataManagerImpl.java:
##########
@@ -0,0 +1,219 @@
+/*
+ * 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.scm;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator;
+import org.apache.hadoop.hdds.scm.metadata.SCMMetadataStoreImpl;
+import org.apache.hadoop.hdds.scm.server.OzoneStorageContainerManager;
+import org.apache.hadoop.hdds.utils.db.DBColumnFamilyDefinition;
+import org.apache.hadoop.hdds.utils.db.DBStore;
+import org.apache.hadoop.hdds.utils.db.DBStoreBuilder;
+import org.apache.hadoop.hdds.utils.db.RDBStore;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.ozone.recon.ReconUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import java.io.File;
+import java.io.IOException;
+
+import static 
org.apache.hadoop.ozone.recon.ReconConstants.RECON_SCM_SNAPSHOT_DB;
+import static 
org.apache.hadoop.ozone.recon.ReconServerConfigKeys.OZONE_RECON_SCM_DB_DIR;
+
+/**
+ * Recon's implementation of the SCM Metadata manager. By extending and
+ * relying on the SCMMetadataStoreImpl, we can make sure all changes made to
+ * schema in SCM will be automatically picked up by Recon.
+ */
+@Singleton
+public class ReconScmMetadataManagerImpl extends SCMMetadataStoreImpl
+    implements ReconScmMetadataManager {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(ReconScmMetadataManagerImpl.class);
+
+  private OzoneConfiguration ozoneConfiguration;
+  private ReconUtils reconUtils;
+  private OzoneStorageContainerManager ozoneStorageContainerManager;
+  private SequenceIdGenerator sequenceIdGen;
+  private ReconNodeManager nodeManager;
+
+  @Inject
+  public ReconScmMetadataManagerImpl(OzoneConfiguration configuration,
+                                     ReconUtils reconUtils) {
+    this.reconUtils = reconUtils;
+    this.ozoneConfiguration = configuration;
+  }
+
+  @Override
+  public void start(OzoneConfiguration configuration) throws IOException {
+    LOG.info("Starting ReconScmMetadataManagerImpl...");
+    File reconDbDir =
+        reconUtils.getReconDbDir(configuration, OZONE_RECON_SCM_DB_DIR);
+    File lastKnownSCMSnapshot =
+        reconUtils.getLastKnownDB(reconDbDir, RECON_SCM_SNAPSHOT_DB);
+    if (lastKnownSCMSnapshot != null) {
+      LOG.info("Last known snapshot for SCM : {}", 
lastKnownSCMSnapshot.getAbsolutePath());
+    }
+  }
+
+  private DBStore createDBAndAddSCMTablesAndCodecs(File dbFile,
+                                                   
ReconSCMSnapshotDBDefinition definition) throws IOException {
+    DBStoreBuilder dbStoreBuilder =
+        DBStoreBuilder.newBuilder(ozoneConfiguration)
+            .setName(dbFile.getName())
+            .setPath(dbFile.toPath().getParent());
+    for (DBColumnFamilyDefinition columnFamily :
+        definition.getColumnFamilies()) {
+      dbStoreBuilder.addTable(columnFamily.getName());
+      dbStoreBuilder.addCodec(columnFamily.getKeyType(),
+          columnFamily.getKeyCodec());
+      dbStoreBuilder.addCodec(columnFamily.getValueType(),
+          columnFamily.getValueCodec());
+    }
+    return dbStoreBuilder.build();
+  }
+
+  /**
+   * Replace existing DB instance with new one.
+   *
+   * @param dbFile new DB file location.
+   */
+  private void initializeRdbStoreWithFile(File dbFile)
+      throws IOException {
+    try {
+      DBStore newStore = createDBAndAddSCMTablesAndCodecs(
+          dbFile, new ReconSCMSnapshotDBDefinition());
+
+      sequenceIdGen.reinitialize(
+          ReconSCMSnapshotDBDefinition.SEQUENCE_ID.getTable(newStore));
+      ozoneStorageContainerManager.getPipelineManager().reinitialize(
+          ReconSCMSnapshotDBDefinition.PIPELINES.getTable(newStore));
+      ozoneStorageContainerManager.getContainerManager().reinitialize(
+          ReconSCMSnapshotDBDefinition.CONTAINERS.getTable(newStore));
+
+      setStore(newStore);
+      ozoneStorageContainerManager.setStore(newStore);
+      LOG.info("Created SCM DB handle from snapshot at {} and sequence Id - 
{}.", dbFile.getAbsolutePath(),
+          getLastSequenceNumberFromDB());
+    } catch (IOException ioEx) {
+      LOG.error("Unable to initialize Recon SCM DB snapshot store.", ioEx);
+    }
+    if (getStore() != null) {
+      initializeScmTables();
+    }
+  }
+
+  /**
+   * Refresh the DB instance to point to a new location. Get rid of the old
+   * DB instance.
+   *
+   * @param newDbLocation New location of the SCM Snapshot DB.
+   */
+  @Override
+  public void updateScmDB(File newDbLocation) throws IOException {
+    DBStore current = ozoneStorageContainerManager.getStore();
+    if (null != current) {
+      File oldDBLocation = current.getDbLocation();
+      if (oldDBLocation.exists()) {
+        LOG.info("Cleaning up old SCM snapshot db at {}.",
+            oldDBLocation.getAbsolutePath());
+        FileUtils.deleteDirectory(oldDBLocation);
+      }
+    }
+    try {
+      initializeRdbStoreWithFile(newDbLocation);
+    } finally {
+      // Always close DBStore if it's replaced.
+      if (current != null && current != 
ozoneStorageContainerManager.getStore()) {
+        current.close();
+      }
+    }
+  }
+
+  /**
+   * Get SCM metadata RocksDB's latest sequence number.
+   * @return latest sequence number.
+   */
+  @Override
+  public long getLastSequenceNumberFromDB() {
+    RDBStore rocksDBStore = (RDBStore) getStore();
+    if (null == rocksDBStore) {
+      return 0;
+    } else {
+      try {
+        LOG.error("rocksDBStore.getDbLocation().getAbsolutePath(): {}", 
rocksDBStore.getDbLocation().getAbsolutePath());

Review Comment:
   > Should we consider including logging message in case the user cannot 
access the db due to permission issues?
   
   Yes.
   
   > Should we consider including logging message in case the user cannot 
access the db due to permission issues?
   
   Yes, added the error log.



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