Return-Path: X-Original-To: apmail-curator-dev-archive@minotaur.apache.org Delivered-To: apmail-curator-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8EEA517588 for ; Wed, 15 Oct 2014 04:18:19 +0000 (UTC) Received: (qmail 64531 invoked by uid 500); 15 Oct 2014 04:18:19 -0000 Delivered-To: apmail-curator-dev-archive@curator.apache.org Received: (qmail 64475 invoked by uid 500); 15 Oct 2014 04:18:18 -0000 Mailing-List: contact dev-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 dev@curator.apache.org Received: (qmail 64464 invoked by uid 99); 15 Oct 2014 04:18:18 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 15 Oct 2014 04:18:18 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 32FE18A14F0; Wed, 15 Oct 2014 04:18:18 +0000 (UTC) From: dragonsinth To: dev@curator.apache.org Reply-To: dev@curator.apache.org References: In-Reply-To: Subject: [GitHub] curator pull request: CURATOR-151: SharedValue/SharedCount API upd... Content-Type: text/plain Message-Id: <20141015041818.32FE18A14F0@tyr.zones.apache.org> Date: Wed, 15 Oct 2014 04:18:18 +0000 (UTC) Github user dragonsinth commented on a diff in the pull request: https://github.com/apache/curator/pull/47#discussion_r18874097 --- Diff: curator-recipes/src/main/java/org/apache/curator/framework/recipes/shared/SharedValue.java --- @@ -129,29 +127,19 @@ public void setValue(byte[] newValue) throws Exception * value is updated. i.e. if the value is not successful you can get the updated value * by calling {@link #getValue()}. * + * @deprecated use {@link #trySetValue(VersionedValue, byte[])} for stronger atomicity + * guarantees. Even if this object's internal state is up-to-date, the caller has no way to + * ensure that they've read the most recently seen value. + * * @param newValue the new value to attempt * @return true if the change attempt was successful, false if not. If the change * was not successful, {@link #getValue()} will return the updated value * @throws Exception ZK errors, interruptions, etc. */ + @Deprecated public boolean trySetValue(byte[] newValue) throws Exception { - Preconditions.checkState(state.get() == State.STARTED, "not started"); - - try - { - VersionedValue localCopy = currentValue.get(); - client.setData().withVersion(localCopy.getVersion()).forPath(path, newValue); - currentValue.set(new VersionedValue(localCopy.getVersion() + 1, Arrays.copyOf(newValue, newValue.length))); - return true; - } - catch ( KeeperException.BadVersionException ignore ) - { - // ignore - } - - readValue(); - return false; + return trySetValue(currentValue.get(), newValue); } --- End diff -- For more reference, check out [5 Things You Didn’t Know About Synchronization in Java and Scala](http://www.takipiblog.com/5-things-you-didnt-know-about-synchronization-in-java-and-scala/). "If the CAS fails the JVM will perform one round of spin locking where the thread parks to effectively put it to sleep between retrying the CAS. If these initial attempts fail (signaling a fairly higher level of contention for the lock) the thread will move itself to a blocked state and enqueue itself in the list of threads vying for the lock and begin a series of spin locks." I'm essentially doing in code what the synchronization did internally. That's why I didn't have to rewrite any test code other than for the API change on the new method that was recently added. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---