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.backup; 019 020import com.google.errorprone.annotations.RestrictedApi; 021import java.io.IOException; 022import org.apache.hadoop.conf.Configuration; 023import org.apache.hadoop.fs.FileSystem; 024import org.apache.hadoop.fs.Path; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.backup.impl.BackupManifest; 027import org.apache.yetus.audience.InterfaceAudience; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; 032 033/** 034 * View to an on-disk Backup Image FileSytem Provides the set of methods necessary to interact with 035 * the on-disk Backup Image data. 036 */ 037@InterfaceAudience.Private 038public final class HBackupFileSystem { 039 public static final Logger LOG = LoggerFactory.getLogger(HBackupFileSystem.class); 040 041 /** 042 * This is utility class. 043 */ 044 private HBackupFileSystem() { 045 } 046 047 /** 048 * Given the backup root dir, backup id and the table name, return the backup image location. 049 * Return value look like: 050 * "hdfs://backup.hbase.org:9000/user/biadmin/backup/backup_1396650096738/default/t1_dn/", where 051 * "hdfs://backup.hbase.org:9000/user/biadmin/backup" is a backup root directory 052 * @param backupRootDir backup root directory 053 * @param backupId backup id 054 * @param tableName table name 055 * @return backupPath String for the particular table 056 */ 057 public static String getTableBackupDir(String backupRootDir, String backupId, 058 TableName tableName) { 059 return backupRootDir + Path.SEPARATOR + backupId + Path.SEPARATOR 060 + tableName.getNamespaceAsString() + Path.SEPARATOR + tableName.getQualifierAsString() 061 + Path.SEPARATOR; 062 } 063 064 /** 065 * Get backup temporary directory 066 * @param backupRootDir backup root 067 * @return backup tmp directory path 068 */ 069 public static Path getBackupTmpDirPath(String backupRootDir) { 070 return new Path(backupRootDir, ".tmp"); 071 } 072 073 /** 074 * Get backup tmp directory for backupId 075 * @param backupRoot backup root 076 * @param backupId backup id 077 * @return backup tmp directory path 078 */ 079 public static Path getBackupTmpDirPathForBackupId(String backupRoot, String backupId) { 080 return new Path(getBackupTmpDirPath(backupRoot), backupId); 081 } 082 083 public static Path getBackupPath(String backupRootDir, String backupId) { 084 return new Path(backupRootDir + Path.SEPARATOR + backupId); 085 } 086 087 /** 088 * Given the backup root dir, backup id and the table name, return the backup image location, 089 * which is also where the backup manifest file is. return value look like: 090 * "hdfs://backup.hbase.org:9000/user/biadmin/backup/backup_1396650096738/default/t1_dn/", where 091 * "hdfs://backup.hbase.org:9000/user/biadmin/backup" is a backup root directory 092 * @param backupRootPath backup root path 093 * @param tableName table name 094 * @param backupId backup Id 095 * @return backupPath for the particular table 096 */ 097 public static Path getTableBackupPath(TableName tableName, Path backupRootPath, String backupId) { 098 return new Path(getTableBackupDir(backupRootPath.toString(), backupId, tableName)); 099 } 100 101 private static Path getManifestPath(Configuration conf, Path backupRootPath, String backupId) 102 throws IOException { 103 return getManifestPath(conf, backupRootPath, backupId, true); 104 } 105 106 /* Visible for testing only */ 107 @RestrictedApi(explanation = "Should only be called internally or in tests", link = "", 108 allowedOnPath = "(.*/src/test/.*|.*/org/apache/hadoop/hbase/backup/HBackupFileSystem.java)") 109 static Path getManifestPath(Configuration conf, Path backupRootPath, String backupId, 110 boolean throwIfNotFound) throws IOException { 111 FileSystem fs = backupRootPath.getFileSystem(conf); 112 Path manifestPath = new Path(getBackupPath(backupRootPath.toString(), backupId) + Path.SEPARATOR 113 + BackupManifest.MANIFEST_FILE_NAME); 114 if (throwIfNotFound && !fs.exists(manifestPath)) { 115 String errorMsg = "Could not find backup manifest " + BackupManifest.MANIFEST_FILE_NAME 116 + " for " + backupId + ". File " + manifestPath + " does not exists. Did " + backupId 117 + " correspond to previously taken backup ?"; 118 throw new IOException(errorMsg); 119 } 120 return manifestPath; 121 } 122 123 public static Path getRootDirFromBackupPath(Path backupPath, String backupId) { 124 if (backupPath.getName().equals(BackupManifest.MANIFEST_FILE_NAME)) { 125 backupPath = backupPath.getParent(); 126 } 127 Preconditions.checkArgument(backupPath.getName().equals(backupId), 128 String.format("Backup path %s must end in backupId %s", backupPath, backupId)); 129 return backupPath.getParent(); 130 } 131 132 public static BackupManifest getManifest(Configuration conf, Path backupRootPath, String backupId) 133 throws IOException { 134 BackupManifest manifest = 135 new BackupManifest(conf, getManifestPath(conf, backupRootPath, backupId)); 136 return manifest; 137 } 138}