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