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.util;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.concurrent.ScheduledThreadPoolExecutor;
023import java.util.concurrent.SynchronousQueue;
024import java.util.concurrent.ThreadPoolExecutor;
025import java.util.concurrent.TimeUnit;
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.ClusterConnection;
032import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
033import org.apache.hadoop.hbase.io.hfile.TestHFile;
034import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
035import org.apache.hadoop.hbase.mob.MobUtils;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.testclassification.MiscTests;
038import org.apache.hadoop.hbase.util.hbck.HFileCorruptionChecker;
039import org.apache.hadoop.hbase.util.hbck.HbckTestingUtil;
040import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
041import org.junit.AfterClass;
042import org.junit.Before;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047
048@Category({MiscTests.class, MediumTests.class})
049public class TestHBaseFsckMOB extends BaseTestHBaseFsck {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053      HBaseClassTestRule.forClass(TestHBaseFsckMOB.class);
054
055  @BeforeClass
056  public static void setUpBeforeClass() throws Exception {
057    TEST_UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
058        MasterSyncCoprocessor.class.getName());
059
060    conf.setInt("hbase.regionserver.handler.count", 2);
061    conf.setInt("hbase.regionserver.metahandler.count", 30);
062
063    conf.setInt("hbase.htable.threads.max", POOL_SIZE);
064    conf.setInt("hbase.hconnection.threads.max", 2 * POOL_SIZE);
065    conf.setInt("hbase.hbck.close.timeout", 2 * REGION_ONLINE_TIMEOUT);
066    conf.setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 8 * REGION_ONLINE_TIMEOUT);
067    TEST_UTIL.startMiniCluster(1);
068
069    tableExecutorService = new ThreadPoolExecutor(1, POOL_SIZE, 60, TimeUnit.SECONDS,
070      new SynchronousQueue<>(), new ThreadFactoryBuilder().setNameFormat("testhbck-pool-%d")
071        .setDaemon(true).setUncaughtExceptionHandler(Threads.LOGGING_EXCEPTION_HANDLER).build());
072
073    hbfsckExecutorService = new ScheduledThreadPoolExecutor(POOL_SIZE);
074
075    AssignmentManager assignmentManager =
076        TEST_UTIL.getHBaseCluster().getMaster().getAssignmentManager();
077    regionStates = assignmentManager.getRegionStates();
078
079    connection = (ClusterConnection) TEST_UTIL.getConnection();
080
081    admin = connection.getAdmin();
082    admin.setBalancerRunning(false, true);
083
084    TEST_UTIL.waitUntilAllRegionsAssigned(TableName.META_TABLE_NAME);
085    TEST_UTIL.waitUntilAllRegionsAssigned(TableName.NAMESPACE_TABLE_NAME);
086  }
087
088  @AfterClass
089  public static void tearDownAfterClass() throws Exception {
090    tableExecutorService.shutdown();
091    hbfsckExecutorService.shutdown();
092    admin.close();
093    TEST_UTIL.shutdownMiniCluster();
094  }
095
096  @Before
097  public void setUp() {
098    EnvironmentEdgeManager.reset();
099  }
100
101
102  /**
103   * This creates a table and then corrupts a mob file.  Hbck should quarantine the file.
104   */
105  @Test
106  public void testQuarantineCorruptMobFile() throws Exception {
107    TableName table = TableName.valueOf(name.getMethodName());
108    try {
109      setupMobTable(table);
110      assertEquals(ROWKEYS.length, countRows());
111      admin.flush(table);
112
113      FileSystem fs = FileSystem.get(conf);
114      Path mobFile = getFlushedMobFile(fs, table);
115      admin.disableTable(table);
116      // create new corrupt mob file.
117      String corruptMobFile = createMobFileName(mobFile.getName());
118      Path corrupt = new Path(mobFile.getParent(), corruptMobFile);
119      TestHFile.truncateFile(fs, mobFile, corrupt);
120      LOG.info("Created corrupted mob file " + corrupt);
121      HBaseFsck.debugLsr(conf, CommonFSUtils.getRootDir(conf));
122      HBaseFsck.debugLsr(conf, MobUtils.getMobHome(conf));
123
124      // A corrupt mob file doesn't abort the start of regions, so we can enable the table.
125      admin.enableTable(table);
126      HBaseFsck res = HbckTestingUtil.doHFileQuarantine(conf, table);
127      assertEquals(0, res.getRetCode());
128      HFileCorruptionChecker hfcc = res.getHFilecorruptionChecker();
129      assertEquals(4, hfcc.getHFilesChecked());
130      assertEquals(0, hfcc.getCorrupted().size());
131      assertEquals(0, hfcc.getFailures().size());
132      assertEquals(0, hfcc.getQuarantined().size());
133      assertEquals(0, hfcc.getMissing().size());
134      assertEquals(5, hfcc.getMobFilesChecked());
135      assertEquals(1, hfcc.getCorruptedMobFiles().size());
136      assertEquals(0, hfcc.getFailureMobFiles().size());
137      assertEquals(1, hfcc.getQuarantinedMobFiles().size());
138      assertEquals(0, hfcc.getMissedMobFiles().size());
139      String quarantinedMobFile = hfcc.getQuarantinedMobFiles().iterator().next().getName();
140      assertEquals(corruptMobFile, quarantinedMobFile);
141    } finally {
142      cleanupTable(table);
143    }
144  }
145}