Skip to content

Instantly share code, notes, and snippets.

@wsky
Last active December 14, 2015 19:59
Show Gist options
  • Save wsky/5140593 to your computer and use it in GitHub Desktop.
Save wsky/5140593 to your computer and use it in GitHub Desktop.
zookeeper sample code
private void runZk() throws IOException, InterruptedException {
int numConnections = 5000;
int tickTime = 2000;
String dataDirectory = System.getProperty("java.io.tmpdir");
File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile();
ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime);
NIOServerCnxn.Factory standaloneServerFactory = new NIOServerCnxn.Factory(new InetSocketAddress(clientPort), numConnections);
standaloneServerFactory.startup(server);
}
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import com.taobao.top.push.Client;
public class ZkStateShareHelper {
public static String zkConnectString;
public static String zkRootPath;
private static ZooKeeper zk;
private static Stat prevStat;
public static void init(String zkConnectString, String zkRootPath) {
try {
zk = new ZooKeeper(zkConnectString, 600000, null);
} catch (IOException e) {
e.printStackTrace();
return;
}
}
public static void shareState(Client client, String backend) {
try {
String path = getPath(client);
Stat stat = zk.exists(path, false);
if (stat == null) {
zk.create(path,
backend.getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL);
prevStat = zk.exists(path, false);
} else if (prevStat == null || prevStat.getVersion() != stat.getVersion()) {
prevStat = stat;
zk.setData(path, backend.getBytes(), stat.getVersion());
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static String getPath(Client client) {
return zkRootPath + "/" + client.getId();
}
}
import java.io.File;
import java.net.InetSocketAddress;
import static org.junit.Assert.*;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.server.NIOServerCnxn;
import org.apache.zookeeper.server.ZooKeeperServer;
import org.junit.Test;
public class ZookeeperTest {
@Test
public void create_get_test() throws Exception {
// zk server
int clientPort = 21818;
int numConnections = 5000;
int tickTime = 2000;
String dataDirectory = System.getProperty("java.io.tmpdir");
File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile();
ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime);
NIOServerCnxn.Factory standaloneServerFactory = new NIOServerCnxn.Factory(new InetSocketAddress(clientPort), numConnections);
standaloneServerFactory.startup(server);
ZooKeeper zk1 = new ZooKeeper("127.0.0.1:" + clientPort, 30000, null);
ZooKeeper zk2 = new ZooKeeper("127.0.0.1:" + clientPort, 30000, null);
try {
String path = "/clientId";
String ip = "10.13.3.1";
zk1.create(path, ip.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
assertEquals(ip, new String(zk1.getData(path, null, null)));
// zk1 first set it
String ipChange = "10.13.3.2";
Stat stat = zk1.exists(path, true);
assertEquals(0, stat.getVersion());
zk1.setData(path, ipChange.getBytes(), stat.getVersion());
// version
assertEquals(1, zk1.exists(path, true).getVersion());
assertEquals(1, zk2.exists(path, true).getVersion());
// data
assertEquals(ipChange, new String(zk1.getData(path, null, null)));
assertEquals(ipChange, new String(zk2.getData(path, null, null)));
// zk2 change it, version plus 1
ipChange = "10.13.3.3";
stat = zk2.exists(path, true);
assertEquals(1, stat.getVersion());
zk2.setData(path, ipChange.getBytes(), stat.getVersion());
// version
assertEquals(2, zk1.exists(path, true).getVersion());
assertEquals(2, zk2.exists(path, true).getVersion());
// data
assertEquals(ipChange, new String(zk1.getData(path, null, null)));
assertEquals(ipChange, new String(zk2.getData(path, null, null)));
// zk1 change it again
ipChange = "10.13.3.4";
stat = zk1.exists(path, true);
assertEquals(2, stat.getVersion());
zk1.setData(path, ipChange.getBytes(), stat.getVersion());
// version
assertEquals(3, zk1.exists(path, true).getVersion());
assertEquals(3, zk2.exists(path, true).getVersion());
// data
assertEquals(ipChange, new String(zk1.getData(path, null, null)));
assertEquals(ipChange, new String(zk2.getData(path, null, null)));
} catch (Exception e) {
throw e;
} finally {
zk1.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment