hemantk-12 commented on code in PR #6533:
URL: https://github.com/apache/ozone/pull/6533#discussion_r1635580276


##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/TransactionInfoRepair.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.repair;
+
+import org.apache.hadoop.hdds.cli.HddsVersionProvider;
+import org.apache.hadoop.hdds.cli.SubcommandWithParent;
+import org.apache.hadoop.hdds.utils.IOUtils;
+import org.apache.hadoop.hdds.utils.TransactionInfo;
+import org.apache.hadoop.hdds.utils.db.StringCodec;
+import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksDB;
+import org.apache.hadoop.ozone.debug.RocksDBUtils;
+import org.kohsuke.MetaInfServices;
+import org.rocksdb.ColumnFamilyDescriptor;
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDBException;
+import picocli.CommandLine;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+
+import static org.apache.hadoop.ozone.OzoneConsts.TRANSACTION_INFO_KEY;
+import static 
org.apache.hadoop.ozone.om.OmMetadataManagerImpl.TRANSACTION_INFO_TABLE;
+
+
+/**
+ * Tool to update the highest term-index in transactionInfoTable.
+ */
[email protected](
+    name = "update-transaction",
+    description = "CLI to update the highest index in transactionInfoTable. 
Currently it only supports for OM.",
+    mixinStandardHelpOptions = true,
+    versionProvider = HddsVersionProvider.class
+)
+@MetaInfServices(SubcommandWithParent.class)
+public class TransactionInfoRepair
+    implements Callable<Void>, SubcommandWithParent  {
+
+  @CommandLine.Spec
+  private static CommandLine.Model.CommandSpec spec;
+
+  @CommandLine.ParentCommand
+  private RDBRepair parent;
+
+  @CommandLine.Option(names = {"--term"},
+      required = true,
+      description = "Highest term of transactionInfoTable. The input should be 
non-zero long integer.")
+  private long highestTransactionTerm;
+
+  @CommandLine.Option(names = {"--index"},
+      required = true,
+      description = "Highest index of transactionInfoTable. The input should 
be non-zero long integer.")
+  private long highestTransactionIndex;
+
+
+  protected void setHighestTransactionTerm(
+      long highestTransactionTerm) {
+    this.highestTransactionTerm = highestTransactionTerm;
+  }
+
+  protected void setHighestTransactionIndex(
+      long highestTransactionIndex) {
+    this.highestTransactionIndex = highestTransactionIndex;
+  }
+
+
+  @Override
+  public Void call() throws Exception {
+    List<ColumnFamilyHandle> cfHandleList = new ArrayList<>();
+    String dbPath = getParent().getDbPath();
+    List<ColumnFamilyDescriptor> cfDescList = 
RocksDBUtils.getColumnFamilyDescriptors(
+        dbPath);
+
+    try (ManagedRocksDB db = ManagedRocksDB.open(dbPath, cfDescList, 
cfHandleList)) {
+      ColumnFamilyHandle transactionInfoCfh = 
RocksDBUtils.getColumnFamilyHandle(TRANSACTION_INFO_TABLE, cfHandleList);
+      if (transactionInfoCfh == null) {
+        throw new IllegalArgumentException(TRANSACTION_INFO_TABLE +
+            " is not in a column family in DB for the given path.");
+      }
+      TransactionInfo originalTransactionInfo =
+          RocksDBUtils.getValue(db, transactionInfoCfh, TRANSACTION_INFO_KEY, 
TransactionInfo.getCodec());
+
+      System.out.println("The original highest transaction Info was " + 
originalTransactionInfo.getTermIndex());
+
+      TransactionInfo transactionInfo = 
TransactionInfo.valueOf(highestTransactionTerm, highestTransactionIndex);
+
+      byte[] transactionInfoBytes = 
TransactionInfo.getCodec().toPersistedFormat(transactionInfo);
+      db.get()
+          .put(transactionInfoCfh, 
StringCodec.get().toPersistedFormat(TRANSACTION_INFO_KEY), 
transactionInfoBytes);
+
+      System.out.println("The highest transaction info has been updated to: " +
+          RocksDBUtils.getValue(db, transactionInfoCfh, TRANSACTION_INFO_KEY,
+              TransactionInfo.getCodec()).getTermIndex());
+    } catch (RocksDBException exception) {
+      System.err.println("Failed to update the RocksDB for the given path: " + 
dbPath);
+      System.err.println(
+          "Make sure that Ozone entity (OM) is not running for the give 
database path and current host.");
+      throw new IOException("Failed to update RocksDB: " + exception);

Review Comment:
   nit:
   ```suggestion
         throw new IOException("Failed to update RocksDB.", exception);
   ```



##########
hadoop-ozone/tools/src/test/java/org/apache/hadoop/ozone/repair/TestTransactionInfoRepair.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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.repair;
+
+import org.apache.hadoop.hdds.utils.TransactionInfo;
+import org.apache.hadoop.hdds.utils.db.Codec;
+import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksDB;
+import org.apache.hadoop.ozone.debug.RocksDBUtils;
+import org.apache.ozone.test.GenericTestUtils;
+import org.apache.ratis.server.protocol.TermIndex;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.function.Supplier;
+
+import static 
org.apache.hadoop.ozone.om.OmMetadataManagerImpl.TRANSACTION_INFO_TABLE;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyList;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests TransactionInfoRepair.
+ */
+public class TestTransactionInfoRepair {
+
+
+  private static final String DB_PATH = "testDBPath";
+  private static final long TEST_TERM = 1;
+  private static final long TEST_INDEX = 1;
+
+  @Test
+  public void testUpdateTransactionInfoTableSuccessful() throws Exception {
+    ManagedRocksDB mdb = mockRockDB();
+    try (GenericTestUtils.SystemOutCapturer outCapturer = new 
GenericTestUtils.SystemOutCapturer()) {
+      testCommand(mdb, mock(ColumnFamilyHandle.class), () -> {
+        try {
+          return outCapturer.getOutput();
+        } catch (UnsupportedEncodingException e) {
+          throw new RuntimeException(e);
+        }
+      }, new String[]{String.format("The original highest transaction Info was 
(t:%s, i:%s)",
+          TEST_TERM, TEST_INDEX),
+              String.format("The highest transaction info has been updated to: 
(t:%s, i:%s)",
+                  TEST_TERM, TEST_INDEX)});
+    }
+  }
+
+  @Test
+  public void testCommandWhenTableNotInDBForGivenPath() throws Exception {
+    ManagedRocksDB mdb = mockRockDB();
+    IllegalArgumentException exception = 
assertThrows(IllegalArgumentException.class,
+        () -> testCommand(mdb, null, null, new String[]{""}));
+    assertThat(exception.getMessage().contains(TRANSACTION_INFO_TABLE +
+        " is not in a column family in DB for the given path"));
+  }
+
+  @Test
+  public void testCommandWhenFailToUpdateRocksDBForGivenPath() throws 
Exception {
+    ManagedRocksDB mdb = mockRockDB();
+    RocksDB rdb = mdb.get();
+
+    doThrow(RocksDBException.class).when(rdb)
+        .put(any(ColumnFamilyHandle.class), any(byte[].class), 
any(byte[].class));
+
+    IOException exception = assertThrows(IOException.class,
+        () -> testCommand(mdb, mock(ColumnFamilyHandle.class), null, new 
String[]{""}));
+    assertThat(exception.getMessage().contains(TRANSACTION_INFO_TABLE +
+        " is not in a column family in DB for the given path"));

Review Comment:
   It should be `assert(message).contains(expectedString)` as @errose28 pointed 
it out.
   ```suggestion
       assertThat(exception.getMessage()).contains("Failed to update RocksDB.");
       assertThat(exception.getCause()).isInstanceOf(RocksDBException.class);
   ```



##########
hadoop-ozone/tools/src/test/java/org/apache/hadoop/ozone/repair/TestTransactionInfoRepair.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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.repair;
+
+import org.apache.hadoop.hdds.utils.TransactionInfo;
+import org.apache.hadoop.hdds.utils.db.Codec;
+import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksDB;
+import org.apache.hadoop.ozone.debug.RocksDBUtils;
+import org.apache.ozone.test.GenericTestUtils;
+import org.apache.ratis.server.protocol.TermIndex;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.function.Supplier;
+
+import static 
org.apache.hadoop.ozone.om.OmMetadataManagerImpl.TRANSACTION_INFO_TABLE;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyList;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests TransactionInfoRepair.
+ */
+public class TestTransactionInfoRepair {
+
+
+  private static final String DB_PATH = "testDBPath";
+  private static final long TEST_TERM = 1;
+  private static final long TEST_INDEX = 1;
+
+  @Test
+  public void testUpdateTransactionInfoTableSuccessful() throws Exception {
+    ManagedRocksDB mdb = mockRockDB();
+    try (GenericTestUtils.SystemOutCapturer outCapturer = new 
GenericTestUtils.SystemOutCapturer()) {
+      testCommand(mdb, mock(ColumnFamilyHandle.class), () -> {
+        try {
+          return outCapturer.getOutput();
+        } catch (UnsupportedEncodingException e) {
+          throw new RuntimeException(e);
+        }
+      }, new String[]{String.format("The original highest transaction Info was 
(t:%s, i:%s)",
+          TEST_TERM, TEST_INDEX),
+              String.format("The highest transaction info has been updated to: 
(t:%s, i:%s)",
+                  TEST_TERM, TEST_INDEX)});
+    }
+  }
+
+  @Test
+  public void testCommandWhenTableNotInDBForGivenPath() throws Exception {
+    ManagedRocksDB mdb = mockRockDB();
+    IllegalArgumentException exception = 
assertThrows(IllegalArgumentException.class,
+        () -> testCommand(mdb, null, null, new String[]{""}));
+    assertThat(exception.getMessage().contains(TRANSACTION_INFO_TABLE +
+        " is not in a column family in DB for the given path"));

Review Comment:
   AssertThat takes the exception message, not the whole statement.
   
   ```suggestion
       assertThat(exception.getMessage()).contains(TRANSACTION_INFO_TABLE +
           " is not in a column family in DB for the given path");
   ```



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