You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by dr...@apache.org on 2016/01/30 04:47:55 UTC
[1/2] curator git commit: CURATOR-294: Make ChildData immutable;
PathChildrenCache replaces instead of mutates. [Forced Update!]
Repository: curator
Updated Branches:
refs/heads/CURATOR-294 81067f5b3 -> b3e3fcc48 (forced update)
CURATOR-294: Make ChildData immutable; PathChildrenCache replaces instead of mutates.
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/5e995ed7
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/5e995ed7
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/5e995ed7
Branch: refs/heads/CURATOR-294
Commit: 5e995ed7a315fba9f1327706735a7f8c1b417a83
Parents: 649e0ba
Author: Scott Blum <dr...@apache.org>
Authored: Thu Jan 28 12:38:57 2016 -0500
Committer: Scott Blum <dr...@apache.org>
Committed: Fri Jan 29 22:46:28 2016 -0500
----------------------------------------------------------------------
.../framework/recipes/cache/ChildData.java | 21 ++++++++------------
.../recipes/cache/PathChildrenCache.java | 5 ++++-
2 files changed, 12 insertions(+), 14 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/5e995ed7/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/ChildData.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/ChildData.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/ChildData.java
index 21e0bc4..0b31bc8 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/ChildData.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/ChildData.java
@@ -25,15 +25,15 @@ import org.apache.curator.utils.PathUtils;
public class ChildData implements Comparable<ChildData>
{
- private final String path;
- private final Stat stat;
- private final AtomicReference<byte[]> data;
+ private final String path;
+ private final Stat stat;
+ private final byte[] data;
public ChildData(String path, Stat stat, byte[] data)
{
this.path = PathUtils.validatePath(path);
this.stat = stat;
- this.data = new AtomicReference<byte[]>(data);
+ this.data = data;
}
/**
@@ -71,7 +71,7 @@ public class ChildData implements Comparable<ChildData>
ChildData childData = (ChildData)o;
- if ( !Arrays.equals(data.get(), childData.data.get()) )
+ if ( !Arrays.equals(data, childData.data) )
{
return false;
}
@@ -92,7 +92,7 @@ public class ChildData implements Comparable<ChildData>
{
int result = path != null ? path.hashCode() : 0;
result = 31 * result + (stat != null ? stat.hashCode() : 0);
- result = 31 * result + (data != null ? Arrays.hashCode(data.get()) : 0);
+ result = 31 * result + Arrays.hashCode(data);
return result;
}
@@ -126,12 +126,7 @@ public class ChildData implements Comparable<ChildData>
*/
public byte[] getData()
{
- return data.get();
- }
-
- void clearData()
- {
- data.set(null);
+ return data;
}
@Override
@@ -140,7 +135,7 @@ public class ChildData implements Comparable<ChildData>
return "ChildData{" +
"path='" + path + '\'' +
", stat=" + stat +
- ", data=" + Arrays.toString(data.get()) +
+ ", data=" + Arrays.toString(data) +
'}';
}
}
http://git-wip-us.apache.org/repos/asf/curator/blob/5e995ed7/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java
index 12769e1..ae30da9 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java
@@ -448,7 +448,10 @@ public class PathChildrenCache implements Closeable
{
if ( (ifVersion < 0) || (ifVersion == data.getStat().getVersion()) )
{
- data.clearData();
+ if ( data.getData() != null )
+ {
+ currentData.replace(fullPath, data, new ChildData(data.getPath(), data.getStat(), null));
+ }
return true;
}
}
[2/2] curator git commit: CURATOR-294: Optimize TreeCache,
fix possible concurrency issue
Posted by dr...@apache.org.
CURATOR-294: Optimize TreeCache, fix possible concurrency issue
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/b3e3fcc4
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/b3e3fcc4
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/b3e3fcc4
Branch: refs/heads/CURATOR-294
Commit: b3e3fcc4848beaa148fc3a5b437e4b19b6e7f325
Parents: 5e995ed
Author: Scott Blum <dr...@apache.org>
Authored: Thu Jan 28 12:58:14 2016 -0500
Committer: Scott Blum <dr...@apache.org>
Committed: Fri Jan 29 22:46:42 2016 -0500
----------------------------------------------------------------------
.../framework/recipes/cache/TreeCache.java | 37 +++++++++++---------
1 file changed, 20 insertions(+), 17 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/b3e3fcc4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
index 4d00266..3f7d8d4 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
@@ -220,8 +220,7 @@ public class TreeCache implements Closeable
final AtomicReference<NodeState> nodeState = new AtomicReference<NodeState>(NodeState.PENDING);
final TreeNode parent;
final String path;
- final AtomicReference<Stat> stat = new AtomicReference<Stat>();
- final AtomicReference<byte[]> data = new AtomicReference<byte[]>();
+ final AtomicReference<ChildData> childData = new AtomicReference<ChildData>();
final AtomicReference<ConcurrentMap<String, TreeNode>> children = new AtomicReference<ConcurrentMap<String, TreeNode>>();
final int depth;
@@ -296,8 +295,7 @@ public class TreeCache implements Closeable
void wasDeleted() throws Exception
{
- Stat oldStat = stat.getAndSet(null);
- byte[] oldData = data.getAndSet(null);
+ ChildData oldChildData = childData.getAndSet(null);
client.clearWatcherReferences(this);
ConcurrentMap<String, TreeNode> childMap = children.getAndSet(null);
if ( childMap != null )
@@ -318,7 +316,7 @@ public class TreeCache implements Closeable
NodeState oldState = nodeState.getAndSet(NodeState.DEAD);
if ( oldState == NodeState.LIVE )
{
- publishEvent(TreeCacheEvent.Type.NODE_REMOVED, new ChildData(path, oldStat, oldData));
+ publishEvent(TreeCacheEvent.Type.NODE_REMOVED, oldChildData);
}
if ( parent == null )
@@ -383,12 +381,12 @@ public class TreeCache implements Closeable
case CHILDREN:
if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
{
- Stat oldStat = stat.get();
- if ( oldStat != null && oldStat.getMzxid() == newStat.getMzxid() )
+ ChildData oldChildData = childData.get();
+ if ( oldChildData != null && oldChildData.getStat().getMzxid() == newStat.getMzxid() )
{
- // Only update stat if mzxid is different, otherwise we might obscure
+ // Only update stat if mzxid is same, otherwise we might obscure
// GET_DATA event updates.
- stat.set(newStat);
+ childData.compareAndSet(oldChildData, new ChildData(oldChildData.getPath(), newStat, oldChildData.getData()));
}
if ( event.getChildren().isEmpty() )
@@ -435,22 +433,27 @@ public class TreeCache implements Closeable
case GET_DATA:
if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
{
+ ChildData toPublish = new ChildData(event.getPath(), newStat, event.getData());
+ ChildData oldChildData;
if ( cacheData )
{
- data.set(event.getData());
+ oldChildData = childData.getAndSet(toPublish);
+ }
+ else
+ {
+ oldChildData = childData.getAndSet(new ChildData(event.getPath(), newStat, null));
}
- Stat oldStat = stat.getAndSet(newStat);
NodeState oldState = nodeState.getAndSet(NodeState.LIVE);
if ( oldState != NodeState.LIVE )
{
- publishEvent(TreeCacheEvent.Type.NODE_ADDED, new ChildData(event.getPath(), newStat, event.getData()));
+ publishEvent(TreeCacheEvent.Type.NODE_ADDED, toPublish);
}
else
{
- if ( oldStat == null || oldStat.getMzxid() != newStat.getMzxid() )
+ if ( oldChildData == null || oldChildData.getStat().getMzxid() != newStat.getMzxid() )
{
- publishEvent(TreeCacheEvent.Type.NODE_UPDATED, new ChildData(event.getPath(), newStat, event.getData()));
+ publishEvent(TreeCacheEvent.Type.NODE_UPDATED, toPublish);
}
}
}
@@ -681,9 +684,9 @@ public class TreeCache implements Closeable
for ( Map.Entry<String, TreeNode> entry : map.entrySet() )
{
TreeNode childNode = entry.getValue();
- ChildData childData = new ChildData(childNode.path, childNode.stat.get(), childNode.data.get());
+ ChildData childData = childNode.childData.get();
// Double-check liveness after retreiving data.
- if ( childNode.nodeState.get() == NodeState.LIVE )
+ if ( childData != null && childNode.nodeState.get() == NodeState.LIVE )
{
builder.put(entry.getKey(), childData);
}
@@ -710,7 +713,7 @@ public class TreeCache implements Closeable
{
return null;
}
- ChildData result = new ChildData(node.path, node.stat.get(), node.data.get());
+ ChildData result = node.childData.get();
// Double-check liveness after retreiving data.
return node.nodeState.get() == NodeState.LIVE ? result : null;
}