You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2016/03/10 23:55:35 UTC
[7/8] curator git commit: Merge branch 'master' into CURATOR-3.0
http://git-wip-us.apache.org/repos/asf/curator/blob/062a7d75/curator-framework/src/main/java/org/apache/curator/framework/imps/RemoveWatchesBuilderImpl.java
----------------------------------------------------------------------
diff --cc curator-framework/src/main/java/org/apache/curator/framework/imps/RemoveWatchesBuilderImpl.java
index 58fae29,0000000..3d4e96d
mode 100644,000000..100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/RemoveWatchesBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/RemoveWatchesBuilderImpl.java
@@@ -1,322 -1,0 +1,335 @@@
+/**
+ * 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.curator.framework.imps;
+
+import org.apache.curator.RetryLoop;
+import org.apache.curator.TimeTrace;
+import org.apache.curator.framework.api.*;
+import org.apache.curator.utils.DebugUtils;
+import org.apache.zookeeper.AsyncCallback;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.Watcher.WatcherType;
+import org.apache.zookeeper.ZooKeeper;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Executor;
+
+
- public class RemoveWatchesBuilderImpl implements RemoveWatchesBuilder, RemoveWatchesType, RemoveWatchesLocal, BackgroundOperation<String>
++public class RemoveWatchesBuilderImpl implements RemoveWatchesBuilder, RemoveWatchesType, RemoveWatchesLocal, BackgroundOperation<String>, ErrorListenerPathable<Void>
+{
+ private CuratorFrameworkImpl client;
+ private Watcher watcher;
+ private CuratorWatcher curatorWatcher;
+ private WatcherType watcherType;
+ private boolean guaranteed;
+ private boolean local;
+ private boolean quietly;
+ private Backgrounding backgrounding;
+
+ public RemoveWatchesBuilderImpl(CuratorFrameworkImpl client)
+ {
+ this.client = client;
+ this.watcher = null;
+ this.curatorWatcher = null;
+ this.watcherType = WatcherType.Any;
+ this.guaranteed = false;
+ this.local = false;
+ this.quietly = false;
+ this.backgrounding = new Backgrounding();
+ }
+
+ void internalRemoval(Watcher watcher, String path) throws Exception
+ {
+ this.watcher = watcher;
+ watcherType = WatcherType.Any;
+ quietly = true;
+ guaranteed = true;
+ if ( Boolean.getBoolean(DebugUtils.PROPERTY_REMOVE_WATCHERS_IN_FOREGROUND) )
+ {
+ this.backgrounding = new Backgrounding();
+ pathInForeground(path);
+ }
+ else
+ {
+ this.backgrounding = new Backgrounding(true);
+ pathInBackground(path);
+ }
+ }
+
+ @Override
+ public RemoveWatchesType remove(Watcher watcher)
+ {
+ this.watcher = watcher;
+ this.curatorWatcher = null;
+ return this;
+ }
+
+ @Override
+ public RemoveWatchesType remove(CuratorWatcher watcher)
+ {
+ this.watcher = null;
+ this.curatorWatcher = watcher;
+ return this;
+ }
+
+ @Override
+ public RemoveWatchesType removeAll()
+ {
+ this.watcher = null;
+ this.curatorWatcher = null;
+ return this;
+ }
+
+ @Override
+ public RemoveWatchesLocal ofType(WatcherType watcherType)
+ {
+ this.watcherType = watcherType;
+
+ return this;
+ }
+
+ @Override
- public Pathable<Void> inBackground(BackgroundCallback callback, Object context)
++ public ErrorListenerPathable<Void> inBackground(BackgroundCallback callback, Object context)
+ {
+ backgrounding = new Backgrounding(callback, context);
+ return this;
+ }
+
+ @Override
- public Pathable<Void> inBackground(BackgroundCallback callback, Object context, Executor executor)
++ public ErrorListenerPathable<Void> inBackground(BackgroundCallback callback, Object context, Executor executor)
+ {
+ backgrounding = new Backgrounding(client, callback, context, executor);
+ return this;
+ }
+
+ @Override
- public Pathable<Void> inBackground(BackgroundCallback callback)
++ public ErrorListenerPathable<Void> inBackground(BackgroundCallback callback)
+ {
+ backgrounding = new Backgrounding(callback);
+ return this;
+ }
+
+ @Override
- public Pathable<Void> inBackground(BackgroundCallback callback, Executor executor)
++ public ErrorListenerPathable<Void> inBackground(BackgroundCallback callback, Executor executor)
+ {
+ backgrounding = new Backgrounding(client, callback, executor);
+ return this;
+ }
+
+ @Override
- public Pathable<Void> inBackground()
++ public ErrorListenerPathable<Void> inBackground()
+ {
+ backgrounding = new Backgrounding(true);
+ return this;
+ }
+
+ @Override
- public Pathable<Void> inBackground(Object context)
++ public ErrorListenerPathable<Void> inBackground(Object context)
+ {
+ backgrounding = new Backgrounding(context);
+ return this;
+ }
-
++
++ @Override
++ public Pathable<Void> withUnhandledErrorListener(UnhandledErrorListener listener)
++ {
++ backgrounding = new Backgrounding(backgrounding, listener);
++ return this;
++ }
++
+ @Override
+ public RemoveWatchesLocal guaranteed()
+ {
+ guaranteed = true;
+ return this;
+ }
+
+ @Override
+ public BackgroundPathableQuietlyable<Void> locally()
+ {
+ local = true;
+ return this;
+ }
+
+ @Override
+ public BackgroundPathable<Void> quietly()
+ {
+ quietly = true;
+ return this;
+ }
+
+ @Override
+ public Void forPath(String path) throws Exception
+ {
+ final String adjustedPath = client.fixForNamespace(path);
+
+ if(backgrounding.inBackground())
+ {
+ pathInBackground(adjustedPath);
+ }
+ else
+ {
+ pathInForeground(adjustedPath);
+ }
+
+ return null;
+ }
+
+ private void pathInBackground(final String path)
+ {
+ OperationAndData.ErrorCallback<String> errorCallback = null;
+
+ //Only need an error callback if we're in guaranteed mode
+ if(guaranteed)
+ {
+ errorCallback = new OperationAndData.ErrorCallback<String>()
+ {
+ @Override
+ public void retriesExhausted(OperationAndData<String> operationAndData)
+ {
+ client.getFailedRemoveWatcherManager().addFailedOperation(new FailedRemoveWatchManager.FailedRemoveWatchDetails(path, watcher));
+ }
+ };
+ }
+
+ client.processBackgroundOperation(new OperationAndData<String>(this, path, backgrounding.getCallback(),
+ errorCallback, backgrounding.getContext(), !local), null);
+ }
+
+ private void pathInForeground(final String path) throws Exception
+ {
+ NamespaceWatcher namespaceWatcher = makeNamespaceWatcher(path);
+ //For the local case we don't want to use the normal retry loop and we don't want to block until a connection is available.
+ //We just execute the removeWatch, and if it fails, ZK will just remove local watches.
+ if ( local )
+ {
+ ZooKeeper zkClient = client.getZooKeeper();
+ if ( namespaceWatcher != null )
+ {
+ zkClient.removeWatches(path, namespaceWatcher, watcherType, local);
+ }
+ else
+ {
+ zkClient.removeAllWatches(path, watcherType, local);
+ }
+ }
+ else
+ {
+ final NamespaceWatcher finalNamespaceWatcher = namespaceWatcher;
+ RetryLoop.callWithRetry(client.getZookeeperClient(),
+ new Callable<Void>()
+ {
+ @Override
+ public Void call() throws Exception
+ {
+ try
+ {
+ ZooKeeper zkClient = client.getZookeeperClient().getZooKeeper();
+
+ if ( finalNamespaceWatcher != null )
+ {
+ zkClient.removeWatches(path, finalNamespaceWatcher, watcherType, false);
+ }
+ else
+ {
+ zkClient.removeAllWatches(path, watcherType, false);
+ }
+ }
+ catch(Exception e)
+ {
+ if( RetryLoop.isRetryException(e) && guaranteed )
+ {
+ //Setup the guaranteed handler
+ client.getFailedRemoveWatcherManager().addFailedOperation(new FailedRemoveWatchManager.FailedRemoveWatchDetails(path, finalNamespaceWatcher));
+ throw e;
+ }
+ else if(e instanceof KeeperException.NoWatcherException && quietly)
+ {
+ // ignore
+ }
+ else
+ {
+ //Rethrow
+ throw e;
+ }
+ }
+
+ return null;
+ }
+ });
+ }
+ }
+
+ private NamespaceWatcher makeNamespaceWatcher(String path)
+ {
+ NamespaceWatcher namespaceWatcher = null;
+ if ( watcher != null )
+ {
+ if ( watcher instanceof NamespaceWatcher )
+ {
+ namespaceWatcher = (NamespaceWatcher)watcher;
+ }
+ else
+ {
+ namespaceWatcher = new NamespaceWatcher(client, watcher, path);
+ }
+ }
+ else if ( curatorWatcher != null )
+ {
+ namespaceWatcher = new NamespaceWatcher(client, curatorWatcher, path);
+ }
+ return namespaceWatcher;
+ }
+
+ @Override
+ public void performBackgroundOperation(final OperationAndData<String> operationAndData)
+ throws Exception
+ {
- final TimeTrace trace = client.getZookeeperClient().startTracer("RemoteWatches-Background");
-
- AsyncCallback.VoidCallback callback = new AsyncCallback.VoidCallback()
++ try
+ {
- @Override
- public void processResult(int rc, String path, Object ctx)
++ final TimeTrace trace = client.getZookeeperClient().startTracer("RemoteWatches-Background");
++
++ AsyncCallback.VoidCallback callback = new AsyncCallback.VoidCallback()
+ {
- trace.commit();
- CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.REMOVE_WATCHES, rc, path, null, ctx, null, null, null, null, null, null);
- client.processBackgroundOperation(operationAndData, event);
- }
- };
++ @Override
++ public void processResult(int rc, String path, Object ctx)
++ {
++ trace.commit();
++ CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.REMOVE_WATCHES, rc, path, null, ctx, null, null, null, null, null, null);
++ client.processBackgroundOperation(operationAndData, event);
++ }
++ };
+
- ZooKeeper zkClient = client.getZooKeeper();
- NamespaceWatcher namespaceWatcher = makeNamespaceWatcher(operationAndData.getData());
- if(namespaceWatcher == null)
- {
- zkClient.removeAllWatches(operationAndData.getData(), watcherType, local, callback, operationAndData.getContext());
++ ZooKeeper zkClient = client.getZooKeeper();
++ NamespaceWatcher namespaceWatcher = makeNamespaceWatcher(operationAndData.getData());
++ if(namespaceWatcher == null)
++ {
++ zkClient.removeAllWatches(operationAndData.getData(), watcherType, local, callback, operationAndData.getContext());
++ }
++ else
++ {
++ zkClient.removeWatches(operationAndData.getData(), namespaceWatcher, watcherType, local, callback, operationAndData.getContext());
++ }
+ }
- else
++ catch ( Throwable e )
+ {
- zkClient.removeWatches(operationAndData.getData(), namespaceWatcher, watcherType, local, callback, operationAndData.getContext());
++ backgrounding.checkError(e);
+ }
-
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/062a7d75/curator-framework/src/main/java/org/apache/curator/framework/imps/SetACLBuilderImpl.java
----------------------------------------------------------------------
diff --cc curator-framework/src/main/java/org/apache/curator/framework/imps/SetACLBuilderImpl.java
index 17e88f8,5507529..312071c
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetACLBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetACLBuilderImpl.java
@@@ -124,26 -131,33 +131,33 @@@ class SetACLBuilderImpl implements SetA
@Override
public void performBackgroundOperation(final OperationAndData<String> operationAndData) throws Exception
{
- final TimeTrace trace = client.getZookeeperClient().startTracer("SetACLBuilderImpl-Background");
- String path = operationAndData.getData();
- client.getZooKeeper().setACL
- (
- path,
- acling.getAclList(path),
- version,
- new AsyncCallback.StatCallback()
- {
- @SuppressWarnings({"unchecked"})
- @Override
- public void processResult(int rc, String path, Object ctx, Stat stat)
+ try
+ {
+ final TimeTrace trace = client.getZookeeperClient().startTracer("SetACLBuilderImpl-Background");
+ String path = operationAndData.getData();
+ client.getZooKeeper().setACL
+ (
+ path,
+ acling.getAclList(path),
+ version,
+ new AsyncCallback.StatCallback()
{
- trace.commit();
- CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.SET_ACL, rc, path, null, ctx, stat, null, null, null, null, null);
- client.processBackgroundOperation(operationAndData, event);
- }
- },
- backgrounding.getContext()
- );
+ @SuppressWarnings({"unchecked"})
+ @Override
+ public void processResult(int rc, String path, Object ctx, Stat stat)
+ {
+ trace.commit();
- CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.SET_ACL, rc, path, null, ctx, stat, null, null, null, null);
++ CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.SET_ACL, rc, path, null, ctx, stat, null, null, null, null, null);
+ client.processBackgroundOperation(operationAndData, event);
+ }
+ },
+ backgrounding.getContext()
+ );
+ }
+ catch ( Throwable e )
+ {
+ backgrounding.checkError(e);
+ }
}
private Stat pathInForeground(final String path) throws Exception
http://git-wip-us.apache.org/repos/asf/curator/blob/062a7d75/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
----------------------------------------------------------------------
diff --cc curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
index 7057c11,62e39cf..e75377d
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
@@@ -20,14 -20,8 +20,7 @@@ package org.apache.curator.framework.im
import org.apache.curator.RetryLoop;
import org.apache.curator.TimeTrace;
- import org.apache.curator.framework.api.BackgroundCallback;
- import org.apache.curator.framework.api.BackgroundPathAndBytesable;
- import org.apache.curator.framework.api.CuratorEvent;
- import org.apache.curator.framework.api.CuratorEventType;
- import org.apache.curator.framework.api.PathAndBytesable;
- import org.apache.curator.framework.api.SetDataBackgroundVersionable;
- import org.apache.curator.framework.api.SetDataBuilder;
- import org.apache.curator.framework.api.VersionPathAndBytesable;
+ import org.apache.curator.framework.api.*;
-import org.apache.curator.framework.api.transaction.CuratorTransactionBridge;
import org.apache.curator.framework.api.transaction.OperationType;
import org.apache.curator.framework.api.transaction.TransactionSetDataBuilder;
import org.apache.zookeeper.AsyncCallback;
@@@ -202,27 -197,41 +195,41 @@@ class SetDataBuilderImpl implements Set
}
@Override
+ public PathAndBytesable<Stat> withUnhandledErrorListener(UnhandledErrorListener listener)
+ {
+ backgrounding = new Backgrounding(backgrounding, listener);
+ return this;
+ }
+
+ @Override
public void performBackgroundOperation(final OperationAndData<PathAndBytes> operationAndData) throws Exception
{
- final TimeTrace trace = client.getZookeeperClient().startTracer("SetDataBuilderImpl-Background");
- client.getZooKeeper().setData
- (
- operationAndData.getData().getPath(),
- operationAndData.getData().getData(),
- version,
- new AsyncCallback.StatCallback()
- {
- @SuppressWarnings({"unchecked"})
- @Override
- public void processResult(int rc, String path, Object ctx, Stat stat)
+ try
+ {
+ final TimeTrace trace = client.getZookeeperClient().startTracer("SetDataBuilderImpl-Background");
+ client.getZooKeeper().setData
+ (
+ operationAndData.getData().getPath(),
+ operationAndData.getData().getData(),
+ version,
+ new AsyncCallback.StatCallback()
{
- trace.commit();
- CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.SET_DATA, rc, path, null, ctx, stat, null, null, null, null, null);
- client.processBackgroundOperation(operationAndData, event);
- }
- },
- backgrounding.getContext()
- );
+ @SuppressWarnings({"unchecked"})
+ @Override
+ public void processResult(int rc, String path, Object ctx, Stat stat)
+ {
+ trace.commit();
- CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.SET_DATA, rc, path, null, ctx, stat, null, null, null, null);
++ CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.SET_DATA, rc, path, null, ctx, stat, null, null, null, null, null);
+ client.processBackgroundOperation(operationAndData, event);
+ }
+ },
+ backgrounding.getContext()
+ );
+ }
+ catch ( Throwable e )
+ {
+ backgrounding.checkError(e);
+ }
}
@Override
http://git-wip-us.apache.org/repos/asf/curator/blob/062a7d75/curator-framework/src/main/java/org/apache/curator/framework/imps/SyncBuilderImpl.java
----------------------------------------------------------------------
diff --cc curator-framework/src/main/java/org/apache/curator/framework/imps/SyncBuilderImpl.java
index be4d33a,1e5f371..51b4e04
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/SyncBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/SyncBuilderImpl.java
@@@ -81,23 -83,37 +83,37 @@@ public class SyncBuilderImpl implement
}
@Override
- public void performBackgroundOperation(final OperationAndData<String> operationAndData) throws Exception
+ public Pathable<Void> withUnhandledErrorListener(UnhandledErrorListener listener)
{
- final TimeTrace trace = client.getZookeeperClient().startTracer("SyncBuilderImpl-Background");
- final String path = operationAndData.getData();
- String adjustedPath = client.fixForNamespace(path);
+ backgrounding = new Backgrounding(backgrounding, listener);
+ return this;
+ }
- AsyncCallback.VoidCallback voidCallback = new AsyncCallback.VoidCallback()
+ @Override
+ public void performBackgroundOperation(final OperationAndData<String> operationAndData) throws Exception
+ {
+ try
{
- @Override
- public void processResult(int rc, String path, Object ctx)
+ final TimeTrace trace = client.getZookeeperClient().startTracer("SyncBuilderImpl-Background");
+ final String path = operationAndData.getData();
+ String adjustedPath = client.fixForNamespace(path);
+
+ AsyncCallback.VoidCallback voidCallback = new AsyncCallback.VoidCallback()
{
- trace.commit();
- CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.SYNC, rc, path, path, ctx, null, null, null, null, null, null);
- client.processBackgroundOperation(operationAndData, event);
- }
- };
- client.getZooKeeper().sync(adjustedPath, voidCallback, backgrounding.getContext());
+ @Override
+ public void processResult(int rc, String path, Object ctx)
+ {
+ trace.commit();
- CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.SYNC, rc, path, path, ctx, null, null, null, null, null);
++ CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.SYNC, rc, path, path, ctx, null, null, null, null, null, null);
+ client.processBackgroundOperation(operationAndData, event);
+ }
+ };
+ client.getZooKeeper().sync(adjustedPath, voidCallback, backgrounding.getContext());
+ }
+ catch ( Throwable e )
+ {
+ backgrounding.checkError(e);
+ }
}
@Override
http://git-wip-us.apache.org/repos/asf/curator/blob/062a7d75/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkBackground.java
----------------------------------------------------------------------
diff --cc curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkBackground.java
index 83dab6b,8e21929..b9fd5c9
--- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkBackground.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkBackground.java
@@@ -20,9 -20,9 +20,10 @@@
package org.apache.curator.framework.imps;
import com.google.common.collect.Lists;
+import com.google.common.collect.Queues;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
+ import org.apache.curator.framework.api.ACLProvider;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.framework.api.UnhandledErrorListener;
@@@ -34,23 -34,66 +35,71 @@@ import org.apache.curator.test.BaseClas
import org.apache.curator.test.Timing;
import org.apache.curator.utils.CloseableUtils;
import org.apache.zookeeper.KeeperException.Code;
+ import org.apache.zookeeper.data.ACL;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.Test;
-import java.util.Arrays;
import java.util.List;
+import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
public class TestFrameworkBackground extends BaseClassForTests
{
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
@Test
+ public void testErrorListener() throws Exception
+ {
+ ACLProvider badAclProvider = new ACLProvider()
+ {
+ @Override
+ public List<ACL> getDefaultAcl()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<ACL> getAclForPath(String path)
+ {
+ throw new UnsupportedOperationException();
+ }
+ };
+ CuratorFramework client = CuratorFrameworkFactory.builder()
+ .connectString(server.getConnectString())
+ .retryPolicy(new RetryOneTime(1))
+ .aclProvider(badAclProvider)
+ .build();
+ try
+ {
+ client.start();
+
+ final CountDownLatch errorLatch = new CountDownLatch(1);
+ UnhandledErrorListener listener = new UnhandledErrorListener()
+ {
+ @Override
+ public void unhandledError(String message, Throwable e)
+ {
+ if ( e instanceof UnsupportedOperationException )
+ {
+ errorLatch.countDown();
+ }
+ }
+ };
+ client.create().inBackground().withUnhandledErrorListener(listener).forPath("/foo");
+ Assert.assertTrue(new Timing().awaitLatch(errorLatch));
+ }
+ finally
+ {
+ CloseableUtils.closeQuietly(client);
+ }
+ }
+
+ @Test
public void testListenerConnectedAtStart() throws Exception
{
server.stop();