errose28 commented on code in PR #6533: URL: https://github.com/apache/ozone/pull/6533#discussion_r1631853110
########## 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.", Review Comment: ```suggestion description = "CLI to update the highest index in transactionInfoTable. Currently it is only supported for OM.", ``` ########## 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: The correct error message here should be that the DB failed to be updated. I think the code is correct and the test is wrong but passing because AssertJ's `assertThat` is expecting a test on the return type like [this](https://assertj.github.io/doc/#assertj-core-simple-example). We should stick with Junit's `assertTrue` here and in other parts of the test code. -- 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]
