1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.util.Set;
22
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.chaos.factories.MonkeyFactory;
28 import org.apache.hadoop.hbase.chaos.monkies.ChaosMonkey;
29 import org.apache.hadoop.hbase.util.AbstractHBaseTool;
30 import org.junit.After;
31 import org.junit.Before;
32
33
34
35
36 public abstract class IntegrationTestBase extends AbstractHBaseTool {
37 public static final String LONG_OPT = "monkey";
38 private static final Log LOG = LogFactory.getLog(IntegrationTestBase.class);
39
40 protected IntegrationTestingUtility util;
41 protected ChaosMonkey monkey;
42 protected String monkeyToUse;
43
44 public IntegrationTestBase() {
45 this(null);
46 }
47
48 public IntegrationTestBase(String monkeyToUse) {
49 this.monkeyToUse = monkeyToUse;
50 }
51
52 @Override
53 protected void addOptions() {
54 addOptWithArg("m", LONG_OPT, "Which chaos monkey to run");
55 }
56
57 @Override
58 protected void processOptions(CommandLine cmd) {
59 if (cmd.hasOption(LONG_OPT)) {
60 monkeyToUse = cmd.getOptionValue(LONG_OPT);
61 }
62 }
63
64 @Override
65 public Configuration getConf() {
66 Configuration c = super.getConf();
67 if (c == null && util != null) {
68 conf = util.getConfiguration();
69 c = conf;
70 }
71 return c;
72 }
73
74 @Override
75 protected int doWork() throws Exception {
76 setUp();
77 int result = -1;
78 try {
79 result = runTestFromCommandLine();
80 } finally {
81 cleanUp();
82 }
83
84 return result;
85 }
86
87 @Before
88 public void setUp() throws Exception {
89 setUpCluster();
90 setUpMonkey();
91 }
92
93 @After
94 public void cleanUp() throws Exception {
95 cleanUpMonkey();
96 cleanUpCluster();
97 }
98
99 public void setUpMonkey() throws Exception {
100 util = getTestingUtil(getConf());
101 MonkeyFactory fact = MonkeyFactory.getFactory(monkeyToUse);
102 if (fact == null) {
103
104 fact = MonkeyFactory.getFactory(
105 util.isDistributedCluster() ? MonkeyFactory.CALM : MonkeyFactory.SLOW_DETERMINISTIC);
106 }
107 monkey = fact.setUtil(util)
108 .setTableName(getTablename())
109 .setColumnFamilies(getColumnFamilies()).build();
110 monkey.start();
111 }
112
113 public void cleanUpMonkey() throws Exception {
114 cleanUpMonkey("Ending test");
115 }
116
117 protected void cleanUpMonkey(String why) throws Exception {
118 if (monkey != null && !monkey.isStopped()) {
119 monkey.stop(why);
120 monkey.waitForStop();
121 }
122 }
123
124 protected IntegrationTestingUtility getTestingUtil(Configuration conf) {
125 if (this.util == null) {
126 if (conf == null) {
127 this.util = new IntegrationTestingUtility();
128 } else {
129 this.util = new IntegrationTestingUtility(conf);
130 }
131 }
132 return util;
133 }
134
135 public abstract void setUpCluster() throws Exception;
136
137 public void cleanUpCluster() throws Exception {
138 LOG.debug("Restoring the cluster");
139 util.restoreCluster();
140 LOG.debug("Done restoring the cluster");
141 }
142
143 public abstract int runTestFromCommandLine() throws Exception;
144
145 public abstract String getTablename();
146
147 protected abstract Set<String> getColumnFamilies();
148 }