001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.master; 020 021import java.io.IOException; 022 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.fs.FileSystem; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.fs.permission.FsAction; 027import org.apache.hadoop.fs.permission.FsPermission; 028import org.apache.hadoop.hbase.ClusterId; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.backup.HFileArchiver; 032import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; 033import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 034import org.apache.hadoop.hbase.client.RegionInfo; 035import org.apache.hadoop.hbase.client.RegionInfoBuilder; 036import org.apache.hadoop.hbase.client.TableDescriptor; 037import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 038import org.apache.hadoop.hbase.exceptions.DeserializationException; 039import org.apache.hadoop.hbase.fs.HFileSystem; 040import org.apache.hadoop.hbase.log.HBaseMarkers; 041import org.apache.hadoop.hbase.mob.MobConstants; 042import org.apache.hadoop.hbase.procedure2.store.wal.WALProcedureStore; 043import org.apache.hadoop.hbase.regionserver.HRegion; 044import org.apache.hadoop.hbase.util.Bytes; 045import org.apache.hadoop.hbase.util.FSTableDescriptors; 046import org.apache.hadoop.hbase.util.FSUtils; 047import org.apache.hadoop.ipc.RemoteException; 048import org.apache.yetus.audience.InterfaceAudience; 049import org.slf4j.Logger; 050import org.slf4j.LoggerFactory; 051 052/** 053 * This class abstracts a bunch of operations the HMaster needs to interact with 054 * the underlying file system like creating the initial layout, checking file 055 * system status, etc. 056 */ 057@InterfaceAudience.Private 058public class MasterFileSystem { 059 private static final Logger LOG = LoggerFactory.getLogger(MasterFileSystem.class); 060 061 /** Parameter name for HBase instance root directory permission*/ 062 public static final String HBASE_DIR_PERMS = "hbase.rootdir.perms"; 063 064 /** Parameter name for HBase WAL directory permission*/ 065 public static final String HBASE_WAL_DIR_PERMS = "hbase.wal.dir.perms"; 066 067 // HBase configuration 068 private final Configuration conf; 069 // Persisted unique cluster ID 070 private ClusterId clusterId; 071 // Keep around for convenience. 072 private final FileSystem fs; 073 // Keep around for convenience. 074 private final FileSystem walFs; 075 // root log directory on the FS 076 private final Path rootdir; 077 // hbase temp directory used for table construction and deletion 078 private final Path tempdir; 079 // root hbase directory on the FS 080 private final Path walRootDir; 081 082 083 /* 084 * In a secure env, the protected sub-directories and files under the HBase rootDir 085 * would be restricted. The sub-directory will have '700' except the bulk load staging dir, 086 * which will have '711'. The default '700' can be overwritten by setting the property 087 * 'hbase.rootdir.perms'. The protected files (version file, clusterId file) will have '600'. 088 * The rootDir itself will be created with HDFS default permissions if it does not exist. 089 * We will check the rootDir permissions to make sure it has 'x' for all to ensure access 090 * to the staging dir. If it does not, we will add it. 091 */ 092 // Permissions for the directories under rootDir that need protection 093 private final FsPermission secureRootSubDirPerms; 094 // Permissions for the files under rootDir that need protection 095 private final FsPermission secureRootFilePerms = new FsPermission("600"); 096 // Permissions for bulk load staging directory under rootDir 097 private final FsPermission HiddenDirPerms = FsPermission.valueOf("-rwx--x--x"); 098 099 private boolean isSecurityEnabled; 100 101 public MasterFileSystem(Configuration conf) throws IOException { 102 this.conf = conf; 103 // Set filesystem to be that of this.rootdir else we get complaints about 104 // mismatched filesystems if hbase.rootdir is hdfs and fs.defaultFS is 105 // default localfs. Presumption is that rootdir is fully-qualified before 106 // we get to here with appropriate fs scheme. 107 this.rootdir = FSUtils.getRootDir(conf); 108 this.tempdir = new Path(this.rootdir, HConstants.HBASE_TEMP_DIRECTORY); 109 // Cover both bases, the old way of setting default fs and the new. 110 // We're supposed to run on 0.20 and 0.21 anyways. 111 this.fs = this.rootdir.getFileSystem(conf); 112 this.walRootDir = FSUtils.getWALRootDir(conf); 113 this.walFs = FSUtils.getWALFileSystem(conf); 114 FSUtils.setFsDefault(conf, new Path(this.walFs.getUri())); 115 walFs.setConf(conf); 116 FSUtils.setFsDefault(conf, new Path(this.fs.getUri())); 117 // make sure the fs has the same conf 118 fs.setConf(conf); 119 this.secureRootSubDirPerms = new FsPermission(conf.get("hbase.rootdir.perms", "700")); 120 this.isSecurityEnabled = "kerberos".equalsIgnoreCase(conf.get("hbase.security.authentication")); 121 // setup the filesystem variable 122 createInitialFileSystemLayout(); 123 HFileSystem.addLocationsOrderInterceptor(conf); 124 } 125 126 /** 127 * Create initial layout in filesystem. 128 * <ol> 129 * <li>Check if the meta region exists and is readable, if not create it. 130 * Create hbase.version and the hbase:meta directory if not one. 131 * </li> 132 * </ol> 133 * Idempotent. 134 */ 135 private void createInitialFileSystemLayout() throws IOException { 136 137 final String[] protectedSubDirs = new String[] { 138 HConstants.BASE_NAMESPACE_DIR, 139 HConstants.HFILE_ARCHIVE_DIRECTORY, 140 HConstants.HBCK_SIDELINEDIR_NAME, 141 MobConstants.MOB_DIR_NAME 142 }; 143 144 final String[] protectedSubLogDirs = new String[] { 145 HConstants.HREGION_LOGDIR_NAME, 146 HConstants.HREGION_OLDLOGDIR_NAME, 147 HConstants.CORRUPT_DIR_NAME, 148 WALProcedureStore.MASTER_PROCEDURE_LOGDIR 149 }; 150 // check if the root directory exists 151 checkRootDir(this.rootdir, conf, this.fs); 152 153 // Check the directories under rootdir. 154 checkTempDir(this.tempdir, conf, this.fs); 155 for (String subDir : protectedSubDirs) { 156 checkSubDir(new Path(this.rootdir, subDir), HBASE_DIR_PERMS); 157 } 158 159 final String perms; 160 if (!this.walRootDir.equals(this.rootdir)) { 161 perms = HBASE_WAL_DIR_PERMS; 162 } else { 163 perms = HBASE_DIR_PERMS; 164 } 165 for (String subDir : protectedSubLogDirs) { 166 checkSubDir(new Path(this.walRootDir, subDir), perms); 167 } 168 169 checkStagingDir(); 170 171 // Handle the last few special files and set the final rootDir permissions 172 // rootDir needs 'x' for all to support bulk load staging dir 173 if (isSecurityEnabled) { 174 fs.setPermission(new Path(rootdir, HConstants.VERSION_FILE_NAME), secureRootFilePerms); 175 fs.setPermission(new Path(rootdir, HConstants.CLUSTER_ID_FILE_NAME), secureRootFilePerms); 176 } 177 FsPermission currentRootPerms = fs.getFileStatus(this.rootdir).getPermission(); 178 if (!currentRootPerms.getUserAction().implies(FsAction.EXECUTE) 179 || !currentRootPerms.getGroupAction().implies(FsAction.EXECUTE) 180 || !currentRootPerms.getOtherAction().implies(FsAction.EXECUTE)) { 181 LOG.warn("rootdir permissions do not contain 'excute' for user, group or other. " 182 + "Automatically adding 'excute' permission for all"); 183 fs.setPermission( 184 this.rootdir, 185 new FsPermission(currentRootPerms.getUserAction().or(FsAction.EXECUTE), currentRootPerms 186 .getGroupAction().or(FsAction.EXECUTE), currentRootPerms.getOtherAction().or( 187 FsAction.EXECUTE))); 188 } 189 } 190 191 public FileSystem getFileSystem() { 192 return this.fs; 193 } 194 195 protected FileSystem getWALFileSystem() { return this.walFs; } 196 197 public Configuration getConfiguration() { 198 return this.conf; 199 } 200 201 /** 202 * @return HBase root directory. 203 */ 204 public Path getRootDir() { 205 return this.rootdir; 206 } 207 208 /** 209 * @return HBase WAL root directory, usually the same as {@link #getRootDir()} but can be 210 * different if hfiles on one fs and WALs on another. The 'WALs' dir gets made underneath 211 * the root dir returned here; i.e. this is '/hbase', not '/hbase/WALs'. 212 */ 213 public Path getWALRootDir() { 214 return this.walRootDir; 215 } 216 217 /** 218 * @return the directory for a give {@code region}. 219 */ 220 public Path getRegionDir(RegionInfo region) { 221 return FSUtils.getRegionDir(FSUtils.getTableDir(getRootDir(), region.getTable()), region); 222 } 223 224 /** 225 * @return HBase temp dir. 226 */ 227 public Path getTempDir() { 228 return this.tempdir; 229 } 230 231 /** 232 * @return The unique identifier generated for this cluster 233 */ 234 public ClusterId getClusterId() { 235 return clusterId; 236 } 237 238 /** 239 * Get the rootdir. Make sure its wholesome and exists before returning. 240 * @param rd 241 * @param c 242 * @param fs 243 * @return hbase.rootdir (after checks for existence and bootstrapping if 244 * needed populating the directory with necessary bootup files). 245 * @throws IOException 246 */ 247 private Path checkRootDir(final Path rd, final Configuration c, final FileSystem fs) 248 throws IOException { 249 // If FS is in safe mode wait till out of it. 250 FSUtils.waitOnSafeMode(c, c.getInt(HConstants.THREAD_WAKE_FREQUENCY, 10 * 1000)); 251 252 // Filesystem is good. Go ahead and check for hbase.rootdir. 253 try { 254 if (!fs.exists(rd)) { 255 fs.mkdirs(rd); 256 // DFS leaves safe mode with 0 DNs when there are 0 blocks. 257 // We used to handle this by checking the current DN count and waiting until 258 // it is nonzero. With security, the check for datanode count doesn't work -- 259 // it is a privileged op. So instead we adopt the strategy of the jobtracker 260 // and simply retry file creation during bootstrap indefinitely. As soon as 261 // there is one datanode it will succeed. Permission problems should have 262 // already been caught by mkdirs above. 263 FSUtils.setVersion(fs, rd, c.getInt(HConstants.THREAD_WAKE_FREQUENCY, 264 10 * 1000), c.getInt(HConstants.VERSION_FILE_WRITE_ATTEMPTS, 265 HConstants.DEFAULT_VERSION_FILE_WRITE_ATTEMPTS)); 266 } else { 267 if (!fs.isDirectory(rd)) { 268 throw new IllegalArgumentException(rd.toString() + " is not a directory"); 269 } 270 // as above 271 FSUtils.checkVersion(fs, rd, true, c.getInt(HConstants.THREAD_WAKE_FREQUENCY, 272 10 * 1000), c.getInt(HConstants.VERSION_FILE_WRITE_ATTEMPTS, 273 HConstants.DEFAULT_VERSION_FILE_WRITE_ATTEMPTS)); 274 } 275 } catch (DeserializationException de) { 276 LOG.error(HBaseMarkers.FATAL, "Please fix invalid configuration for " 277 + HConstants.HBASE_DIR, de); 278 IOException ioe = new IOException(); 279 ioe.initCause(de); 280 throw ioe; 281 } catch (IllegalArgumentException iae) { 282 LOG.error(HBaseMarkers.FATAL, "Please fix invalid configuration for " 283 + HConstants.HBASE_DIR + " " + rd.toString(), iae); 284 throw iae; 285 } 286 // Make sure cluster ID exists 287 if (!FSUtils.checkClusterIdExists(fs, rd, c.getInt( 288 HConstants.THREAD_WAKE_FREQUENCY, 10 * 1000))) { 289 FSUtils.setClusterId(fs, rd, new ClusterId(), c.getInt(HConstants.THREAD_WAKE_FREQUENCY, 10 * 1000)); 290 } 291 clusterId = FSUtils.getClusterId(fs, rd); 292 293 // Make sure the meta region directory exists! 294 if (!FSUtils.metaRegionExists(fs, rd)) { 295 bootstrap(rd, c); 296 } 297 298 // Create tableinfo-s for hbase:meta if not already there. 299 // assume, created table descriptor is for enabling table 300 // meta table is a system table, so descriptors are predefined, 301 // we should get them from registry. 302 FSTableDescriptors fsd = new FSTableDescriptors(c, fs, rd); 303 fsd.createTableDescriptor(fsd.get(TableName.META_TABLE_NAME)); 304 305 return rd; 306 } 307 308 /** 309 * Make sure the hbase temp directory exists and is empty. 310 * NOTE that this method is only executed once just after the master becomes the active one. 311 */ 312 private void checkTempDir(final Path tmpdir, final Configuration c, final FileSystem fs) 313 throws IOException { 314 // If the temp directory exists, clear the content (left over, from the previous run) 315 if (fs.exists(tmpdir)) { 316 // Archive table in temp, maybe left over from failed deletion, 317 // if not the cleaner will take care of them. 318 for (Path tabledir: FSUtils.getTableDirs(fs, tmpdir)) { 319 for (Path regiondir: FSUtils.getRegionDirs(fs, tabledir)) { 320 HFileArchiver.archiveRegion(fs, this.rootdir, tabledir, regiondir); 321 } 322 } 323 if (!fs.delete(tmpdir, true)) { 324 throw new IOException("Unable to clean the temp directory: " + tmpdir); 325 } 326 } 327 328 // Create the temp directory 329 if (isSecurityEnabled) { 330 if (!fs.mkdirs(tmpdir, secureRootSubDirPerms)) { 331 throw new IOException("HBase temp directory '" + tmpdir + "' creation failure."); 332 } 333 } else { 334 if (!fs.mkdirs(tmpdir)) { 335 throw new IOException("HBase temp directory '" + tmpdir + "' creation failure."); 336 } 337 } 338 } 339 340 /** 341 * Make sure the directories under rootDir have good permissions. Create if necessary. 342 * @param p 343 * @throws IOException 344 */ 345 private void checkSubDir(final Path p, final String dirPermsConfName) throws IOException { 346 FileSystem fs = p.getFileSystem(conf); 347 FsPermission dirPerms = new FsPermission(conf.get(dirPermsConfName, "700")); 348 if (!fs.exists(p)) { 349 if (isSecurityEnabled) { 350 if (!fs.mkdirs(p, secureRootSubDirPerms)) { 351 throw new IOException("HBase directory '" + p + "' creation failure."); 352 } 353 } else { 354 if (!fs.mkdirs(p)) { 355 throw new IOException("HBase directory '" + p + "' creation failure."); 356 } 357 } 358 } 359 else { 360 if (isSecurityEnabled && !dirPerms.equals(fs.getFileStatus(p).getPermission())) { 361 // check whether the permission match 362 LOG.warn("Found HBase directory permissions NOT matching expected permissions for " 363 + p.toString() + " permissions=" + fs.getFileStatus(p).getPermission() 364 + ", expecting " + dirPerms + ". Automatically setting the permissions. " 365 + "You can change the permissions by setting \"" + dirPermsConfName + "\" in hbase-site.xml " 366 + "and restarting the master"); 367 fs.setPermission(p, dirPerms); 368 } 369 } 370 } 371 372 /** 373 * Check permissions for bulk load staging directory. This directory has special hidden 374 * permissions. Create it if necessary. 375 * @throws IOException 376 */ 377 private void checkStagingDir() throws IOException { 378 Path p = new Path(this.rootdir, HConstants.BULKLOAD_STAGING_DIR_NAME); 379 try { 380 if (!this.fs.exists(p)) { 381 if (!this.fs.mkdirs(p, HiddenDirPerms)) { 382 throw new IOException("Failed to create staging directory " + p.toString()); 383 } 384 } else { 385 this.fs.setPermission(p, HiddenDirPerms); 386 } 387 } catch (IOException e) { 388 LOG.error("Failed to create or set permission on staging directory " + p.toString()); 389 throw new IOException("Failed to create or set permission on staging directory " 390 + p.toString(), e); 391 } 392 } 393 394 private static void bootstrap(final Path rd, final Configuration c) 395 throws IOException { 396 LOG.info("BOOTSTRAP: creating hbase:meta region"); 397 try { 398 // Bootstrapping, make sure blockcache is off. Else, one will be 399 // created here in bootstrap and it'll need to be cleaned up. Better to 400 // not make it in first place. Turn off block caching for bootstrap. 401 // Enable after. 402 TableDescriptor metaDescriptor = new FSTableDescriptors(c).get(TableName.META_TABLE_NAME); 403 HRegion meta = HRegion.createHRegion(RegionInfoBuilder.FIRST_META_REGIONINFO, rd, 404 c, setInfoFamilyCachingForMeta(metaDescriptor, false), null); 405 meta.close(); 406 } catch (IOException e) { 407 e = e instanceof RemoteException ? 408 ((RemoteException)e).unwrapRemoteException() : e; 409 LOG.error("bootstrap", e); 410 throw e; 411 } 412 } 413 414 /** 415 * Enable in memory caching for hbase:meta 416 */ 417 public static TableDescriptor setInfoFamilyCachingForMeta(TableDescriptor metaDescriptor, final boolean b) { 418 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(metaDescriptor); 419 for (ColumnFamilyDescriptor hcd: metaDescriptor.getColumnFamilies()) { 420 if (Bytes.equals(hcd.getName(), HConstants.CATALOG_FAMILY)) { 421 builder.modifyColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(hcd) 422 .setBlockCacheEnabled(b) 423 .setInMemory(b) 424 .build()); 425 } 426 } 427 return builder.build(); 428 } 429 430 public void deleteFamilyFromFS(RegionInfo region, byte[] familyName) 431 throws IOException { 432 deleteFamilyFromFS(rootdir, region, familyName); 433 } 434 435 public void deleteFamilyFromFS(Path rootDir, RegionInfo region, byte[] familyName) 436 throws IOException { 437 // archive family store files 438 Path tableDir = FSUtils.getTableDir(rootDir, region.getTable()); 439 HFileArchiver.archiveFamily(fs, conf, region, tableDir, familyName); 440 441 // delete the family folder 442 Path familyDir = new Path(tableDir, 443 new Path(region.getEncodedName(), Bytes.toString(familyName))); 444 if (fs.delete(familyDir, true) == false) { 445 if (fs.exists(familyDir)) { 446 throw new IOException("Could not delete family " 447 + Bytes.toString(familyName) + " from FileSystem for region " 448 + region.getRegionNameAsString() + "(" + region.getEncodedName() 449 + ")"); 450 } 451 } 452 } 453 454 public void stop() { 455 } 456 457 public void logFileSystemState(Logger log) throws IOException { 458 FSUtils.logFileSystemState(fs, rootdir, log); 459 } 460}