Skip to content

Commit 5afdc76

Browse files
committed
upgrade for new server of zookeeper: ZOOKEEPER-2795
1 parent 959722f commit 5afdc76

File tree

5 files changed

+137
-75
lines changed

5 files changed

+137
-75
lines changed

README.md

+3-9
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@
44
[![Build Status](https://travis-ci.org/adyliu/zkclient.png?branch=master)](https://travis-ci.org/adyliu/zkclient)
55

66

7-
* [2013-12-25] zkclient 2.1.1 released.
8-
* [2013-09-04] zkclient 2.1 released.
9-
* [2012-12-04] zkclient 2.0 released.
10-
* [2012-04-12] zkclient 1.0 released.
11-
12-
137
## Maven Dependency
148

15-
The version 2.1.1 has been synchronized to [Maven Central Repository](http://repo1.maven.org/maven2/com/github/adyliu/zkclient/).
9+
The latest version has been synchronized to [Maven Central Repository](http://repo1.maven.org/maven2/com/github/adyliu/zkclient/).
1610

1711
<dependency>
1812
<groupId>com.github.adyliu</groupId>
1913
<artifactId>zkclient</artifactId>
20-
<version>2.1.1</version>
14+
<version>2.2.0</version>
2115
</dependency>
2216

2317

24-
* The version 2.x supports both zookeeper 3.3.x and 3.4.x.
18+
* The version 2.x supports both zookeeper 3.3.x and 3.4.x+.
2519
* The version [1.x](http://repo1.maven.org/maven2/com/github/adyliu/zkclient/) needs zookeeper 3.3.x.
2620

2721
## Wiki & Usage

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.adyliu</groupId>
66
<artifactId>zkclient</artifactId>
77
<name>zookeeper client wrapper</name>
8-
<version>2.1.2</version>
8+
<version>2.2.0</version>
99
<packaging>jar</packaging>
1010
<description>A zookeeper client wrapper.</description>
1111
<url>https://github.com/adyliu/zkclient</url>
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>org.apache.zookeeper</groupId>
4747
<artifactId>zookeeper</artifactId>
48-
<version>3.4.9</version>
48+
<version>[3.4.9,3.4.14]</version>
4949
<exclusions>
5050
<exclusion>
5151
<artifactId>jline</artifactId>
+110-63
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2010 the original author or authors.
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
* <p>
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,22 +15,22 @@
1515
*/
1616
package com.github.zkclient;
1717

18-
import java.io.File;
19-
import java.io.IOException;
20-
21-
import javax.annotation.PostConstruct;
22-
import javax.annotation.PreDestroy;
23-
24-
import org.apache.zookeeper.server.ZooKeeperServer;
25-
2618
import com.github.zkclient.exception.ZkException;
27-
import com.github.zkclient.exception.ZkInterruptedException;
19+
import org.apache.zookeeper.client.FourLetterWordMain;
20+
import org.apache.zookeeper.server.ServerConfig;
21+
import org.apache.zookeeper.server.ZooKeeperServerMain;
2822
import org.slf4j.Logger;
2923
import org.slf4j.LoggerFactory;
3024

31-
public class ZkServer {
25+
import javax.annotation.PostConstruct;
26+
import javax.annotation.PreDestroy;
27+
import java.io.File;
28+
import java.net.ConnectException;
3229

33-
private final static Logger LOG = LoggerFactory.getLogger(ZkServer.class);;
30+
public class ZkServer extends ZooKeeperServerMain {
31+
32+
private final static Logger LOG = LoggerFactory.getLogger(ZkServer.class);
33+
;
3434

3535
public static final int DEFAULT_PORT = 2181;
3636

@@ -42,10 +42,6 @@ public class ZkServer {
4242

4343
private final String _logDir;
4444

45-
private ZooKeeperServer _zk;
46-
47-
private ServerCnxnFactory _nioFactory;
48-
4945
private ZkClient _zkClient;
5046

5147
private final int _port;
@@ -54,6 +50,10 @@ public class ZkServer {
5450

5551
private final int _minSessionTimeout;
5652

53+
private volatile boolean shutdown = false;
54+
55+
private boolean daemon = true;
56+
5757
public ZkServer(String dataDir, String logDir) {
5858
this(dataDir, logDir, DEFAULT_PORT);
5959
}
@@ -80,6 +80,7 @@ public int getPort() {
8080

8181
@PostConstruct
8282
public void start() {
83+
shutdown = false;
8384
startZkServer();
8485
_zkClient = new ZkClient("localhost:" + _port, 10000);
8586
}
@@ -90,68 +91,114 @@ private void startZkServer() {
9091
final File dataDir = new File(_dataDir);
9192
final File dataLogDir = new File(_logDir);
9293
dataDir.mkdirs();
93-
dataLogDir.mkdirs();
9494

9595
// single zk server
96-
LOG.info("Start single zookeeper server...");
97-
LOG.info("data dir: " + dataDir.getAbsolutePath());
98-
LOG.info("data log dir: " + dataLogDir.getAbsolutePath());
99-
startSingleZkServer(_tickTime, dataDir, dataLogDir, port);
96+
LOG.info("Start single zookeeper server, port={} data={} ", port, dataDir.getAbsolutePath());
97+
//
98+
final ZooKeeperServerMain serverMain = this;
99+
final InnerServerConfig config = new InnerServerConfig();
100+
config.parse(new String[]{"" + port, dataDir.getAbsolutePath(), "" + _tickTime, "60"});
101+
config.setMinSessionTimeout(_minSessionTimeout);
102+
//
103+
final String threadName = "inner-zkserver-" + port;
104+
final Thread innerThread = new Thread(new Runnable() {
105+
@Override
106+
public void run() {
107+
try {
108+
serverMain.runFromConfig(config);
109+
} catch (Exception e) {
110+
throw new ZkException("Unable to start single ZooKeeper server.", e);
111+
}
112+
}
113+
}, threadName);
114+
innerThread.setDaemon(daemon);
115+
innerThread.start();
116+
//
117+
waitForServerUp(port, 30000, false);
118+
100119
} else {
101120
throw new IllegalStateException("Zookeeper port " + port + " was already in use. Running in single machine mode?");
102121
}
103122
}
104123

105-
private void startSingleZkServer(final int tickTime, final File dataDir, final File dataLogDir, final int port) {
106-
try {
107-
_zk = new ZooKeeperServer(dataDir, dataLogDir, tickTime);
108-
_zk.setMinSessionTimeout(_minSessionTimeout);
109-
_nioFactory = ServerCnxnFactory.createFactory(port, 60);
110-
_nioFactory.startup(_zk);
111-
} catch (IOException e) {
112-
throw new ZkException("Unable to start single ZooKeeper server.", e);
113-
} catch (InterruptedException e) {
114-
throw new ZkInterruptedException(e);
115-
}
116-
}
117-
118124
@PreDestroy
119125
public void shutdown() {
120-
ZooKeeperServer zk = _zk;
121-
if (zk == null) {
122-
LOG.warn("shutdown duplication");
123-
return;
124-
}else {
125-
_zk = null;
126+
if (!shutdown) {
127+
shutdown = true;
128+
LOG.info("Shutting down ZkServer port={}...", _port);
129+
if (_zkClient != null) {
130+
try {
131+
_zkClient.close();
132+
} catch (ZkException e) {
133+
LOG.warn("Error on closing zkclient: " + e.getClass().getName());
134+
}
135+
_zkClient = null;
136+
}
137+
super.shutdown();
138+
waitForServerDown(_port, 30000, false);
139+
LOG.info("Shutting down ZkServer port={}...done", _port);
126140
}
127-
LOG.info("Shutting down ZkServer...");
128-
try {
129-
_zkClient.close();
130-
} catch (ZkException e) {
131-
LOG.warn("Error on closing zkclient: " + e.getClass().getName());
141+
}
142+
143+
144+
public ZkClient getZkClient() {
145+
return _zkClient;
146+
}
147+
148+
class InnerServerConfig extends ServerConfig {
149+
public void setMinSessionTimeout(int minSessionTimeout) {
150+
this.minSessionTimeout = minSessionTimeout;
132151
}
133-
if (_nioFactory != null) {
134-
_nioFactory.shutdown();
152+
}
153+
154+
public static boolean waitForServerUp(int port, long timeout, boolean secure) {
155+
long start = System.currentTimeMillis();
156+
while (true) {
135157
try {
136-
_nioFactory.join();
137-
} catch (InterruptedException e) {
138-
Thread.currentThread().interrupt();
158+
// if there are multiple hostports, just take the first one
159+
String result = FourLetterWordMain.send4LetterWord("127.0.0.1", port, "stat");
160+
if (result.startsWith("Zookeeper version:") &&
161+
!result.contains("READ-ONLY")) {
162+
return true;
163+
}
164+
} catch (ConnectException e) {
165+
// ignore as this is expected, do not log stacktrace
166+
LOG.debug("server {} not up: {}", port, e.toString());
167+
} catch (Exception e) {
168+
// ignore as this is expected
169+
LOG.info("server {} not up", port, e);
170+
}
171+
172+
if (System.currentTimeMillis() > start + timeout) {
173+
break;
139174
}
140-
_nioFactory = null;
141-
}
142-
zk.shutdown();
143-
if (zk.getZKDatabase() != null) {
144175
try {
145-
// release file description
146-
zk.getZKDatabase().close();
147-
} catch (IOException e) {
148-
LOG.error(e.getMessage(), e);
176+
Thread.sleep(250);
177+
} catch (InterruptedException e) {
178+
// ignore
149179
}
150180
}
151-
LOG.info("Shutting down ZkServer...done");
181+
return false;
152182
}
153183

154-
public ZkClient getZkClient() {
155-
return _zkClient;
184+
public static boolean waitForServerDown(int port, long timeout, boolean secure) {
185+
long start = System.currentTimeMillis();
186+
while (true) {
187+
try {
188+
FourLetterWordMain.send4LetterWord("127.0.0.1", port, "stat");
189+
} catch (Exception e) {
190+
return true;
191+
}
192+
193+
if (System.currentTimeMillis() > start + timeout) {
194+
break;
195+
}
196+
try {
197+
Thread.sleep(250);
198+
} catch (InterruptedException e) {
199+
// ignore
200+
}
201+
}
202+
return false;
156203
}
157204
}

src/test/java/com/github/zkclient/ZkClientTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static ZkServer startZkServer(String testName, int port) throws IOExcept
7979

8080
@AfterClass
8181
public static void cleanup() throws IOException {
82-
deleteFile(new File(".", "build/test").getCanonicalFile());
82+
deleteFile(new File(".", "build/test").getCanonicalFile());
8383
}
8484

8585
@Before
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.zkclient;
2+
3+
import java.io.File;
4+
import java.util.Arrays;
5+
6+
public class ZkServerDemo {
7+
public static void main(String[] args) throws Exception{
8+
File dataDir = new File("/tmp/zkdemo");
9+
dataDir.mkdirs();
10+
ZkServer server = new ZkServer(dataDir.getAbsolutePath(), dataDir.getAbsolutePath());
11+
server.start();
12+
13+
ZkClient client = server.getZkClient();
14+
client.createPersistent("/a", true);
15+
byte[] dat = client.readData("/a");
16+
System.out.println(Arrays.toString(dat));
17+
client.writeData("/a", "OK".getBytes());
18+
System.out.println("agian="+Arrays.toString(dat));
19+
//server.shutdown();
20+
}
21+
}

0 commit comments

Comments
 (0)