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  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.io.IOException;
25  
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.TableName;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.testclassification.SmallTests;
33  import org.apache.hadoop.hbase.executor.ExecutorService;
34  import org.apache.hadoop.hbase.master.MasterFileSystem;
35  import org.apache.hadoop.hbase.master.MasterServices;
36  import org.apache.hadoop.hbase.master.MetricsMaster;
37  import org.apache.hadoop.hbase.master.cleaner.HFileCleaner;
38  import org.apache.hadoop.hbase.master.cleaner.HFileLinkCleaner;
39  import org.apache.hadoop.hbase.procedure.ProcedureCoordinator;
40  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
41  import org.apache.zookeeper.KeeperException;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  import org.mockito.Mockito;
45  
46  /**
47   * Test basic snapshot manager functionality
48   */
49  @Category(SmallTests.class)
50  public class TestSnapshotManager {
51    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
52  
53    MasterServices services = Mockito.mock(MasterServices.class);
54    MetricsMaster metrics = Mockito.mock(MetricsMaster.class);
55    ProcedureCoordinator coordinator = Mockito.mock(ProcedureCoordinator.class);
56    ExecutorService pool = Mockito.mock(ExecutorService.class);
57    MasterFileSystem mfs = Mockito.mock(MasterFileSystem.class);
58    FileSystem fs;
59    {
60      try {
61        fs = UTIL.getTestFileSystem();
62      } catch (IOException e) {
63        throw new RuntimeException("Couldn't get test filesystem", e);
64      }
65    }
66  
67     private SnapshotManager getNewManager() throws IOException, KeeperException {
68      return getNewManager(UTIL.getConfiguration());
69    }
70  
71    private SnapshotManager getNewManager(final Configuration conf)
72        throws IOException, KeeperException {
73      Mockito.reset(services);
74      Mockito.when(services.getConfiguration()).thenReturn(conf);
75      Mockito.when(services.getMasterFileSystem()).thenReturn(mfs);
76      Mockito.when(mfs.getFileSystem()).thenReturn(fs);
77      Mockito.when(mfs.getRootDir()).thenReturn(UTIL.getDataTestDir());
78      return new SnapshotManager(services, metrics, coordinator, pool);
79    }
80  
81    @Test
82    public void testInProcess() throws KeeperException, IOException {
83      TableName tableName = TableName.valueOf("testTable");
84      SnapshotManager manager = getNewManager();
85      TakeSnapshotHandler handler = Mockito.mock(TakeSnapshotHandler.class);
86      assertFalse("Manager is in process when there is no current handler",
87          manager.isTakingSnapshot(tableName));
88      manager.setSnapshotHandlerForTesting(tableName, handler);
89      Mockito.when(handler.isFinished()).thenReturn(false);
90      assertTrue("Manager isn't in process when handler is running",
91          manager.isTakingSnapshot(tableName));
92      Mockito.when(handler.isFinished()).thenReturn(true);
93      assertFalse("Manager is process when handler isn't running",
94          manager.isTakingSnapshot(tableName));
95    }
96  
97    /**
98     * Verify the snapshot support based on the configuration.
99     */
100   @Test
101   public void testSnapshotSupportConfiguration() throws Exception {
102     // No configuration (no cleaners, not enabled): snapshot feature disabled
103     Configuration conf = new Configuration();
104     SnapshotManager manager = getNewManager(conf);
105     assertFalse("Snapshot should be disabled with no configuration", isSnapshotSupported(manager));
106 
107     // force snapshot feature to be enabled
108     conf = new Configuration();
109     conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
110     manager = getNewManager(conf);
111     assertTrue("Snapshot should be enabled", isSnapshotSupported(manager));
112 
113     // force snapshot feature to be disabled
114     conf = new Configuration();
115     conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, false);
116     manager = getNewManager(conf);
117     assertFalse("Snapshot should be disabled", isSnapshotSupported(manager));
118 
119     // force snapshot feature to be disabled, even if cleaners are present
120     conf = new Configuration();
121     conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS,
122       SnapshotHFileCleaner.class.getName(), HFileLinkCleaner.class.getName());
123     conf.set(HConstants.HBASE_MASTER_LOGCLEANER_PLUGINS, SnapshotLogCleaner.class.getName());
124     conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, false);
125     manager = getNewManager(conf);
126     assertFalse("Snapshot should be disabled", isSnapshotSupported(manager));
127 
128     // cleaners are present, but missing snapshot enabled property
129     conf = new Configuration();
130     conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS,
131       SnapshotHFileCleaner.class.getName(), HFileLinkCleaner.class.getName());
132     conf.set(HConstants.HBASE_MASTER_LOGCLEANER_PLUGINS, SnapshotLogCleaner.class.getName());
133     manager = getNewManager(conf);
134     assertTrue("Snapshot should be enabled, because cleaners are present",
135       isSnapshotSupported(manager));
136 
137     // Create a "test snapshot"
138     Path rootDir = UTIL.getDataTestDir();
139     Path testSnapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(
140       "testSnapshotSupportConfiguration", rootDir);
141     fs.mkdirs(testSnapshotDir);
142     try {
143       // force snapshot feature to be disabled, but snapshots are present
144       conf = new Configuration();
145       conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, false);
146       manager = getNewManager(conf);
147       fail("Master should not start when snapshot is disabled, but snapshots are present");
148     } catch (UnsupportedOperationException e) {
149       // expected
150     } finally {
151       fs.delete(testSnapshotDir, true);
152     }
153   }
154 
155   private boolean isSnapshotSupported(final SnapshotManager manager) {
156     try {
157       manager.checkSnapshotSupport();
158       return true;
159     } catch (UnsupportedOperationException e) {
160       return false;
161     }
162   }
163 }