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
46 public static final String NO_CLUSTER_CLEANUP_LONG_OPT = "noClusterCleanUp";
47 public static final String MONKEY_LONG_OPT = "monkey";
48 public static final String CHAOS_MONKEY_PROPS = "monkeyProps";
49 private static final Log LOG = LogFactory.getLog(IntegrationTestBase.class);
50
51 protected IntegrationTestingUtility util;
52 protected ChaosMonkey monkey;
53 protected String monkeyToUse;
54 protected Properties monkeyProps;
55 protected boolean noClusterCleanUp = false;
56
57 public IntegrationTestBase() {
58 this(null);
59 }
60
61 public IntegrationTestBase(String monkeyToUse) {
62 this.monkeyToUse = monkeyToUse;
63 }
64
65 @Override
66 protected void addOptions() {
67 addOptWithArg("m", MONKEY_LONG_OPT, "Which chaos monkey to run");
68 addOptNoArg("ncc", NO_CLUSTER_CLEANUP_LONG_OPT,
69 "Don't clean up the cluster at the end");
70 addOptWithArg(CHAOS_MONKEY_PROPS, "The properties file for specifying chaos "
71 + "monkey properties.");
72 }
73
74
75
76
77
78
79
80
81 protected void processBaseOptions(CommandLine cmd) {
82 if (cmd.hasOption(MONKEY_LONG_OPT)) {
83 monkeyToUse = cmd.getOptionValue(MONKEY_LONG_OPT);
84 }
85 if (cmd.hasOption(NO_CLUSTER_CLEANUP_LONG_OPT)) {
86 noClusterCleanUp = true;
87 }
88 monkeyProps = new Properties();
89 if (cmd.hasOption(CHAOS_MONKEY_PROPS)) {
90 String chaosMonkeyPropsFile = cmd.getOptionValue(CHAOS_MONKEY_PROPS);
91 if (StringUtils.isNotEmpty(chaosMonkeyPropsFile)) {
92 try {
93 monkeyProps.load(this.getClass().getClassLoader()
94 .getResourceAsStream(chaosMonkeyPropsFile));
95 } catch (IOException e) {
96 LOG.warn(e);
97 System.exit(EXIT_FAILURE);
98 }
99 }
100 }
101 }
102
103 @Override
104 protected void processOptions(CommandLine cmd) {
105 processBaseOptions(cmd);
106 }
107
108 @Override
109 public Configuration getConf() {
110 Configuration c = super.getConf();
111 if (c == null && util != null) {
112 conf = util.getConfiguration();
113 c = conf;
114 }
115 return c;
116 }
117
118 @Override
119 protected int doWork() throws Exception {
120 ChoreService choreService = null;
121
122
123
124
125 final ScheduledChore authChore = AuthUtil.getAuthChore(conf);
126 if (authChore != null) {
127 choreService = new ChoreService("INTEGRATION_TEST");
128 choreService.scheduleChore(authChore);
129 }
130
131 setUp();
132 int result = -1;
133 try {
134 result = runTestFromCommandLine();
135 } finally {
136 cleanUp();
137 }
138
139 if (choreService != null) {
140 choreService.shutdown();
141 }
142
143 return result;
144 }
145
146 @Before
147 public void setUp() throws Exception {
148 setUpCluster();
149 setUpMonkey();
150 }
151
152 @After
153 public void cleanUp() throws Exception {
154 cleanUpMonkey();
155 cleanUpCluster();
156 }
157
158 public void setUpMonkey() throws Exception {
159 util = getTestingUtil(getConf());
160 MonkeyFactory fact = MonkeyFactory.getFactory(monkeyToUse);
161 if (fact == null) {
162 fact = getDefaultMonkeyFactory();
163 }
164 monkey = fact.setUtil(util)
165 .setTableName(getTablename())
166 .setProperties(monkeyProps)
167 .setColumnFamilies(getColumnFamilies()).build();
168 startMonkey();
169 }
170
171 protected MonkeyFactory getDefaultMonkeyFactory() {
172
173 return MonkeyFactory.getFactory(
174 util.isDistributedCluster() ? MonkeyFactory.CALM : MonkeyFactory.SLOW_DETERMINISTIC);
175 }
176
177 protected void startMonkey() throws Exception {
178 monkey.start();
179 }
180
181 public void cleanUpMonkey() throws Exception {
182 cleanUpMonkey("Ending test");
183 }
184
185 protected void cleanUpMonkey(String why) throws Exception {
186 if (monkey != null && !monkey.isStopped()) {
187 monkey.stop(why);
188 monkey.waitForStop();
189 }
190 }
191
192 protected IntegrationTestingUtility getTestingUtil(Configuration conf) {
193 if (this.util == null) {
194 if (conf == null) {
195 this.util = new IntegrationTestingUtility();
196 this.setConf(util.getConfiguration());
197 } else {
198 this.util = new IntegrationTestingUtility(conf);
199 }
200 }
201 return util;
202 }
203
204 public abstract void setUpCluster() throws Exception;
205
206 public void cleanUpCluster() throws Exception {
207 if (util.isDistributedCluster() && (monkey == null || !monkey.isDestructive())) {
208 noClusterCleanUp = true;
209 }
210 if (noClusterCleanUp) {
211 LOG.debug("noClusterCleanUp is set, skip restoring the cluster");
212 return;
213 }
214 LOG.debug("Restoring the cluster");
215 util.restoreCluster();
216 LOG.debug("Done restoring the cluster");
217 }
218
219 public abstract int runTestFromCommandLine() throws Exception;
220
221
222
223
224
225 public abstract TableName getTablename();
226
227
228
229
230
231 protected abstract Set<String> getColumnFamilies();
232 }