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.cleaner;
20
21 import java.io.IOException;
22 import java.util.List;
23 import java.util.concurrent.TimeUnit;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.ScheduledChore;
28 import org.apache.hadoop.hbase.Stoppable;
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
31 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
32 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
33
34
35
36
37
38 @InterfaceAudience.Private
39 public class SnapshotCleanerChore extends ScheduledChore {
40
41 private static final Log LOG = LogFactory.getLog(SnapshotCleanerChore.class);
42 private static final String SNAPSHOT_CLEANER_CHORE_NAME = "SnapshotCleaner";
43 private static final String SNAPSHOT_CLEANER_INTERVAL = "hbase.master.cleaner.snapshot.interval";
44 private static final int SNAPSHOT_CLEANER_DEFAULT_INTERVAL = 1800 * 1000;
45 private static final String DELETE_SNAPSHOT_EVENT =
46 "Eligible Snapshot for cleanup due to expired TTL.";
47
48 private final SnapshotManager snapshotManager;
49
50
51
52
53
54
55
56
57 public SnapshotCleanerChore(Stoppable stopper, Configuration configuration,
58 SnapshotManager snapshotManager) {
59 super(SNAPSHOT_CLEANER_CHORE_NAME, stopper, configuration.getInt(SNAPSHOT_CLEANER_INTERVAL,
60 SNAPSHOT_CLEANER_DEFAULT_INTERVAL));
61 this.snapshotManager = snapshotManager;
62 }
63
64 @Override
65 protected void chore() {
66 if (LOG.isTraceEnabled()) {
67 LOG.trace("Snapshot Cleaner Chore is starting up...");
68 }
69 try {
70 List<SnapshotDescription> completedSnapshotsList =
71 this.snapshotManager.getCompletedSnapshots();
72 for (SnapshotDescription snapshotDescription : completedSnapshotsList) {
73 long snapshotCreatedTime = snapshotDescription.getCreationTime();
74 long snapshotTtl = snapshotDescription.getTtl();
75
76
77
78
79
80 if (snapshotCreatedTime > 0 && snapshotTtl > 0 &&
81 snapshotTtl < TimeUnit.MILLISECONDS.toSeconds(Long.MAX_VALUE)) {
82 long currentTime = EnvironmentEdgeManager.currentTime();
83 if ((snapshotCreatedTime + TimeUnit.SECONDS.toMillis(snapshotTtl)) < currentTime) {
84 LOG.info("Event: " + DELETE_SNAPSHOT_EVENT + " Name: " + snapshotDescription.getName() +
85 ", CreatedTime: " + snapshotCreatedTime + ", TTL: " + snapshotTtl +
86 ", currentTime: " + currentTime);
87 deleteExpiredSnapshot(snapshotDescription);
88 }
89 }
90 }
91 } catch (IOException e) {
92 LOG.error("Error while cleaning up Snapshots...", e);
93 }
94 if (LOG.isTraceEnabled()) {
95 LOG.trace("Snapshot Cleaner Chore is closing...");
96 }
97 }
98
99 private void deleteExpiredSnapshot(SnapshotDescription snapshotDescription) {
100 try {
101 this.snapshotManager.deleteSnapshot(snapshotDescription);
102 } catch (Exception e) {
103 LOG.error("Error while deleting Snapshot: " + snapshotDescription.getName(), e);
104 }
105 }
106
107 }