You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2008/07/24 11:31:01 UTC
svn commit: r679325 - in /activemq/sandbox/zookeeper/zookeeper-protocols/src:
main/java/org/apache/zookeeper/protocols/
test/java/org/apache/zookeeper/protocols/
Author: jstrachan
Date: Thu Jul 24 02:30:59 2008
New Revision: 679325
URL: http://svn.apache.org/viewvc?rev=679325&view=rev
Log:
switched from Runnable to using the WhenOwnerListener interface so that the owner/leader can be notified when it becomes the leader and more importantly, when it is no longer the leader due to connection to ZK going away or a session expiring etc. For more background see: https://issues.apache.org/jira/browse/ZOOKEEPER-78?focusedCommentId=12616413#action_12616413
Added:
activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerListener.java (with props)
Modified:
activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WriteLock.java
activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockTest.java
Added: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerListener.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerListener.java?rev=679325&view=auto
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerListener.java (added)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerListener.java Thu Jul 24 02:30:59 2008
@@ -0,0 +1,48 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zookeeper.protocols;
+
+/**
+ * A listener used to respond to the event that the current znode has become the owner
+ * of a lock or the leader of a group - or when due to a connection problem or session timeout
+ * has no longer become the owner.
+ *
+ * Implementors of this interface may be called first via the {@link #whenOwner()} method
+ * and later on they may be called withe {@link #whenNotOwner()} if due to a failure condition
+ * they are no longer the owner. However its worth remembering that neither method may be invoked
+ * if the znode never becomes the leader.
+ *
+ * @version $Revision: 1.1 $
+ */
+public interface WhenOwnerListener {
+
+ /**
+ * Invoked when this znode has become the owner of a lock or the leader of a group
+ */
+ void whenOwner();
+
+ /**
+ * Invoked when the owner of a lock or leader of a group is now no longer the owner
+ * due to some kind of failure scenario such as the connection to ZooKeeper failing
+ * or the session timing out.
+ *
+ * Note that this method is only invoked after a znode has become the owner; i.e. after
+ * a call to {@link #whenOwner()}
+ */
+ void whenNotOwner();
+}
Propchange: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerListener.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WriteLock.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WriteLock.java?rev=679325&r1=679324&r2=679325&view=diff
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WriteLock.java (original)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WriteLock.java Thu Jul 24 02:30:59 2008
@@ -22,7 +22,6 @@
import org.apache.zookeeper.Watcher;
import static org.apache.zookeeper.ZooDefs.CreateFlags.EPHEMERAL;
import static org.apache.zookeeper.ZooDefs.CreateFlags.SEQUENCE;
-import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.proto.WatcherEvent;
@@ -49,17 +48,17 @@
private String ownerId;
private String lastChildId;
private byte[] data = {0x12, 0x34};
- private Runnable whenOwner;
+ private WhenOwnerListener whenOwnerListener;
public WriteLock(ZooKeeperFacade zookeeper, String dir) {
super(zookeeper);
this.dir = dir;
}
- public WriteLock(ZooKeeperFacade zookeeper, String dir, Runnable whenOwner) {
+ public WriteLock(ZooKeeperFacade zookeeper, String dir, WhenOwnerListener whenOwnerListener) {
super(zookeeper);
this.dir = dir;
- this.whenOwner = whenOwner;
+ this.whenOwnerListener = whenOwnerListener;
}
/**
@@ -171,8 +170,8 @@
}
} else {
if (isOwner()) {
- if (whenOwner != null) {
- whenOwner.run();
+ if (whenOwnerListener != null) {
+ whenOwnerListener.whenOwner();
}
return Boolean.TRUE;
}
@@ -209,15 +208,15 @@
return id != null && ownerId != null && id.equals(ownerId);
}
- public Runnable getWhenOwner() {
- return whenOwner;
+ public WhenOwnerListener getWhenOwnerListener() {
+ return whenOwnerListener;
}
/**
- * Allows a runnable to be associated when this node becomes the owner of the lock
+ * Allows a WhenOwnerListener to be associated when this node becomes the owner of the lock
*/
- public void setWhenOwner(Runnable whenOwner) {
- this.whenOwner = whenOwner;
+ public void setWhenOwnerListener(WhenOwnerListener whenOwnerListener) {
+ this.whenOwnerListener = whenOwnerListener;
}
// Implementation methods
Modified: activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockTest.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockTest.java?rev=679325&r1=679324&r2=679325&view=diff
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockTest.java (original)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockTest.java Thu Jul 24 02:30:59 2008
@@ -19,7 +19,6 @@
import org.apache.log4j.Logger;
import org.apache.zookeeper.Watcher;
-import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.proto.WatcherEvent;
import java.util.concurrent.CountDownLatch;
@@ -55,10 +54,14 @@
}
});
WriteLock leader = new WriteLock(keeper, dir);
- leader.setWhenOwner(new Runnable() {
- public void run() {
+ leader.setWhenOwnerListener(new WhenOwnerListener() {
+ public void whenOwner() {
latch.countDown();
}
+
+ public void whenNotOwner() {
+ // TODO
+ }
});
nodes[i] = leader;