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  
22  import java.io.IOException;
23  import java.util.*;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.fs.FileStatus;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.testclassification.MediumTests;
32  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
33  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
34  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils.SnapshotMock;
35  import org.apache.hadoop.hbase.util.FSUtils;
36  import org.junit.After;
37  import org.junit.AfterClass;
38  import org.junit.BeforeClass;
39  import org.junit.Ignore;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  /**
44   * Test that we correctly reload the cache, filter directories, etc.
45   */
46  @Category(MediumTests.class)
47  public class TestSnapshotFileCache {
48  
49    private static final Log LOG = LogFactory.getLog(TestSnapshotFileCache.class);
50    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
51    private static long sequenceId = 0;
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    @Ignore("See HBASE-19275")
76    public void testLoadAndDelete() throws IOException {
77      // don't refresh the cache unless we tell it to
78      long period = Long.MAX_VALUE;
79      SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
80          "test-snapshot-file-cache-refresh", new SnapshotFiles());
81  
82      createAndTestSnapshotV1(cache, "snapshot1a", false, true);
83  
84      createAndTestSnapshotV2(cache, "snapshot2a", false, true);
85    }
86  
87    @Test
88    @Ignore("See HBASE-19275")
89    public void testReloadModifiedDirectory() throws IOException {
90      // don't refresh the cache unless we tell it to
91      long period = Long.MAX_VALUE;
92      SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
93          "test-snapshot-file-cache-refresh", new SnapshotFiles());
94  
95      createAndTestSnapshotV1(cache, "snapshot1", false, true);
96      // now delete the snapshot and add a file with a different name
97      createAndTestSnapshotV1(cache, "snapshot1", false, false);
98  
99      createAndTestSnapshotV2(cache, "snapshot2", false, true);
100     // now delete the snapshot and add a file with a different name
101     createAndTestSnapshotV2(cache, "snapshot2", false, false);
102   }
103 
104   @Test
105   public void testSnapshotTempDirReload() throws IOException {
106     long period = Long.MAX_VALUE;
107     // This doesn't refresh cache until we invoke it explicitly
108     SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
109         "test-snapshot-file-cache-refresh", new SnapshotFiles());
110 
111     // Add a new non-tmp snapshot
112     createAndTestSnapshotV1(cache, "snapshot0v1", false, false);
113     createAndTestSnapshotV1(cache, "snapshot0v2", false, false);
114   }
115 
116   class SnapshotFiles implements SnapshotFileCache.SnapshotFileInspector {
117     public Collection<String> filesUnderSnapshot(final Path snapshotDir) throws IOException {
118       Collection<String> files =  new HashSet<String>();
119       files.addAll(SnapshotReferenceUtil.getHFileNames(UTIL.getConfiguration(), fs, snapshotDir));
120       return files;
121     }
122   };
123 
124   private SnapshotMock.SnapshotBuilder createAndTestSnapshotV1(final SnapshotFileCache cache,
125       final String name, final boolean tmp, final boolean removeOnExit) throws IOException {
126     SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
127     SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV1(name, name);
128     createAndTestSnapshot(cache, builder, tmp, removeOnExit);
129     return builder;
130   }
131 
132   private void createAndTestSnapshotV2(final SnapshotFileCache cache, final String name,
133       final boolean tmp, final boolean removeOnExit) throws IOException {
134     SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
135     SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(name, name);
136     createAndTestSnapshot(cache, builder, tmp, removeOnExit);
137   }
138 
139   private void createAndTestSnapshot(final SnapshotFileCache cache,
140       final SnapshotMock.SnapshotBuilder builder,
141       final boolean tmp, final boolean removeOnExit) throws IOException {
142     List<Path> files = new ArrayList<Path>();
143     for (int i = 0; i < 3; ++i) {
144       for (Path filePath: builder.addRegion()) {
145         files.add(filePath);
146       }
147     }
148 
149     // Finalize the snapshot
150     builder.commit();
151 
152     // Make sure that all files are still present
153     for (Path path: files) {
154       assertFalse("Cache didn't find " + path, contains(getNonSnapshotFiles(cache, path), path));
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         assertFalse("Cache didn't find " + filePath,
166           contains(getNonSnapshotFiles(cache, filePath), filePath));
167       }
168 
169       // then trigger a refresh
170       cache.triggerCacheRefreshForTesting();
171 
172       // and not it shouldn't find those files
173       for (Path filePath: files) {
174         assertFalse("Cache found '" + filePath + "', but it shouldn't have.",
175             contains(getNonSnapshotFiles(cache, filePath), filePath));
176       }
177     }
178   }
179 
180   private static boolean contains(Iterable<FileStatus> files, Path filePath) {
181     for (FileStatus status: files) {
182       if (filePath.equals(status.getPath())) {
183         return true;
184       }
185     }
186     return false;
187   }
188 
189   private static Iterable<FileStatus> getNonSnapshotFiles(SnapshotFileCache cache, Path storeFile)
190       throws IOException {
191     return cache.getUnreferencedFiles(
192         Arrays.asList(FSUtils.listStatus(fs, storeFile.getParent())), null
193     );
194   }
195 }