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