View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master.snapshot;
19  
20  import static org.junit.Assert.assertFalse;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.FileSystem;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.testclassification.SmallTests;
30  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.hbase.util.FSUtils;
33  import org.junit.AfterClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  /**
38   * Test that the snapshot log cleaner finds logs referenced in a snapshot
39   */
40  @Category(SmallTests.class)
41  public class TestSnapshotLogCleaner {
42  
43    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
44  
45    @AfterClass
46    public static void cleanup() throws IOException {
47      Configuration conf = TEST_UTIL.getConfiguration();
48      Path rootDir = FSUtils.getRootDir(conf);
49      FileSystem fs = FileSystem.get(conf);
50      // cleanup
51      fs.delete(rootDir, true);
52    }
53  
54    @Test
55    public void testFindsSnapshotFilesWhenCleaning() throws IOException {
56      Configuration conf = TEST_UTIL.getConfiguration();
57      FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
58      Path rootDir = FSUtils.getRootDir(conf);
59      FileSystem fs = FileSystem.get(conf);
60      SnapshotLogCleaner cleaner = new SnapshotLogCleaner();
61      cleaner.setConf(conf);
62  
63      // write an hfile to the snapshot directory
64      String snapshotName = "snapshot";
65      byte[] snapshot = Bytes.toBytes(snapshotName);
66      Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
67      Path snapshotLogDir = new Path(snapshotDir, HConstants.HREGION_LOGDIR_NAME);
68      String timestamp = "1339643343027";
69      String hostFromMaster = "localhost%2C59648%2C1339643336601";
70  
71      Path hostSnapshotLogDir = new Path(snapshotLogDir, hostFromMaster);
72      String snapshotlogfile = hostFromMaster + "." + timestamp + ".hbase";
73  
74      // add the reference to log in the snapshot
75      fs.create(new Path(hostSnapshotLogDir, snapshotlogfile));
76  
77      // now check to see if that log file would get deleted.
78      Path oldlogDir = new Path(rootDir, HConstants.HREGION_OLDLOGDIR_NAME);
79      Path logFile = new Path(oldlogDir, snapshotlogfile);
80      fs.create(logFile);
81  
82      // make sure that the file isn't deletable
83      assertFalse(cleaner.isFileDeletable(fs.getFileStatus(logFile)));
84    }
85  }