1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mob;
20
21 import java.io.IOException;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.conf.Configured;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.hbase.HBaseConfiguration;
30 import org.apache.hadoop.hbase.HColumnDescriptor;
31 import org.apache.hadoop.hbase.HConstants;
32 import org.apache.hadoop.hbase.HTableDescriptor;
33 import org.apache.hadoop.hbase.TableName;
34 import org.apache.hadoop.hbase.client.HBaseAdmin;
35 import org.apache.hadoop.hbase.io.hfile.CacheConfig;
36 import org.apache.hadoop.hbase.util.Bytes;
37 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
38 import org.apache.hadoop.util.Tool;
39 import org.apache.hadoop.util.ToolRunner;
40
41 import com.google.protobuf.ServiceException;
42
43
44
45
46 @InterfaceAudience.Private
47 public class ExpiredMobFileCleaner extends Configured implements Tool {
48
49 private static final Log LOG = LogFactory.getLog(ExpiredMobFileCleaner.class);
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public void cleanExpiredMobFiles(String tableName, HColumnDescriptor family)
64 throws ServiceException, IOException {
65 Configuration conf = getConf();
66 TableName tn = TableName.valueOf(tableName);
67 FileSystem fs = FileSystem.get(conf);
68 LOG.info("Cleaning the expired MOB files of " + family.getNameAsString() + " in " + tableName);
69
70 Configuration copyOfConf = new Configuration(conf);
71 copyOfConf.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0f);
72 CacheConfig cacheConfig = new CacheConfig(copyOfConf);
73 MobUtils.cleanExpiredMobFiles(fs, conf, tn, family, cacheConfig,
74 EnvironmentEdgeManager.currentTime());
75 }
76
77 public static void main(String[] args) throws Exception {
78 Configuration conf = HBaseConfiguration.create();
79 ToolRunner.run(conf, new ExpiredMobFileCleaner(), args);
80 }
81
82 private void printUsage() {
83 System.err.println("Usage:\n" + "--------------------------\n"
84 + ExpiredMobFileCleaner.class.getName() + " tableName familyName");
85 System.err.println(" tableName The table name");
86 System.err.println(" familyName The column family name");
87 }
88
89 public int run(String[] args) throws Exception {
90 if (args.length != 2) {
91 printUsage();
92 return 1;
93 }
94 String tableName = args[0];
95 String familyName = args[1];
96 TableName tn = TableName.valueOf(tableName);
97 HBaseAdmin.checkHBaseAvailable(getConf());
98 HBaseAdmin admin = new HBaseAdmin(getConf());
99 try {
100 HTableDescriptor htd = admin.getTableDescriptor(tn);
101 HColumnDescriptor family = htd.getFamily(Bytes.toBytes(familyName));
102 if (family == null || !family.isMobEnabled()) {
103 throw new IOException("Column family " + familyName + " is not a MOB column family");
104 }
105 if (family.getMinVersions() > 0) {
106 throw new IOException(
107 "The minVersions of the column family is not 0, could not be handled by this cleaner");
108 }
109 cleanExpiredMobFiles(tableName, family);
110 return 0;
111 } finally {
112 try {
113 admin.close();
114 } catch (IOException e) {
115 LOG.error("Failed to close the HBaseAdmin.", e);
116 }
117 }
118 }
119
120 }