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