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