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