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 java.io.IOException;
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.conf.Configuration;
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.ReferenceServerWALsTask;
35  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
36  import org.apache.hadoop.hbase.snapshot.TakeSnapshotUtils;
37  import org.apache.hadoop.hbase.util.FSUtils;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  import org.mockito.Mockito;
41  
42  /**
43   * Test that the WAL reference task works as expected
44   */
45  @Category(SmallTests.class)
46  public class TestWALReferenceTask {
47  
48    private static final Log LOG = LogFactory.getLog(TestWALReferenceTask.class);
49    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
50  
51    @Test
52    public void testRun() throws IOException {
53      Configuration conf = UTIL.getConfiguration();
54      FileSystem fs = UTIL.getTestFileSystem();
55      // setup the log dir
56      Path testDir = UTIL.getDataTestDir();
57      Set<String> servers = new HashSet<String>();
58      Path logDir = new Path(testDir, HConstants.HREGION_LOGDIR_NAME);
59      Path server1Dir = new Path(logDir, "Server1");
60      servers.add(server1Dir.getName());
61      Path server2Dir = new Path(logDir, "me.hbase.com,56073,1348618509968");
62      servers.add(server2Dir.getName());
63      // logs under server 1
64      Path log1_1 = new Path(server1Dir, "me.hbase.com%2C56073%2C1348618509968.1348618520536");
65      Path log1_2 = new Path(server1Dir, "me.hbase.com%2C56073%2C1348618509968.1234567890123");
66      // logs under server 2
67      Path log2_1 = new Path(server2Dir, "me.hbase.com%2C56074%2C1348618509998.1348618515589");
68      Path log2_2 = new Path(server2Dir, "me.hbase.com%2C56073%2C1348618509968.1234567890123");
69  
70      // create all the log files
71      fs.createNewFile(log1_1);
72      fs.createNewFile(log1_2);
73      fs.createNewFile(log2_1);
74      fs.createNewFile(log2_2);
75  
76      FSUtils.logFileSystemState(fs, testDir, LOG);
77      FSUtils.setRootDir(conf, testDir);
78      SnapshotDescription snapshot = SnapshotDescription.newBuilder()
79          .setName("testWALReferenceSnapshot").build();
80      ForeignExceptionDispatcher listener = Mockito.mock(ForeignExceptionDispatcher.class);
81  
82      // reference all the files in the first server directory
83      ReferenceServerWALsTask task = new ReferenceServerWALsTask(snapshot, listener, server1Dir,
84          conf, fs);
85      task.call();
86  
87      // reference all the files in the first server directory
88      task = new ReferenceServerWALsTask(snapshot, listener, server2Dir, conf, fs);
89      task.call();
90  
91      // verify that we got everything
92      FSUtils.logFileSystemState(fs, testDir, LOG);
93      Path workingDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshot, testDir);
94      Path snapshotLogDir = new Path(workingDir, HConstants.HREGION_LOGDIR_NAME);
95  
96      // make sure we reference the all the wal files
97      TakeSnapshotUtils.verifyAllLogsGotReferenced(fs, logDir, servers, snapshot, snapshotLogDir);
98  
99      // make sure we never got an error
100     Mockito.verify(listener, Mockito.atLeastOnce()).rethrowException();
101     Mockito.verifyNoMoreInteractions(listener);
102   }
103 }