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.cleaner;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.List;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.hadoop.conf.Configuration;
33  import org.apache.hadoop.fs.FileStatus;
34  import org.apache.hadoop.fs.FileSystem;
35  import org.apache.hadoop.fs.Path;
36  import org.apache.hadoop.hbase.TableName;
37  import org.apache.hadoop.hbase.HBaseTestingUtility;
38  import org.apache.hadoop.hbase.HConstants;
39  import org.apache.hadoop.hbase.MediumTests;
40  import org.apache.hadoop.hbase.client.HBaseAdmin;
41  import org.apache.hadoop.hbase.client.HTable;
42  import org.apache.hadoop.hbase.master.HMaster;
43  import org.apache.hadoop.hbase.master.snapshot.DisabledTableSnapshotHandler;
44  import org.apache.hadoop.hbase.master.snapshot.SnapshotHFileCleaner;
45  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
46  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
47  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
48  import org.apache.hadoop.hbase.protobuf.generated.MasterAdminProtos.DeleteSnapshotRequest;
49  import org.apache.hadoop.hbase.protobuf.generated.MasterAdminProtos.IsSnapshotDoneRequest;
50  import org.apache.hadoop.hbase.protobuf.generated.MasterAdminProtos.IsSnapshotDoneResponse;
51  import org.apache.hadoop.hbase.protobuf.generated.MasterAdminProtos.ListSnapshotRequest;
52  import org.apache.hadoop.hbase.protobuf.generated.MasterAdminProtos.ListSnapshotResponse;
53  import org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy;
54  import org.apache.hadoop.hbase.regionserver.HRegion;
55  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
56  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
57  import org.apache.hadoop.hbase.snapshot.UnknownSnapshotException;
58  import org.apache.hadoop.hbase.util.Bytes;
59  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
60  import org.apache.hadoop.hbase.util.FSUtils;
61  import org.junit.After;
62  import org.junit.AfterClass;
63  import org.junit.Before;
64  import org.junit.BeforeClass;
65  import org.junit.Test;
66  import org.junit.experimental.categories.Category;
67  import org.mockito.Mockito;
68  
69  import com.google.common.collect.Lists;
70  import com.google.protobuf.ServiceException;
71  
72  /**
73   * Test the master-related aspects of a snapshot
74   */
75  @Category(MediumTests.class)
76  public class TestSnapshotFromMaster {
77  
78    private static final Log LOG = LogFactory.getLog(TestSnapshotFromMaster.class);
79    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
80    private static final int NUM_RS = 2;
81    private static Path rootDir;
82    private static Path snapshots;
83    private static FileSystem fs;
84    private static HMaster master;
85  
86    // for hfile archiving test.
87    private static Path archiveDir;
88    private static final byte[] TEST_FAM = Bytes.toBytes("fam");
89    private static final TableName TABLE_NAME =
90        TableName.valueOf("test");
91    // refresh the cache every 1/2 second
92    private static final long cacheRefreshPeriod = 500;
93  
94    /**
95     * Setup the config for the cluster
96     */
97    @BeforeClass
98    public static void setupCluster() throws Exception {
99      setupConf(UTIL.getConfiguration());
100     UTIL.startMiniCluster(NUM_RS);
101     fs = UTIL.getDFSCluster().getFileSystem();
102     master = UTIL.getMiniHBaseCluster().getMaster();
103     rootDir = master.getMasterFileSystem().getRootDir();
104     snapshots = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
105     archiveDir = new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY);
106   }
107 
108   private static void setupConf(Configuration conf) {
109     // disable the ui
110     conf.setInt("hbase.regionsever.info.port", -1);
111     // change the flush size to a small amount, regulating number of store files
112     conf.setInt("hbase.hregion.memstore.flush.size", 25000);
113     // so make sure we get a compaction when doing a load, but keep around some
114     // files in the store
115     conf.setInt("hbase.hstore.compaction.min", 3);
116     conf.setInt("hbase.hstore.compactionThreshold", 5);
117     // block writes if we get to 12 store files
118     conf.setInt("hbase.hstore.blockingStoreFiles", 12);
119     // Ensure no extra cleaners on by default (e.g. TimeToLiveHFileCleaner)
120     conf.set(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, "");
121     conf.set(HConstants.HBASE_MASTER_LOGCLEANER_PLUGINS, "");
122     // Enable snapshot
123     conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
124     conf.setLong(SnapshotHFileCleaner.HFILE_CACHE_REFRESH_PERIOD_CONF_KEY, cacheRefreshPeriod);
125     conf.set(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
126       ConstantSizeRegionSplitPolicy.class.getName());
127 
128   }
129 
130   @Before
131   public void setup() throws Exception {
132     UTIL.createTable(TABLE_NAME, TEST_FAM);
133     master.getSnapshotManagerForTesting().setSnapshotHandlerForTesting(TABLE_NAME, null);
134   }
135 
136   @After
137   public void tearDown() throws Exception {
138     UTIL.deleteTable(TABLE_NAME);
139     SnapshotTestingUtils.deleteAllSnapshots(UTIL.getHBaseAdmin());
140     SnapshotTestingUtils.deleteArchiveDirectory(UTIL);
141   }
142 
143   @AfterClass
144   public static void cleanupTest() throws Exception {
145     try {
146       UTIL.shutdownMiniCluster();
147     } catch (Exception e) {
148       // NOOP;
149     }
150   }
151 
152   /**
153    * Test that the contract from the master for checking on a snapshot are valid.
154    * <p>
155    * <ol>
156    * <li>If a snapshot fails with an error, we expect to get the source error.</li>
157    * <li>If there is no snapshot name supplied, we should get an error.</li>
158    * <li>If asking about a snapshot has hasn't occurred, you should get an error.</li>
159    * </ol>
160    */
161   @Test(timeout = 300000)
162   public void testIsDoneContract() throws Exception {
163 
164     IsSnapshotDoneRequest.Builder builder = IsSnapshotDoneRequest.newBuilder();
165 
166     String snapshotName = "asyncExpectedFailureTest";
167 
168     // check that we get an exception when looking up snapshot where one hasn't happened
169     SnapshotTestingUtils.expectSnapshotDoneException(master, builder.build(),
170       UnknownSnapshotException.class);
171 
172     // and that we get the same issue, even if we specify a name
173     SnapshotDescription desc = SnapshotDescription.newBuilder()
174       .setName(snapshotName).setTable(TABLE_NAME.getNameAsString()).build();
175     builder.setSnapshot(desc);
176     SnapshotTestingUtils.expectSnapshotDoneException(master, builder.build(),
177       UnknownSnapshotException.class);
178 
179     // set a mock handler to simulate a snapshot
180     DisabledTableSnapshotHandler mockHandler = Mockito.mock(DisabledTableSnapshotHandler.class);
181     Mockito.when(mockHandler.getException()).thenReturn(null);
182     Mockito.when(mockHandler.getSnapshot()).thenReturn(desc);
183     Mockito.when(mockHandler.isFinished()).thenReturn(new Boolean(true));
184     Mockito.when(mockHandler.getCompletionTimestamp())
185       .thenReturn(EnvironmentEdgeManager.currentTimeMillis());
186 
187     master.getSnapshotManagerForTesting()
188         .setSnapshotHandlerForTesting(TABLE_NAME, mockHandler);
189 
190     // if we do a lookup without a snapshot name, we should fail - you should always know your name
191     builder = IsSnapshotDoneRequest.newBuilder();
192     SnapshotTestingUtils.expectSnapshotDoneException(master, builder.build(),
193       UnknownSnapshotException.class);
194 
195     // then do the lookup for the snapshot that it is done
196     builder.setSnapshot(desc);
197     IsSnapshotDoneResponse response = master.isSnapshotDone(null, builder.build());
198     assertTrue("Snapshot didn't complete when it should have.", response.getDone());
199 
200     // now try the case where we are looking for a snapshot we didn't take
201     builder.setSnapshot(SnapshotDescription.newBuilder().setName("Not A Snapshot").build());
202     SnapshotTestingUtils.expectSnapshotDoneException(master, builder.build(),
203       UnknownSnapshotException.class);
204 
205     // then create a snapshot to the fs and make sure that we can find it when checking done
206     snapshotName = "completed";
207     Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
208     desc = desc.toBuilder().setName(snapshotName).build();
209     SnapshotDescriptionUtils.writeSnapshotInfo(desc, snapshotDir, fs);
210 
211     builder.setSnapshot(desc);
212     response = master.isSnapshotDone(null, builder.build());
213     assertTrue("Completed, on-disk snapshot not found", response.getDone());
214   }
215 
216   @Test(timeout = 300000)
217   public void testGetCompletedSnapshots() throws Exception {
218     // first check when there are no snapshots
219     ListSnapshotRequest request = ListSnapshotRequest.newBuilder().build();
220     ListSnapshotResponse response = master.getCompletedSnapshots(null, request);
221     assertEquals("Found unexpected number of snapshots", 0, response.getSnapshotsCount());
222 
223     // write one snapshot to the fs
224     String snapshotName = "completed";
225     Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
226     SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName(snapshotName).build();
227     SnapshotDescriptionUtils.writeSnapshotInfo(snapshot, snapshotDir, fs);
228 
229     // check that we get one snapshot
230     response = master.getCompletedSnapshots(null, request);
231     assertEquals("Found unexpected number of snapshots", 1, response.getSnapshotsCount());
232     List<SnapshotDescription> snapshots = response.getSnapshotsList();
233     List<SnapshotDescription> expected = Lists.newArrayList(snapshot);
234     assertEquals("Returned snapshots don't match created snapshots", expected, snapshots);
235 
236     // write a second snapshot
237     snapshotName = "completed_two";
238     snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
239     snapshot = SnapshotDescription.newBuilder().setName(snapshotName).build();
240     SnapshotDescriptionUtils.writeSnapshotInfo(snapshot, snapshotDir, fs);
241     expected.add(snapshot);
242 
243     // check that we get one snapshot
244     response = master.getCompletedSnapshots(null, request);
245     assertEquals("Found unexpected number of snapshots", 2, response.getSnapshotsCount());
246     snapshots = response.getSnapshotsList();
247     assertEquals("Returned snapshots don't match created snapshots", expected, snapshots);
248   }
249 
250   @Test(timeout = 300000)
251   public void testDeleteSnapshot() throws Exception {
252 
253     String snapshotName = "completed";
254     SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName(snapshotName).build();
255 
256     DeleteSnapshotRequest request = DeleteSnapshotRequest.newBuilder().setSnapshot(snapshot)
257         .build();
258     try {
259       master.deleteSnapshot(null, request);
260       fail("Master didn't throw exception when attempting to delete snapshot that doesn't exist");
261     } catch (ServiceException e) {
262       LOG.debug("Correctly failed delete of non-existant snapshot:" + e.getMessage());
263     }
264 
265     // write one snapshot to the fs
266     Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
267     SnapshotDescriptionUtils.writeSnapshotInfo(snapshot, snapshotDir, fs);
268 
269     // then delete the existing snapshot,which shouldn't cause an exception to be thrown
270     master.deleteSnapshot(null, request);
271   }
272 
273   /**
274    * Test that the snapshot hfile archive cleaner works correctly. HFiles that are in snapshots
275    * should be retained, while those that are not in a snapshot should be deleted.
276    * @throws Exception on failure
277    */
278   @Test(timeout = 300000)
279   public void testSnapshotHFileArchiving() throws Exception {
280     HBaseAdmin admin = UTIL.getHBaseAdmin();
281     // make sure we don't fail on listing snapshots
282     SnapshotTestingUtils.assertNoSnapshots(admin);
283     // load the table (creates 4 hfiles)
284     UTIL.loadTable(new HTable(UTIL.getConfiguration(), TABLE_NAME), TEST_FAM);
285 
286     // disable the table so we can take a snapshot
287     admin.disableTable(TABLE_NAME);
288 
289     // take a snapshot of the table
290     String snapshotName = "snapshot";
291     byte[] snapshotNameBytes = Bytes.toBytes(snapshotName);
292     admin.snapshot(snapshotNameBytes, TABLE_NAME);
293 
294     Configuration conf = master.getConfiguration();
295     LOG.info("After snapshot File-System state");
296     FSUtils.logFileSystemState(fs, rootDir, LOG);
297 
298     // ensure we only have one snapshot
299     SnapshotTestingUtils.assertOneSnapshotThatMatches(admin, snapshotNameBytes, TABLE_NAME);
300 
301     // renable the table so we can compact the regions
302     admin.enableTable(TABLE_NAME);
303 
304     // compact the files so we get some archived files for the table we just snapshotted
305     List<HRegion> regions = UTIL.getHBaseCluster().getRegions(TABLE_NAME);
306     for (HRegion region : regions) {
307       region.waitForFlushesAndCompactions(); // enable can trigger a compaction, wait for it.
308       region.compactStores(); // min is 3 so will compact and archive
309     }
310     LOG.info("After compaction File-System state");
311     FSUtils.logFileSystemState(fs, rootDir, LOG);
312 
313     // make sure the cleaner has run
314     LOG.debug("Running hfile cleaners");
315     ensureHFileCleanersRun();
316     LOG.info("After cleaners File-System state: " + rootDir);
317     FSUtils.logFileSystemState(fs, rootDir, LOG);
318 
319     // get the snapshot files for the table
320     Path snapshotTable = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
321     FileStatus[] snapshotHFiles = SnapshotTestingUtils.listHFiles(fs, snapshotTable);
322     // check that the files in the archive contain the ones that we need for the snapshot
323     LOG.debug("Have snapshot hfiles:");
324     for (FileStatus file : snapshotHFiles) {
325       LOG.debug(file.getPath());
326     }
327     // get the archived files for the table
328     Collection<String> files = getArchivedHFiles(archiveDir, rootDir, fs, TABLE_NAME);
329 
330     // and make sure that there is a proper subset
331     for (FileStatus file : snapshotHFiles) {
332       assertTrue("Archived hfiles " + files + " is missing snapshot file:" + file.getPath(),
333         files.contains(file.getPath().getName()));
334     }
335 
336     // delete the existing snapshot
337     admin.deleteSnapshot(snapshotNameBytes);
338     SnapshotTestingUtils.assertNoSnapshots(admin);
339 
340     // make sure that we don't keep around the hfiles that aren't in a snapshot
341     // make sure we wait long enough to refresh the snapshot hfile
342     List<BaseHFileCleanerDelegate> delegates = UTIL.getMiniHBaseCluster().getMaster()
343         .getHFileCleaner().cleanersChain;
344     for (BaseHFileCleanerDelegate delegate: delegates) {
345       if (delegate instanceof SnapshotHFileCleaner) {
346         ((SnapshotHFileCleaner)delegate).getFileCacheForTesting().triggerCacheRefreshForTesting();
347       }
348     }
349     // run the cleaner again
350     LOG.debug("Running hfile cleaners");
351     ensureHFileCleanersRun();
352     LOG.info("After delete snapshot cleaners run File-System state");
353     FSUtils.logFileSystemState(fs, rootDir, LOG);
354 
355     files = getArchivedHFiles(archiveDir, rootDir, fs, TABLE_NAME);
356     assertEquals("Still have some hfiles in the archive, when their snapshot has been deleted.", 0,
357       files.size());
358   }
359 
360   /**
361    * @return all the HFiles for a given table that have been archived
362    * @throws IOException on expected failure
363    */
364   private final Collection<String> getArchivedHFiles(Path archiveDir, Path rootDir,
365       FileSystem fs, TableName tableName) throws IOException {
366     Path tableArchive = FSUtils.getTableDir(archiveDir, tableName);
367     FileStatus[] archivedHFiles = SnapshotTestingUtils.listHFiles(fs, tableArchive);
368     List<String> files = new ArrayList<String>(archivedHFiles.length);
369     LOG.debug("Have archived hfiles: " + tableArchive);
370     for (FileStatus file : archivedHFiles) {
371       LOG.debug(file.getPath());
372       files.add(file.getPath().getName());
373     }
374     // sort the archived files
375 
376     Collections.sort(files);
377     return files;
378   }
379 
380   /**
381    * Make sure the {@link HFileCleaner HFileCleaners} run at least once
382    */
383   private static void ensureHFileCleanersRun() {
384     UTIL.getHBaseCluster().getMaster().getHFileCleaner().chore();
385   }
386 }