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.master.cleaner;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.FileStatus;
028import org.apache.hadoop.fs.FileSystem;
029import org.apache.hadoop.fs.Path;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseTestingUtil;
032import org.apache.hadoop.hbase.Server;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.client.RegionInfo;
035import org.apache.hadoop.hbase.client.RegionInfoBuilder;
036import org.apache.hadoop.hbase.io.HFileLink;
037import org.apache.hadoop.hbase.testclassification.MasterTests;
038import org.apache.hadoop.hbase.testclassification.MediumTests;
039import org.apache.hadoop.hbase.util.CommonFSUtils;
040import org.apache.hadoop.hbase.util.HFileArchiveUtil;
041import org.apache.hadoop.hbase.util.MockServer;
042import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
043import org.junit.AfterClass;
044import org.junit.BeforeClass;
045import org.junit.ClassRule;
046import org.junit.Rule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049import org.junit.rules.TestName;
050
051/**
052 * Test the HFileLink Cleaner. HFiles with links cannot be deleted until a link is present.
053 */
054@Category({ MasterTests.class, MediumTests.class })
055public class TestHFileLinkCleaner {
056
057  @ClassRule
058  public static final HBaseClassTestRule CLASS_RULE =
059    HBaseClassTestRule.forClass(TestHFileLinkCleaner.class);
060
061  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
062
063  private static DirScanPool POOL;
064
065  @Rule
066  public TestName name = new TestName();
067
068  @BeforeClass
069  public static void setUp() {
070    POOL = DirScanPool.getHFileCleanerScanPool(TEST_UTIL.getConfiguration());
071  }
072
073  @AfterClass
074  public static void tearDown() {
075    POOL.shutdownNow();
076  }
077
078  @Test
079  public void testHFileLinkCleaning() throws Exception {
080    Configuration conf = TEST_UTIL.getConfiguration();
081    CommonFSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
082    conf.set(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, HFileLinkCleaner.class.getName());
083    Path rootDir = CommonFSUtils.getRootDir(conf);
084    FileSystem fs = FileSystem.get(conf);
085
086    final TableName tableName = TableName.valueOf(name.getMethodName());
087    final TableName tableLinkName = TableName.valueOf(name.getMethodName() + "-link");
088    final String hfileName = "1234567890";
089    final String familyName = "cf";
090
091    RegionInfo hri = RegionInfoBuilder.newBuilder(tableName).build();
092    RegionInfo hriLink = RegionInfoBuilder.newBuilder(tableLinkName).build();
093
094    Path archiveDir = HFileArchiveUtil.getArchivePath(conf);
095    Path archiveStoreDir = HFileArchiveUtil.getStoreArchivePath(conf,
096          tableName, hri.getEncodedName(), familyName);
097
098    // Create hfile /hbase/table-link/region/cf/getEncodedName.HFILE(conf);
099    Path familyPath = getFamilyDirPath(archiveDir, tableName, hri.getEncodedName(), familyName);
100    fs.mkdirs(familyPath);
101    Path hfilePath = new Path(familyPath, hfileName);
102    fs.createNewFile(hfilePath);
103
104    // Create link to hfile
105    Path familyLinkPath =
106      getFamilyDirPath(rootDir, tableLinkName, hriLink.getEncodedName(), familyName);
107    fs.mkdirs(familyLinkPath);
108    HFileLink.create(conf, fs, familyLinkPath, hri, hfileName);
109    Path linkBackRefDir = HFileLink.getBackReferencesDir(archiveStoreDir, hfileName);
110    assertTrue(fs.exists(linkBackRefDir));
111    FileStatus[] backRefs = fs.listStatus(linkBackRefDir);
112    assertEquals(1, backRefs.length);
113    Path linkBackRef = backRefs[0].getPath();
114
115    // Initialize cleaner
116    final long ttl = 1000;
117    conf.setLong(TimeToLiveHFileCleaner.TTL_CONF_KEY, ttl);
118    Server server = new DummyServer();
119    HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archiveDir, POOL);
120
121    // Link backref cannot be removed
122    cleaner.chore();
123    assertTrue(fs.exists(linkBackRef));
124    assertTrue(fs.exists(hfilePath));
125
126    // Link backref can be removed
127    fs.rename(CommonFSUtils.getTableDir(rootDir, tableLinkName),
128      CommonFSUtils.getTableDir(archiveDir, tableLinkName));
129    cleaner.chore();
130    assertFalse("Link should be deleted", fs.exists(linkBackRef));
131
132    // HFile can be removed
133    Thread.sleep(ttl * 2);
134    cleaner.chore();
135    assertFalse("HFile should be deleted", fs.exists(hfilePath));
136
137    // Remove everything
138    for (int i = 0; i < 4; ++i) {
139      Thread.sleep(ttl * 2);
140      cleaner.chore();
141    }
142    assertFalse("HFile should be deleted",
143      fs.exists(CommonFSUtils.getTableDir(archiveDir, tableName)));
144    assertFalse("Link should be deleted",
145      fs.exists(CommonFSUtils.getTableDir(archiveDir, tableLinkName)));
146  }
147
148  private static Path getFamilyDirPath(final Path rootDir, final TableName table,
149    final String region, final String family) {
150    return new Path(new Path(CommonFSUtils.getTableDir(rootDir, table), region), family);
151  }
152
153  static class DummyServer extends MockServer {
154
155    @Override
156    public Configuration getConfiguration() {
157      return TEST_UTIL.getConfiguration();
158    }
159
160    @Override
161    public ZKWatcher getZooKeeper() {
162      try {
163        return new ZKWatcher(getConfiguration(), "dummy server", this);
164      } catch (IOException e) {
165        e.printStackTrace();
166      }
167      return null;
168    }
169  }
170}