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