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