1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.backup.example;
19
20 import java.io.IOException;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
27 import org.apache.hadoop.hbase.client.HConnection;
28 import org.apache.hadoop.hbase.master.cleaner.HFileCleaner;
29 import org.apache.hadoop.hbase.util.Bytes;
30 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
31 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
32 import org.apache.zookeeper.KeeperException;
33
34
35
36
37
38
39
40 @InterfaceAudience.Private
41 class HFileArchiveManager {
42
43 private final String archiveZnode;
44 private static final Log LOG = LogFactory.getLog(HFileArchiveManager.class);
45 private final ZooKeeperWatcher zooKeeper;
46 private volatile boolean stopped = false;
47
48 public HFileArchiveManager(HConnection connection, Configuration conf)
49 throws ZooKeeperConnectionException, IOException {
50 this.zooKeeper = new ZooKeeperWatcher(conf, "hfileArchiveManager-on-" + connection.toString(),
51 connection);
52 this.archiveZnode = ZKTableArchiveClient.getArchiveZNode(this.zooKeeper.getConfiguration(),
53 this.zooKeeper);
54 }
55
56
57
58
59
60
61
62
63
64 public HFileArchiveManager enableHFileBackup(byte[] table) throws KeeperException {
65 enable(this.zooKeeper, table);
66 return this;
67 }
68
69
70
71
72
73
74
75
76 public HFileArchiveManager disableHFileBackup(byte[] table) throws KeeperException {
77 disable(this.zooKeeper, table);
78 return this;
79 }
80
81
82
83
84
85
86 public HFileArchiveManager disableHFileBackup() throws IOException {
87 LOG.debug("Disabling backups on all tables.");
88 try {
89 ZKUtil.deleteNodeRecursively(this.zooKeeper, archiveZnode);
90 return this;
91 } catch (KeeperException e) {
92 throw new IOException("Unexpected ZK exception!", e);
93 }
94 }
95
96
97
98
99
100
101
102
103
104
105
106 private void enable(ZooKeeperWatcher zooKeeper, byte[] table)
107 throws KeeperException {
108 LOG.debug("Ensuring archiving znode exists");
109 ZKUtil.createAndFailSilent(zooKeeper, archiveZnode);
110
111
112 String tableNode = this.getTableNode(table);
113 LOG.debug("Creating: " + tableNode + ", data: []");
114 ZKUtil.createSetData(zooKeeper, tableNode, new byte[0]);
115 }
116
117
118
119
120
121
122
123
124
125 private void disable(ZooKeeperWatcher zooKeeper, byte[] table) throws KeeperException {
126
127 zooKeeper.sync(archiveZnode);
128
129
130 if (ZKUtil.checkExists(zooKeeper, archiveZnode) < 0) {
131 return;
132 }
133
134 String tableNode = this.getTableNode(table);
135
136 zooKeeper.sync(tableNode);
137
138 LOG.debug("Attempting to delete table node:" + tableNode);
139 ZKUtil.deleteNodeRecursively(zooKeeper, tableNode);
140 }
141
142 public void stop() {
143 if (!this.stopped) {
144 this.stopped = true;
145 LOG.debug("Stopping HFileArchiveManager...");
146 this.zooKeeper.close();
147 }
148 }
149
150
151
152
153
154
155
156 public boolean isArchivingEnabled(byte[] table) throws KeeperException {
157 String tableNode = this.getTableNode(table);
158 return ZKUtil.checkExists(zooKeeper, tableNode) >= 0;
159 }
160
161
162
163
164
165
166 private String getTableNode(byte[] table) {
167 return ZKUtil.joinZNode(archiveZnode, Bytes.toString(table));
168 }
169 }