1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.master;
20
21 import java.io.IOException;
22 import java.util.Map;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.classification.InterfaceAudience;
27 import org.apache.hadoop.hbase.ScheduledChore;
28 import org.apache.hadoop.hbase.HColumnDescriptor;
29 import org.apache.hadoop.hbase.HTableDescriptor;
30 import org.apache.hadoop.hbase.TableDescriptors;
31 import org.apache.hadoop.hbase.exceptions.LockTimeoutException;
32 import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
33 import org.apache.hadoop.hbase.mob.ExpiredMobFileCleaner;
34 import org.apache.hadoop.hbase.mob.MobConstants;
35 import org.apache.hadoop.hbase.mob.MobUtils;
36
37
38
39
40
41 @InterfaceAudience.Private
42 public class ExpiredMobFileCleanerChore extends ScheduledChore {
43
44 private static final Log LOG = LogFactory.getLog(ExpiredMobFileCleanerChore.class);
45 private final HMaster master;
46 private TableLockManager tableLockManager;
47 private ExpiredMobFileCleaner cleaner;
48
49 public ExpiredMobFileCleanerChore(HMaster master) {
50 super(master.getServerName() + "-ExpiredMobFileCleanerChore", master,
51 master.getConfiguration().getInt(MobConstants.MOB_CLEANER_PERIOD,
52 MobConstants.DEFAULT_MOB_CLEANER_PERIOD));
53
54 this.master = master;
55 this.tableLockManager = master.getTableLockManager();
56 cleaner = new ExpiredMobFileCleaner();
57 cleaner.setConf(master.getConfiguration());
58 }
59
60 @Override
61 protected void chore() {
62 try {
63 TableDescriptors htds = master.getTableDescriptors();
64 Map<String, HTableDescriptor> map = htds.getAll();
65 for (HTableDescriptor htd : map.values()) {
66 for (HColumnDescriptor hcd : htd.getColumnFamilies()) {
67 if (hcd.isMobEnabled() && hcd.getMinVersions() == 0) {
68
69
70 boolean tableLocked = false;
71 TableLock lock = null;
72 try {
73
74 if (tableLockManager != null) {
75 lock = tableLockManager.readLock(MobUtils.getTableLockName(htd.getTableName()),
76 "Run ExpiredMobFileCleanerChore");
77 lock.acquire();
78 }
79 tableLocked = true;
80 cleaner.cleanExpiredMobFiles(htd.getTableName().getNameAsString(), hcd);
81 } catch (LockTimeoutException e) {
82 LOG.info("Fail to acquire the lock because of timeout, maybe a"
83 + " MobFileCompactor is running", e);
84 } catch (Exception e) {
85 LOG.error(
86 "Fail to clean the expired mob files for the column " + hcd.getNameAsString()
87 + " in the table " + htd.getNameAsString(), e);
88 } finally {
89 if (lock != null && tableLocked) {
90 try {
91 lock.release();
92 } catch (IOException e) {
93 LOG.error(
94 "Fail to release the write lock for the table " + htd.getNameAsString(), e);
95 }
96 }
97 }
98 }
99 }
100 }
101 } catch (Exception e) {
102 LOG.error("Fail to clean the expired mob files", e);
103 }
104 }
105
106 }