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.snapshot;
19  
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.hadoop.fs.FileStatus;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.SmallTests;
32  import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
33  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
34  import org.apache.hadoop.hbase.snapshot.ReferenceRegionHFilesTask;
35  import org.apache.hadoop.hbase.util.FSUtils;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  import org.mockito.Mockito;
39  
40  @Category(SmallTests.class)
41  public class TestReferenceRegionHFilesTask {
42    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
43  
44    @Test
45    public void testRun() throws IOException {
46      FileSystem fs = UTIL.getTestFileSystem();
47      // setup the region internals
48      Path testdir = UTIL.getDataTestDir();
49      Path regionDir = new Path(testdir, "region");
50      Path family1 = new Path(regionDir, "fam1");
51      // make an empty family
52      Path family2 = new Path(regionDir, "fam2");
53      fs.mkdirs(family2);
54  
55      // add some files to family 1
56      Path file1 = new Path(family1, "05f99689ae254693836613d1884c6b63");
57      fs.createNewFile(file1);
58      Path file2 = new Path(family1, "7ac9898bf41d445aa0003e3d699d5d26");
59      fs.createNewFile(file2);
60  
61      // create the snapshot directory
62      Path snapshotRegionDir = new Path(testdir, HConstants.SNAPSHOT_DIR_NAME);
63      fs.mkdirs(snapshotRegionDir);
64  
65      SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName("name")
66          .setTable("table").build();
67      ForeignExceptionDispatcher monitor = Mockito.mock(ForeignExceptionDispatcher.class);
68      ReferenceRegionHFilesTask task = new ReferenceRegionHFilesTask(snapshot, monitor, regionDir,
69          fs, snapshotRegionDir);
70      ReferenceRegionHFilesTask taskSpy = Mockito.spy(task);
71      task.call();
72  
73      // make sure we never get an error
74      Mockito.verify(taskSpy, Mockito.never()).snapshotFailure(Mockito.anyString(),
75          Mockito.any(Exception.class));
76  
77      // verify that all the hfiles get referenced
78      List<String> hfiles = new ArrayList<String>(2);
79      FileStatus[] regions = FSUtils.listStatus(fs, snapshotRegionDir);
80      for (FileStatus region : regions) {
81        FileStatus[] fams = FSUtils.listStatus(fs, region.getPath());
82        for (FileStatus fam : fams) {
83          FileStatus[] files = FSUtils.listStatus(fs, fam.getPath());
84          for (FileStatus file : files) {
85            hfiles.add(file.getPath().getName());
86          }
87        }
88      }
89      assertTrue("Didn't reference :" + file1, hfiles.contains(file1.getName()));
90      assertTrue("Didn't reference :" + file1, hfiles.contains(file2.getName()));
91    }
92  }