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