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.testclassification.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.MetricsMaster;
36  import org.apache.hadoop.hbase.master.cleaner.HFileCleaner;
37  import org.apache.hadoop.hbase.master.cleaner.HFileLinkCleaner;
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    MetricsMaster metrics = Mockito.mock(MetricsMaster.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      TableName tableName = TableName.valueOf("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.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, false);
123     manager = getNewManager(conf);
124     assertFalse("Snapshot should be disabled", isSnapshotSupported(manager));
125 
126     // cleaners are present, but missing snapshot enabled property
127     conf = new Configuration();
128     conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS,
129       SnapshotHFileCleaner.class.getName(), HFileLinkCleaner.class.getName());
130     manager = getNewManager(conf);
131     assertTrue("Snapshot should be enabled, because cleaners are present",
132       isSnapshotSupported(manager));
133 
134     // Create a "test snapshot"
135     Path rootDir = UTIL.getDataTestDir();
136     Path testSnapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(
137       "testSnapshotSupportConfiguration", rootDir);
138     fs.mkdirs(testSnapshotDir);
139     try {
140       // force snapshot feature to be disabled, but snapshots are present
141       conf = new Configuration();
142       conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, false);
143       manager = getNewManager(conf);
144       fail("Master should not start when snapshot is disabled, but snapshots are present");
145     } catch (UnsupportedOperationException e) {
146       // expected
147     } finally {
148       fs.delete(testSnapshotDir, true);
149     }
150   }
151 
152   private boolean isSnapshotSupported(final SnapshotManager manager) {
153     try {
154       manager.checkSnapshotSupport();
155       return true;
156     } catch (UnsupportedOperationException e) {
157       return false;
158     }
159   }
160 }