001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.snapshot;
019
020import java.io.IOException;
021import java.security.PrivilegedExceptionAction;
022import java.util.Collections;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FSDataInputStream;
026import org.apache.hadoop.fs.FSDataOutputStream;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.fs.permission.FsPermission;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Admin;
033import org.apache.hadoop.hbase.client.Connection;
034import org.apache.hadoop.hbase.client.ConnectionFactory;
035import org.apache.hadoop.hbase.security.User;
036import org.apache.hadoop.hbase.security.access.AccessControlLists;
037import org.apache.hadoop.hbase.security.access.ShadedAccessControlUtil;
038import org.apache.hadoop.hbase.security.access.TablePermission;
039import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
040import org.apache.hadoop.hbase.util.FSUtils;
041import org.apache.yetus.audience.InterfaceAudience;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045import org.apache.hbase.thirdparty.com.google.common.collect.ListMultimap;
046import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
047import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotDescription;
048
049/**
050 * Utility class to help manage {@link SnapshotDescription SnapshotDesriptions}.
051 * <p>
052 * Snapshots are laid out on disk like this:
053 *
054 * <pre>
055 * /hbase/.snapshots
056 *          /.tmp                &lt;---- working directory
057 *          /[snapshot name]     &lt;----- completed snapshot
058 * </pre>
059 *
060 * A completed snapshot named 'completed' then looks like (multiple regions, servers, files, etc.
061 * signified by '...' on the same directory depth).
062 *
063 * <pre>
064 * /hbase/.snapshots/completed
065 *                   .snapshotinfo          &lt;--- Description of the snapshot
066 *                   .tableinfo             &lt;--- Copy of the tableinfo
067 *                    /.logs
068 *                        /[server_name]
069 *                            /... [log files]
070 *                         ...
071 *                   /[region name]           &lt;---- All the region's information
072 *                   .regioninfo              &lt;---- Copy of the HRegionInfo
073 *                      /[column family name]
074 *                          /[hfile name]     &lt;--- name of the hfile in the real region
075 *                          ...
076 *                      ...
077 *                    ...
078 * </pre>
079 *
080 * Utility methods in this class are useful for getting the correct locations for different parts of
081 * the snapshot, as well as moving completed snapshots into place (see
082 * {@link #completeSnapshot}, and writing the
083 * {@link SnapshotDescription} to the working snapshot directory.
084 */
085@InterfaceAudience.Private
086public final class SnapshotDescriptionUtils {
087
088  /**
089   * Filter that only accepts completed snapshot directories
090   */
091  public static class CompletedSnaphotDirectoriesFilter extends FSUtils.BlackListDirFilter {
092
093    /**
094     * @param fs
095     */
096    public CompletedSnaphotDirectoriesFilter(FileSystem fs) {
097      super(fs, Collections.singletonList(SNAPSHOT_TMP_DIR_NAME));
098    }
099  }
100
101  private static final Logger LOG = LoggerFactory.getLogger(SnapshotDescriptionUtils.class);
102  /**
103   * Version of the fs layout for a snapshot. Future snapshots may have different file layouts,
104   * which we may need to read in differently.
105   */
106  public static final int SNAPSHOT_LAYOUT_VERSION = SnapshotManifestV2.DESCRIPTOR_VERSION;
107
108  // snapshot directory constants
109  /**
110   * The file contains the snapshot basic information and it is under the directory of a snapshot.
111   */
112  public static final String SNAPSHOTINFO_FILE = ".snapshotinfo";
113
114  /** Temporary directory under the snapshot directory to store in-progress snapshots */
115  public static final String SNAPSHOT_TMP_DIR_NAME = ".tmp";
116
117  /**
118   * The configuration property that determines the filepath of the snapshot
119   * base working directory
120   */
121  public static final String SNAPSHOT_WORKING_DIR = "hbase.snapshot.working.dir";
122
123  /** This tag will be created in in-progess snapshots */
124  public static final String SNAPSHOT_IN_PROGRESS = ".inprogress";
125  // snapshot operation values
126  /** Default value if no start time is specified */
127  public static final long NO_SNAPSHOT_START_TIME_SPECIFIED = 0;
128
129
130  public static final String MASTER_SNAPSHOT_TIMEOUT_MILLIS = "hbase.snapshot.master.timeout.millis";
131
132  /** By default, wait 300 seconds for a snapshot to complete */
133  public static final long DEFAULT_MAX_WAIT_TIME = 60000 * 5 ;
134
135
136  /**
137   * By default, check to see if the snapshot is complete (ms)
138   * @deprecated Use {@link #DEFAULT_MAX_WAIT_TIME} instead.
139   * */
140  @Deprecated
141  public static final int SNAPSHOT_TIMEOUT_MILLIS_DEFAULT = 60000 * 5;
142
143  /**
144   * Conf key for # of ms elapsed before injecting a snapshot timeout error when waiting for
145   * completion.
146   * @deprecated Use {@link #MASTER_SNAPSHOT_TIMEOUT_MILLIS} instead.
147   */
148  @Deprecated
149  public static final String SNAPSHOT_TIMEOUT_MILLIS_KEY = "hbase.snapshot.master.timeoutMillis";
150
151  private SnapshotDescriptionUtils() {
152    // private constructor for utility class
153  }
154
155  /**
156   * @param conf {@link Configuration} from which to check for the timeout
157   * @param type type of snapshot being taken
158   * @param defaultMaxWaitTime Default amount of time to wait, if none is in the configuration
159   * @return the max amount of time the master should wait for a snapshot to complete
160   */
161  public static long getMaxMasterTimeout(Configuration conf, SnapshotDescription.Type type,
162      long defaultMaxWaitTime) {
163    String confKey;
164    switch (type) {
165    case DISABLED:
166    default:
167      confKey = MASTER_SNAPSHOT_TIMEOUT_MILLIS;
168    }
169    return Math.max(conf.getLong(confKey, defaultMaxWaitTime),
170        conf.getLong(SNAPSHOT_TIMEOUT_MILLIS_KEY, defaultMaxWaitTime));
171  }
172
173  /**
174   * Get the snapshot root directory. All the snapshots are kept under this directory, i.e.
175   * ${hbase.rootdir}/.snapshot
176   * @param rootDir hbase root directory
177   * @return the base directory in which all snapshots are kept
178   */
179  public static Path getSnapshotRootDir(final Path rootDir) {
180    return new Path(rootDir, HConstants.SNAPSHOT_DIR_NAME);
181  }
182
183  /**
184   * Get the directory for a specified snapshot. This directory is a sub-directory of snapshot root
185   * directory and all the data files for a snapshot are kept under this directory.
186   * @param snapshot snapshot being taken
187   * @param rootDir hbase root directory
188   * @return the final directory for the completed snapshot
189   */
190  public static Path getCompletedSnapshotDir(final SnapshotDescription snapshot, final Path rootDir) {
191    return getCompletedSnapshotDir(snapshot.getName(), rootDir);
192  }
193
194  /**
195   * Get the directory for a completed snapshot. This directory is a sub-directory of snapshot root
196   * directory and all the data files for a snapshot are kept under this directory.
197   * @param snapshotName name of the snapshot being taken
198   * @param rootDir hbase root directory
199   * @return the final directory for the completed snapshot
200   */
201  public static Path getCompletedSnapshotDir(final String snapshotName, final Path rootDir) {
202    return getSpecifiedSnapshotDir(getSnapshotsDir(rootDir), snapshotName);
203  }
204
205  /**
206   * Get the general working directory for snapshots - where they are built, where they are
207   * temporarily copied on export, etc.
208   * @param rootDir root directory of the HBase installation
209   * @param conf Configuration of the HBase instance
210   * @return Path to the snapshot tmp directory, relative to the passed root directory
211   */
212  public static Path getWorkingSnapshotDir(final Path rootDir, final Configuration conf) {
213    return new Path(conf.get(SNAPSHOT_WORKING_DIR,
214        getDefaultWorkingSnapshotDir(rootDir).toString()));
215  }
216
217  /**
218   * Get the directory to build a snapshot, before it is finalized
219   * @param snapshot snapshot that will be built
220   * @param rootDir root directory of the hbase installation
221   * @param conf Configuration of the HBase instance
222   * @return {@link Path} where one can build a snapshot
223   */
224  public static Path getWorkingSnapshotDir(SnapshotDescription snapshot, final Path rootDir,
225      Configuration conf) {
226    return getWorkingSnapshotDir(snapshot.getName(), rootDir, conf);
227  }
228
229  /**
230   * Get the directory to build a snapshot, before it is finalized
231   * @param snapshotName name of the snapshot
232   * @param rootDir root directory of the hbase installation
233   * @param conf Configuration of the HBase instance
234   * @return {@link Path} where one can build a snapshot
235   */
236  public static Path getWorkingSnapshotDir(String snapshotName, final Path rootDir,
237      Configuration conf) {
238    return getSpecifiedSnapshotDir(getWorkingSnapshotDir(rootDir, conf), snapshotName);
239  }
240
241  /**
242   * Get the directory within the given filepath to store the snapshot instance
243   * @param snapshotsDir directory to store snapshot directory within
244   * @param snapshotName name of the snapshot to take
245   * @return the final directory for the snapshot in the given filepath
246   */
247  private static final Path getSpecifiedSnapshotDir(final Path snapshotsDir, String snapshotName) {
248    return new Path(snapshotsDir, snapshotName);
249  }
250
251  /**
252   * @param rootDir hbase root directory
253   * @return the directory for all completed snapshots;
254   */
255  public static final Path getSnapshotsDir(Path rootDir) {
256    return new Path(rootDir, HConstants.SNAPSHOT_DIR_NAME);
257  }
258
259  /**
260   * Determines if the given workingDir is a subdirectory of the given "root directory"
261   * @param workingDir a directory to check
262   * @param rootDir root directory of the HBase installation
263   * @return true if the given workingDir is a subdirectory of the given root directory,
264   *   false otherwise
265   */
266  public static boolean isSubDirectoryOf(final Path workingDir, final Path rootDir) {
267    return workingDir.toString().startsWith(rootDir.toString() + Path.SEPARATOR);
268  }
269
270  /**
271   * Determines if the given workingDir is a subdirectory of the default working snapshot directory
272   * @param workingDir a directory to check
273   * @param conf configuration for the HBase cluster
274   * @return true if the given workingDir is a subdirectory of the default working directory for
275   *   snapshots, false otherwise
276   */
277  public static boolean isWithinDefaultWorkingDir(final Path workingDir, Configuration conf) {
278    Path defaultWorkingDir = getDefaultWorkingSnapshotDir(new Path(conf.get(HConstants.HBASE_DIR)));
279    return workingDir.equals(defaultWorkingDir) || isSubDirectoryOf(workingDir, defaultWorkingDir);
280  }
281
282  /**
283   * Get the default working directory for snapshots - where they are built, where they are
284   * temporarily copied on export, etc.
285   * @param rootDir root directory of the HBase installation
286   * @return Path to the default snapshot tmp directory, relative to the passed root directory
287   */
288  private static Path getDefaultWorkingSnapshotDir(final Path rootDir) {
289    return new Path(getSnapshotsDir(rootDir), SNAPSHOT_TMP_DIR_NAME);
290  }
291
292  /**
293   * Convert the passed snapshot description into a 'full' snapshot description based on default
294   * parameters, if none have been supplied. This resolves any 'optional' parameters that aren't
295   * supplied to their default values.
296   * @param snapshot general snapshot descriptor
297   * @param conf Configuration to read configured snapshot defaults if snapshot is not complete
298   * @return a valid snapshot description
299   * @throws IllegalArgumentException if the {@link SnapshotDescription} is not a complete
300   *           {@link SnapshotDescription}.
301   */
302  public static SnapshotDescription validate(SnapshotDescription snapshot, Configuration conf)
303      throws IllegalArgumentException, IOException {
304    if (!snapshot.hasTable()) {
305      throw new IllegalArgumentException(
306          "Descriptor doesn't apply to a table, so we can't build it.");
307    }
308
309    // set the creation time, if one hasn't been set
310    long time = snapshot.getCreationTime();
311    if (time == SnapshotDescriptionUtils.NO_SNAPSHOT_START_TIME_SPECIFIED) {
312      time = EnvironmentEdgeManager.currentTime();
313      LOG.debug("Creation time not specified, setting to:" + time + " (current time:"
314          + EnvironmentEdgeManager.currentTime() + ").");
315      SnapshotDescription.Builder builder = snapshot.toBuilder();
316      builder.setCreationTime(time);
317      snapshot = builder.build();
318    }
319
320    // set the acl to snapshot if security feature is enabled.
321    if (isSecurityAvailable(conf)) {
322      snapshot = writeAclToSnapshotDescription(snapshot, conf);
323    }
324    return snapshot;
325  }
326
327  /**
328   * Write the snapshot description into the working directory of a snapshot
329   * @param snapshot description of the snapshot being taken
330   * @param workingDir working directory of the snapshot
331   * @param fs {@link FileSystem} on which the snapshot should be taken
332   * @throws IOException if we can't reach the filesystem and the file cannot be cleaned up on
333   *           failure
334   */
335  public static void writeSnapshotInfo(SnapshotDescription snapshot, Path workingDir, FileSystem fs)
336      throws IOException {
337    FsPermission perms = FSUtils.getFilePermissions(fs, fs.getConf(),
338      HConstants.DATA_FILE_UMASK_KEY);
339    Path snapshotInfo = new Path(workingDir, SnapshotDescriptionUtils.SNAPSHOTINFO_FILE);
340    try {
341      FSDataOutputStream out = FSUtils.create(fs, snapshotInfo, perms, true);
342      try {
343        snapshot.writeTo(out);
344      } finally {
345        out.close();
346      }
347    } catch (IOException e) {
348      // if we get an exception, try to remove the snapshot info
349      if (!fs.delete(snapshotInfo, false)) {
350        String msg = "Couldn't delete snapshot info file: " + snapshotInfo;
351        LOG.error(msg);
352        throw new IOException(msg);
353      }
354    }
355  }
356
357  /**
358   * Create in-progress tag under .tmp of in-progress snapshot
359   * */
360  public static void createInProgressTag(Path workingDir, FileSystem fs) throws IOException {
361    FsPermission perms = FSUtils.getFilePermissions(fs, fs.getConf(),
362      HConstants.DATA_FILE_UMASK_KEY);
363    Path snapshot_in_progress = new Path(workingDir, SnapshotDescriptionUtils.SNAPSHOT_IN_PROGRESS);
364    FSUtils.create(fs, snapshot_in_progress, perms, true);
365  }
366
367  /**
368   * Read in the {@link org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription} stored for the snapshot in the passed directory
369   * @param fs filesystem where the snapshot was taken
370   * @param snapshotDir directory where the snapshot was stored
371   * @return the stored snapshot description
372   * @throws CorruptedSnapshotException if the
373   * snapshot cannot be read
374   */
375  public static SnapshotDescription readSnapshotInfo(FileSystem fs, Path snapshotDir)
376      throws CorruptedSnapshotException {
377    Path snapshotInfo = new Path(snapshotDir, SNAPSHOTINFO_FILE);
378    try {
379      FSDataInputStream in = null;
380      try {
381        in = fs.open(snapshotInfo);
382        SnapshotDescription desc = SnapshotDescription.parseFrom(in);
383        return desc;
384      } finally {
385        if (in != null) in.close();
386      }
387    } catch (IOException e) {
388      throw new CorruptedSnapshotException("Couldn't read snapshot info from:" + snapshotInfo, e);
389    }
390  }
391
392  /**
393   * Move the finished snapshot to its final, publicly visible directory - this marks the snapshot
394   * as 'complete'.
395   * @param snapshot description of the snapshot being tabken
396   * @param rootdir root directory of the hbase installation
397   * @param workingDir directory where the in progress snapshot was built
398   * @param fs {@link FileSystem} where the snapshot was built
399   * @throws org.apache.hadoop.hbase.snapshot.SnapshotCreationException if the
400   * snapshot could not be moved
401   * @throws IOException the filesystem could not be reached
402   */
403  public static void completeSnapshot(SnapshotDescription snapshot, Path rootdir, Path workingDir,
404      FileSystem fs) throws SnapshotCreationException, IOException {
405    Path finishedDir = getCompletedSnapshotDir(snapshot, rootdir);
406    LOG.debug("Snapshot is done, just moving the snapshot from " + workingDir + " to "
407        + finishedDir);
408    if (!fs.rename(workingDir, finishedDir)) {
409      throw new SnapshotCreationException(
410          "Failed to move working directory(" + workingDir + ") to completed directory("
411              + finishedDir + ").", ProtobufUtil.createSnapshotDesc(snapshot));
412    }
413  }
414
415  /**
416   * Check if the user is this table snapshot's owner
417   * @param snapshot the table snapshot description
418   * @param user the user
419   * @return true if the user is the owner of the snapshot,
420   *         false otherwise or the snapshot owner field is not present.
421   */
422  public static boolean isSnapshotOwner(org.apache.hadoop.hbase.client.SnapshotDescription snapshot,
423      User user) {
424    if (user == null) return false;
425    return user.getShortName().equals(snapshot.getOwner());
426  }
427
428  public static boolean isSecurityAvailable(Configuration conf) throws IOException {
429    try (Connection conn = ConnectionFactory.createConnection(conf)) {
430      try (Admin admin = conn.getAdmin()) {
431        return admin.tableExists(AccessControlLists.ACL_TABLE_NAME);
432      }
433    }
434  }
435
436  private static SnapshotDescription writeAclToSnapshotDescription(SnapshotDescription snapshot,
437      Configuration conf) throws IOException {
438    ListMultimap<String, TablePermission> perms =
439        User.runAsLoginUser(new PrivilegedExceptionAction<ListMultimap<String, TablePermission>>() {
440          @Override
441          public ListMultimap<String, TablePermission> run() throws Exception {
442            return AccessControlLists.getTablePermissions(conf,
443              TableName.valueOf(snapshot.getTable()));
444          }
445        });
446    return snapshot.toBuilder()
447        .setUsersAndPermissions(ShadedAccessControlUtil.toUserTablePermissions(perms)).build();
448  }
449}