Skip to content

Commit 4d86d3b

Browse files
committed
HBASE-26614 Refactor code related to "dump"ing ZK nodes (#3969)
The code starting at `ZKUtil.dump(ZKWatcher)` is a small mess – it has cyclic dependencies woven through itself, `ZKWatcher` and `RecoverableZooKeeper`. It also initializes a static variable in `ZKUtil` through the factory for `RecoverableZooKeeper` instances. Let's decouple and clean it up. Signed-off-by: Duo Zhang <[email protected]> Signed-off-by: Josh Elser <[email protected]>
1 parent 7e84223 commit 4d86d3b

File tree

11 files changed

+381
-331
lines changed

11 files changed

+381
-331
lines changed

hbase-it/src/test/java/org/apache/hadoop/hbase/test/IntegrationTestZKAndFSPermissions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.apache.hadoop.hbase.util.AbstractHBaseTool;
3636
import org.apache.hadoop.hbase.util.CommonFSUtils;
3737
import org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper;
38-
import org.apache.hadoop.hbase.zookeeper.ZKUtil;
3938
import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
4039
import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
4140
import org.apache.hadoop.util.ToolRunner;
@@ -142,7 +141,7 @@ protected int doWork() throws Exception {
142141
private void testZNodeACLs() throws IOException, KeeperException, InterruptedException {
143142

144143
ZKWatcher watcher = new ZKWatcher(conf, "IntegrationTestZnodeACLs", null);
145-
RecoverableZooKeeper zk = ZKUtil.connect(this.conf, watcher);
144+
RecoverableZooKeeper zk = RecoverableZooKeeper.connect(this.conf, watcher);
146145

147146
String baseZNode = watcher.getZNodePaths().baseZNode;
148147

hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/DumpReplicationQueues.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.apache.hadoop.hbase.replication.ReplicationQueueStorage;
4949
import org.apache.hadoop.hbase.replication.ReplicationStorageFactory;
5050
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
51+
import org.apache.hadoop.hbase.zookeeper.ZKDump;
5152
import org.apache.hadoop.hbase.zookeeper.ZKUtil;
5253
import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
5354
import org.apache.hadoop.util.Tool;
@@ -241,7 +242,7 @@ private int dumpReplicationQueues(DumpOptions opts) throws Exception {
241242
} else {
242243
// use ZK instead
243244
System.out.print("Dumping replication znodes via ZooKeeper:");
244-
System.out.println(ZKUtil.getReplicationZnodesDump(zkw));
245+
System.out.println(ZKDump.getReplicationZnodesDump(zkw));
245246
}
246247
return (0);
247248
} catch (IOException e) {

hbase-server/src/main/resources/hbase-webapps/master/zk.jsp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
--%>
2020
<%@ page contentType="text/html;charset=UTF-8"
2121
import="org.apache.commons.lang3.StringEscapeUtils"
22-
import="org.apache.hadoop.hbase.zookeeper.ZKUtil"
23-
import="org.apache.hadoop.hbase.zookeeper.ZKWatcher"
2422
import="org.apache.hadoop.hbase.master.HMaster"
23+
import="org.apache.hadoop.hbase.zookeeper.ZKDump"
24+
import="org.apache.hadoop.hbase.zookeeper.ZKWatcher"
2525
%>
2626
<%
2727
HMaster master = (HMaster)getServletContext().getAttribute(HMaster.MASTER);
@@ -38,7 +38,7 @@
3838
</div>
3939
<div class="row">
4040
<div class="span12">
41-
<pre><%= StringEscapeUtils.escapeHtml4(ZKUtil.dump(watcher).trim()) %></pre>
41+
<pre><%= StringEscapeUtils.escapeHtml4(ZKDump.dump(watcher).trim()) %></pre>
4242
</div>
4343
</div>
4444
</div>

hbase-server/src/test/java/org/apache/hadoop/hbase/replication/TestNamespaceReplicationWithBulkLoadedData.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.apache.hadoop.hbase.util.Bytes;
4545
import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
4646
import org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper;
47-
import org.apache.hadoop.hbase.zookeeper.ZKUtil;
4847
import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
4948

5049
import org.junit.After;
@@ -282,7 +281,7 @@ public void testBulkLoadReplicationActiveActive() throws Exception {
282281
// Verify hfile-refs for 1:ns_peer1, expect is empty
283282
MiniZooKeeperCluster zkCluster = UTIL1.getZkCluster();
284283
ZKWatcher watcher = new ZKWatcher(UTIL1.getConfiguration(), "TestZnodeHFiles-refs", null);
285-
RecoverableZooKeeper zk = ZKUtil.connect(UTIL1.getConfiguration(), watcher);
284+
RecoverableZooKeeper zk = RecoverableZooKeeper.connect(UTIL1.getConfiguration(), watcher);
286285
ZKReplicationQueueStorage replicationQueueStorage =
287286
new ZKReplicationQueueStorage(watcher, UTIL1.getConfiguration());
288287
Set<String> hfiles = replicationQueueStorage.getAllHFileRefs();

hbase-shell/src/main/ruby/hbase/admin.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def zk_dump
452452
)
453453
zk = @zk_wrapper.getRecoverableZooKeeper.getZooKeeper
454454
@zk_main = org.apache.zookeeper.ZooKeeperMain.new(zk)
455-
org.apache.hadoop.hbase.zookeeper.ZKUtil.dump(@zk_wrapper)
455+
org.apache.hadoop.hbase.zookeeper.ZKDump.dump(@zk_wrapper)
456456
end
457457

458458
#----------------------------------------------------------------------------------------------

hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MetaTableLocator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private MetaTableLocator() {
5858
* @return server name or null if we failed to get the data.
5959
*/
6060
@RestrictedApi(explanation = "Should only be called in tests or ZKUtil", link = "",
61-
allowedOnPath = ".*/src/test/.*|.*/ZKUtil\\.java")
61+
allowedOnPath = ".*/src/test/.*|.*/ZKDump\\.java")
6262
public static ServerName getMetaRegionLocation(final ZKWatcher zkw) {
6363
try {
6464
RegionState state = getMetaRegionState(zkw);
@@ -75,7 +75,7 @@ public static ServerName getMetaRegionLocation(final ZKWatcher zkw) {
7575
* @return server name
7676
*/
7777
@RestrictedApi(explanation = "Should only be called in self or ZKUtil", link = "",
78-
allowedOnPath = ".*(MetaTableLocator|ZKUtil)\\.java")
78+
allowedOnPath = ".*(MetaTableLocator|ZKDump)\\.java")
7979
public static ServerName getMetaRegionLocation(final ZKWatcher zkw, int replicaId) {
8080
try {
8181
RegionState state = getMetaRegionState(zkw, replicaId);

hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/RecoverableZooKeeper.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.ArrayList;
2626
import java.util.LinkedList;
2727
import java.util.List;
28+
import org.apache.hadoop.conf.Configuration;
29+
import org.apache.hadoop.hbase.HConstants;
2830
import org.apache.hadoop.hbase.trace.TraceUtil;
2931
import org.apache.hadoop.hbase.util.Bytes;
3032
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
@@ -84,6 +86,57 @@ public class RecoverableZooKeeper {
8486
private final String quorumServers;
8587
private final int maxMultiSize;
8688

89+
/**
90+
* See {@link #connect(Configuration, String, Watcher, String)}
91+
*/
92+
public static RecoverableZooKeeper connect(Configuration conf, Watcher watcher)
93+
throws IOException {
94+
String ensemble = ZKConfig.getZKQuorumServersString(conf);
95+
return connect(conf, ensemble, watcher);
96+
}
97+
98+
/**
99+
* See {@link #connect(Configuration, String, Watcher, String)}
100+
*/
101+
public static RecoverableZooKeeper connect(Configuration conf, String ensemble,
102+
Watcher watcher)
103+
throws IOException {
104+
return connect(conf, ensemble, watcher, null);
105+
}
106+
107+
/**
108+
* Creates a new connection to ZooKeeper, pulling settings and ensemble config
109+
* from the specified configuration object using methods from {@link ZKConfig}.
110+
*
111+
* Sets the connection status monitoring watcher to the specified watcher.
112+
*
113+
* @param conf configuration to pull ensemble and other settings from
114+
* @param watcher watcher to monitor connection changes
115+
* @param ensemble ZooKeeper servers quorum string
116+
* @param identifier value used to identify this client instance.
117+
* @return connection to zookeeper
118+
* @throws IOException if unable to connect to zk or config problem
119+
*/
120+
public static RecoverableZooKeeper connect(Configuration conf, String ensemble,
121+
Watcher watcher, final String identifier)
122+
throws IOException {
123+
if(ensemble == null) {
124+
throw new IOException("Unable to determine ZooKeeper ensemble");
125+
}
126+
int timeout = conf.getInt(HConstants.ZK_SESSION_TIMEOUT,
127+
HConstants.DEFAULT_ZK_SESSION_TIMEOUT);
128+
if (LOG.isTraceEnabled()) {
129+
LOG.trace("{} opening connection to ZooKeeper ensemble={}", identifier, ensemble);
130+
}
131+
int retry = conf.getInt("zookeeper.recovery.retry", 3);
132+
int retryIntervalMillis =
133+
conf.getInt("zookeeper.recovery.retry.intervalmill", 1000);
134+
int maxSleepTime = conf.getInt("zookeeper.recovery.retry.maxsleeptime", 60000);
135+
int multiMaxSize = conf.getInt("zookeeper.multi.max.size", 1024*1024);
136+
return new RecoverableZooKeeper(ensemble, timeout, watcher,
137+
retry, retryIntervalMillis, maxSleepTime, identifier, multiMaxSize);
138+
}
139+
87140
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="DE_MIGHT_IGNORE",
88141
justification="None. Its always been this way.")
89142
public RecoverableZooKeeper(String quorumServers, int sessionTimeout,

0 commit comments

Comments
 (0)