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