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.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  
23  import org.apache.hadoop.fs.FSDataOutputStream;
24  import org.apache.hadoop.fs.FileStatus;
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.SmallTests;
29  import org.apache.hadoop.hbase.errorhandling.ForeignException;
30  import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
31  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
32  import org.apache.hadoop.hbase.regionserver.wal.HLogUtil;
33  import org.apache.hadoop.hbase.snapshot.CopyRecoveredEditsTask;
34  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
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  /**
41   * Test that we correctly copy the recovered edits from a directory
42   */
43  @Category(SmallTests.class)
44  public class TestCopyRecoveredEditsTask {
45  
46    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
47  
48    @Test
49    public void testCopyFiles() throws Exception {
50  
51      SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName("snapshot").build();
52      ForeignExceptionDispatcher monitor = Mockito.mock(ForeignExceptionDispatcher.class);
53      FileSystem fs = UTIL.getTestFileSystem();
54      Path root = UTIL.getDataTestDir();
55      String regionName = "regionA";
56      Path regionDir = new Path(root, regionName);
57      Path workingDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshot, root);
58  
59      try {
60        // doesn't really matter where the region's snapshot directory is, but this is pretty close
61        Path snapshotRegionDir = new Path(workingDir, regionName);
62        fs.mkdirs(snapshotRegionDir);
63  
64        // put some stuff in the recovered.edits directory
65        Path edits = HLogUtil.getRegionDirRecoveredEditsDir(regionDir);
66        fs.mkdirs(edits);
67        // make a file with some data
68        Path file1 = new Path(edits, "0000000000000002352");
69        FSDataOutputStream out = fs.create(file1);
70        byte[] data = new byte[] { 1, 2, 3, 4 };
71        out.write(data);
72        out.close();
73        // make an empty file
74        Path empty = new Path(edits, "empty");
75        fs.createNewFile(empty);
76  
77        CopyRecoveredEditsTask task = new CopyRecoveredEditsTask(snapshot, monitor, fs, regionDir,
78            snapshotRegionDir);
79        CopyRecoveredEditsTask taskSpy = Mockito.spy(task);
80        taskSpy.call();
81  
82        Path snapshotEdits = HLogUtil.getRegionDirRecoveredEditsDir(snapshotRegionDir);
83        FileStatus[] snapshotEditFiles = FSUtils.listStatus(fs, snapshotEdits);
84        assertEquals("Got wrong number of files in the snapshot edits", 1, snapshotEditFiles.length);
85        FileStatus file = snapshotEditFiles[0];
86        assertEquals("Didn't copy expected file", file1.getName(), file.getPath().getName());
87  
88        Mockito.verify(monitor, Mockito.never()).receive(Mockito.any(ForeignException.class));
89        Mockito.verify(taskSpy, Mockito.never()).snapshotFailure(Mockito.anyString(),
90             Mockito.any(Exception.class));
91  
92      } finally {
93        // cleanup the working directory
94        FSUtils.delete(fs, regionDir, true);
95        FSUtils.delete(fs, workingDir, true);
96      }
97    }
98  
99    /**
100    * Check that we don't get an exception if there is no recovered edits directory to copy
101    * @throws Exception on failure
102    */
103   @Test
104   public void testNoEditsDir() throws Exception {
105     SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName("snapshot").build();
106     ForeignExceptionDispatcher monitor = Mockito.mock(ForeignExceptionDispatcher.class);
107     FileSystem fs = UTIL.getTestFileSystem();
108     Path root = UTIL.getDataTestDir();
109     String regionName = "regionA";
110     Path regionDir = new Path(root, regionName);
111     Path workingDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshot, root);
112     try {
113       // doesn't really matter where the region's snapshot directory is, but this is pretty close
114       Path snapshotRegionDir = new Path(workingDir, regionName);
115       fs.mkdirs(snapshotRegionDir);
116       Path regionEdits = HLogUtil.getRegionDirRecoveredEditsDir(regionDir);
117       assertFalse("Edits dir exists already - it shouldn't", fs.exists(regionEdits));
118 
119       CopyRecoveredEditsTask task = new CopyRecoveredEditsTask(snapshot, monitor, fs, regionDir,
120           snapshotRegionDir);
121       task.call();
122     } finally {
123       // cleanup the working directory
124       FSUtils.delete(fs, regionDir, true);
125       FSUtils.delete(fs, workingDir, true);
126     }
127   }
128 }