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.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.when;
25  
26  import java.io.IOException;
27  import java.util.*;
28  import java.util.concurrent.atomic.AtomicInteger;
29  
30  import com.google.common.collect.Iterables;
31  import com.google.common.collect.Lists;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.hadoop.fs.FileStatus;
35  import org.apache.hadoop.fs.FileSystem;
36  import org.apache.hadoop.fs.Path;
37  import org.apache.hadoop.hbase.HBaseTestingUtility;
38  import org.apache.hadoop.hbase.HRegionInfo;
39  import org.apache.hadoop.hbase.protobuf.generated.SnapshotProtos;
40  import org.apache.hadoop.hbase.testclassification.MediumTests;
41  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
42  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
43  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils.SnapshotMock;
44  import org.apache.hadoop.hbase.util.FSUtils;
45  import org.junit.After;
46  import org.junit.AfterClass;
47  import org.junit.BeforeClass;
48  import org.junit.Ignore;
49  import org.junit.Test;
50  import org.junit.experimental.categories.Category;
51  
52  /**
53   * Test that we correctly reload the cache, filter directories, etc.
54   */
55  @Category(MediumTests.class)
56  public class TestSnapshotFileCache {
57  
58    private static final Log LOG = LogFactory.getLog(TestSnapshotFileCache.class);
59    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
60    private static FileSystem fs;
61    private static Path rootDir;
62  
63    @BeforeClass
64    public static void startCluster() throws Exception {
65      UTIL.startMiniDFSCluster(1);
66      fs = UTIL.getDFSCluster().getFileSystem();
67      rootDir = UTIL.getDefaultRootDirPath();
68    }
69  
70    @AfterClass
71    public static void stopCluster() throws Exception {
72      UTIL.shutdownMiniDFSCluster();
73    }
74  
75    @After
76    public void cleanupFiles() throws Exception {
77      // cleanup the snapshot directory
78      Path snapshotDir = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
79      fs.delete(snapshotDir, true);
80    }
81  
82    @Test(timeout = 10000000)
83    @Ignore("See HBASE-19275")
84    public void testLoadAndDelete() throws IOException {
85      // don't refresh the cache unless we tell it to
86      long period = Long.MAX_VALUE;
87      SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
88          "test-snapshot-file-cache-refresh", new SnapshotFiles());
89  
90      createAndTestSnapshotV1(cache, "snapshot1a", false, true);
91      createAndTestSnapshotV1(cache, "snapshot1b", true, true);
92  
93      createAndTestSnapshotV2(cache, "snapshot2a", false, true);
94      createAndTestSnapshotV2(cache, "snapshot2b", true, true);
95    }
96  
97    @Test
98    @Ignore("See HBASE-19275")
99    public void testReloadModifiedDirectory() throws IOException {
100     // don't refresh the cache unless we tell it to
101     long period = Long.MAX_VALUE;
102     SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
103         "test-snapshot-file-cache-refresh", new SnapshotFiles());
104 
105     createAndTestSnapshotV1(cache, "snapshot1", false, true);
106     // now delete the snapshot and add a file with a different name
107     createAndTestSnapshotV1(cache, "snapshot1", false, false);
108 
109     createAndTestSnapshotV2(cache, "snapshot2", false, true);
110     // now delete the snapshot and add a file with a different name
111     createAndTestSnapshotV2(cache, "snapshot2", false, false);
112   }
113 
114   @Test
115   public void testSnapshotTempDirReload() throws IOException {
116     long period = Long.MAX_VALUE;
117     // This doesn't refresh cache until we invoke it explicitly
118     SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
119         "test-snapshot-file-cache-refresh", new SnapshotFiles());
120 
121     // Add a new non-tmp snapshot
122     createAndTestSnapshotV1(cache, "snapshot0v1", false, false);
123     createAndTestSnapshotV1(cache, "snapshot0v2", false, false);
124 
125     // Add a new tmp snapshot
126     createAndTestSnapshotV2(cache, "snapshot1", true, false);
127 
128     // Add another tmp snapshot
129     createAndTestSnapshotV2(cache, "snapshot2", true, false);
130   }
131 
132   @Test
133   public void testWeNeverCacheTmpDirAndLoadIt() throws Exception {
134 
135     final AtomicInteger count = new AtomicInteger(0);
136     // don't refresh the cache unless we tell it to
137     long period = Long.MAX_VALUE;
138     SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
139         "test-snapshot-file-cache-refresh", new SnapshotFiles()) {
140       @Override
141       List<String> getSnapshotsInProgress(final SnapshotManager snapshotManager)
142               throws IOException {
143         List<String> result = super.getSnapshotsInProgress(snapshotManager);
144         count.incrementAndGet();
145         return result;
146       }
147 
148       @Override public void triggerCacheRefreshForTesting() {
149         super.triggerCacheRefreshForTesting();
150       }
151     };
152 
153     SnapshotMock.SnapshotBuilder complete =
154         createAndTestSnapshotV1(cache, "snapshot", false, false);
155 
156     int countBeforeCheck = count.get();
157 
158     FSUtils.logFileSystemState(fs, rootDir, LOG);
159 
160     List<FileStatus> allStoreFiles = getStoreFilesForSnapshot(complete);
161     Iterable<FileStatus> deletableFiles = cache.getUnreferencedFiles(allStoreFiles, null);
162     assertTrue(Iterables.isEmpty(deletableFiles));
163     // no need for tmp dir check as all files are accounted for.
164     assertEquals(0, count.get() - countBeforeCheck);
165 
166 
167     // add a random file to make sure we refresh
168     FileStatus randomFile = mockStoreFile(UUID.randomUUID().toString());
169     allStoreFiles.add(randomFile);
170     deletableFiles = cache.getUnreferencedFiles(allStoreFiles, null);
171     assertEquals(randomFile, Iterables.getOnlyElement(deletableFiles));
172     assertEquals(1, count.get() - countBeforeCheck); // we check the tmp directory
173   }
174 
175   private List<FileStatus> getStoreFilesForSnapshot(SnapshotMock.SnapshotBuilder builder)
176       throws IOException {
177     final List<FileStatus> allStoreFiles = Lists.newArrayList();
178     SnapshotReferenceUtil
179         .visitReferencedFiles(UTIL.getConfiguration(), fs, builder.getSnapshotsDir(),
180             new SnapshotReferenceUtil.SnapshotVisitor() {
181               @Override public void storeFile(HRegionInfo regionInfo, String familyName,
182                   SnapshotProtos.SnapshotRegionManifest.StoreFile storeFile) throws IOException {
183                 FileStatus status = mockStoreFile(storeFile.getName());
184                 allStoreFiles.add(status);
185               }
186             });
187     return allStoreFiles;
188   }
189 
190   private FileStatus mockStoreFile(String storeFileName) {
191     FileStatus status = mock(FileStatus.class);
192     Path path = mock(Path.class);
193     when(path.getName()).thenReturn(storeFileName);
194     when(status.getPath()).thenReturn(path);
195     return status;
196   }
197 
198   class SnapshotFiles implements SnapshotFileCache.SnapshotFileInspector {
199     public Collection<String> filesUnderSnapshot(final Path snapshotDir) throws IOException {
200       Collection<String> files =  new HashSet<String>();
201       files.addAll(SnapshotReferenceUtil.getHFileNames(UTIL.getConfiguration(), fs, snapshotDir));
202       return files;
203     }
204   };
205 
206   private SnapshotMock.SnapshotBuilder createAndTestSnapshotV1(final SnapshotFileCache cache,
207       final String name, final boolean tmp, final boolean removeOnExit) throws IOException {
208     SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
209     SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV1(name, name);
210     createAndTestSnapshot(cache, builder, tmp, removeOnExit);
211     return builder;
212   }
213 
214   private void createAndTestSnapshotV2(final SnapshotFileCache cache, final String name,
215       final boolean tmp, final boolean removeOnExit) throws IOException {
216     SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
217     SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(name, name);
218     createAndTestSnapshot(cache, builder, tmp, removeOnExit);
219   }
220 
221   private void createAndTestSnapshot(final SnapshotFileCache cache,
222       final SnapshotMock.SnapshotBuilder builder,
223       final boolean tmp, final boolean removeOnExit) throws IOException {
224     List<Path> files = new ArrayList<Path>();
225     for (int i = 0; i < 3; ++i) {
226       for (Path filePath: builder.addRegion()) {
227         if (tmp) {
228           // We should be able to find all the files while the snapshot creation is in-progress
229           FSUtils.logFileSystemState(fs, rootDir, LOG);
230           assertFalse("Cache didn't find " + filePath,
231             contains(getNonSnapshotFiles(cache, filePath), filePath));
232         }
233         files.add(filePath);
234       }
235     }
236 
237     // Finalize the snapshot
238     if (!tmp) {
239       builder.commit();
240     }
241 
242     // Make sure that all files are still present
243     for (Path path: files) {
244       assertFalse("Cache didn't find " + path, contains(getNonSnapshotFiles(cache, path), path));
245     }
246 
247     FSUtils.logFileSystemState(fs, rootDir, LOG);
248     if (removeOnExit) {
249       LOG.debug("Deleting snapshot.");
250       fs.delete(builder.getSnapshotsDir(), true);
251       FSUtils.logFileSystemState(fs, rootDir, LOG);
252 
253       // The files should be in cache until next refresh
254       for (Path filePath: files) {
255         assertFalse("Cache didn't find " + filePath,
256           contains(getNonSnapshotFiles(cache, filePath), filePath));
257       }
258 
259       // then trigger a refresh
260       cache.triggerCacheRefreshForTesting();
261 
262       // and not it shouldn't find those files
263       for (Path filePath: files) {
264         assertFalse("Cache found '" + filePath + "', but it shouldn't have.",
265             contains(getNonSnapshotFiles(cache, filePath), filePath));
266       }
267     }
268   }
269 
270   private static boolean contains(Iterable<FileStatus> files, Path filePath) {
271     for (FileStatus status: files) {
272       if (filePath.equals(status.getPath())) {
273         return true;
274       }
275     }
276     return false;
277   }
278 
279   private static Iterable<FileStatus> getNonSnapshotFiles(SnapshotFileCache cache, Path storeFile)
280       throws IOException {
281     return cache.getUnreferencedFiles(
282         Arrays.asList(FSUtils.listStatus(fs, storeFile.getParent())), null
283     );
284   }
285 }