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.snapshot;
19  
20  import java.io.IOException;
21  import java.util.Collections;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.fs.FSDataInputStream;
27  import org.apache.hadoop.fs.FSDataOutputStream;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.fs.permission.FsPermission;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
33  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
34  import org.apache.hadoop.hbase.snapshot.SnapshotManifestV1;
35  import org.apache.hadoop.hbase.snapshot.SnapshotManifestV2;
36  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
37  import org.apache.hadoop.hbase.util.FSUtils;
38  
39  /**
40   * Utility class to help manage {@link SnapshotDescription SnapshotDesriptions}.
41   * <p>
42   * Snapshots are laid out on disk like this:
43   *
44   * <pre>
45   * /hbase/.snapshots
46   *          /.tmp                <---- working directory
47   *          /[snapshot name]     <----- completed snapshot
48   * </pre>
49   *
50   * A completed snapshot named 'completed' then looks like (multiple regions, servers, files, etc.
51   * signified by '...' on the same directory depth).
52   *
53   * <pre>
54   * /hbase/.snapshots/completed
55   *                   .snapshotinfo          <--- Description of the snapshot
56   *                   .tableinfo             <--- Copy of the tableinfo
57   *                    /.logs
58   *                        /[server_name]
59   *                            /... [log files]
60   *                         ...
61   *                   /[region name]           <---- All the region's information
62   *                   .regioninfo              <---- Copy of the HRegionInfo
63   *                      /[column family name]
64   *                          /[hfile name]     <--- name of the hfile in the real region
65   *                          ...
66   *                      ...
67   *                    ...
68   * </pre>
69   *
70   * Utility methods in this class are useful for getting the correct locations for different parts of
71   * the snapshot, as well as moving completed snapshots into place (see
72   * {@link #completeSnapshot}, and writing the
73   * {@link SnapshotDescription} to the working snapshot directory.
74   */
75  public class SnapshotDescriptionUtils {
76  
77    /**
78     * Filter that only accepts completed snapshot directories
79     */
80    public static class CompletedSnaphotDirectoriesFilter extends FSUtils.BlackListDirFilter {
81  
82      /**
83       * @param fs
84       */
85      public CompletedSnaphotDirectoriesFilter(FileSystem fs) {
86        super(fs, Collections.singletonList(SNAPSHOT_TMP_DIR_NAME));
87      }
88    }
89  
90    private static final Log LOG = LogFactory.getLog(SnapshotDescriptionUtils.class);
91  
92    /**
93     * Version of the fs layout for new snapshot.
94     * Each version of hbase should be able to read older formats.
95     */
96    public static final String SNAPSHOT_LAYOUT_CONF_KEY = "hbase.snapshot.format.version";
97    public static final int SNAPSHOT_LAYOUT_LATEST_FORMAT = SnapshotManifestV2.DESCRIPTOR_VERSION;
98  
99    // snapshot directory constants
100   /**
101    * The file contains the snapshot basic information and it is under the directory of a snapshot.
102    */
103   public static final String SNAPSHOTINFO_FILE = ".snapshotinfo";
104 
105   /** Temporary directory under the snapshot directory to store in-progress snapshots */
106   public static final String SNAPSHOT_TMP_DIR_NAME = ".tmp";
107   // snapshot operation values
108   /** Default value if no start time is specified */
109   public static final long NO_SNAPSHOT_START_TIME_SPECIFIED = 0;
110 
111 
112   public static final String MASTER_SNAPSHOT_TIMEOUT_MILLIS = "hbase.snapshot.master.timeout.millis";
113 
114   /** By default, wait 300 seconds for a snapshot to complete */
115   public static final long DEFAULT_MAX_WAIT_TIME = 60000 * 5 ;
116 
117 
118   /**
119    * By default, check to see if the snapshot is complete (ms)
120    * @deprecated Use {@link #DEFAULT_MAX_WAIT_TIME} instead.
121    * */
122   @Deprecated
123   public static final int SNAPSHOT_TIMEOUT_MILLIS_DEFAULT = 60000 * 5;
124 
125   /**
126    * Conf key for # of ms elapsed before injecting a snapshot timeout error when waiting for
127    * completion.
128    * @deprecated Use {@link #MASTER_SNAPSHOT_TIMEOUT_MILLIS} instead.
129    */
130   @Deprecated
131   public static final String SNAPSHOT_TIMEOUT_MILLIS_KEY = "hbase.snapshot.master.timeoutMillis";
132 
133   private SnapshotDescriptionUtils() {
134     // private constructor for utility class
135   }
136 
137   public static int getDefaultSnapshotLayoutFormat(final Configuration conf) {
138     int layoutFormat = conf.getInt(SNAPSHOT_LAYOUT_CONF_KEY, SNAPSHOT_LAYOUT_LATEST_FORMAT);
139     if (layoutFormat >= SnapshotManifestV2.DESCRIPTOR_VERSION) {
140       return SnapshotManifestV2.DESCRIPTOR_VERSION;
141     }
142     return SnapshotManifestV1.DESCRIPTOR_VERSION;
143   }
144 
145 
146   /**
147    * @param conf {@link Configuration} from which to check for the timeout
148    * @param type type of snapshot being taken
149    * @param defaultMaxWaitTime Default amount of time to wait, if none is in the configuration
150    * @return the max amount of time the master should wait for a snapshot to complete
151    */
152   public static long getMaxMasterTimeout(Configuration conf, SnapshotDescription.Type type,
153       long defaultMaxWaitTime) {
154     String confKey;
155     switch (type) {
156     case DISABLED:
157     default:
158       confKey = MASTER_SNAPSHOT_TIMEOUT_MILLIS;
159     }
160     return Math.max(conf.getLong(confKey, defaultMaxWaitTime),
161         conf.getLong(SNAPSHOT_TIMEOUT_MILLIS_KEY, defaultMaxWaitTime));
162   }
163 
164   /**
165    * Get the snapshot root directory. All the snapshots are kept under this directory, i.e.
166    * ${hbase.rootdir}/.snapshot
167    * @param rootDir hbase root directory
168    * @return the base directory in which all snapshots are kept
169    */
170   public static Path getSnapshotRootDir(final Path rootDir) {
171     return new Path(rootDir, HConstants.SNAPSHOT_DIR_NAME);
172   }
173 
174   /**
175    * Get the directory for a specified snapshot. This directory is a sub-directory of snapshot root
176    * directory and all the data files for a snapshot are kept under this directory.
177    * @param snapshot snapshot being taken
178    * @param rootDir hbase root directory
179    * @return the final directory for the completed snapshot
180    */
181   public static Path getCompletedSnapshotDir(final SnapshotDescription snapshot, final Path rootDir) {
182     return getCompletedSnapshotDir(snapshot.getName(), rootDir);
183   }
184 
185   /**
186    * Get the directory for a completed snapshot. This directory is a sub-directory of snapshot root
187    * directory and all the data files for a snapshot are kept under this directory.
188    * @param snapshotName name of the snapshot being taken
189    * @param rootDir hbase root directory
190    * @return the final directory for the completed snapshot
191    */
192   public static Path getCompletedSnapshotDir(final String snapshotName, final Path rootDir) {
193     return getCompletedSnapshotDir(getSnapshotsDir(rootDir), snapshotName);
194   }
195 
196   /**
197    * Get the general working directory for snapshots - where they are built, where they are
198    * temporarily copied on export, etc.
199    * @param rootDir root directory of the HBase installation
200    * @return Path to the snapshot tmp directory, relative to the passed root directory
201    */
202   public static Path getWorkingSnapshotDir(final Path rootDir) {
203     return new Path(getSnapshotsDir(rootDir), SNAPSHOT_TMP_DIR_NAME);
204   }
205 
206   /**
207    * Get the directory to build a snapshot, before it is finalized
208    * @param snapshot snapshot that will be built
209    * @param rootDir root directory of the hbase installation
210    * @return {@link Path} where one can build a snapshot
211    */
212   public static Path getWorkingSnapshotDir(SnapshotDescription snapshot, final Path rootDir) {
213     return getCompletedSnapshotDir(getWorkingSnapshotDir(rootDir), snapshot.getName());
214   }
215 
216   /**
217    * Get the directory to build a snapshot, before it is finalized
218    * @param snapshotName name of the snapshot
219    * @param rootDir root directory of the hbase installation
220    * @return {@link Path} where one can build a snapshot
221    */
222   public static Path getWorkingSnapshotDir(String snapshotName, final Path rootDir) {
223     return getCompletedSnapshotDir(getWorkingSnapshotDir(rootDir), snapshotName);
224   }
225 
226   /**
227    * Get the directory to store the snapshot instance
228    * @param snapshotsDir hbase-global directory for storing all snapshots
229    * @param snapshotName name of the snapshot to take
230    * @return the final directory for the completed snapshot
231    */
232   private static final Path getCompletedSnapshotDir(final Path snapshotsDir, String snapshotName) {
233     return new Path(snapshotsDir, snapshotName);
234   }
235 
236   /**
237    * @param rootDir hbase root directory
238    * @return the directory for all completed snapshots;
239    */
240   public static final Path getSnapshotsDir(Path rootDir) {
241     return new Path(rootDir, HConstants.SNAPSHOT_DIR_NAME);
242   }
243 
244   /**
245    * Convert the passed snapshot description into a 'full' snapshot description based on default
246    * parameters, if none have been supplied. This resolves any 'optional' parameters that aren't
247    * supplied to their default values.
248    * @param snapshot general snapshot descriptor
249    * @param conf Configuration to read configured snapshot defaults if snapshot is not complete
250    * @return a valid snapshot description
251    * @throws IllegalArgumentException if the {@link SnapshotDescription} is not a complete
252    *           {@link SnapshotDescription}.
253    */
254   public static SnapshotDescription validate(SnapshotDescription snapshot, Configuration conf)
255       throws IllegalArgumentException {
256     if (!snapshot.hasTable()) {
257       throw new IllegalArgumentException(
258         "Descriptor doesn't apply to a table, so we can't build it.");
259     }
260 
261     // set the creation time, if one hasn't been set
262     long time = snapshot.getCreationTime();
263     if (time == SnapshotDescriptionUtils.NO_SNAPSHOT_START_TIME_SPECIFIED) {
264       time = EnvironmentEdgeManager.currentTimeMillis();
265       LOG.debug("Creation time not specified, setting to:" + time + " (current time:"
266           + EnvironmentEdgeManager.currentTimeMillis() + ").");
267       SnapshotDescription.Builder builder = snapshot.toBuilder();
268       builder.setCreationTime(time);
269       snapshot = builder.build();
270     }
271     return snapshot;
272   }
273 
274   /**
275    * Write the snapshot description into the working directory of a snapshot
276    * @param snapshot description of the snapshot being taken
277    * @param workingDir working directory of the snapshot
278    * @param fs {@link FileSystem} on which the snapshot should be taken
279    * @throws IOException if we can't reach the filesystem and the file cannot be cleaned up on
280    *           failure
281    */
282   public static void writeSnapshotInfo(SnapshotDescription snapshot, Path workingDir, FileSystem fs)
283       throws IOException {
284     FsPermission perms = FSUtils.getFilePermissions(fs, fs.getConf(),
285       HConstants.DATA_FILE_UMASK_KEY);
286     Path snapshotInfo = new Path(workingDir, SnapshotDescriptionUtils.SNAPSHOTINFO_FILE);
287     try {
288       FSDataOutputStream out = FSUtils.create(fs, snapshotInfo, perms, true);
289       try {
290         snapshot.writeTo(out);
291       } finally {
292         out.close();
293       }
294     } catch (IOException e) {
295       // if we get an exception, try to remove the snapshot info
296       if (!fs.delete(snapshotInfo, false)) {
297         String msg = "Couldn't delete snapshot info file: " + snapshotInfo;
298         LOG.error(msg);
299         throw new IOException(msg);
300       }
301     }
302   }
303 
304   /**
305    * Read in the {@link org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription} stored for the snapshot in the passed directory
306    * @param fs filesystem where the snapshot was taken
307    * @param snapshotDir directory where the snapshot was stored
308    * @return the stored snapshot description
309    * @throws CorruptedSnapshotException if the
310    * snapshot cannot be read
311    */
312   public static SnapshotDescription readSnapshotInfo(FileSystem fs, Path snapshotDir)
313       throws CorruptedSnapshotException {
314     Path snapshotInfo = new Path(snapshotDir, SNAPSHOTINFO_FILE);
315     try {
316       FSDataInputStream in = null;
317       try {
318         in = fs.open(snapshotInfo);
319         SnapshotDescription desc = SnapshotDescription.parseFrom(in);
320         return desc;
321       } finally {
322         if (in != null) in.close();
323       }
324     } catch (IOException e) {
325       throw new CorruptedSnapshotException("Couldn't read snapshot info from:" + snapshotInfo, e);
326     }
327   }
328 
329   /**
330    * Move the finished snapshot to its final, publicly visible directory - this marks the snapshot
331    * as 'complete'.
332    * @param snapshot description of the snapshot being tabken
333    * @param rootdir root directory of the hbase installation
334    * @param workingDir directory where the in progress snapshot was built
335    * @param fs {@link FileSystem} where the snapshot was built
336    * @throws org.apache.hadoop.hbase.snapshot.SnapshotCreationException if the
337    * snapshot could not be moved
338    * @throws IOException the filesystem could not be reached
339    */
340   public static void completeSnapshot(SnapshotDescription snapshot, Path rootdir, Path workingDir,
341       FileSystem fs) throws SnapshotCreationException, IOException {
342     Path finishedDir = getCompletedSnapshotDir(snapshot, rootdir);
343     LOG.debug("Snapshot is done, just moving the snapshot from " + workingDir + " to "
344         + finishedDir);
345     if (!fs.rename(workingDir, finishedDir)) {
346       throw new SnapshotCreationException("Failed to move working directory(" + workingDir
347           + ") to completed directory(" + finishedDir + ").", snapshot);
348     }
349   }
350 
351 }