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.io.IOException;
22 import java.util.Properties;
23 import java.util.Set;
24
25 import org.apache.commons.cli.CommandLine;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.chaos.factories.MonkeyFactory;
31 import org.apache.hadoop.hbase.chaos.monkies.ChaosMonkey;
32 import org.apache.hadoop.hbase.util.AbstractHBaseTool;
33 import org.junit.After;
34 import org.junit.Before;
35
36
37
38
39
40
41
42
43
44 public abstract class IntegrationTestBase extends AbstractHBaseTool {
45 public static final String LONG_OPT = "monkey";
46 public static final String CHAOS_MONKEY_PROPS = "monkeyProps";
47 private static final Log LOG = LogFactory.getLog(IntegrationTestBase.class);
48
49 protected IntegrationTestingUtility util;
50 protected ChaosMonkey monkey;
51 protected String monkeyToUse;
52 protected Properties monkeyProps;
53
54 public IntegrationTestBase() {
55 this(null);
56 }
57
58 public IntegrationTestBase(String monkeyToUse) {
59 this.monkeyToUse = monkeyToUse;
60 }
61
62 @Override
63 protected void addOptions() {
64 addOptWithArg("m", LONG_OPT, "Which chaos monkey to run");
65 addOptWithArg(CHAOS_MONKEY_PROPS, "The properties file for specifying chaos "
66 + "monkey properties.");
67 }
68
69
70
71
72
73
74
75
76 protected void processBaseOptions(CommandLine cmd) {
77 if (cmd.hasOption(LONG_OPT)) {
78 monkeyToUse = cmd.getOptionValue(LONG_OPT);
79 }
80 monkeyProps = new Properties();
81 if (cmd.hasOption(CHAOS_MONKEY_PROPS)) {
82 String chaosMonkeyPropsFile = cmd.getOptionValue(CHAOS_MONKEY_PROPS);
83 if (StringUtils.isNotEmpty(chaosMonkeyPropsFile)) {
84 try {
85 monkeyProps.load(this.getClass().getClassLoader()
86 .getResourceAsStream(chaosMonkeyPropsFile));
87 } catch (IOException e) {
88 LOG.warn(e);
89 System.exit(EXIT_FAILURE);
90 }
91 }
92 }
93 }
94
95 @Override
96 protected void processOptions(CommandLine cmd) {
97 processBaseOptions(cmd);
98 }
99
100 @Override
101 public Configuration getConf() {
102 Configuration c = super.getConf();
103 if (c == null && util != null) {
104 conf = util.getConfiguration();
105 c = conf;
106 }
107 return c;
108 }
109
110 @Override
111 protected int doWork() throws Exception {
112 setUp();
113 int result = -1;
114 try {
115 result = runTestFromCommandLine();
116 } finally {
117 cleanUp();
118 }
119
120 return result;
121 }
122
123 @Before
124 public void setUp() throws Exception {
125 setUpCluster();
126 setUpMonkey();
127 }
128
129 @After
130 public void cleanUp() throws Exception {
131 cleanUpMonkey();
132 cleanUpCluster();
133 }
134
135 public void setUpMonkey() throws Exception {
136 util = getTestingUtil(getConf());
137 MonkeyFactory fact = MonkeyFactory.getFactory(monkeyToUse);
138 if (fact == null) {
139 fact = getDefaultMonkeyFactory();
140 }
141 monkey = fact.setUtil(util)
142 .setTableName(getTablename())
143 .setProperties(monkeyProps)
144 .setColumnFamilies(getColumnFamilies()).build();
145 startMonkey();
146 }
147
148 protected void startMonkey() throws Exception {
149 monkey.start();
150 }
151
152 public void cleanUpMonkey() throws Exception {
153 cleanUpMonkey("Ending test");
154 }
155
156 protected void cleanUpMonkey(String why) throws Exception {
157 if (monkey != null && !monkey.isStopped()) {
158 monkey.stop(why);
159 monkey.waitForStop();
160 }
161 }
162
163 protected IntegrationTestingUtility getTestingUtil(Configuration conf) {
164 if (this.util == null) {
165 if (conf == null) {
166 this.util = new IntegrationTestingUtility();
167 this.setConf(util.getConfiguration());
168 } else {
169 this.util = new IntegrationTestingUtility(conf);
170 }
171 }
172 return util;
173 }
174
175 protected MonkeyFactory getDefaultMonkeyFactory() {
176 return MonkeyFactory.getFactory(
177 util.isDistributedCluster() ? MonkeyFactory.CALM : MonkeyFactory.SLOW_DETERMINISTIC);
178 }
179
180 public abstract void setUpCluster() throws Exception;
181
182 public void cleanUpCluster() throws Exception {
183 if (!util.isDistributedCluster() || (monkey != null && monkey.isDestructive())) {
184 LOG.debug("Restoring the cluster");
185 util.restoreCluster();
186 LOG.debug("Done restoring the cluster");
187 }
188 }
189
190 public abstract int runTestFromCommandLine() throws Exception;
191
192 public abstract String getTablename();
193
194 protected abstract Set<String> getColumnFamilies();
195 }