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 static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.when;
25  
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.List;
30  
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.fs.FileSystem;
33  import org.apache.hadoop.fs.Path;
34  import org.apache.hadoop.hbase.Cell;
35  import org.apache.hadoop.hbase.CellUtil;
36  import org.apache.hadoop.hbase.HBaseTestingUtility;
37  import org.apache.hadoop.hbase.HColumnDescriptor;
38  import org.apache.hadoop.hbase.HConstants;
39  import org.apache.hadoop.hbase.HRegionInfo;
40  import org.apache.hadoop.hbase.HTableDescriptor;
41  import org.apache.hadoop.hbase.testclassification.SmallTests;
42  import org.apache.hadoop.hbase.Stoppable;
43  import org.apache.hadoop.hbase.TableName;
44  import org.apache.hadoop.hbase.client.Durability;
45  import org.apache.hadoop.hbase.client.Get;
46  import org.apache.hadoop.hbase.client.Put;
47  import org.apache.hadoop.hbase.client.Result;
48  import org.apache.hadoop.hbase.wal.WALFactory;
49  import org.apache.hadoop.hbase.util.Bytes;
50  import org.apache.hadoop.hbase.util.FSUtils;
51  import org.apache.hadoop.hbase.util.StoppableImplementation;
52  import org.junit.Assert;
53  import org.junit.Before;
54  import org.junit.Test;
55  import org.junit.experimental.categories.Category;
56  
57  @Category(SmallTests.class)
58  public class TestStoreFileRefresherChore {
59  
60    private HBaseTestingUtility TEST_UTIL;
61    private Path testDir;
62  
63    @Before
64    public void setUp() throws IOException {
65      TEST_UTIL = new HBaseTestingUtility();
66      testDir = TEST_UTIL.getDataTestDir("TestStoreFileRefresherChore");
67      FSUtils.setRootDir(TEST_UTIL.getConfiguration(), testDir);
68    }
69  
70    private HTableDescriptor getTableDesc(TableName tableName, byte[]... families) {
71      HTableDescriptor htd = new HTableDescriptor(tableName);
72      for (byte[] family : families) {
73        HColumnDescriptor hcd = new HColumnDescriptor(family);
74        // Set default to be three versions.
75        hcd.setMaxVersions(Integer.MAX_VALUE);
76        htd.addFamily(hcd);
77      }
78      return htd;
79    }
80  
81    static class FailingHRegionFileSystem extends HRegionFileSystem {
82      boolean fail = false;
83      FailingHRegionFileSystem(Configuration conf, FileSystem fs, Path tableDir, HRegionInfo regionInfo) {
84        super(conf, fs, tableDir, regionInfo);
85      }
86  
87      @Override
88      public Collection<StoreFileInfo> getStoreFiles(String familyName) throws IOException {
89        if (fail) {
90          throw new IOException("simulating FS failure");
91        }
92        return super.getStoreFiles(familyName);
93      }
94    }
95  
96    private Region initHRegion(HTableDescriptor htd, byte[] startKey, byte[] stopKey, int replicaId)
97        throws IOException {
98      Configuration conf = TEST_UTIL.getConfiguration();
99      Path tableDir = FSUtils.getTableDir(testDir, htd.getTableName());
100 
101     HRegionInfo info = new HRegionInfo(htd.getTableName(), startKey, stopKey, false, 0, replicaId);
102 
103     HRegionFileSystem fs = new FailingHRegionFileSystem(conf, tableDir.getFileSystem(conf), tableDir,
104       info);
105     final Configuration walConf = new Configuration(conf);
106     FSUtils.setRootDir(walConf, tableDir);
107     final WALFactory wals = new WALFactory(walConf, null, "log_" + replicaId);
108     HRegion region =
109         new HRegion(fs, wals.getWAL(info.getEncodedNameAsBytes(), info.getTable().getNamespace()),
110             conf, htd, null);
111 
112     region.initialize();
113 
114     return region;
115   }
116 
117   private void putData(Region region, int startRow, int numRows, byte[] qf, byte[]... families)
118       throws IOException {
119     for (int i = startRow; i < startRow + numRows; i++) {
120       Put put = new Put(Bytes.toBytes("" + i));
121       put.setDurability(Durability.SKIP_WAL);
122       for (byte[] family : families) {
123         put.add(family, qf, null);
124       }
125       region.put(put);
126     }
127   }
128 
129   private void verifyData(Region newReg, int startRow, int numRows, byte[] qf, byte[]... families)
130       throws IOException {
131     for (int i = startRow; i < startRow + numRows; i++) {
132       byte[] row = Bytes.toBytes("" + i);
133       Get get = new Get(row);
134       for (byte[] family : families) {
135         get.addColumn(family, qf);
136       }
137       Result result = newReg.get(get);
138       Cell[] raw = result.rawCells();
139       assertEquals(families.length, result.size());
140       for (int j = 0; j < families.length; j++) {
141         assertTrue(CellUtil.matchingRow(raw[j], row));
142         assertTrue(CellUtil.matchingFamily(raw[j], families[j]));
143         assertTrue(CellUtil.matchingQualifier(raw[j], qf));
144       }
145     }
146   }
147 
148   static class StaleStorefileRefresherChore extends StorefileRefresherChore {
149     boolean isStale = false;
150     public StaleStorefileRefresherChore(int period, HRegionServer regionServer,
151         Stoppable stoppable) {
152       super(period, false, regionServer, stoppable);
153     }
154     @Override
155     protected boolean isRegionStale(String encodedName, long time) {
156       return isStale;
157     }
158   }
159 
160   @Test
161   public void testIsStale() throws IOException {
162     int period = 0;
163     byte[][] families = new byte[][] {Bytes.toBytes("cf")};
164     byte[] qf = Bytes.toBytes("cq");
165 
166     HRegionServer regionServer = mock(HRegionServer.class);
167     List<Region> regions = new ArrayList<Region>();
168     when(regionServer.getOnlineRegionsLocalContext()).thenReturn(regions);
169     when(regionServer.getConfiguration()).thenReturn(TEST_UTIL.getConfiguration());
170 
171     HTableDescriptor htd = getTableDesc(TableName.valueOf("testIsStale"), families);
172     htd.setRegionReplication(2);
173 
174     Region primary = initHRegion(htd, HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW, 0);
175     Region replica1 = initHRegion(htd, HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW, 1);
176     regions.add(primary);
177     regions.add(replica1);
178 
179     StaleStorefileRefresherChore chore = new StaleStorefileRefresherChore(period, regionServer, new StoppableImplementation());
180 
181     // write some data to primary and flush
182     putData(primary, 0, 100, qf, families);
183     primary.flush(true);
184     verifyData(primary, 0, 100, qf, families);
185 
186     try {
187       verifyData(replica1, 0, 100, qf, families);
188       Assert.fail("should have failed");
189     } catch(AssertionError ex) {
190       // expected
191     }
192     chore.chore();
193     verifyData(replica1, 0, 100, qf, families);
194 
195     // simulate an fs failure where we cannot refresh the store files for the replica
196     ((FailingHRegionFileSystem)((HRegion)replica1).getRegionFileSystem()).fail = true;
197 
198     // write some more data to primary and flush
199     putData(primary, 100, 100, qf, families);
200     primary.flush(true);
201     verifyData(primary, 0, 200, qf, families);
202 
203     chore.chore(); // should not throw ex, but we cannot refresh the store files
204 
205     verifyData(replica1, 0, 100, qf, families);
206     try {
207       verifyData(replica1, 100, 100, qf, families);
208       Assert.fail("should have failed");
209     } catch(AssertionError ex) {
210       // expected
211     }
212 
213     chore.isStale = true;
214     chore.chore(); //now after this, we cannot read back any value
215     try {
216       verifyData(replica1, 0, 100, qf, families);
217       Assert.fail("should have failed with IOException");
218     } catch(IOException ex) {
219       // expected
220     }
221   }
222 }