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.HBaseTestingUtility;
37  import org.apache.hadoop.hbase.HConstants;
38  import org.apache.hadoop.hbase.HTableDescriptor;
39  import org.apache.hadoop.hbase.MediumTests;
40  import org.apache.hadoop.hbase.TableName;
41  import org.apache.hadoop.hbase.client.HBaseAdmin;
42  import org.apache.hadoop.hbase.client.HTable;
43  import org.apache.hadoop.hbase.master.HMaster;
44  import org.apache.hadoop.hbase.master.snapshot.DisabledTableSnapshotHandler;
45  import org.apache.hadoop.hbase.master.snapshot.SnapshotHFileCleaner;
46  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
47  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
48  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.DeleteSnapshotRequest;
49  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetCompletedSnapshotsRequest;
50  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetCompletedSnapshotsResponse;
51  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsSnapshotDoneRequest;
52  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsSnapshotDoneResponse;
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     GetCompletedSnapshotsRequest request = GetCompletedSnapshotsRequest.newBuilder().build();
220     GetCompletedSnapshotsResponse 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 
284     // recreate test table with disabled compactions; otherwise compaction may happen before
285     // snapshot, the call after snapshot will be a no-op and checks will fail
286     UTIL.deleteTable(TABLE_NAME);
287     HTableDescriptor htd = new HTableDescriptor(TABLE_NAME);
288     htd.setCompactionEnabled(false);
289     UTIL.createTable(htd, new byte[][] { TEST_FAM }, UTIL.getConfiguration());
290     // load the table (creates 4 hfiles)
291     UTIL.loadTable(new HTable(UTIL.getConfiguration(), TABLE_NAME), TEST_FAM);
292     UTIL.flush(TABLE_NAME);
293     // Put some more data into the table so for sure we get more storefiles.
294     UTIL.loadTable(new HTable(UTIL.getConfiguration(), TABLE_NAME), TEST_FAM);
295 
296     // disable the table so we can take a snapshot
297     admin.disableTable(TABLE_NAME);
298     htd.setCompactionEnabled(true);
299 
300     // take a snapshot of the table
301     String snapshotName = "snapshot";
302     byte[] snapshotNameBytes = Bytes.toBytes(snapshotName);
303     admin.snapshot(snapshotNameBytes, TABLE_NAME);
304 
305     LOG.info("After snapshot File-System state");
306     FSUtils.logFileSystemState(fs, rootDir, LOG);
307 
308     // ensure we only have one snapshot
309     SnapshotTestingUtils.assertOneSnapshotThatMatches(admin, snapshotNameBytes, TABLE_NAME);
310 
311     // enable compactions now
312     admin.modifyTable(TABLE_NAME, htd);
313 
314     // renable the table so we can compact the regions
315     admin.enableTable(TABLE_NAME);
316 
317     // compact the files so we get some archived files for the table we just snapshotted
318     List<HRegion> regions = UTIL.getHBaseCluster().getRegions(TABLE_NAME);
319     for (HRegion region : regions) {
320       region.waitForFlushesAndCompactions(); // enable can trigger a compaction, wait for it.
321       region.compactStores(); // min is 3 so will compact and archive
322     }
323     LOG.info("After compaction File-System state");
324     FSUtils.logFileSystemState(fs, rootDir, LOG);
325 
326     // make sure the cleaner has run
327     LOG.debug("Running hfile cleaners");
328     ensureHFileCleanersRun();
329     LOG.info("After cleaners File-System state: " + rootDir);
330     FSUtils.logFileSystemState(fs, rootDir, LOG);
331 
332     // get the snapshot files for the table
333     Path snapshotTable = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
334     Path[] snapshotHFiles = SnapshotTestingUtils.listHFiles(fs, snapshotTable);
335     // check that the files in the archive contain the ones that we need for the snapshot
336     LOG.debug("Have snapshot hfiles:");
337     for (Path file : snapshotHFiles) {
338       LOG.debug(file);
339     }
340     // get the archived files for the table
341     Collection<String> files = getArchivedHFiles(archiveDir, rootDir, fs, TABLE_NAME);
342 
343     // and make sure that there is a proper subset
344     for (Path file : snapshotHFiles) {
345       assertTrue("Archived hfiles " + files + " is missing snapshot file:" + file,
346         files.contains(file.getName()));
347     }
348 
349     // delete the existing snapshot
350     admin.deleteSnapshot(snapshotNameBytes);
351     SnapshotTestingUtils.assertNoSnapshots(admin);
352 
353     // make sure that we don't keep around the hfiles that aren't in a snapshot
354     // make sure we wait long enough to refresh the snapshot hfile
355     List<BaseHFileCleanerDelegate> delegates = UTIL.getMiniHBaseCluster().getMaster()
356         .getHFileCleaner().cleanersChain;
357     for (BaseHFileCleanerDelegate delegate: delegates) {
358       if (delegate instanceof SnapshotHFileCleaner) {
359         ((SnapshotHFileCleaner)delegate).getFileCacheForTesting().triggerCacheRefreshForTesting();
360       }
361     }
362     // run the cleaner again
363     LOG.debug("Running hfile cleaners");
364     ensureHFileCleanersRun();
365     LOG.info("After delete snapshot cleaners run File-System state");
366     FSUtils.logFileSystemState(fs, rootDir, LOG);
367 
368     files = getArchivedHFiles(archiveDir, rootDir, fs, TABLE_NAME);
369     assertEquals("Still have some hfiles in the archive, when their snapshot has been deleted.", 0,
370       files.size());
371   }
372 
373   /**
374    * @return all the HFiles for a given table that have been archived
375    * @throws IOException on expected failure
376    */
377   private final Collection<String> getArchivedHFiles(Path archiveDir, Path rootDir,
378       FileSystem fs, TableName tableName) throws IOException {
379     Path tableArchive = FSUtils.getTableDir(archiveDir, tableName);
380     Path[] archivedHFiles = SnapshotTestingUtils.listHFiles(fs, tableArchive);
381     List<String> files = new ArrayList<String>(archivedHFiles.length);
382     LOG.debug("Have archived hfiles: " + tableArchive);
383     for (Path file : archivedHFiles) {
384       LOG.debug(file);
385       files.add(file.getName());
386     }
387     // sort the archived files
388 
389     Collections.sort(files);
390     return files;
391   }
392 
393   /**
394    * Make sure the {@link HFileCleaner HFileCleaners} run at least once
395    */
396   private static void ensureHFileCleanersRun() {
397     UTIL.getHBaseCluster().getMaster().getHFileCleaner().chore();
398   }
399 }