Created
July 5, 2010 02:06
-
-
Save AshwinJay/463930 to your computer and use it in GitHub Desktop.
Simple Apache ZooKeeper test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Waiting to connect | |
WatchedEvent state:SyncConnected type:None path:null | |
Written 50 | |
..snip snip... | |
Written 850 | |
Written 900 | |
Written 950 | |
Written 1000 | |
Wrote 1000 in 3854(ms) | |
Retrieved 1000 in 15(ms) | |
Children: (1000) | |
msg0000000788 | |
msg0000000789 | |
..snip snip... | |
msg0000000357 | |
msg0000000358 | |
Retrieving data... | |
/all-msg/mbox-1A/msg0000000788 {This is a short msg. Hello #789. Blah blah.} | |
/all-msg/mbox-1A/msg0000000789 {This is a short msg. Hello #790. Blah blah.} | |
/all-msg/mbox-1A/msg0000000784 {This is a short msg. Hello #785. Blah blah.} | |
..snip snip... | |
/all-msg/mbox-1A/msg0000000356 {This is a short msg. Hello #357. Blah blah.} | |
/all-msg/mbox-1A/msg0000000357 {This is a short msg. Hello #358. Blah blah.} | |
/all-msg/mbox-1A/msg0000000358 {This is a short msg. Hello #359. Blah blah.} | |
Retrieved data for 1000 children in 1119(ms) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.zookeeper.*; | |
import org.apache.zookeeper.Watcher.Event.KeeperState; | |
import org.apache.zookeeper.ZooDefs.Ids; | |
import org.apache.zookeeper.data.Stat; | |
import java.util.List; | |
import java.util.concurrent.atomic.AtomicReference; | |
/* | |
* Author: Ashwin Jayaprakash / Date: Jul 4, 2010 / Time: 6:35:45 PM / Contact: http://www.ashwinjayaprakash.com | |
*/ | |
public class SimpleZkTest { | |
public static void main(String[] args) throws Exception { | |
final AtomicReference<KeeperState> stateRef = new AtomicReference<KeeperState>(); | |
ZooKeeper zk = new ZooKeeper("localhost", 2181, new Watcher() { | |
public void process(WatchedEvent watchedEvent) { | |
System.out.println(watchedEvent); | |
stateRef.set(watchedEvent.getState()); | |
} | |
}); | |
//----------------- | |
//Error handling here is not exactly the recommended way. | |
try { | |
KeeperState state = null; | |
while ((state = stateRef.get()) == null) { | |
System.out.println("Waiting to connect"); | |
Thread.sleep(50); | |
} | |
if (state != KeeperState.SyncConnected) { | |
throw new Exception("Oops! " + state); | |
} | |
//----------------- | |
//Create root folder. | |
String root = "/all-msg"; | |
Stat stat = zk.exists(root, false); | |
if (stat == null) { | |
zk.create(root, new byte[]{}, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); | |
} | |
//----------------- | |
//Create another nested folder for this test. | |
String mbox1A = root + "/mbox-1A"; | |
stat = zk.exists(mbox1A, false); | |
if (stat == null) { | |
zk.create(mbox1A, new byte[]{}, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); | |
} | |
//----------------- | |
//Write a few short messages under that folder. | |
String msgMbox1A = mbox1A + "/msg"; | |
long start = System.currentTimeMillis(); | |
int i = 1; | |
for (; i <= 1000; i++) { | |
String msg = "This is a short msg. Hello #" + i + ". Blah blah."; | |
zk.create(msgMbox1A, msg.getBytes(), | |
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); | |
if (i % 50 == 0) { | |
System.out.println("Written " + i); | |
} | |
} | |
long end = System.currentTimeMillis(); | |
System.out.println("Wrote " + (i - 1) + " in " + (end - start) + "(ms)"); | |
//----------------- | |
//Just get the headers or msg names. | |
start = System.currentTimeMillis(); | |
List<String> msgNames = zk.getChildren(mbox1A, false); | |
end = System.currentTimeMillis(); | |
System.out.println("Retrieved " + msgNames.size() + " in " + (end - start) + "(ms)"); | |
System.out.println("Children: (" + msgNames.size() + ")"); | |
for (String child : msgNames) { | |
System.out.println(" " + child); | |
} | |
//----------------- | |
//Now get the msgs. | |
start = System.currentTimeMillis(); | |
System.out.println("Retrieving data..."); | |
for (String msgName : msgNames) { | |
String fqn = mbox1A + "/" + msgName; | |
byte[] bytes = zk.getData(fqn, false, null); | |
String msg = new String(bytes); | |
System.out.println(" " + fqn + " {" + msg + "}"); | |
} | |
end = System.currentTimeMillis(); | |
System.out.println( | |
"Retrieved data for " + msgNames.size() + " children in " | |
+ (end - start) + "(ms)"); | |
} | |
finally { | |
zk.close(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
..snip snip... | |
set GC_PARAMS=-XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+DisableExplicitGC -XX:+PrintTenuringDistribution -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintReferenceGC | |
set HEAP=-Xms1g -Xmx1g -XX:NewSize=256m -XX:MaxNewSize=256m | |
echo on | |
java %GC_PARAMS% %HEAP% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %* | |
..snip snip... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment