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.region;
019
020import java.io.IOException;
021import java.lang.management.MemoryType;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.Path;
024import org.apache.hadoop.hbase.ChoreService;
025import org.apache.hadoop.hbase.CoordinatedStateManager;
026import org.apache.hadoop.hbase.HBaseConfiguration;
027import org.apache.hadoop.hbase.Server;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.client.ClusterConnection;
030import org.apache.hadoop.hbase.client.Connection;
031import org.apache.hadoop.hbase.io.util.MemorySizeUtil;
032import org.apache.hadoop.hbase.master.region.MasterRegion;
033import org.apache.hadoop.hbase.master.region.MasterRegionFactory;
034import org.apache.hadoop.hbase.procedure2.store.ProcedureStorePerformanceEvaluation;
035import org.apache.hadoop.hbase.regionserver.ChunkCreator;
036import org.apache.hadoop.hbase.regionserver.MemStoreLAB;
037import org.apache.hadoop.hbase.util.CommonFSUtils;
038import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
039import org.apache.hadoop.hbase.util.Pair;
040import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
041
042public class RegionProcedureStorePerformanceEvaluation
043  extends ProcedureStorePerformanceEvaluation<RegionProcedureStore> {
044
045  private static final class MockServer implements Server {
046
047    private final Configuration conf;
048
049    private final ServerName serverName =
050      ServerName.valueOf("localhost", 12345, EnvironmentEdgeManager.currentTime());
051
052    private volatile boolean abort = false;
053
054    public MockServer(Configuration conf) {
055      this.conf = conf;
056    }
057
058    @Override
059    public void abort(String why, Throwable e) {
060      abort = true;
061    }
062
063    @Override
064    public boolean isAborted() {
065      return abort;
066    }
067
068    @Override
069    public void stop(String why) {
070    }
071
072    @Override
073    public boolean isStopped() {
074      return false;
075    }
076
077    @Override
078    public Configuration getConfiguration() {
079      return conf;
080    }
081
082    @Override
083    public ZKWatcher getZooKeeper() {
084      throw new UnsupportedOperationException();
085    }
086
087    @Override
088    public Connection getConnection() {
089      throw new UnsupportedOperationException();
090    }
091
092    @Override
093    public Connection createConnection(Configuration conf) throws IOException {
094      throw new UnsupportedOperationException();
095    }
096
097    @Override
098    public ClusterConnection getClusterConnection() {
099      throw new UnsupportedOperationException();
100    }
101
102    @Override
103    public ServerName getServerName() {
104      return serverName;
105    }
106
107    @Override
108    public CoordinatedStateManager getCoordinatedStateManager() {
109      throw new UnsupportedOperationException();
110    }
111
112    @Override
113    public ChoreService getChoreService() {
114      throw new UnsupportedOperationException();
115    }
116  }
117
118  private MasterRegion region;
119
120  @Override
121  protected RegionProcedureStore createProcedureStore(Path storeDir) throws IOException {
122    Pair<Long, MemoryType> pair = MemorySizeUtil.getGlobalMemStoreSize(conf);
123    long globalMemStoreSize = pair.getFirst();
124    boolean offheap = pair.getSecond() == MemoryType.NON_HEAP;
125    float poolSizePercentage = offheap
126      ? 1.0F
127      : conf.getFloat(MemStoreLAB.CHUNK_POOL_MAXSIZE_KEY, MemStoreLAB.POOL_MAX_SIZE_DEFAULT);
128    float initialCountPercentage =
129      conf.getFloat(MemStoreLAB.CHUNK_POOL_INITIALSIZE_KEY, MemStoreLAB.POOL_INITIAL_SIZE_DEFAULT);
130    int chunkSize = conf.getInt(MemStoreLAB.CHUNK_SIZE_KEY, MemStoreLAB.CHUNK_SIZE_DEFAULT);
131    float indexChunkSizePercent = conf.getFloat(MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_KEY,
132      MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT);
133    ChunkCreator.initialize(chunkSize, offheap, globalMemStoreSize, poolSizePercentage,
134      initialCountPercentage, null, indexChunkSizePercent);
135    conf.setBoolean(MasterRegionFactory.USE_HSYNC_KEY, "hsync".equals(syncType));
136    CommonFSUtils.setRootDir(conf, storeDir);
137    MockServer server = new MockServer(conf);
138    region = MasterRegionFactory.create(server);
139    return new RegionProcedureStore(server, region, (fs, apth) -> {
140    });
141  }
142
143  @Override
144  protected void printRawFormatResult(long timeTakenNs) {
145    System.out.println(String.format("RESULT [%s=%s, %s=%s, %s=%s, %s=%s, " + "total_time_ms=%s]",
146      NUM_PROCS_OPTION.getOpt(), numProcs, STATE_SIZE_OPTION.getOpt(), stateSize,
147      SYNC_OPTION.getOpt(), syncType, NUM_THREADS_OPTION.getOpt(), numThreads, timeTakenNs));
148  }
149
150  @Override
151  protected void preWrite(long procId) throws IOException {
152  }
153
154  @Override
155  protected void postStop(RegionProcedureStore store) throws IOException {
156    region.close(true);
157  }
158
159  public static void main(String[] args) throws IOException {
160    RegionProcedureStorePerformanceEvaluation tool =
161      new RegionProcedureStorePerformanceEvaluation();
162    tool.setConf(HBaseConfiguration.create());
163    tool.run(args);
164  }
165}