1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
91
92 @Test
93 public void testSnapshotSupportConfiguration() throws Exception {
94
95 Configuration conf = new Configuration();
96 SnapshotManager manager = getNewManager(conf);
97 assertFalse("Snapshot should be disabled with no configuration", isSnapshotSupported(manager));
98
99
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
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
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
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
130 Path rootDir = UTIL.getDataTestDir();
131 Path testSnapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(
132 "testSnapshotSupportConfiguration", rootDir);
133 fs.mkdirs(testSnapshotDir);
134 try {
135
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
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 }