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.security.access; 019 020import java.io.IOException; 021import java.util.Map; 022 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.fs.FileStatus; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.hbase.HBaseInterfaceAudience; 027import org.apache.hadoop.hbase.HConstants; 028import org.apache.hadoop.hbase.MetaTableAccessor; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.master.HMaster; 031import org.apache.hadoop.hbase.master.cleaner.BaseHFileCleanerDelegate; 032import org.apache.yetus.audience.InterfaceAudience; 033import org.apache.yetus.audience.InterfaceStability; 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; 038 039/** 040 * Implementation of a file cleaner that checks if a empty directory with no subdirs and subfiles is 041 * deletable when user scan snapshot feature is enabled 042 */ 043@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG) 044@InterfaceStability.Evolving 045public class SnapshotScannerHDFSAclCleaner extends BaseHFileCleanerDelegate { 046 private static final Logger LOG = LoggerFactory.getLogger(SnapshotScannerHDFSAclCleaner.class); 047 048 private HMaster master; 049 private boolean userScanSnapshotEnabled = false; 050 051 @Override 052 public void init(Map<String, Object> params) { 053 if (params != null && params.containsKey(HMaster.MASTER)) { 054 this.master = (HMaster) params.get(HMaster.MASTER); 055 } 056 } 057 058 @Override 059 public void setConf(Configuration conf) { 060 super.setConf(conf); 061 userScanSnapshotEnabled = SnapshotScannerHDFSAclHelper.isAclSyncToHdfsEnabled(conf); 062 } 063 064 @Override 065 protected boolean isFileDeletable(FileStatus fStat) { 066 // This plugin does not handle the file deletions, so return true by default 067 return true; 068 } 069 070 @Override 071 public boolean isEmptyDirDeletable(Path dir) { 072 if (userScanSnapshotEnabled) { 073 /* 074 * If user scan snapshot feature is enabled(see HBASE-21995), then when namespace or table 075 * exists, the archive namespace or table directories should not be deleted because the HDFS 076 * acls are set at these directories; the archive data directory should not be deleted because 077 * the HDFS acls of global permission is set at this directory. 078 */ 079 return isEmptyArchiveDirDeletable(dir); 080 } 081 return true; 082 } 083 084 private boolean isEmptyArchiveDirDeletable(Path dir) { 085 try { 086 if (isArchiveDataDir(dir)) { 087 return false; 088 } else if (isArchiveNamespaceDir(dir) && namespaceExists(dir.getName())) { 089 return false; 090 } else if (isArchiveTableDir(dir) 091 && tableExists(TableName.valueOf(dir.getParent().getName(), dir.getName()))) { 092 return false; 093 } 094 return true; 095 } catch (IOException e) { 096 LOG.warn("Check if empty dir {} is deletable error", dir, e); 097 return false; 098 } 099 } 100 101 @VisibleForTesting 102 static boolean isArchiveDataDir(Path path) { 103 if (path != null && path.getName().equals(HConstants.BASE_NAMESPACE_DIR)) { 104 Path parent = path.getParent(); 105 return parent != null && parent.getName().equals(HConstants.HFILE_ARCHIVE_DIRECTORY); 106 } 107 return false; 108 } 109 110 @VisibleForTesting 111 static boolean isArchiveNamespaceDir(Path path) { 112 return path != null && isArchiveDataDir(path.getParent()); 113 } 114 115 @VisibleForTesting 116 static boolean isArchiveTableDir(Path path) { 117 return path != null && isArchiveNamespaceDir(path.getParent()); 118 } 119 120 private boolean namespaceExists(String namespace) throws IOException { 121 return master != null && master.listNamespaces().contains(namespace); 122 } 123 124 private boolean tableExists(TableName tableName) throws IOException { 125 return master != null && MetaTableAccessor.tableExists(master.getConnection(), tableName); 126 } 127}