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.Collection;
25  import java.util.ArrayList;
26  import java.util.HashSet;
27  import java.util.List;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.hbase.HBaseTestingUtility;
34  import org.apache.hadoop.hbase.MediumTests;
35  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
36  import org.apache.hadoop.hbase.regionserver.wal.HLogUtil;
37  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
38  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
39  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils.SnapshotMock;
40  import org.apache.hadoop.hbase.util.FSUtils;
41  import org.junit.After;
42  import org.junit.AfterClass;
43  import org.junit.BeforeClass;
44  import org.junit.Test;
45  import org.junit.experimental.categories.Category;
46  
47  /**
48   * Test that we correctly reload the cache, filter directories, etc.
49   */
50  @Category(MediumTests.class)
51  public class TestSnapshotFileCache {
52  
53    private static final Log LOG = LogFactory.getLog(TestSnapshotFileCache.class);
54    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
55    private static long sequenceId = 0;
56    private static FileSystem fs;
57    private static Path rootDir;
58  
59    @BeforeClass
60    public static void startCluster() throws Exception {
61      UTIL.startMiniDFSCluster(1);
62      fs = UTIL.getDFSCluster().getFileSystem();
63      rootDir = UTIL.getDefaultRootDirPath();
64    }
65  
66    @AfterClass
67    public static void stopCluster() throws Exception {
68      UTIL.shutdownMiniDFSCluster();
69    }
70  
71    @After
72    public void cleanupFiles() throws Exception {
73      // cleanup the snapshot directory
74      Path snapshotDir = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
75      fs.delete(snapshotDir, true);
76    }
77  
78    @Test(timeout = 10000000)
79    public void testLoadAndDelete() throws IOException {
80      // don't refresh the cache unless we tell it to
81      long period = Long.MAX_VALUE;
82      SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
83          "test-snapshot-file-cache-refresh", new SnapshotFiles());
84  
85      createAndTestSnapshotV1(cache, "snapshot1a", false, true);
86      createAndTestSnapshotV1(cache, "snapshot1b", true, true);
87  
88      createAndTestSnapshotV2(cache, "snapshot2a", false, true);
89      createAndTestSnapshotV2(cache, "snapshot2b", true, true);
90    }
91  
92    @Test
93    public void testJustFindLogsDirectory() throws Exception {
94      // don't refresh the cache unless we tell it to
95      long period = Long.MAX_VALUE;
96      Path snapshotDir = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
97      SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
98          "test-snapshot-file-cache-refresh", new SnapshotFileCache.SnapshotFileInspector() {
99              public Collection<String> filesUnderSnapshot(final Path snapshotDir)
100                 throws IOException {
101               return SnapshotReferenceUtil.getHLogNames(fs, snapshotDir);
102             }
103         });
104 
105     // create a file in a 'completed' snapshot
106     SnapshotDescription desc = SnapshotDescription.newBuilder().setName("snapshot").build();
107     Path snapshot = SnapshotDescriptionUtils.getCompletedSnapshotDir(desc, rootDir);
108     SnapshotDescriptionUtils.writeSnapshotInfo(desc, snapshot, fs);
109     Path file1 = new Path(new Path(new Path(snapshot, "7e91021"), "fam"), "file1");
110     fs.createNewFile(file1);
111 
112     // and another file in the logs directory
113     Path logs = getSnapshotHLogsDir(snapshot, "server");
114     Path log = new Path(logs, "me.hbase.com%2C58939%2C1350424310315.1350424315552");
115     fs.createNewFile(log);
116 
117     FSUtils.logFileSystemState(fs, rootDir, LOG);
118 
119     // then make sure the cache only finds the log files
120     assertFalse("Cache found '" + file1 + "', but it shouldn't have.",
121       cache.contains(file1.getName()));
122     assertTrue("Cache didn't find:" + log, cache.contains(log.getName()));
123   }
124 
125   /**
126    * Get the log directory for a specific snapshot
127    * @param snapshotDir directory where the specific snapshot will be store
128    * @param serverName name of the parent regionserver for the log files
129    * @return path to the log home directory for the archive files.
130    */
131   public static Path getSnapshotHLogsDir(Path snapshotDir, String serverName) {
132     return new Path(snapshotDir, HLogUtil.getHLogDirectoryName(serverName));
133   }
134 
135   @Test
136   public void testReloadModifiedDirectory() throws IOException {
137     // don't refresh the cache unless we tell it to
138     long period = Long.MAX_VALUE;
139     SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
140         "test-snapshot-file-cache-refresh", new SnapshotFiles());
141 
142     createAndTestSnapshotV1(cache, "snapshot1", false, true);
143     // now delete the snapshot and add a file with a different name
144     createAndTestSnapshotV1(cache, "snapshot1", false, false);
145 
146     createAndTestSnapshotV2(cache, "snapshot2", false, true);
147     // now delete the snapshot and add a file with a different name
148     createAndTestSnapshotV2(cache, "snapshot2", false, false);
149   }
150 
151   @Test
152   public void testSnapshotTempDirReload() throws IOException {
153     long period = Long.MAX_VALUE;
154     // This doesn't refresh cache until we invoke it explicitly
155     SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
156         "test-snapshot-file-cache-refresh", new SnapshotFiles());
157 
158     // Add a new non-tmp snapshot
159     createAndTestSnapshotV1(cache, "snapshot0v1", false, false);
160     createAndTestSnapshotV1(cache, "snapshot0v2", false, false);
161 
162     // Add a new tmp snapshot
163     createAndTestSnapshotV2(cache, "snapshot1", true, false);
164 
165     // Add another tmp snapshot
166     createAndTestSnapshotV2(cache, "snapshot2", true, false);
167   }
168 
169   class SnapshotFiles implements SnapshotFileCache.SnapshotFileInspector {
170     public Collection<String> filesUnderSnapshot(final Path snapshotDir) throws IOException {
171       Collection<String> files =  new HashSet<String>();
172       files.addAll(SnapshotReferenceUtil.getHLogNames(fs, snapshotDir));
173       files.addAll(SnapshotReferenceUtil.getHFileNames(UTIL.getConfiguration(), fs, snapshotDir));
174       return files;
175     }
176   };
177 
178   private void createAndTestSnapshotV1(final SnapshotFileCache cache, final String name,
179       final boolean tmp, final boolean removeOnExit) throws IOException {
180     SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
181     SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV1(name);
182     createAndTestSnapshot(cache, builder, tmp, removeOnExit);
183   }
184 
185   private void createAndTestSnapshotV2(final SnapshotFileCache cache, final String name,
186       final boolean tmp, final boolean removeOnExit) throws IOException {
187     SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
188     SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(name);
189     createAndTestSnapshot(cache, builder, tmp, removeOnExit);
190   }
191 
192   private void createAndTestSnapshot(final SnapshotFileCache cache,
193       final SnapshotMock.SnapshotBuilder builder,
194       final boolean tmp, final boolean removeOnExit) throws IOException {
195     List<String> files = new ArrayList<String>();
196     for (int i = 0; i < 3; ++i) {
197       for (Path filePath: builder.addRegion()) {
198         String fileName = filePath.getName();
199         if (tmp) {
200           // We should be able to find all the files while the snapshot creation is in-progress
201           FSUtils.logFileSystemState(fs, rootDir, LOG);
202           assertTrue("Cache didn't find " + fileName, cache.contains(fileName));
203         }
204         files.add(fileName);
205       }
206     }
207 
208     // Finalize the snapshot
209     if (!tmp) {
210       builder.commit();
211     }
212 
213     // Make sure that all files are still present
214     for (String fileName: files) {
215       assertTrue("Cache didn't find " + fileName, cache.contains(fileName));
216     }
217 
218     FSUtils.logFileSystemState(fs, rootDir, LOG);
219     if (removeOnExit) {
220       LOG.debug("Deleting snapshot.");
221       fs.delete(builder.getSnapshotsDir(), true);
222       FSUtils.logFileSystemState(fs, rootDir, LOG);
223 
224       // The files should be in cache until next refresh
225       for (String fileName: files) {
226         assertTrue("Cache didn't find " + fileName, cache.contains(fileName));
227       }
228 
229       // then trigger a refresh
230       cache.triggerCacheRefreshForTesting();
231       // and not it shouldn't find those files
232       for (String fileName: files) {
233         assertFalse("Cache found '" + fileName + "', but it shouldn't have.",
234                     cache.contains(fileName));
235       }
236     }
237   }
238 }