Skip to content
Permalink
Browse files
CURATOR-549 (#334)
Support persistent watchers in ZK 3.6+ while maintaining background compatibility with previous versions of ZK. Adds Curator Framework DSL calls to ZooKeeper's addWatch() method. Subsequent PRs will add recipes. Both the older Framework and the async Framework now have methods to add persistent watchers.

Co-authored-by: randgalt <randgalt@apache.org>
  • Loading branch information
Randgalt and randgalt committed Mar 20, 2020
1 parent 7bbb719 commit d65669b64f003326c98843b32b997e3ffab1e442
Showing 23 changed files with 838 additions and 24 deletions.
@@ -31,10 +31,11 @@
*/
public class Compatibility
{
private static final Logger log = LoggerFactory.getLogger(Compatibility.class);

private static final Method getReachableOrOneMethod;
private static final Field addrField;

private static final Logger log = LoggerFactory.getLogger(Compatibility.class);
private static final boolean hasPersistentWatchers;

static
{
@@ -62,6 +63,19 @@
log.error("Could not get addr field! Reconfiguration fail!");
}
addrField = localAddrField;

boolean localHasPersistentWatchers;
try
{
Class.forName("org.apache.zookeeper.AddWatchMode");
localHasPersistentWatchers = true;
}
catch ( ClassNotFoundException e )
{
localHasPersistentWatchers = false;
log.info("Persistent Watchers are not available in the version of the ZooKeeper library being used");
}
hasPersistentWatchers = localHasPersistentWatchers;
}

public static boolean hasGetReachableOrOneMethod()
@@ -101,4 +115,9 @@ else if (addrField != null)
}
return (address != null) ? address.getAddress().getHostAddress() : "unknown";
}

public static boolean hasPersistentWatchers()
{
return hasPersistentWatchers;
}
}
@@ -30,6 +30,7 @@ public void testIsZk36()
{
Assert.assertTrue(Compatibility.hasGetReachableOrOneMethod());
Assert.assertTrue(Compatibility.hasAddrField());
Assert.assertTrue(Compatibility.hasPersistentWatchers());
}

@Override
@@ -196,9 +196,19 @@ public interface CuratorFramework extends Closeable
* Start a remove watches builder.
*
* @return builder object
* @deprecated use {@link #watchers()} in ZooKeeper 3.6+
*/
public RemoveWatchesBuilder watches();

/**
* Start a watch builder. Supported only when ZooKeeper JAR of version 3.6 or
* above is used, throws {@code IllegalStateException} for ZooKeeper JAR 3.5 or below
*
* @return builder object
* @throws IllegalStateException ZooKeeper JAR is 3.5 or below
*/
public WatchesBuilder watchers();

/**
* Returns the listenable interface for the Connect State
*
@@ -0,0 +1,32 @@
/**
* 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.api;

import org.apache.zookeeper.AddWatchMode;

public interface AddWatchBuilder extends AddWatchBuilder2
{
/**
* The mode to use. By default, {@link org.apache.zookeeper.AddWatchMode#PERSISTENT_RECURSIVE} is used
*
* @param mode mode to use
* @return this
*/
AddWatchBuilder2 withMode(AddWatchMode mode);
}
@@ -0,0 +1,27 @@
/**
* 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.api;

public interface AddWatchBuilder2 extends
Backgroundable<WatchableBase<Pathable<Void>>>,
WatchableBase<Pathable<Void>>,
Pathable<Void>
{
}
@@ -18,30 +18,12 @@
*/
package org.apache.curator.framework.api;

import org.apache.zookeeper.Watcher;

public interface Watchable<T>
public interface Watchable<T> extends WatchableBase<T>
{
/**
* Have the operation set a watch
*
* @return this
*/
public T watched();

/**
* Set a watcher for the operation
*
* @param watcher the watcher
* @return this
*/
public T usingWatcher(Watcher watcher);

/**
* Set a watcher for the operation
*
* @param watcher the watcher
* @return this
*/
public T usingWatcher(CuratorWatcher watcher);
T watched();
}
@@ -0,0 +1,40 @@
/**
* 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.api;

import org.apache.zookeeper.Watcher;

public interface WatchableBase<T>
{
/**
* Set a watcher for the operation
*
* @param watcher the watcher
* @return this
*/
T usingWatcher(Watcher watcher);

/**
* Set a watcher for the operation
*
* @param watcher the watcher
* @return this
*/
T usingWatcher(CuratorWatcher watcher);
}
@@ -0,0 +1,33 @@
/**
* 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.api;

/**
* Builder to allow watches to be removed
*/
public interface WatchesBuilder extends RemoveWatchesBuilder
{
/**
* Start an add watch operation
*
* @return builder
*/
AddWatchBuilder add();
}

0 comments on commit d65669b

Please sign in to comment.