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 = getDefaultMonkeyFactory();
105 }
106 monkey = fact.setUtil(util)
107 .setTableName(getTablename())
108 .setColumnFamilies(getColumnFamilies()).build();
109 monkey.start();
110 }
111
112 public void cleanUpMonkey() throws Exception {
113 cleanUpMonkey("Ending test");
114 }
115
116 protected void cleanUpMonkey(String why) throws Exception {
117 if (monkey != null && !monkey.isStopped()) {
118 monkey.stop(why);
119 monkey.waitForStop();
120 }
121 }
122
123 protected IntegrationTestingUtility getTestingUtil(Configuration conf) {
124 if (this.util == null) {
125 if (conf == null) {
126 this.util = new IntegrationTestingUtility();
127 } else {
128 this.util = new IntegrationTestingUtility(conf);
129 }
130 }
131 return util;
132 }
133
134 protected MonkeyFactory getDefaultMonkeyFactory() {
135 return MonkeyFactory.getFactory(
136 util.isDistributedCluster() ? MonkeyFactory.CALM : MonkeyFactory.SLOW_DETERMINISTIC);
137 }
138
139 public abstract void setUpCluster() throws Exception;
140
141 public void cleanUpCluster() throws Exception {
142 LOG.debug("Restoring the cluster");
143 util.restoreCluster();
144 LOG.debug("Done restoring the cluster");
145 }
146
147 public abstract int runTestFromCommandLine() throws Exception;
148
149 public abstract String getTablename();
150
151 protected abstract Set<String> getColumnFamilies();
152 }