HBASE-23222 MOB compaction supportability improvements
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / mob / ManualMobMaintHFileCleaner.java
blob408030d7ca14db80349be041c9269346d1105c32
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.mob;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
23 import org.apache.hadoop.fs.FileStatus;
24 import org.apache.hadoop.fs.Path;
25 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.master.cleaner.BaseHFileCleanerDelegate;
28 import org.apache.hadoop.hbase.util.FSUtils;
29 import org.apache.yetus.audience.InterfaceAudience;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 /**
34 * {@link BaseHFileCleanerDelegate} that prevents cleaning HFiles from a mob region
36 * keeps a map of table name strings to mob region name strings over the life of
37 * a JVM instance. if there's churn of unique table names we'll eat memory until
38 * Master restart.
40 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
41 public class ManualMobMaintHFileCleaner extends BaseHFileCleanerDelegate {
43 private static final Logger LOG = LoggerFactory.getLogger(ManualMobMaintHFileCleaner.class);
45 // We need to avoid making HRegionInfo objects for every table we check.
46 private static final ConcurrentMap<TableName, String> MOB_REGIONS = new ConcurrentHashMap<>();
48 @Override
49 public boolean isFileDeletable(FileStatus fStat) {
50 try {
51 // if its a directory, then it can be deleted
52 if (fStat.isDirectory()) {
53 return true;
56 Path file = fStat.getPath();
58 // we need the table and region to determine if this is from a mob region
59 // we don't need to worry about hfilelink back references, because the hfilelink cleaner will
60 // retain them.
61 Path family = file.getParent();
62 Path region = family.getParent();
63 Path table = region.getParent();
65 TableName tableName = FSUtils.getTableName(table);
67 String mobRegion = MOB_REGIONS.get(tableName);
68 if (mobRegion == null) {
69 String tmp = MobUtils.getMobRegionInfo(tableName).getEncodedName();
70 if (tmp == null) {
71 LOG.error("couldn't determine mob region for table {} keeping files just in case.",
72 tableName);
73 return false;
75 mobRegion = MOB_REGIONS.putIfAbsent(tableName, tmp);
76 // a return of null means that tmp is now in the map for future lookups.
77 if (mobRegion == null) {
78 mobRegion = tmp;
80 LOG.debug("Had to calculate name of mob region for table {} and it is {}", tableName,
81 mobRegion);
84 boolean ret = !mobRegion.equals(region.getName());
85 if (LOG.isDebugEnabled() && !ret) {
86 LOG.debug("Keeping file '{}' because it is from mob dir", fStat.getPath());
88 return ret;
89 } catch (RuntimeException e) {
90 LOG.error("Failed to determine mob status of '{}', keeping it just in case.", fStat.getPath(),
91 e);
92 return false;