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  
23  import java.io.IOException;
24  import java.util.*;
25  
26  import com.google.common.collect.Iterables;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.fs.FileStatus;
30  import org.apache.hadoop.fs.FileSystem;
31  import org.apache.hadoop.fs.Path;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.testclassification.MediumTests;
34  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
35  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
36  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils.SnapshotMock;
37  import org.apache.hadoop.hbase.util.FSUtils;
38  import org.junit.After;
39  import org.junit.AfterClass;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  /**
45   * Test that we correctly reload the cache, filter directories, etc.
46   */
47  @Category(MediumTests.class)
48  public class TestSnapshotFileCache {
49  
50    private static final Log LOG = LogFactory.getLog(TestSnapshotFileCache.class);
51    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
52    private static FileSystem fs;
53    private static Path rootDir;
54  
55    @BeforeClass
56    public static void startCluster() throws Exception {
57      UTIL.startMiniDFSCluster(1);
58      fs = UTIL.getDFSCluster().getFileSystem();
59      rootDir = UTIL.getDefaultRootDirPath();
60    }
61  
62    @AfterClass
63    public static void stopCluster() throws Exception {
64      UTIL.shutdownMiniDFSCluster();
65    }
66  
67    @After
68    public void cleanupFiles() throws Exception {
69      // cleanup the snapshot directory
70      Path snapshotDir = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
71      fs.delete(snapshotDir, true);
72    }
73  
74    @Test(timeout = 10000000)
75    public void testLoadAndDelete() throws IOException {
76      // don't refresh the cache unless we tell it to
77      long period = Long.MAX_VALUE;
78      SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
79          "test-snapshot-file-cache-refresh", new SnapshotFiles());
80  
81      createAndTestSnapshotV1(cache, "snapshot1a", false, true);
82  
83      createAndTestSnapshotV2(cache, "snapshot2a", false, true);
84    }
85  
86    @Test
87    public void testReloadModifiedDirectory() throws IOException {
88      // don't refresh the cache unless we tell it to
89      long period = Long.MAX_VALUE;
90      SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
91          "test-snapshot-file-cache-refresh", new SnapshotFiles());
92  
93      createAndTestSnapshotV1(cache, "snapshot1", false, true);
94      // now delete the snapshot and add a file with a different name
95      createAndTestSnapshotV1(cache, "snapshot1", false, false);
96  
97      createAndTestSnapshotV2(cache, "snapshot2", false, true);
98      // now delete the snapshot and add a file with a different name
99      createAndTestSnapshotV2(cache, "snapshot2", false, false);
100   }
101 
102   @Test
103   public void testSnapshotTempDirReload() throws IOException {
104     long period = Long.MAX_VALUE;
105     // This doesn't refresh cache until we invoke it explicitly
106     SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
107         "test-snapshot-file-cache-refresh", new SnapshotFiles());
108 
109     // Add a new non-tmp snapshot
110     createAndTestSnapshotV1(cache, "snapshot0v1", false, false);
111     createAndTestSnapshotV1(cache, "snapshot0v2", false, false);
112   }
113 
114   class SnapshotFiles implements SnapshotFileCache.SnapshotFileInspector {
115     public Collection<String> filesUnderSnapshot(final Path snapshotDir) throws IOException {
116       Collection<String> files =  new HashSet<String>();
117       files.addAll(SnapshotReferenceUtil.getHFileNames(UTIL.getConfiguration(), fs, snapshotDir));
118       return files;
119     }
120   };
121 
122   private SnapshotMock.SnapshotBuilder createAndTestSnapshotV1(final SnapshotFileCache cache,
123       final String name, final boolean tmp, final boolean removeOnExit) throws IOException {
124     SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
125     SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV1(name, name);
126     createAndTestSnapshot(cache, builder, tmp, removeOnExit);
127     return builder;
128   }
129 
130   private void createAndTestSnapshotV2(final SnapshotFileCache cache, final String name,
131       final boolean tmp, final boolean removeOnExit) throws IOException {
132     SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
133     SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(name, name);
134     createAndTestSnapshot(cache, builder, tmp, removeOnExit);
135   }
136 
137   private void createAndTestSnapshot(final SnapshotFileCache cache,
138       final SnapshotMock.SnapshotBuilder builder,
139       final boolean tmp, final boolean removeOnExit) throws IOException {
140     List<Path> files = new ArrayList<Path>();
141     for (int i = 0; i < 3; ++i) {
142       for (Path filePath: builder.addRegion()) {
143         files.add(filePath);
144       }
145     }
146 
147     // Finalize the snapshot
148     builder.commit();
149 
150     // Make sure that all files are still present
151     for (Path path: files) {
152       Iterable<FileStatus> nonSnapshotFiles = getNonSnapshotFiles(cache, path);
153       assertFalse("Cache didn't find " + path.getName(),
154           Iterables.contains(nonSnapshotFiles, path.getName()));
155     }
156 
157     FSUtils.logFileSystemState(fs, rootDir, LOG);
158     if (removeOnExit) {
159       LOG.debug("Deleting snapshot.");
160       fs.delete(builder.getSnapshotsDir(), true);
161       FSUtils.logFileSystemState(fs, rootDir, LOG);
162 
163       // The files should be in cache until next refresh
164       for (Path filePath: files) {
165         Iterable<FileStatus> nonSnapshotFiles = getNonSnapshotFiles(cache, filePath);
166         assertFalse("Cache didn't find " + filePath.getName(), Iterables.contains(nonSnapshotFiles,
167             filePath.getName()));
168       }
169 
170       // then trigger a refresh
171       cache.triggerCacheRefreshForTesting();
172       // and not it shouldn't find those files
173       for (Path filePath: files) {
174         Iterable<FileStatus> nonSnapshotFiles = getNonSnapshotFiles(cache, filePath);
175         assertTrue("Cache found '" + filePath.getName() + "', but it shouldn't have.",
176             !Iterables.contains(nonSnapshotFiles, filePath.getName()));
177       }
178     }
179   }
180 
181   private Iterable<FileStatus> getNonSnapshotFiles(SnapshotFileCache cache, Path storeFile)
182       throws IOException {
183     return cache.getUnreferencedFiles(
184         Arrays.asList(FSUtils.listStatus(fs, storeFile.getParent())), null
185     );
186   }
187 }