Skip to content

Commit bf15458

Browse files
HBASE-27938 PE load any custom implementation of tests at runtime (#5307) (#5899)
Signed-off-by: Duo Zhang <[email protected]> Signed-off-by: Wellington Chevreuil <[email protected]> Signed-off-by: Viraj Jasani <[email protected]>
1 parent 860ac60 commit bf15458

File tree

2 files changed

+94
-5
lines changed

2 files changed

+94
-5
lines changed

hbase-mapreduce/src/test/java/org/apache/hadoop/hbase/PerformanceEvaluation.java

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Locale;
3737
import java.util.Map;
3838
import java.util.NoSuchElementException;
39+
import java.util.Properties;
3940
import java.util.Queue;
4041
import java.util.Random;
4142
import java.util.TreeMap;
@@ -360,7 +361,8 @@ static boolean checkTable(Admin admin, TestOptions opts) throws IOException {
360361
// {RegionSplitPolicy,replica count} does not match requested, or when the
361362
// number of column families does not match requested.
362363
if (
363-
(exists && opts.presplitRegions != DEFAULT_OPTS.presplitRegions)
364+
(exists && opts.presplitRegions != DEFAULT_OPTS.presplitRegions
365+
&& opts.presplitRegions != admin.getRegions(tableName).size())
364366
|| (!isReadCmd && desc != null
365367
&& !StringUtils.equals(desc.getRegionSplitPolicyClassName(), opts.splitPolicy))
366368
|| (!isReadCmd && desc != null && desc.getRegionReplication() != opts.replicas)
@@ -721,6 +723,7 @@ static class TestOptions {
721723
boolean cacheBlocks = true;
722724
Scan.ReadType scanReadType = Scan.ReadType.DEFAULT;
723725
long bufferSize = 2l * 1024l * 1024l;
726+
Properties commandProperties;
724727

725728
public TestOptions() {
726729
}
@@ -777,6 +780,11 @@ public TestOptions(TestOptions that) {
777780
this.cacheBlocks = that.cacheBlocks;
778781
this.scanReadType = that.scanReadType;
779782
this.bufferSize = that.bufferSize;
783+
this.commandProperties = that.commandProperties;
784+
}
785+
786+
public Properties getCommandProperties() {
787+
return commandProperties;
780788
}
781789

782790
public int getCaching() {
@@ -1142,10 +1150,10 @@ private static long nextRandomSeed() {
11421150
protected final Configuration conf;
11431151
protected final TestOptions opts;
11441152

1145-
private final Status status;
1153+
protected final Status status;
11461154

11471155
private String testName;
1148-
private Histogram latencyHistogram;
1156+
protected Histogram latencyHistogram;
11491157
private Histogram replicaLatencyHistogram;
11501158
private Histogram valueSizeHistogram;
11511159
private Histogram rpcCallsHistogram;
@@ -2618,7 +2626,7 @@ protected static void printUsage(final String shortName, final String message) {
26182626
System.err.println(message);
26192627
}
26202628
System.err.print("Usage: hbase " + shortName);
2621-
System.err.println(" <OPTIONS> [-D<property=value>]* <command> <nclients>");
2629+
System.err.println(" <OPTIONS> [-D<property=value>]* <command|class> <nclients>");
26222630
System.err.println();
26232631
System.err.println("General Options:");
26242632
System.err.println(
@@ -2719,6 +2727,13 @@ protected static void printUsage(final String shortName, final String message) {
27192727
System.err.println(String.format(" %-20s %s", command.getName(), command.getDescription()));
27202728
}
27212729
System.err.println();
2730+
System.err.println("Class:");
2731+
System.err.println("To run any custom implementation of PerformanceEvaluation.Test, "
2732+
+ "provide the classname of the implementaion class in place of "
2733+
+ "command name and it will be loaded at runtime from classpath.:");
2734+
System.err.println("Please consider to contribute back "
2735+
+ "this custom test impl into a builtin PE command for the benefit of the community");
2736+
System.err.println();
27222737
System.err.println("Args:");
27232738
System.err.println(" nclients Integer. Required. Total number of clients "
27242739
+ "(and HRegionServers) running. 1 <= value <= 500");
@@ -3013,6 +3028,20 @@ static TestOptions parseOpts(Queue<String> args) {
30133028
continue;
30143029
}
30153030

3031+
final String commandPropertiesFile = "--commandPropertiesFile=";
3032+
if (cmd.startsWith(commandPropertiesFile)) {
3033+
String fileName = String.valueOf(cmd.substring(commandPropertiesFile.length()));
3034+
Properties properties = new Properties();
3035+
try {
3036+
properties
3037+
.load(PerformanceEvaluation.class.getClassLoader().getResourceAsStream(fileName));
3038+
opts.commandProperties = properties;
3039+
} catch (IOException e) {
3040+
LOG.error("Failed to load metricIds from properties file", e);
3041+
}
3042+
continue;
3043+
}
3044+
30163045
validateParsedOpts(opts);
30173046

30183047
if (isCommandClass(cmd)) {
@@ -3126,7 +3155,20 @@ public int run(String[] args) throws Exception {
31263155
}
31273156

31283157
private static boolean isCommandClass(String cmd) {
3129-
return COMMANDS.containsKey(cmd);
3158+
return COMMANDS.containsKey(cmd) || isCustomTestClass(cmd);
3159+
}
3160+
3161+
private static boolean isCustomTestClass(String cmd) {
3162+
Class<? extends Test> cmdClass;
3163+
try {
3164+
cmdClass =
3165+
(Class<? extends Test>) PerformanceEvaluation.class.getClassLoader().loadClass(cmd);
3166+
addCommandDescriptor(cmdClass, cmd, "custom command");
3167+
return true;
3168+
} catch (Throwable th) {
3169+
LOG.info("No class found for command: " + cmd, th);
3170+
return false;
3171+
}
31303172
}
31313173

31323174
private static Class<? extends TestBase> determineCommandClass(String cmd) {

hbase-mapreduce/src/test/java/org/apache/hadoop/hbase/TestPerformanceEvaluation.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,26 @@
2828
import com.codahale.metrics.UniformReservoir;
2929
import java.io.BufferedReader;
3030
import java.io.ByteArrayInputStream;
31+
import java.io.File;
32+
import java.io.FileWriter;
3133
import java.io.IOException;
3234
import java.io.InputStreamReader;
3335
import java.lang.reflect.Constructor;
3436
import java.lang.reflect.InvocationTargetException;
3537
import java.nio.charset.StandardCharsets;
3638
import java.util.LinkedList;
3739
import java.util.NoSuchElementException;
40+
import java.util.Properties;
3841
import java.util.Queue;
3942
import java.util.Random;
4043
import java.util.concurrent.ThreadLocalRandom;
4144
import org.apache.hadoop.fs.FSDataInputStream;
4245
import org.apache.hadoop.fs.FileSystem;
4346
import org.apache.hadoop.fs.Path;
4447
import org.apache.hadoop.hbase.PerformanceEvaluation.RandomReadTest;
48+
import org.apache.hadoop.hbase.PerformanceEvaluation.Status;
4549
import org.apache.hadoop.hbase.PerformanceEvaluation.TestOptions;
50+
import org.apache.hadoop.hbase.client.Connection;
4651
import org.apache.hadoop.hbase.regionserver.CompactingMemStore;
4752
import org.apache.hadoop.hbase.testclassification.MiscTests;
4853
import org.apache.hadoop.hbase.testclassification.SmallTests;
@@ -357,4 +362,46 @@ public void testParseOptsValueRandom() {
357362
assertEquals(true, options.valueRandom);
358363
}
359364

365+
@Test
366+
public void testCustomTestClassOptions() throws IOException {
367+
Queue<String> opts = new LinkedList<>();
368+
// create custom properties that can be used for a custom test class
369+
Properties commandProps = new Properties();
370+
commandProps.put("prop1", "val1");
371+
String cmdPropsFilePath =
372+
this.getClass().getClassLoader().getResource("").getPath() + "cmd_properties.txt";
373+
FileWriter writer = new FileWriter(new File(cmdPropsFilePath));
374+
commandProps.store(writer, null);
375+
// create opts for the custom test class - commandPropertiesFile, testClassName
376+
opts.offer("--commandPropertiesFile=" + "cmd_properties.txt");
377+
String testClassName = "org.apache.hadoop.hbase.TestPerformanceEvaluation$PESampleTestImpl";
378+
opts.offer(testClassName);
379+
opts.offer("1");
380+
PerformanceEvaluation.TestOptions options = PerformanceEvaluation.parseOpts(opts);
381+
assertNotNull(options);
382+
assertNotNull(options.getCmdName());
383+
assertEquals(testClassName, options.getCmdName());
384+
assertNotNull(options.getCommandProperties());
385+
assertEquals("val1", options.getCommandProperties().get("prop1"));
386+
}
387+
388+
class PESampleTestImpl extends PerformanceEvaluation.Test {
389+
390+
PESampleTestImpl(Connection con, TestOptions options, Status status) {
391+
super(con, options, status);
392+
}
393+
394+
@Override
395+
void onStartup() throws IOException {
396+
}
397+
398+
@Override
399+
void onTakedown() throws IOException {
400+
}
401+
402+
@Override
403+
boolean testRow(int i, long startTime) throws IOException, InterruptedException {
404+
return false;
405+
}
406+
}
360407
}

0 commit comments

Comments
 (0)