001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.procedure2.store.wal;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.FSDataOutputStream;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseConfiguration;
026import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
027import org.apache.hadoop.hbase.procedure2.store.LeaseRecovery;
028import org.apache.hadoop.hbase.procedure2.store.ProcedureStorePerformanceEvaluation;
029
030import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine;
031import org.apache.hbase.thirdparty.org.apache.commons.cli.Option;
032
033public class ProcedureWALPerformanceEvaluation
034  extends ProcedureStorePerformanceEvaluation<WALProcedureStore> {
035
036  // Command line options and defaults.
037  public static int DEFAULT_NUM_WALS = 0;
038  public static Option NUM_WALS_OPTION = new Option("wals", true,
039    "Number of WALs to write. If -ve or 0, uses " + WALProcedureStore.ROLL_THRESHOLD_CONF_KEY
040      + " conf to roll the logs. Default: " + DEFAULT_NUM_WALS);
041
042  private long numProcsPerWal = Long.MAX_VALUE; // never roll wall based on this value.
043  private int numWals;
044
045  // Non-default configurations.
046  private void setupConf() {
047    conf.setBoolean(WALProcedureStore.USE_HSYNC_CONF_KEY, "hsync".equals(syncType));
048    if (numWals > 0) {
049      conf.setLong(WALProcedureStore.ROLL_THRESHOLD_CONF_KEY, Long.MAX_VALUE);
050      numProcsPerWal = numProcs / numWals;
051    }
052  }
053
054  /**
055   * Processes and validates command line options.
056   */
057  @Override
058  public void processOptions(CommandLine cmd) {
059    super.processOptions(cmd);
060    numWals = getOptionAsInt(cmd, NUM_WALS_OPTION.getOpt(), DEFAULT_NUM_WALS);
061    setupConf();
062  }
063
064  @Override
065  public void addOptions() {
066    super.addOptions();
067    addOption(NUM_WALS_OPTION);
068  }
069
070  @Override
071  protected WALProcedureStore createProcedureStore(Path storeDir) throws IOException {
072    if ("nosync".equals(syncType)) {
073      return new NoSyncWalProcedureStore(conf, storeDir);
074    } else {
075      return ProcedureTestingUtility.createWalStore(conf, storeDir);
076    }
077  }
078
079  @Override
080  protected void printRawFormatResult(long timeTakenNs) {
081    System.out
082      .println(String.format("RESULT [%s=%s, %s=%s, %s=%s, %s=%s, %s=%s, " + "total_time_ms=%s]",
083        NUM_PROCS_OPTION.getOpt(), numProcs, STATE_SIZE_OPTION.getOpt(), stateSize,
084        SYNC_OPTION.getOpt(), syncType, NUM_THREADS_OPTION.getOpt(), numThreads,
085        NUM_WALS_OPTION.getOpt(), numWals, timeTakenNs));
086  }
087
088  @Override
089  protected void preWrite(long procId) throws IOException {
090    if (procId > 0 && procId % numProcsPerWal == 0) {
091      store.rollWriterForTesting();
092      System.out.println(
093        "Starting new log : " + store.getActiveLogs().get(store.getActiveLogs().size() - 1));
094    }
095  }
096  ///////////////////////////////
097  // HELPER CLASSES
098  ///////////////////////////////
099
100  private static class NoSyncWalProcedureStore extends WALProcedureStore {
101    public NoSyncWalProcedureStore(final Configuration conf, final Path logDir) throws IOException {
102      super(conf, logDir, null, new LeaseRecovery() {
103        @Override
104        public void recoverFileLease(FileSystem fs, Path path) throws IOException {
105          // no-op
106        }
107      });
108    }
109
110    @Override
111    protected void syncStream(FSDataOutputStream stream) {
112      // no-op
113    }
114  }
115
116  public static void main(String[] args) throws IOException {
117    ProcedureWALPerformanceEvaluation tool = new ProcedureWALPerformanceEvaluation();
118    tool.setConf(HBaseConfiguration.create());
119    tool.run(args);
120  }
121}