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 java.io.IOException;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.Cell;
030import org.apache.hadoop.hbase.CellComparatorImpl;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseTestingUtil;
033import org.apache.hadoop.hbase.HConstants;
034import org.apache.hadoop.hbase.MemoryCompactionPolicy;
035import org.apache.hadoop.hbase.PrivateCellUtil;
036import org.apache.hadoop.hbase.TableName;
037import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
038import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
039import org.apache.hadoop.hbase.client.RegionInfo;
040import org.apache.hadoop.hbase.client.RegionInfoBuilder;
041import org.apache.hadoop.hbase.client.Scan;
042import org.apache.hadoop.hbase.client.TableDescriptor;
043import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
044import org.apache.hadoop.hbase.io.hfile.BlockCache;
045import org.apache.hadoop.hbase.io.hfile.BlockCacheFactory;
046import org.apache.hadoop.hbase.testclassification.MediumTests;
047import org.apache.hadoop.hbase.util.Bytes;
048import org.apache.hadoop.hbase.util.CommonFSUtils;
049import org.apache.hadoop.hbase.util.FSUtils;
050import org.apache.hadoop.hbase.wal.WAL;
051import org.apache.hadoop.hbase.wal.WALEdit;
052import org.apache.hadoop.hbase.wal.WALFactory;
053import org.apache.hadoop.hbase.wal.WALKey;
054import org.apache.hadoop.hbase.wal.WALSplitUtil;
055import org.junit.BeforeClass;
056import org.junit.ClassRule;
057import org.junit.Rule;
058import org.junit.Test;
059import org.junit.experimental.categories.Category;
060import org.junit.rules.TestName;
061import org.slf4j.Logger;
062import org.slf4j.LoggerFactory;
063
064/**
065 * Tests around replay of recovered.edits content.
066 */
067@Category({MediumTests.class})
068public class TestRecoveredEdits {
069
070  @ClassRule
071  public static final HBaseClassTestRule CLASS_RULE =
072      HBaseClassTestRule.forClass(TestRecoveredEdits.class);
073
074  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
075  private static final Logger LOG = LoggerFactory.getLogger(TestRecoveredEdits.class);
076
077  private static BlockCache blockCache;
078
079  @Rule public TestName testName = new TestName();
080
081  /**
082   * Path to a recovered.edits file in hbase-server test resources folder.
083   * This is a little fragile getting this path to a file of 10M of edits.
084   */
085  @SuppressWarnings("checkstyle:VisibilityModifier")
086  public static final Path RECOVEREDEDITS_PATH = new Path(
087    System.getProperty("test.build.classes", "target/test-classes"),
088    "0000000000000016310");
089
090  /**
091   * Name of table referenced by edits in the recovered.edits file.
092   */
093  public static final String RECOVEREDEDITS_TABLENAME = "IntegrationTestBigLinkedList";
094
095  /**
096   * Column family referenced by edits in the recovered.edits file.
097   */
098  public static final byte [] RECOVEREDEDITS_COLUMNFAMILY = Bytes.toBytes("meta");
099  public static final byte[][] RECOVEREDITS_COLUMNFAMILY_ARRAY =
100    new byte[][] {RECOVEREDEDITS_COLUMNFAMILY};
101  public static final ColumnFamilyDescriptor RECOVEREDEDITS_CFD =
102    ColumnFamilyDescriptorBuilder.newBuilder(RECOVEREDEDITS_COLUMNFAMILY).build();
103
104  /**
105   * Name of table mentioned edits from recovered.edits
106   */
107  @BeforeClass
108  public static void setUpBeforeClass() throws Exception {
109    blockCache = BlockCacheFactory.createBlockCache(TEST_UTIL.getConfiguration());
110  }
111
112  /**
113   * HBASE-12782 ITBLL fails for me if generator does anything but 5M per maptask.
114   * Create a region. Close it. Then copy into place a file to replay, one that is bigger than
115   * configured flush size so we bring on lots of flushes.  Then reopen and confirm all edits
116   * made it in.
117   */
118  @Test
119  public void testReplayWorksThoughLotsOfFlushing() throws IOException {
120    for (MemoryCompactionPolicy policy : MemoryCompactionPolicy.values()) {
121      testReplayWorksWithMemoryCompactionPolicy(policy);
122    }
123  }
124
125  private void testReplayWorksWithMemoryCompactionPolicy(MemoryCompactionPolicy policy) throws
126    IOException {
127    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
128    // Set it so we flush every 1M or so.  Thats a lot.
129    conf.setInt(HConstants.HREGION_MEMSTORE_FLUSH_SIZE, 1024*1024);
130    conf.set(CompactingMemStore.COMPACTING_MEMSTORE_TYPE_KEY, String.valueOf(policy).toLowerCase());
131    TableDescriptor tableDescriptor = TableDescriptorBuilder.
132      newBuilder(TableName.valueOf(testName.getMethodName())).
133      setColumnFamily(RECOVEREDEDITS_CFD) .build();
134    RegionInfo hri = RegionInfoBuilder.newBuilder(tableDescriptor.getTableName()).build();
135    final String encodedRegionName = hri.getEncodedName();
136    Path hbaseRootDir = TEST_UTIL.getDataTestDir();
137    FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration());
138    Path tableDir = CommonFSUtils.getTableDir(hbaseRootDir, tableDescriptor.getTableName());
139    HRegionFileSystem hrfs =
140        new HRegionFileSystem(TEST_UTIL.getConfiguration(), fs, tableDir, hri);
141    if (fs.exists(hrfs.getRegionDir())) {
142      LOG.info("Region directory already exists. Deleting.");
143      fs.delete(hrfs.getRegionDir(), true);
144    }
145    HRegion region = HBaseTestingUtil
146        .createRegionAndWAL(hri, hbaseRootDir, conf, tableDescriptor, blockCache);
147    assertEquals(encodedRegionName, region.getRegionInfo().getEncodedName());
148    List<String> storeFiles = region.getStoreFileList(RECOVEREDITS_COLUMNFAMILY_ARRAY);
149    // There should be no store files.
150    assertTrue(storeFiles.isEmpty());
151    region.close();
152    Path regionDir = FSUtils.getRegionDirFromRootDir(hbaseRootDir, hri);
153    Path recoveredEditsDir = WALSplitUtil.getRegionDirRecoveredEditsDir(regionDir);
154    // Copy this file under the region's recovered.edits dir so it is replayed on reopen.
155    Path destination = new Path(recoveredEditsDir, RECOVEREDEDITS_PATH.getName());
156    fs.copyToLocalFile(RECOVEREDEDITS_PATH, destination);
157    assertTrue(fs.exists(destination));
158    // Now the file 0000000000000016310 is under recovered.edits, reopen the region to replay.
159    region = HRegion.openHRegion(region, null);
160    assertEquals(encodedRegionName, region.getRegionInfo().getEncodedName());
161    storeFiles = region.getStoreFileList(RECOVEREDITS_COLUMNFAMILY_ARRAY);
162    // Our 0000000000000016310 is 10MB. Most of the edits are for one region. Lets assume that if
163    // we flush at 1MB, that there are at least 3 flushed files that are there because of the
164    // replay of edits.
165    if(policy == MemoryCompactionPolicy.EAGER || policy == MemoryCompactionPolicy.ADAPTIVE) {
166      assertTrue("Files count=" + storeFiles.size(), storeFiles.size() >= 1);
167    } else {
168      assertTrue("Files count=" + storeFiles.size(), storeFiles.size() > 10);
169    }
170    // Now verify all edits made it into the region.
171    int count = verifyAllEditsMadeItIn(fs, conf, RECOVEREDEDITS_PATH, region);
172    assertTrue(count > 0);
173    LOG.info("Checked " + count + " edits made it in");
174  }
175
176  /**
177   * @return Return how many edits seen.
178   */
179  // Used by TestWALPlayer over in hbase-mapreduce too.
180  public static int verifyAllEditsMadeItIn(final FileSystem fs, final Configuration conf,
181      final Path edits, final HRegion region) throws IOException {
182    int count = 0;
183    // Read all cells from recover edits
184    List<Cell> walCells = new ArrayList<>();
185    try (WAL.Reader reader = WALFactory.createReader(fs, edits, conf)) {
186      WAL.Entry entry;
187      while ((entry = reader.next()) != null) {
188        WALKey key = entry.getKey();
189        WALEdit val = entry.getEdit();
190        count++;
191        // Check this edit is for this region.
192        if (!Bytes.equals(key.getEncodedRegionName(),
193            region.getRegionInfo().getEncodedNameAsBytes())) {
194          continue;
195        }
196        Cell previous = null;
197        for (Cell cell : val.getCells()) {
198          if (WALEdit.isMetaEditFamily(cell)) {
199            continue;
200          }
201          if (previous != null && CellComparatorImpl.COMPARATOR.compareRows(previous, cell) == 0) {
202            continue;
203          }
204          previous = cell;
205          walCells.add(cell);
206        }
207      }
208    }
209
210    // Read all cells from region
211    List<Cell> regionCells = new ArrayList<>();
212    try (RegionScanner scanner = region.getScanner(new Scan())) {
213      List<Cell> tmpCells;
214      do {
215        tmpCells = new ArrayList<>();
216        scanner.nextRaw(tmpCells);
217        regionCells.addAll(tmpCells);
218      } while (!tmpCells.isEmpty());
219    }
220
221    Collections.sort(walCells, CellComparatorImpl.COMPARATOR);
222    int found = 0;
223    for (int i = 0, j = 0; i < walCells.size() && j < regionCells.size(); ) {
224      int compareResult = PrivateCellUtil
225          .compareKeyIgnoresMvcc(CellComparatorImpl.COMPARATOR, walCells.get(i),
226              regionCells.get(j));
227      if (compareResult == 0) {
228        i++;
229        j++;
230        found++;
231      } else if (compareResult > 0) {
232        j++;
233      } else {
234        i++;
235      }
236    }
237    assertEquals("Only found " + found + " cells in region, but there are " + walCells.size() +
238        " cells in recover edits", found, walCells.size());
239    return count;
240  }
241}