001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022import static org.junit.Assert.fail;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.when;
025
026import java.io.IOException;
027import java.util.ArrayList;
028import java.util.Arrays;
029import java.util.List;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.fs.FileSystem;
032import org.apache.hadoop.fs.Path;
033import org.apache.hadoop.hbase.Cell;
034import org.apache.hadoop.hbase.CellUtil;
035import org.apache.hadoop.hbase.HBaseClassTestRule;
036import org.apache.hadoop.hbase.HBaseTestingUtil;
037import org.apache.hadoop.hbase.HConstants;
038import org.apache.hadoop.hbase.Stoppable;
039import org.apache.hadoop.hbase.TableName;
040import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
041import org.apache.hadoop.hbase.client.Durability;
042import org.apache.hadoop.hbase.client.Get;
043import org.apache.hadoop.hbase.client.Put;
044import org.apache.hadoop.hbase.client.RegionInfo;
045import org.apache.hadoop.hbase.client.RegionInfoBuilder;
046import org.apache.hadoop.hbase.client.Result;
047import org.apache.hadoop.hbase.client.TableDescriptor;
048import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
049import org.apache.hadoop.hbase.testclassification.MediumTests;
050import org.apache.hadoop.hbase.testclassification.RegionServerTests;
051import org.apache.hadoop.hbase.util.Bytes;
052import org.apache.hadoop.hbase.util.CommonFSUtils;
053import org.apache.hadoop.hbase.util.StoppableImplementation;
054import org.apache.hadoop.hbase.wal.WALFactory;
055import org.junit.Before;
056import org.junit.ClassRule;
057import org.junit.Rule;
058import org.junit.Test;
059import org.junit.experimental.categories.Category;
060import org.junit.rules.TestName;
061
062@Category({RegionServerTests.class, MediumTests.class})
063public class TestStoreFileRefresherChore {
064
065  @ClassRule
066  public static final HBaseClassTestRule CLASS_RULE =
067      HBaseClassTestRule.forClass(TestStoreFileRefresherChore.class);
068
069  private HBaseTestingUtil TEST_UTIL;
070  private Path testDir;
071
072  @Rule
073  public TestName name = new TestName();
074
075  @Before
076  public void setUp() throws IOException {
077    TEST_UTIL = new HBaseTestingUtil();
078    testDir = TEST_UTIL.getDataTestDir("TestStoreFileRefresherChore");
079    CommonFSUtils.setRootDir(TEST_UTIL.getConfiguration(), testDir);
080  }
081
082  private TableDescriptor getTableDesc(TableName tableName, int regionReplication,
083      byte[]... families) {
084    TableDescriptorBuilder builder =
085        TableDescriptorBuilder.newBuilder(tableName).setRegionReplication(regionReplication);
086    Arrays.stream(families).map(family -> ColumnFamilyDescriptorBuilder.newBuilder(family)
087        .setMaxVersions(Integer.MAX_VALUE).build()).forEachOrdered(builder::setColumnFamily);
088    return builder.build();
089  }
090
091  static class FailingHRegionFileSystem extends HRegionFileSystem {
092    boolean fail = false;
093
094    FailingHRegionFileSystem(Configuration conf, FileSystem fs, Path tableDir,
095        RegionInfo regionInfo) {
096      super(conf, fs, tableDir, regionInfo);
097    }
098
099    @Override
100    public List<StoreFileInfo> getStoreFiles(String familyName) throws IOException {
101      if (fail) {
102        throw new IOException("simulating FS failure");
103      }
104      return super.getStoreFiles(familyName);
105    }
106  }
107
108  private HRegion initHRegion(TableDescriptor htd, byte[] startKey, byte[] stopKey, int replicaId)
109      throws IOException {
110    Configuration conf = TEST_UTIL.getConfiguration();
111    Path tableDir = CommonFSUtils.getTableDir(testDir, htd.getTableName());
112
113    RegionInfo info = RegionInfoBuilder.newBuilder(htd.getTableName()).setStartKey(startKey)
114        .setEndKey(stopKey).setRegionId(0L).setReplicaId(replicaId).build();
115    HRegionFileSystem fs =
116        new FailingHRegionFileSystem(conf, tableDir.getFileSystem(conf), tableDir, info);
117    final Configuration walConf = new Configuration(conf);
118    CommonFSUtils.setRootDir(walConf, tableDir);
119    final WALFactory wals = new WALFactory(walConf, "log_" + replicaId);
120    ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0,
121      0, null, MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT);
122    HRegion region =
123        new HRegion(fs, wals.getWAL(info),
124            conf, htd, null);
125
126    region.initialize();
127
128    return region;
129  }
130
131  private void putData(Region region, int startRow, int numRows, byte[] qf, byte[]... families)
132      throws IOException {
133    for (int i = startRow; i < startRow + numRows; i++) {
134      Put put = new Put(Bytes.toBytes("" + i));
135      put.setDurability(Durability.SKIP_WAL);
136      for (byte[] family : families) {
137        put.addColumn(family, qf, null);
138      }
139      region.put(put);
140    }
141  }
142
143  private void verifyDataExpectFail(Region newReg, int startRow, int numRows, byte[] qf,
144      byte[]... families) throws IOException {
145    boolean threw = false;
146    try {
147      verifyData(newReg, startRow, numRows, qf, families);
148    } catch (AssertionError e) {
149      threw = true;
150    }
151    if (!threw) {
152      fail("Expected data verification to fail");
153    }
154  }
155
156  private void verifyData(Region newReg, int startRow, int numRows, byte[] qf, byte[]... families)
157      throws IOException {
158    for (int i = startRow; i < startRow + numRows; i++) {
159      byte[] row = Bytes.toBytes("" + i);
160      Get get = new Get(row);
161      for (byte[] family : families) {
162        get.addColumn(family, qf);
163      }
164      Result result = newReg.get(get);
165      Cell[] raw = result.rawCells();
166      assertEquals(families.length, result.size());
167      for (int j = 0; j < families.length; j++) {
168        assertTrue(CellUtil.matchingRows(raw[j], row));
169        assertTrue(CellUtil.matchingFamily(raw[j], families[j]));
170        assertTrue(CellUtil.matchingQualifier(raw[j], qf));
171      }
172    }
173  }
174
175  static class StaleStorefileRefresherChore extends StorefileRefresherChore {
176    boolean isStale = false;
177    public StaleStorefileRefresherChore(int period, HRegionServer regionServer,
178        Stoppable stoppable) {
179      super(period, false, regionServer, stoppable);
180    }
181    @Override
182    protected boolean isRegionStale(String encodedName, long time) {
183      return isStale;
184    }
185  }
186
187  @Test
188  public void testIsStale() throws IOException {
189    int period = 0;
190    byte[][] families = new byte[][] {Bytes.toBytes("cf")};
191    byte[] qf = Bytes.toBytes("cq");
192
193    HRegionServer regionServer = mock(HRegionServer.class);
194    List<HRegion> regions = new ArrayList<>();
195    when(regionServer.getOnlineRegionsLocalContext()).thenReturn(regions);
196    when(regionServer.getConfiguration()).thenReturn(TEST_UTIL.getConfiguration());
197
198    TableDescriptor htd = getTableDesc(TableName.valueOf(name.getMethodName()), 2, families);
199    HRegion primary = initHRegion(htd, HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW, 0);
200    HRegion replica1 = initHRegion(htd, HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW, 1);
201    regions.add(primary);
202    regions.add(replica1);
203
204    StaleStorefileRefresherChore chore = new StaleStorefileRefresherChore(period, regionServer,
205      new StoppableImplementation());
206
207    // write some data to primary and flush
208    putData(primary, 0, 100, qf, families);
209    primary.flush(true);
210    verifyData(primary, 0, 100, qf, families);
211
212    verifyDataExpectFail(replica1, 0, 100, qf, families);
213    chore.chore();
214    verifyData(replica1, 0, 100, qf, families);
215
216    // simulate an fs failure where we cannot refresh the store files for the replica
217    ((FailingHRegionFileSystem)replica1.getRegionFileSystem()).fail = true;
218
219    // write some more data to primary and flush
220    putData(primary, 100, 100, qf, families);
221    primary.flush(true);
222    verifyData(primary, 0, 200, qf, families);
223
224    chore.chore(); // should not throw ex, but we cannot refresh the store files
225
226    verifyData(replica1, 0, 100, qf, families);
227    verifyDataExpectFail(replica1, 100, 100, qf, families);
228
229    chore.isStale = true;
230    chore.chore(); //now after this, we cannot read back any value
231    try {
232      verifyData(replica1, 0, 100, qf, families);
233      fail("should have failed with IOException");
234    } catch(IOException ex) {
235      // expected
236    }
237  }
238}