|
| 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.security.access; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.apache.hadoop.conf.Configuration; |
| 24 | +import org.apache.hadoop.fs.FileStatus; |
| 25 | +import org.apache.hadoop.fs.Path; |
| 26 | +import org.apache.hadoop.hbase.HBaseInterfaceAudience; |
| 27 | +import org.apache.hadoop.hbase.HConstants; |
| 28 | +import org.apache.hadoop.hbase.MetaTableAccessor; |
| 29 | +import org.apache.hadoop.hbase.TableName; |
| 30 | +import org.apache.hadoop.hbase.coprocessor.CoprocessorHost; |
| 31 | +import org.apache.hadoop.hbase.master.HMaster; |
| 32 | +import org.apache.hadoop.hbase.master.cleaner.BaseHFileCleanerDelegate; |
| 33 | +import org.apache.yetus.audience.InterfaceAudience; |
| 34 | +import org.apache.yetus.audience.InterfaceStability; |
| 35 | +import org.slf4j.Logger; |
| 36 | +import org.slf4j.LoggerFactory; |
| 37 | + |
| 38 | +import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; |
| 39 | + |
| 40 | +/** |
| 41 | + * Implementation of a file cleaner that checks if a empty directory with no subdirs and subfiles is |
| 42 | + * deletable when user scan snapshot feature is enabled |
| 43 | + */ |
| 44 | +@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG) |
| 45 | +@InterfaceStability.Evolving |
| 46 | +public class SnapshotScannerHDFSAclCleaner extends BaseHFileCleanerDelegate { |
| 47 | + private static final Logger LOG = LoggerFactory.getLogger(SnapshotScannerHDFSAclCleaner.class); |
| 48 | + |
| 49 | + private HMaster master; |
| 50 | + private boolean userScanSnapshotEnabled = false; |
| 51 | + |
| 52 | + @Override |
| 53 | + public void init(Map<String, Object> params) { |
| 54 | + if (params != null && params.containsKey(HMaster.MASTER)) { |
| 55 | + this.master = (HMaster) params.get(HMaster.MASTER); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void setConf(Configuration conf) { |
| 61 | + super.setConf(conf); |
| 62 | + userScanSnapshotEnabled = isUserScanSnapshotEnabled(conf); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected boolean isFileDeletable(FileStatus fStat) { |
| 67 | + // This plugin does not handle the file deletions, so return true by default |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean isEmptyDirDeletable(Path dir) { |
| 73 | + if (userScanSnapshotEnabled) { |
| 74 | + /* |
| 75 | + * If user scan snapshot feature is enabled(see HBASE-21995), then when namespace or table |
| 76 | + * exists, the archive namespace or table directories should not be deleted because the HDFS |
| 77 | + * acls are set at these directories; the archive data directory should not be deleted because |
| 78 | + * the HDFS acls of global permission is set at this directory. |
| 79 | + */ |
| 80 | + return isEmptyArchiveDirDeletable(dir); |
| 81 | + } |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + private boolean isUserScanSnapshotEnabled(Configuration conf) { |
| 86 | + String masterCoprocessors = conf.get(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY); |
| 87 | + return conf.getBoolean(SnapshotScannerHDFSAclHelper.USER_SCAN_SNAPSHOT_ENABLE, false) |
| 88 | + && masterCoprocessors.contains(SnapshotScannerHDFSAclController.class.getName()) |
| 89 | + && masterCoprocessors.contains(AccessController.class.getName()); |
| 90 | + } |
| 91 | + |
| 92 | + private boolean isEmptyArchiveDirDeletable(Path dir) { |
| 93 | + try { |
| 94 | + if (isArchiveDataDir(dir)) { |
| 95 | + return false; |
| 96 | + } else if (isArchiveNamespaceDir(dir) && namespaceExists(dir.getName())) { |
| 97 | + return false; |
| 98 | + } else if (isArchiveTableDir(dir)) { |
| 99 | + return !tableExists(TableName.valueOf(dir.getParent().getName(), dir.getName())); |
| 100 | + } |
| 101 | + return true; |
| 102 | + } catch (IOException e) { |
| 103 | + LOG.warn("Check if empty dir {} is deletable error", dir, e); |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @VisibleForTesting |
| 109 | + static boolean isArchiveDataDir(Path path) { |
| 110 | + if (path != null && path.getName().equals(HConstants.BASE_NAMESPACE_DIR)) { |
| 111 | + Path parent = path.getParent(); |
| 112 | + return parent != null && parent.getName().equals(HConstants.HFILE_ARCHIVE_DIRECTORY); |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + @VisibleForTesting |
| 118 | + static boolean isArchiveNamespaceDir(Path path) { |
| 119 | + return path != null && isArchiveDataDir(path.getParent()); |
| 120 | + } |
| 121 | + |
| 122 | + @VisibleForTesting |
| 123 | + static boolean isArchiveTableDir(Path path) { |
| 124 | + return path != null && isArchiveNamespaceDir(path.getParent()); |
| 125 | + } |
| 126 | + |
| 127 | + private boolean namespaceExists(String namespace) throws IOException { |
| 128 | + return master != null && master.listNamespaces().contains(namespace); |
| 129 | + } |
| 130 | + |
| 131 | + private boolean tableExists(TableName tableName) throws IOException { |
| 132 | + return master != null && MetaTableAccessor.tableExists(master.getConnection(), tableName); |
| 133 | + } |
| 134 | +} |
0 commit comments