Skip to content

Commit ad88ed3

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

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;
@@ -362,7 +363,8 @@ static boolean checkTable(Admin admin, TestOptions opts) throws IOException {
362363
// {RegionSplitPolicy,replica count} does not match requested, or when the
363364
// number of column families does not match requested.
364365
if (
365-
(exists && opts.presplitRegions != DEFAULT_OPTS.presplitRegions)
366+
(exists && opts.presplitRegions != DEFAULT_OPTS.presplitRegions
367+
&& opts.presplitRegions != admin.getRegions(tableName).size())
366368
|| (!isReadCmd && desc != null
367369
&& !StringUtils.equals(desc.getRegionSplitPolicyClassName(), opts.splitPolicy))
368370
|| (!isReadCmd && desc != null && desc.getRegionReplication() != opts.replicas)
@@ -730,6 +732,7 @@ static class TestOptions {
730732
boolean cacheBlocks = true;
731733
Scan.ReadType scanReadType = Scan.ReadType.DEFAULT;
732734
long bufferSize = 2l * 1024l * 1024l;
735+
Properties commandProperties;
733736

734737
public TestOptions() {
735738
}
@@ -786,6 +789,11 @@ public TestOptions(TestOptions that) {
786789
this.cacheBlocks = that.cacheBlocks;
787790
this.scanReadType = that.scanReadType;
788791
this.bufferSize = that.bufferSize;
792+
this.commandProperties = that.commandProperties;
793+
}
794+
795+
public Properties getCommandProperties() {
796+
return commandProperties;
789797
}
790798

791799
public int getCaching() {
@@ -1151,10 +1159,10 @@ private static long nextRandomSeed() {
11511159
protected final Configuration conf;
11521160
protected final TestOptions opts;
11531161

1154-
private final Status status;
1162+
protected final Status status;
11551163

11561164
private String testName;
1157-
private Histogram latencyHistogram;
1165+
protected Histogram latencyHistogram;
11581166
private Histogram replicaLatencyHistogram;
11591167
private Histogram valueSizeHistogram;
11601168
private Histogram rpcCallsHistogram;
@@ -2626,7 +2634,7 @@ protected static void printUsage(final String shortName, final String message) {
26262634
System.err.println(message);
26272635
}
26282636
System.err.print("Usage: hbase " + shortName);
2629-
System.err.println(" <OPTIONS> [-D<property=value>]* <command> <nclients>");
2637+
System.err.println(" <OPTIONS> [-D<property=value>]* <command|class> <nclients>");
26302638
System.err.println();
26312639
System.err.println("General Options:");
26322640
System.err.println(
@@ -2727,6 +2735,13 @@ protected static void printUsage(final String shortName, final String message) {
27272735
System.err.println(String.format(" %-20s %s", command.getName(), command.getDescription()));
27282736
}
27292737
System.err.println();
2738+
System.err.println("Class:");
2739+
System.err.println("To run any custom implementation of PerformanceEvaluation.Test, "
2740+
+ "provide the classname of the implementaion class in place of "
2741+
+ "command name and it will be loaded at runtime from classpath.:");
2742+
System.err.println("Please consider to contribute back "
2743+
+ "this custom test impl into a builtin PE command for the benefit of the community");
2744+
System.err.println();
27302745
System.err.println("Args:");
27312746
System.err.println(" nclients Integer. Required. Total number of clients "
27322747
+ "(and HRegionServers) running. 1 <= value <= 500");
@@ -3021,6 +3036,20 @@ static TestOptions parseOpts(Queue<String> args) {
30213036
continue;
30223037
}
30233038

3039+
final String commandPropertiesFile = "--commandPropertiesFile=";
3040+
if (cmd.startsWith(commandPropertiesFile)) {
3041+
String fileName = String.valueOf(cmd.substring(commandPropertiesFile.length()));
3042+
Properties properties = new Properties();
3043+
try {
3044+
properties
3045+
.load(PerformanceEvaluation.class.getClassLoader().getResourceAsStream(fileName));
3046+
opts.commandProperties = properties;
3047+
} catch (IOException e) {
3048+
LOG.error("Failed to load metricIds from properties file", e);
3049+
}
3050+
continue;
3051+
}
3052+
30243053
validateParsedOpts(opts);
30253054

30263055
if (isCommandClass(cmd)) {
@@ -3134,7 +3163,20 @@ public int run(String[] args) throws Exception {
31343163
}
31353164

31363165
private static boolean isCommandClass(String cmd) {
3137-
return COMMANDS.containsKey(cmd);
3166+
return COMMANDS.containsKey(cmd) || isCustomTestClass(cmd);
3167+
}
3168+
3169+
private static boolean isCustomTestClass(String cmd) {
3170+
Class<? extends Test> cmdClass;
3171+
try {
3172+
cmdClass =
3173+
(Class<? extends Test>) PerformanceEvaluation.class.getClassLoader().loadClass(cmd);
3174+
addCommandDescriptor(cmdClass, cmd, "custom command");
3175+
return true;
3176+
} catch (Throwable th) {
3177+
LOG.info("No class found for command: " + cmd, th);
3178+
return false;
3179+
}
31383180
}
31393181

31403182
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,22 +28,27 @@
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;
4650
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
51+
import org.apache.hadoop.hbase.client.Connection;
4752
import org.apache.hadoop.hbase.client.TableDescriptor;
4853
import org.apache.hadoop.hbase.regionserver.CompactingMemStore;
4954
import org.apache.hadoop.hbase.testclassification.MiscTests;
@@ -359,4 +364,46 @@ public void testParseOptsValueRandom() {
359364
assertEquals(true, options.valueRandom);
360365
}
361366

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

0 commit comments

Comments
 (0)