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)
71        throws IOException, KeeperException {
72      Mockito.reset(services);
73      Mockito.when(services.getConfiguration()).thenReturn(conf);
74      Mockito.when(services.getMasterFileSystem()).thenReturn(mfs);
75      Mockito.when(mfs.getFileSystem()).thenReturn(fs);
76      Mockito.when(mfs.getRootDir()).thenReturn(UTIL.getDataTestDir());
77      return new SnapshotManager(services, metrics, coordinator, pool);
78    }
79  
80    @Test
81    public void testInProcess() throws KeeperException, IOException {
82      String tableName = "testTable";
83      SnapshotManager manager = getNewManager();
84      TakeSnapshotHandler handler = Mockito.mock(TakeSnapshotHandler.class);
85      assertFalse("Manager is in process when there is no current handler",
86          manager.isTakingSnapshot(tableName));
87      manager.setSnapshotHandlerForTesting(tableName, handler);
88      Mockito.when(handler.isFinished()).thenReturn(false);
89      assertTrue("Manager isn't in process when handler is running",
90          manager.isTakingSnapshot(tableName));
91      Mockito.when(handler.isFinished()).thenReturn(true);
92      assertFalse("Manager is process when handler isn't running",
93          manager.isTakingSnapshot(tableName));
94    }
95  
96    /**
97     * Verify the snapshot support based on the configuration.
98     */
99    @Test
100   public void testSnapshotSupportConfiguration() throws Exception {
101     // No configuration (no cleaners, not enabled): snapshot feature disabled
102     Configuration conf = new Configuration();
103     SnapshotManager manager = getNewManager(conf);
104     assertFalse("Snapshot should be disabled with no configuration", isSnapshotSupported(manager));
105 
106     // force snapshot feature to be enabled
107     conf = new Configuration();
108     conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
109     manager = getNewManager(conf);
110     assertTrue("Snapshot should be enabled", isSnapshotSupported(manager));
111 
112     // force snapshot feature to be disabled
113     conf = new Configuration();
114     conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, false);
115     manager = getNewManager(conf);
116     assertFalse("Snapshot should be disabled", isSnapshotSupported(manager));
117 
118     // force snapshot feature to be disabled, even if cleaners are present
119     conf = new Configuration();
120     conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS,
121       SnapshotHFileCleaner.class.getName(), HFileLinkCleaner.class.getName());
122     conf.set(HConstants.HBASE_MASTER_LOGCLEANER_PLUGINS, SnapshotLogCleaner.class.getName());
123     conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, false);
124     manager = getNewManager(conf);
125     assertFalse("Snapshot should be disabled", isSnapshotSupported(manager));
126 
127     // cleaners are present, but missing snapshot enabled property
128     conf = new Configuration();
129     conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS,
130       SnapshotHFileCleaner.class.getName(), HFileLinkCleaner.class.getName());
131     conf.set(HConstants.HBASE_MASTER_LOGCLEANER_PLUGINS, SnapshotLogCleaner.class.getName());
132     manager = getNewManager(conf);
133     assertTrue("Snapshot should be enabled, because cleaners are present",
134       isSnapshotSupported(manager));
135 
136     // Create a "test snapshot"
137     Path rootDir = UTIL.getDataTestDir();
138     Path testSnapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(
139       "testSnapshotSupportConfiguration", rootDir);
140     fs.mkdirs(testSnapshotDir);
141     try {
142       // force snapshot feature to be disabled, but snapshots are present
143       conf = new Configuration();
144       conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, false);
145       manager = getNewManager(conf);
146       fail("Master should not start when snapshot is disabled, but snapshots are present");
147     } catch (UnsupportedOperationException e) {
148       // expected
149     } finally {
150       fs.delete(testSnapshotDir, true);
151     }
152   }
153 
154   private boolean isSnapshotSupported(final SnapshotManager manager) {
155     try {
156       manager.checkSnapshotSupport();
157       return true;
158     } catch (UnsupportedOperationException e) {
159       return false;
160     }
161   }
162 }