swamirishi commented on code in PR #8173:
URL: https://github.com/apache/ozone/pull/8173#discussion_r2388611768


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/DBConfigFromFile.java:
##########
@@ -103,37 +108,87 @@ public static String getOptionsFileNameFromDB(String 
dbFileName) {
    * control OzoneManager.db configs from a file, we need to create a file
    * called OzoneManager.db.ini and place that file in $OZONE_DIR/etc/hadoop.
    *
-   * @param dbFileName - The DB File Name, for example, OzoneManager.db.
-   * @param cfDescs - ColumnFamily Handles.
+   * @param dbPath - The DB File Name, for example, OzoneManager.db.
    * @return DBOptions, Options to be used for opening/creating the DB.
    */
-  public static ManagedDBOptions readFromFile(String dbFileName,
-      List<ColumnFamilyDescriptor> cfDescs) throws RocksDatabaseException {
-    Preconditions.checkNotNull(dbFileName);
-    Preconditions.checkNotNull(cfDescs);
-    Preconditions.checkArgument(!cfDescs.isEmpty());
-
-    //TODO: Add Documentation on how to support RocksDB Mem Env.
-    Env env = Env.getDefault();
-    ManagedDBOptions options = null;
-    File configLocation = getConfigLocation();
-    if (configLocation != null &&
-        StringUtils.isNotBlank(configLocation.toString())) {
-      Path optionsFile = Paths.get(configLocation.toString(),
-          getOptionsFileNameFromDB(dbFileName));
-
-      if (optionsFile.toFile().exists()) {
-        options = new ManagedDBOptions();
-        try {
-          OptionsUtil.loadOptionsFromFile(optionsFile.toString(),
-              env, options, cfDescs, true);
-
-        } catch (RocksDBException rdEx) {
-          throw new RocksDatabaseException("Failed to loadOptionsFromFile " + 
optionsFile, rdEx);
-        }
+  public static ManagedDBOptions readDBOptionsFromFile(Path dbPath) throws 
RocksDBException {
+    Path generatedDBPath = generateDBPath(dbPath);
+    if (!generatedDBPath.toFile().exists()) {
+      LOG.warn("Error trying to read generated rocksDB file: {}, file does not 
exists.", generatedDBPath);
+      return null;
+    }
+    List<ColumnFamilyDescriptor> descriptors = new ArrayList<>();
+    try {
+      //TODO: Add Documentation on how to support RocksDB Mem Env.
+      ManagedDBOptions options = new ManagedDBOptions();
+      try (ManagedConfigOptions configOptions = new ManagedConfigOptions()) {
+        OptionsUtil.loadOptionsFromFile(configOptions, 
generatedDBPath.toString(), options, descriptors);
+      } catch (RocksDBException rdEx) {
+        options.close();
+        throw new RocksDBException("There was an error opening rocksDB Options 
file: " + rdEx.getMessage());
+      }
+      return options;
+    } finally {
+      //readDBOptions will be freed once the store using it is closed, but the 
descriptors need to be closed.
+      closeDescriptors(descriptors);
+    }
+  }
+
+  public static ManagedColumnFamilyOptions readCFOptionsFromFile(Path 
optionsPath, String cfName)

Review Comment:
   Both readCFOptionsFromFile and readDBOptionsFromFile has a very similar 
logic for initializing can we create a private static function which unifies 
the common parts in the function



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/DBConfigFromFile.java:
##########
@@ -103,37 +108,87 @@ public static String getOptionsFileNameFromDB(String 
dbFileName) {
    * control OzoneManager.db configs from a file, we need to create a file
    * called OzoneManager.db.ini and place that file in $OZONE_DIR/etc/hadoop.
    *
-   * @param dbFileName - The DB File Name, for example, OzoneManager.db.
-   * @param cfDescs - ColumnFamily Handles.
+   * @param dbPath - The DB File Name, for example, OzoneManager.db.
    * @return DBOptions, Options to be used for opening/creating the DB.
    */
-  public static ManagedDBOptions readFromFile(String dbFileName,
-      List<ColumnFamilyDescriptor> cfDescs) throws RocksDatabaseException {
-    Preconditions.checkNotNull(dbFileName);
-    Preconditions.checkNotNull(cfDescs);
-    Preconditions.checkArgument(!cfDescs.isEmpty());
-
-    //TODO: Add Documentation on how to support RocksDB Mem Env.
-    Env env = Env.getDefault();
-    ManagedDBOptions options = null;
-    File configLocation = getConfigLocation();
-    if (configLocation != null &&
-        StringUtils.isNotBlank(configLocation.toString())) {
-      Path optionsFile = Paths.get(configLocation.toString(),
-          getOptionsFileNameFromDB(dbFileName));
-
-      if (optionsFile.toFile().exists()) {
-        options = new ManagedDBOptions();
-        try {
-          OptionsUtil.loadOptionsFromFile(optionsFile.toString(),
-              env, options, cfDescs, true);
-
-        } catch (RocksDBException rdEx) {
-          throw new RocksDatabaseException("Failed to loadOptionsFromFile " + 
optionsFile, rdEx);
-        }
+  public static ManagedDBOptions readDBOptionsFromFile(Path dbPath) throws 
RocksDBException {
+    Path generatedDBPath = generateDBPath(dbPath);
+    if (!generatedDBPath.toFile().exists()) {
+      LOG.warn("Error trying to read generated rocksDB file: {}, file does not 
exists.", generatedDBPath);
+      return null;
+    }
+    List<ColumnFamilyDescriptor> descriptors = new ArrayList<>();
+    try {
+      //TODO: Add Documentation on how to support RocksDB Mem Env.
+      ManagedDBOptions options = new ManagedDBOptions();
+      try (ManagedConfigOptions configOptions = new ManagedConfigOptions()) {
+        OptionsUtil.loadOptionsFromFile(configOptions, 
generatedDBPath.toString(), options, descriptors);
+      } catch (RocksDBException rdEx) {
+        options.close();
+        throw new RocksDBException("There was an error opening rocksDB Options 
file: " + rdEx.getMessage());
+      }
+      return options;
+    } finally {
+      //readDBOptions will be freed once the store using it is closed, but the 
descriptors need to be closed.
+      closeDescriptors(descriptors);
+    }
+  }
+
+  public static ManagedColumnFamilyOptions readCFOptionsFromFile(Path 
optionsPath, String cfName)
+      throws RocksDBException {
+    Path generatedDBPath = generateDBPath(optionsPath);
+    if (!generatedDBPath.toFile().exists()) {
+      LOG.warn("Error trying to read column family options from file: {}, file 
does not exists.", generatedDBPath);
+      return null;
+    }
+    List<ColumnFamilyDescriptor> descriptors = new ArrayList<>();
+    String defaultColumnFamilyString = 
StringUtils.toEncodedString(DEFAULT_COLUMN_FAMILY, StandardCharsets.UTF_8);
+    String validatedCfName = StringUtils.isEmpty(cfName) ? 
defaultColumnFamilyString : cfName;
+    ManagedColumnFamilyOptions resultCfOptions = null;
+    try (ManagedConfigOptions ignored = new ManagedConfigOptions(); 
ManagedDBOptions options = new ManagedDBOptions()) {
+      OptionsUtil.loadOptionsFromFile(ignored, generatedDBPath.toString(), 
options, descriptors);
+      ColumnFamilyDescriptor descriptor = descriptors.stream()
+          .filter(desc -> StringUtils.toEncodedString(desc.getName(), 
StandardCharsets.UTF_8).equals(validatedCfName))
+          .findAny().orElse(null);

Review Comment:
   ```suggestion
             .findFirst().orElse(null);
   ```



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