1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.fs.FileSystem;
25 import org.apache.hadoop.fs.Path;
26 import org.apache.hadoop.hbase.HBaseTestingUtility;
27 import org.apache.hadoop.hbase.HConstants;
28 import org.apache.hadoop.hbase.HRegionInfo;
29 import org.apache.hadoop.hbase.HTableDescriptor;
30 import org.apache.hadoop.hbase.MiniHBaseCluster;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.client.Admin;
33 import org.apache.hadoop.hbase.client.HTable;
34 import org.apache.hadoop.hbase.client.Result;
35 import org.apache.hadoop.hbase.client.ResultScanner;
36 import org.apache.hadoop.hbase.client.Scan;
37 import org.apache.hadoop.hbase.coprocessor.BaseRegionServerObserver;
38 import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
39 import org.apache.hadoop.hbase.coprocessor.ObserverContext;
40 import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment;
41 import org.apache.hadoop.hbase.testclassification.MediumTests;
42 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
43 import org.apache.hadoop.hbase.util.Bytes;
44 import org.apache.hadoop.hbase.util.JVMClusterUtil;
45 import org.apache.hadoop.hbase.wal.WAL;
46 import org.apache.hadoop.hdfs.DFSConfigKeys;
47 import org.apache.hadoop.hdfs.MiniDFSCluster;
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.experimental.categories.Category;
52
53 import java.io.IOException;
54 import java.util.Collection;
55
56 import static org.junit.Assert.assertFalse;
57 import static org.junit.Assert.assertTrue;
58
59
60
61
62 @Category({RegionServerTests.class, MediumTests.class})
63 public class TestRegionServerAbort {
64 private static final byte[] FAMILY_BYTES = Bytes.toBytes("f");
65
66 private static final Log LOG = LogFactory.getLog(TestRegionServerAbort.class);
67
68 private HBaseTestingUtility testUtil;
69 private Configuration conf;
70 private MiniDFSCluster dfsCluster;
71 private MiniHBaseCluster cluster;
72
73 @Before
74 public void setup() throws Exception {
75 testUtil = new HBaseTestingUtility();
76 conf = testUtil.getConfiguration();
77 conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
78 StopBlockingRegionObserver.class.getName());
79
80 conf.set("dfs.blocksize", Long.toString(100 * 1024));
81
82 conf.set(DFSConfigKeys.DFS_CLIENT_READ_PREFETCH_SIZE_KEY, Long.toString(100 * 1024));
83 conf.set(HConstants.REGION_IMPL, ErrorThrowingHRegion.class.getName());
84
85 testUtil.startMiniZKCluster();
86 dfsCluster = testUtil.startMiniDFSCluster(2);
87 cluster = testUtil.startMiniHBaseCluster(1, 2);
88 }
89
90 @After
91 public void tearDown() throws Exception {
92 for (JVMClusterUtil.RegionServerThread t : cluster.getRegionServerThreads()) {
93 HRegionServer rs = t.getRegionServer();
94 RegionServerCoprocessorHost cpHost = rs.getRegionServerCoprocessorHost();
95 StopBlockingRegionObserver cp = (StopBlockingRegionObserver)
96 cpHost.findCoprocessor(StopBlockingRegionObserver.class.getName());
97 cp.setStopAllowed(true);
98 }
99 testUtil.shutdownMiniCluster();
100 }
101
102
103
104
105
106 @Test
107 public void testAbortFromRPC() throws Exception {
108 TableName tableName = TableName.valueOf("testAbortFromRPC");
109
110 HTable table = testUtil.createTable(tableName, FAMILY_BYTES);
111
112
113 testUtil.loadTable(table, FAMILY_BYTES);
114 LOG.info("Wrote data");
115
116 cluster.flushcache(tableName);
117 LOG.info("Flushed table");
118
119
120 HRegion firstRegion = cluster.findRegionsForTable(tableName).get(0);
121
122
123 HRegionFileSystem regionFS = firstRegion.getRegionFileSystem();
124 Collection<StoreFileInfo> storeFileInfos = regionFS.getStoreFiles(FAMILY_BYTES);
125 assertFalse(storeFileInfos.isEmpty());
126 StoreFileInfo firstStoreFile = storeFileInfos.iterator().next();
127
128
129
130
131 LOG.info("Moving store file " + firstStoreFile.getPath());
132 FileSystem fs = regionFS.getFileSystem();
133 Path tmpdir = new Path("/tmp");
134 fs.mkdirs(tmpdir);
135 assertTrue(fs.rename(firstStoreFile.getPath(),
136 new Path(tmpdir, firstStoreFile.getPath().getName())));
137
138
139 ResultScanner scanner = table.getScanner(new Scan());
140 int count = 0;
141 for (Result f : scanner) {
142 count++;
143 }
144 LOG.info("Finished scan with " + count + " results");
145
146
147
148 assertTrue(firstRegion.getRegionServerServices().isAborted());
149 assertTrue(firstRegion.getRegionServerServices().isStopped());
150 }
151
152
153
154
155 @Test
156 public void testStopOverrideFromCoprocessor() throws Exception {
157 Admin admin = testUtil.getHBaseAdmin();
158 HRegionServer regionserver = cluster.getRegionServer(0);
159 admin.stopRegionServer(regionserver.getServerName().getHostAndPort());
160
161
162 assertFalse(cluster.getRegionServer(0).isAborted());
163 assertFalse(cluster.getRegionServer(0).isStopped());
164 }
165
166 public static class StopBlockingRegionObserver extends BaseRegionServerObserver {
167 private boolean stopAllowed;
168
169 @Override
170 public void preStopRegionServer(ObserverContext<RegionServerCoprocessorEnvironment> env)
171 throws IOException {
172 if (!stopAllowed) {
173 throw new IOException("Stop not allowed");
174 }
175 }
176
177 public void setStopAllowed(boolean allowed) {
178 this.stopAllowed = allowed;
179 }
180
181 public boolean isStopAllowed() {
182 return stopAllowed;
183 }
184 }
185
186
187
188
189 public static class ErrorThrowingHRegion extends HRegion {
190 public ErrorThrowingHRegion(Path tableDir, WAL wal, FileSystem fs, Configuration confParam,
191 HRegionInfo regionInfo, HTableDescriptor htd,
192 RegionServerServices rsServices) {
193 super(tableDir, wal, fs, confParam, regionInfo, htd, rsServices);
194 }
195
196 public ErrorThrowingHRegion(HRegionFileSystem fs, WAL wal, Configuration confParam,
197 HTableDescriptor htd, RegionServerServices rsServices) {
198 super(fs, wal, confParam, htd, rsServices);
199 }
200
201 @Override
202 protected boolean refreshStoreFiles(boolean force) throws IOException {
203
204 if (force) {
205 throw new IOException("Failing file refresh for testing");
206 }
207 return super.refreshStoreFiles(force);
208 }
209 }
210 }