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