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  package org.apache.hadoop.hbase.regionserver;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.fail;
22  
23  import java.io.IOException;
24  import java.util.List;
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.hbase.CoordinatedStateManager;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.Waiter.ExplainingPredicate;
34  import org.apache.hadoop.hbase.YouAreDeadException;
35  import org.apache.hadoop.hbase.client.Get;
36  import org.apache.hadoop.hbase.client.Put;
37  import org.apache.hadoop.hbase.client.Table;
38  import org.apache.hadoop.hbase.testclassification.LargeTests;
39  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
42  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
43  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
44  import org.junit.After;
45  import org.junit.Before;
46  import org.junit.Test;
47  import org.junit.experimental.categories.Category;
48  
49  /**
50   * This testcase is used to ensure that the compaction marker will fail a compaction if the RS is
51   * already dead. It can not eliminate FNFE when scanning but it does reduce the possibility a lot.
52   */
53  @Category({ RegionServerTests.class, LargeTests.class })
54  public class TestCompactionInDeadRegionServer {
55    private static final Log LOG = LogFactory.getLog(TestCompactionInDeadRegionServer.class);
56  
57    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
58  
59    private static final TableName TABLE_NAME = TableName.valueOf("test");
60  
61    private static final byte[] CF = Bytes.toBytes("cf");
62  
63    private static final byte[] CQ = Bytes.toBytes("cq");
64  
65    public static final class IgnoreYouAreDeadRS extends HRegionServer {
66  
67      public IgnoreYouAreDeadRS(Configuration conf) throws IOException, InterruptedException {
68        super(conf);
69      }
70  
71      public IgnoreYouAreDeadRS(Configuration conf, CoordinatedStateManager csm)
72          throws IOException, InterruptedException {
73        super(conf, csm);
74      }
75  
76      @Override
77      protected void tryRegionServerReport(long reportStartTime, long reportEndTime)
78          throws IOException {
79        try {
80          super.tryRegionServerReport(reportStartTime, reportEndTime);
81        } catch (YouAreDeadException e) {
82          // ignore, do not abort
83        }
84      }
85    }
86  
87    @Before
88    public void setUp() throws Exception {
89      UTIL.getConfiguration().setInt(HConstants.ZK_SESSION_TIMEOUT, 2000);
90      UTIL.getConfiguration().setClass(HConstants.REGION_SERVER_IMPL, IgnoreYouAreDeadRS.class,
91        HRegionServer.class);
92      UTIL.startMiniCluster(2);
93      Table table = UTIL.createTable(TABLE_NAME, CF);
94      for (int i = 0; i < 10; i++) {
95        table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
96      }
97      UTIL.getHBaseAdmin().flush(TABLE_NAME);
98      for (int i = 10; i < 20; i++) {
99        table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
100     }
101     UTIL.getHBaseAdmin().flush(TABLE_NAME);
102   }
103 
104   @After
105   public void tearDown() throws Exception {
106     UTIL.shutdownMiniCluster();
107   }
108 
109   @Test
110   public void test() throws Exception {
111     HRegionServer regionSvr = UTIL.getRSForFirstRegionInTable(TABLE_NAME);
112     Region region = regionSvr.getOnlineRegions(TABLE_NAME).get(0);
113     String regName = region.getRegionInfo().getEncodedName();
114     List<Region> metaRegs = regionSvr.getOnlineRegions(TableName.META_TABLE_NAME);
115     if (metaRegs != null && !metaRegs.isEmpty()) {
116       LOG.info("meta is on the same server: " + regionSvr);
117       // when region is on same server as hbase:meta, reassigning meta would abort the server
118       // since WAL is broken.
119       // so the region is moved to a different server
120       HRegionServer otherRs = UTIL.getOtherRegionServer(regionSvr);
121       UTIL.moveRegionAndWait(region.getRegionInfo(), otherRs.getServerName());
122       LOG.info("Moved region: " + regName + " to " + otherRs.getServerName());
123     }
124     final HRegionServer rsToSuspend = UTIL.getRSForFirstRegionInTable(TABLE_NAME);
125     region = (HRegion) rsToSuspend.getOnlineRegions(TABLE_NAME).get(0);
126     ZooKeeperWatcher watcher = UTIL.getZooKeeperWatcher();
127     watcher.getRecoverableZooKeeper()
128         .delete(ZKUtil.joinZNode(watcher.rsZNode, rsToSuspend.getServerName().toString()), -1);
129     UTIL.waitFor(60000, 1000, new ExplainingPredicate<Exception>() {
130 
131       @Override
132       public boolean evaluate() throws Exception {
133         for (RegionServerThread thread : UTIL.getHBaseCluster().getRegionServerThreads()) {
134           HRegionServer rs = thread.getRegionServer();
135           if (rs != rsToSuspend) {
136             return !rs.getOnlineRegions(TABLE_NAME).isEmpty();
137           }
138         }
139         return false;
140       }
141 
142       @Override
143       public String explainFailure() throws Exception {
144         return "The region for " + TABLE_NAME + " is still on " + rsToSuspend.getServerName();
145       }
146     });
147     try {
148       ((HRegion)region).compact(true);
149       fail("Should fail as our wal file has already been closed, " +
150           "and walDir has also been renamed");
151     } catch (Exception e) {
152       LOG.debug("expected exception: ", e);
153     }
154     Table table = UTIL.getConnection().getTable(TABLE_NAME);
155     // should not hit FNFE
156     for (int i = 0; i < 20; i++) {
157       assertEquals(i, Bytes.toInt(table.get(new Get(Bytes.toBytes(i))).getValue(CF, CQ)));
158     }
159   }
160 }