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