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