Return-Path: X-Original-To: apmail-curator-commits-archive@minotaur.apache.org Delivered-To: apmail-curator-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1AC1611585 for ; Fri, 23 May 2014 15:50:03 +0000 (UTC) Received: (qmail 6134 invoked by uid 500); 23 May 2014 15:50:03 -0000 Delivered-To: apmail-curator-commits-archive@curator.apache.org Received: (qmail 6104 invoked by uid 500); 23 May 2014 15:50:03 -0000 Mailing-List: contact commits-help@curator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@curator.apache.org Delivered-To: mailing list commits@curator.apache.org Received: (qmail 6095 invoked by uid 99); 23 May 2014 15:50:03 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 23 May 2014 15:50:03 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id BFD109A05A2; Fri, 23 May 2014 15:50:02 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: randgalt@apache.org To: commits@curator.apache.org Message-Id: <384d3817b13a4e13962254268abdc33a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: git commit: DistributedAtomicValues are initially in a "null" state (like a database). In this state, there's no good way currently to initialize it so that operations such as compareAndSet() work well. So, add a new method, initialize(), that sets a val Date: Fri, 23 May 2014 15:50:02 +0000 (UTC) Repository: curator Updated Branches: refs/heads/master 07db9f6c1 -> ecac48caa DistributedAtomicValues are initially in a "null" state (like a database). In this state, there's no good way currently to initialize it so that operations such as compareAndSet() work well. So, add a new method, initialize(), that sets a value if the node doesn't exist. This closes #8 Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/ecac48ca Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/ecac48ca Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/ecac48ca Branch: refs/heads/master Commit: ecac48caa9f68ce3f74cecf168dd05d7bbfa843e Parents: 07db9f6 Author: randgalt Authored: Fri May 23 10:46:17 2014 -0500 Committer: randgalt Committed: Fri May 23 10:46:17 2014 -0500 ---------------------------------------------------------------------- .../atomic/DistributedAtomicInteger.java | 6 ++++ .../recipes/atomic/DistributedAtomicLong.java | 6 ++++ .../recipes/atomic/DistributedAtomicNumber.java | 30 +++++++++++++------- .../recipes/atomic/DistributedAtomicValue.java | 23 +++++++++++++++ .../atomic/TestDistributedAtomicLong.java | 24 ++++++++++++++++ 5 files changed, 79 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/ecac48ca/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicInteger.java ---------------------------------------------------------------------- diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicInteger.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicInteger.java index 1b526bb..8c167c9 100644 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicInteger.java +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicInteger.java @@ -90,6 +90,12 @@ public class DistributedAtomicInteger implements DistributedAtomicNumber return new AtomicLong(value.trySet(valueToBytes(newValue))); } + @Override + public boolean initialize(Long initialize) throws Exception + { + return value.initialize(valueToBytes(initialize)); + } + /** * Add 1 to the current value and return the new value information. Remember to always * check {@link AtomicValue#succeeded()}. http://git-wip-us.apache.org/repos/asf/curator/blob/ecac48ca/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicNumber.java ---------------------------------------------------------------------- diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicNumber.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicNumber.java index d479814..0039a13 100644 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicNumber.java +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicNumber.java @@ -16,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.apache.curator.framework.recipes.atomic; public interface DistributedAtomicNumber @@ -27,20 +28,19 @@ public interface DistributedAtomicNumber * @return value info * @throws Exception ZooKeeper errors */ - public AtomicValue get() throws Exception; + public AtomicValue get() throws Exception; /** * Atomically sets the value to the given updated value * if the current value {@code ==} the expected value. * Remember to always check {@link AtomicValue#succeeded()}. * - * * @param expectedValue the expected value - * @param newValue the new value for the counter + * @param newValue the new value for the counter * @return value info * @throws Exception ZooKeeper errors */ - public AtomicValue compareAndSet(T expectedValue, T newValue) throws Exception; + public AtomicValue compareAndSet(T expectedValue, T newValue) throws Exception; /** * Attempt to atomically set the value to the given value. Remember to always @@ -50,7 +50,17 @@ public interface DistributedAtomicNumber * @return value info * @throws Exception ZooKeeper errors */ - public AtomicValue trySet(T newValue) throws Exception; + public AtomicValue trySet(T newValue) throws Exception; + + /** + * Atomic values are initially set to the equivalent of NULL in a database. + * Use this method to initialize the value. The value will be set if and only iff the node does not exist. + * + * @param value the initial value to set + * @return true if the value was set, false if the node already existed + * @throws Exception ZooKeeper errors + */ + public boolean initialize(T value) throws Exception; /** * Forcibly sets the value of the counter without any guarantees of atomicity. @@ -58,7 +68,7 @@ public interface DistributedAtomicNumber * @param newValue the new value * @throws Exception ZooKeeper errors */ - public void forceSet(T newValue) throws Exception; + public void forceSet(T newValue) throws Exception; /** * Add 1 to the current value and return the new value information. Remember to always @@ -67,7 +77,7 @@ public interface DistributedAtomicNumber * @return value info * @throws Exception ZooKeeper errors */ - public AtomicValue increment() throws Exception; + public AtomicValue increment() throws Exception; /** * Subtract 1 from the current value and return the new value information. Remember to always @@ -76,7 +86,7 @@ public interface DistributedAtomicNumber * @return value info * @throws Exception ZooKeeper errors */ - public AtomicValue decrement() throws Exception; + public AtomicValue decrement() throws Exception; /** * Add delta to the current value and return the new value information. Remember to always @@ -86,7 +96,7 @@ public interface DistributedAtomicNumber * @return value info * @throws Exception ZooKeeper errors */ - public AtomicValue add(T delta) throws Exception; + public AtomicValue add(T delta) throws Exception; /** * Subtract delta from the current value and return the new value information. Remember to always @@ -96,5 +106,5 @@ public interface DistributedAtomicNumber * @return value info * @throws Exception ZooKeeper errors */ - public AtomicValue subtract(T delta) throws Exception; + public AtomicValue subtract(T delta) throws Exception; } http://git-wip-us.apache.org/repos/asf/curator/blob/ecac48ca/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicValue.java ---------------------------------------------------------------------- diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicValue.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicValue.java index 910ece7..4ba3097 100644 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicValue.java +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/atomic/DistributedAtomicValue.java @@ -187,6 +187,29 @@ public class DistributedAtomicValue return result; } + /** + * Atomic values are initially set to the equivalent of NULL in a database. + * Use this method to initialize the value. The value will be set if and only iff the node does not exist. + * + * @param value the initial value to set + * @return true if the value was set, false if the node already existed + * @throws Exception ZooKeeper errors + */ + public boolean initialize(byte[] value) throws Exception + { + ensurePath.ensure(client.getZookeeperClient()); + try + { + client.create().forPath(path, value); + } + catch ( KeeperException.NodeExistsException ignore ) + { + // ignore + return false; + } + return true; + } + AtomicValue trySet(MakeValue makeValue) throws Exception { MutableAtomicValue result = new MutableAtomicValue(null, null, false); http://git-wip-us.apache.org/repos/asf/curator/blob/ecac48ca/curator-recipes/src/test/java/org/apache/curator/framework/recipes/atomic/TestDistributedAtomicLong.java ---------------------------------------------------------------------- diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/atomic/TestDistributedAtomicLong.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/atomic/TestDistributedAtomicLong.java index c2e0888..9d9fe03 100644 --- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/atomic/TestDistributedAtomicLong.java +++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/atomic/TestDistributedAtomicLong.java @@ -26,6 +26,7 @@ import org.apache.curator.retry.RetryOneTime; import org.apache.commons.math.stat.descriptive.SummaryStatistics; import org.apache.commons.math.stat.descriptive.SynchronizedSummaryStatistics; import org.apache.curator.test.BaseClassForTests; +import org.apache.curator.utils.CloseableUtils; import org.testng.Assert; import org.testng.annotations.Test; import org.testng.collections.Lists; @@ -75,6 +76,29 @@ public class TestDistributedAtomicLong extends BaseClassForTests } @Test + public void testCompareAndSetWithFreshInstance() throws Exception + { + CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); + try + { + client.start(); + DistributedAtomicLong dal = new DistributedAtomicLong(client, "/counter", new RetryOneTime(1)); + AtomicValue result = dal.compareAndSet(0L, 1L); + Assert.assertFalse(result.succeeded()); + + Assert.assertTrue(dal.initialize(0L)); + result = dal.compareAndSet(0L, 1L); + Assert.assertTrue(result.succeeded()); + + Assert.assertFalse(dal.initialize(0L)); + } + finally + { + CloseableUtils.closeQuietly(client); + } + } + + @Test public void testCompareAndSet() throws Exception { final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));