1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.client.HConnectionManager;
31 import org.apache.hadoop.hbase.client.HTable;
32 import org.apache.hadoop.hbase.util.FSUtils;
33 import org.apache.hadoop.hdfs.MiniDFSCluster;
34 import org.apache.hadoop.util.ReflectionUtils;
35
36
37
38
39
40
41 public abstract class HBaseClusterTestCase extends HBaseTestCase {
42 private static final Log LOG = LogFactory.getLog(HBaseClusterTestCase.class);
43 public MiniHBaseCluster cluster;
44 protected MiniDFSCluster dfsCluster;
45 protected MiniZooKeeperCluster zooKeeperCluster;
46 protected int regionServers;
47 protected boolean startDfs;
48 private boolean openMetaTable = true;
49
50
51 public HBaseClusterTestCase() {
52 this(1);
53 }
54
55
56
57
58
59
60
61 public HBaseClusterTestCase(int regionServers) {
62 this(regionServers, true);
63 }
64
65
66
67
68
69
70
71
72
73
74 public HBaseClusterTestCase(int regionServers, boolean startDfs) {
75 super();
76 this.startDfs = startDfs;
77 this.regionServers = regionServers;
78 }
79
80 protected void setOpenMetaTable(boolean val) {
81 openMetaTable = val;
82 }
83
84
85
86
87
88
89 protected void preHBaseClusterSetup() throws Exception {
90
91 }
92
93
94
95
96 protected void hBaseClusterSetup() throws Exception {
97 File testDir = new File(getUnitTestdir(getName()).toString());
98
99
100
101 this.zooKeeperCluster = new MiniZooKeeperCluster();
102 int clientPort = this.zooKeeperCluster.startup(testDir);
103 conf.set("hbase.zookeeper.property.clientPort", Integer.toString(clientPort));
104
105
106 this.cluster = new MiniHBaseCluster(conf, regionServers);
107
108 if (openMetaTable) {
109
110 new HTable(conf, HConstants.META_TABLE_NAME);
111 }
112 }
113
114
115
116
117 protected void postHBaseClusterSetup() throws Exception {
118
119 }
120
121 @Override
122 protected void setUp() throws Exception {
123 try {
124 if (this.startDfs) {
125
126 this.dfsCluster = new MiniDFSCluster(0, this.conf, 2, true, true, true,
127 null, null, null, null);
128
129
130
131 FileSystem filesystem = dfsCluster.getFileSystem();
132 conf.set("fs.defaultFS", filesystem.getUri().toString());
133 Path parentdir = filesystem.getHomeDirectory();
134 conf.set(HConstants.HBASE_DIR, parentdir.toString());
135 filesystem.mkdirs(parentdir);
136 FSUtils.setVersion(filesystem, parentdir);
137 }
138
139
140
141 super.setUp();
142
143
144 preHBaseClusterSetup();
145
146
147 hBaseClusterSetup();
148
149
150 postHBaseClusterSetup();
151 } catch (Exception e) {
152 LOG.error("Exception in setup!", e);
153 if (cluster != null) {
154 cluster.shutdown();
155 }
156 if (zooKeeperCluster != null) {
157 zooKeeperCluster.shutdown();
158 }
159 if (dfsCluster != null) {
160 shutdownDfs(dfsCluster);
161 }
162 throw e;
163 }
164 }
165
166 @Override
167 protected void tearDown() throws Exception {
168 if (!openMetaTable) {
169
170 new HTable(conf, HConstants.META_TABLE_NAME);
171 }
172 super.tearDown();
173 try {
174 HConnectionManager.deleteConnectionInfo(conf, true);
175 if (this.cluster != null) {
176 try {
177 this.cluster.shutdown();
178 } catch (Exception e) {
179 LOG.warn("Closing mini dfs", e);
180 }
181 try {
182 this.zooKeeperCluster.shutdown();
183 } catch (IOException e) {
184 LOG.warn("Shutting down ZooKeeper cluster", e);
185 }
186 }
187 if (startDfs) {
188 shutdownDfs(dfsCluster);
189 }
190 } catch (Exception e) {
191 LOG.error(e);
192 }
193
194
195 }
196
197
198
199
200
201
202
203 public void threadDumpingJoin() {
204 if (this.cluster.getRegionServerThreads() != null) {
205 for(Thread t: this.cluster.getRegionServerThreads()) {
206 threadDumpingJoin(t);
207 }
208 }
209 threadDumpingJoin(this.cluster.getMaster());
210 }
211
212 protected void threadDumpingJoin(final Thread t) {
213 if (t == null) {
214 return;
215 }
216 long startTime = System.currentTimeMillis();
217 while (t.isAlive()) {
218 try {
219 Thread.sleep(1000);
220 } catch (InterruptedException e) {
221 LOG.info("Continuing...", e);
222 }
223 if (System.currentTimeMillis() - startTime > 60000) {
224 startTime = System.currentTimeMillis();
225 ReflectionUtils.printThreadInfo(new PrintWriter(System.out),
226 "Automatic Stack Trace every 60 seconds waiting on " +
227 t.getName());
228 }
229 }
230 }
231 }