Re: Welcome Henry as ZooKeeper committer

2009-12-04 Thread Steve Cheng
Congratulations Henry!

Steven

On Fri, Dec 4, 2009 at 3:09 PM, Patrick Hunt ph...@apache.org wrote:

 The ZooKeeper team and the Hadoop PMC would like to welcome Henry
 Robinson as the newest committer on ZooKeeper! Henry, please add
 yourself to the ZooKeeper website's list of committers.

 Thanks for your hard work on behalf of ZooKeeper, Hadoop and the ASF,

 Patrick




Re: C formatting conventions

2009-10-24 Thread Steve Cheng
Hmm, well I did some alchemy and throwing these lines into my .emacs seems
to do the right thing.

(setq c-basic-offset 4)
 (setq c-default-style '((java-mode . java)
  (awk-mode . awk)
  (other . linux)))



On Fri, Oct 23, 2009 at 6:23 PM, Patrick Hunt ph...@apache.org wrote:

 not quite that simple I'm afraid ;-) some good friday reading if you
 haven't seen before

 http://kerneltrap.org/node/11

 perhaps we should use lindent on our code? whoops ... ;-)

 Patrick


 Benjamin Reed wrote:

 i think we follow linux coding conventions. i think i wrote most of the
 code using the linux emacs style.

 ben

 Steve Cheng wrote:

 Hi,
 What are the C formatting conventions for ZooKeeper? Couldn't find
 anything
 on the HowToContribute page.

 Thank you,

 Steven






C formatting conventions

2009-10-23 Thread Steve Cheng
Hi,
What are the C formatting conventions for ZooKeeper? Couldn't find anything
on the HowToContribute page.

Thank you,

Steven


Zookeeper Test Project

2009-10-05 Thread Steve Cheng
Hi,

The test project on the ProjectSuggestions page, to create tests for the
examples, sounded like a nice way to get started with ZooKeeper, I've
created one queue test and one lock test so far.

I'd like to make a good range of tests, a number of basic sanity ones like
the queue test, and some more involved ones like the lock test.  The lock
test does polling right now, another test would also test watching.  I think
it's useful to test both.

Any comments on this would be great, I noticed the bug about the test case
runtime so I'm not sure what that means for more tests, thanks,

Steven
Index: src/java/test/org/apache/zookeeper/test/ExampleTest.java
===
--- src/java/test/org/apache/zookeeper/test/ExampleTest.java	(revision 0)
+++ src/java/test/org/apache/zookeeper/test/ExampleTest.java	(revision 0)
@@ -0,0 +1,149 @@
+package org.apache.zookeeper.test;
+
+import java.util.Calendar;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.ZooDefs.Ids;
+import org.junit.Test;
+
+public class ExampleTest extends ClientBase {
+	protected static final Logger LOG = Logger.getLogger(ExampleTest.class);
+
+	@Override
+	protected void tearDown() throws Exception {
+		super.tearDown();
+		LOG.info(FINISHED  + getName());
+	}
+
+	static class CountdownTimer {
+		long startTime;
+		long waitTime;
+
+		CountdownTimer(long millis) {
+			waitTime = millis;
+			startTime = Calendar.getInstance().getTimeInMillis();
+		}
+
+		boolean waiting() {
+			return (Calendar.getInstance().getTimeInMillis() - startTime)  waitTime;
+		}
+	}
+
+	@Test
+	public void testQueue() throws Exception {
+		ZooKeeper zkProducer = null;
+		ZooKeeper zkConsumer = null;
+		String queue_handle = /queue;
+		try {
+
+			zkProducer = createClient();
+			zkConsumer = createClient();
+
+			zkProducer.create(queue_handle, new byte[0], Ids.OPEN_ACL_UNSAFE,
+	CreateMode.PERSISTENT);
+			zkProducer.create(queue_handle + /element, 0.getBytes(),
+	Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
+			zkProducer.create(queue_handle + /element, 1.getBytes(),
+	Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
+			ListString children = null;
+			CountdownTimer waitForQueueElements = new CountdownTimer(1000);
+			while (waitForQueueElements.waiting()
+	 ((children = zkConsumer.getChildren(queue_handle, true))
+			.size() != 2)) {
+			}
+			assertEquals(children.size(), 2);
+			String child1 = children.get(0);
+			String child2 = children.get(1);
+			int compareResult = child1.compareTo(child2);
+			assertNotSame(compareResult, 0);
+			if (compareResult  0) {
+			} else {
+String temp = child1;
+child1 = child2;
+child2 = temp;
+			}
+			String child1data = new String(zkConsumer.getData(queue_handle
+	+ / + child1, false, null));
+			String child2data = new String(zkConsumer.getData(queue_handle
+	+ / + child2, false, null));
+			assertEquals(child1data, 0);
+			assertEquals(child2data, 1);
+		} finally {
+			if (zkProducer != null) {
+zkProducer.close();
+			}
+			if (zkConsumer != null) {
+zkConsumer.close();
+			}
+		}
+
+	}
+
+	@Test
+	public void testLock() throws Exception {
+		final int num_contenders = 10;
+		final ZooKeeper zookeepers[] = new ZooKeeper[num_contenders];
+		final String contenderLockNodes[] = new String[num_contenders];
+		final String lock_handle = /lock;
+		final String lock_element = /node-;
+		final int lock_prefix_length = lock_handle.length() + lock_element.length();
+		try{
+			for(int i=0; i  zookeepers.length; i++){
+zookeepers[i] = createClient();
+			}
+
+			zookeepers[0].create(lock_handle, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+
+			//Get lock nodes
+			for(int i=0; i zookeepers.length; i++){
+contenderLockNodes[i] = zookeepers[i].create(lock_handle+lock_element, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
+			}
+
+			boolean contenderSucceeded[] = new boolean[num_contenders];
+			//Poll the locks in a round robin
+			CountdownTimer lockAcquire = new CountdownTimer(1000*num_contenders);
+			
+			boolean waiting = true;
+			while(lockAcquire.waiting()  waiting){
+waiting = false;
+for(int contenderId=0; contenderId  num_contenders; contenderId++){
+	if(contenderSucceeded[contenderId]) break;
+	ZooKeeper zooKeeper = zookeepers[contenderId];
+	String lockNode = contenderLockNodes[contenderId];
+	long lockId = Long.parseLong(lockNode.substring(lock_prefix_length));
+
+	ListString children = zooKeeper.getChildren(lock_handle, false);
+	
+	long coveredId = -1;
+	for(String child : children){
+		long childId = Long.parseLong(child.substring(lock_prefix_length));
+		if((coveredId  childId)  (childId  lockId)){
+			coveredId = childId;
+		}
+	}		

Tips for setting up Zookeeper in Eclipse

2009-09-04 Thread Steve Cheng
Hi,

I'm usually an emacs user so it took quite a while to setup zookeeper in
Eclipse, but it seems alright now.

I started with the Hadoop tutorial to pull Zookeeper from the svn repo:

http://www.cloudera.com/blog/2009/04/20/configuring-eclipse-for-hadoop-development-a-screencast/

You need to adjust the build paths though since it assumes src/ is the root
of the package structure.

Right click project, Build path, Configure Build Path

Go to the Source tab, then you need to individually add the dirs under
src/java:

I added them all, but I think you can get away with:

src/java/main
src/java/systest
src/java/test
src/java/generated

Then go to the Libraries tab and Add JARs from the src/java/lib directory
you need jline, junit, log4j.

With this setup I could run ClientTest by right clicking it and Run as
Junit Test like in the Hadoop tutorial.

Steven