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