deniskuzZ commented on a change in pull request #1073: URL: https://github.com/apache/hive/pull/1073#discussion_r456427550
########## File path: standalone-metastore/metastore-tools/metastore-benchmarks/src/main/java/org/apache/hadoop/hive/metastore/tools/ACIDBenchmarks.java ########## @@ -0,0 +1,247 @@ +package org.apache.hadoop.hive.metastore.tools; + +import org.apache.hadoop.hive.metastore.api.DataOperationType; +import org.apache.hadoop.hive.metastore.api.LockComponent; +import org.apache.hadoop.hive.metastore.api.LockRequest; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.thrift.TException; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; + +import static org.apache.hadoop.hive.metastore.tools.BenchmarkUtils.createManyTables; +import static org.apache.hadoop.hive.metastore.tools.BenchmarkUtils.dropManyTables; +import static org.apache.hadoop.hive.metastore.tools.Util.throwingSupplierWrapper; + +public class ACIDBenchmarks { + + private static final Logger LOG = LoggerFactory.getLogger(CoreContext.class); + + @State(Scope.Benchmark) + public static class CoreContext { + @Param("1") + protected int howMany; + + @State(Scope.Thread) + public static class ThreadState { + HMSClient client; + + @Setup + public void doSetup() throws Exception { + LOG.debug("Creating client"); + client = HMSConfig.getInstance().newClient(); + } + + @TearDown + public void doTearDown() throws Exception { + client.close(); + LOG.debug("Closed a connection to metastore."); + } + } + + @Setup + public void setup() { + LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + Configuration ctxConfig = ctx.getConfiguration(); + ctxConfig.getLoggerConfig(CoreContext.class.getName()).setLevel(Level.INFO); + ctx.updateLoggers(ctxConfig); + } + } + + @State(Scope.Benchmark) + public static class TestOpenTxn extends CoreContext { + + @State(Scope.Thread) + public static class ThreadState extends CoreContext.ThreadState { + List<Long> openTxns = new ArrayList<>(); + + @TearDown + public void doTearDown() throws Exception { + client.abortTxns(openTxns); + LOG.debug("aborted all opened txns"); + } + + void addTxn(List<Long> openTxn) { + openTxns.addAll(openTxn); + } + } + + @Benchmark + public void openTxn(TestOpenTxn.ThreadState state) throws TException { + state.addTxn(state.client.openTxn(howMany)); + LOG.debug("opened txns, count=", howMany); + } + } + + @State(Scope.Benchmark) + public static class TestLocking extends CoreContext { + private int nTables; + + @Param("0") + private int nPartitions; + + private List<LockComponent> lockComponents; + + @Setup + public void setup() { + this.nTables = (nPartitions != 0) ? howMany / nPartitions : howMany; + createLockComponents(); + } + + @State(Scope.Thread) + public static class ThreadState extends CoreContext.ThreadState { + List<Long> openTxns = new ArrayList<>(); + long txnId; + + @Setup(org.openjdk.jmh.annotations.Level.Invocation) + public void iterSetup() { + txnId = executeOpenTxnAndGetTxnId(client); + LOG.debug("opened txn, id={}", txnId); + openTxns.add(txnId); + } + + @TearDown + public void doTearDown() throws Exception { + client.abortTxns(openTxns); + if (BenchmarkUtils.checkTxnsCleaned(client, openTxns) == false) { + LOG.error("Something went wrong with the cleanup of txns"); + } + LOG.debug("aborted all opened txns"); + } + } + + @Benchmark + public void lock(TestLocking.ThreadState state) { + LOG.debug("sending lock request"); + executeLock(state.client, state.txnId, lockComponents); + } + + private void createLockComponents() { + lockComponents = new ArrayList<>(); + + for (int i = 0; i < nTables; i++) { + for (int j = 0; j < nPartitions - (nPartitions > 1 ? 1 : 0); j++) { + lockComponents.add( + new Util.LockComponentBuilder() + .setDbName("default") + .setTableName(String.format("tmp_table_%d", i)) + .setPartitionName("p_" + j) + .setShared() + .setOperationType(DataOperationType.SELECT) + .build()); + } + if (nPartitions != 1) { + lockComponents.add( + new Util.LockComponentBuilder() + .setDbName("default") + .setTableName(String.format("tmp_table_%d", i)) + .setShared() + .setOperationType(DataOperationType.SELECT) + .build()); + } + } + } + + private static long executeOpenTxnAndGetTxnId(HMSClient client) { + return throwingSupplierWrapper(() -> client.openTxn(1).get(0)); + } + + private void executeLock(HMSClient client, long txnId, List<LockComponent> lockComponents) { + LockRequest req = new LockRequest(lockComponents, "hclient", "localhost"); + req.setTxnid(txnId); + throwingSupplierWrapper(() -> client.lock(req)); + } + } + + @State(Scope.Benchmark) + public static class TestAllocateTableWriteIds extends CoreContext { + String dbName = "test_db"; + String tblName = "tmp_table"; + + @State(Scope.Thread) + public static class ThreadState extends CoreContext.ThreadState { + List<Long> openTxns = new ArrayList<>(); + long txnId; + + @Setup + public void iterSetup() { Review comment: i think, it will execute both, check for example doTearDown in ThreadState confs. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org