View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Tests around regionserver shutdown and abort
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      // make sure we have multiple blocks so that the client does not prefetch all block locations
80      conf.set("dfs.blocksize", Long.toString(100 * 1024));
81      // prefetch the first block
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    * Test that a regionserver is able to abort properly, even when a coprocessor
104    * throws an exception in preStopRegionServer().
105    */
106   @Test
107   public void testAbortFromRPC() throws Exception {
108     TableName tableName = TableName.valueOf("testAbortFromRPC");
109     // create a test table
110     HTable table = testUtil.createTable(tableName, FAMILY_BYTES);
111 
112     // write some edits
113     testUtil.loadTable(table, FAMILY_BYTES);
114     LOG.info("Wrote data");
115     // force a flush
116     cluster.flushcache(tableName);
117     LOG.info("Flushed table");
118 
119     // delete a store file from the table region
120     HRegion firstRegion = cluster.findRegionsForTable(tableName).get(0);
121 
122     // aborting from region
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     // move the store file away
129     // we will still be able to read the first block, since the location was pre-fetched on open
130     // but attempts to read subsequent blocks will fail
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     // start a scan, this should trigger a regionserver abort
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     // should have triggered an abort due to FileNotFoundException
146 
147     // verify that the regionserver is stopped
148     assertTrue(firstRegion.getRegionServerServices().isAborted());
149     assertTrue(firstRegion.getRegionServerServices().isStopped());
150   }
151 
152   /**
153    * Test that a coprocessor is able to override a normal regionserver stop request.
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     // regionserver should have failed to stop due to coprocessor
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    * Throws an exception during store file refresh in order to trigger a regionserver abort.
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       // forced when called through RegionScannerImpl.handleFileNotFound()
204       if (force) {
205         throw new IOException("Failing file refresh for testing");
206       }
207       return super.refreshStoreFiles(force);
208     }
209   }
210 }