Github user madrob commented on a diff in the pull request: https://github.com/apache/curator/pull/171#discussion_r113031493 --- Diff: curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentTtlNode.java --- @@ -0,0 +1,208 @@ +/** + * 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.recipes.nodes; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.utils.ThreadUtils; +import org.apache.curator.utils.ZKPaths; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.Closeable; +import java.io.IOException; +import java.util.Objects; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +/** + * <p> + * Manages a {@link PersistentNode} that uses {@link CreateMode#CONTAINER}. Asynchronously + * it creates or updates a child on the persistent node that is marked with a provided TTL. + * </p> + * + * <p> + * The effect of this is to have a node that can be watched, etc. The child node serves as + * a method of having the parent node deleted if the TTL expires. i.e. if the process + * that is running the PersistentTtlNode crashes and the TTL elapses, first the child node + * will be deleted due to the TTL expiration and then the parent node will be deleted as it's + * a container node with no children. + * </p> + * + * <p> + * PersistentTtlNode is useful when you need to create a TTL node but don't want to keep + * it alive manually by periodically setting data - PersistentTtlNode does that for you. Further + * the keep-alive is done in a way that does not generate watch triggers on the parent node. + * </p> + */ +public class PersistentTtlNode implements Closeable +{ + public static final String DEFAULT_CHILD_NODE_NAME = "touch"; + public static final int DEFAULT_TOUCH_SCHEDULE_FACTOR = 2; + + private final Logger log = LoggerFactory.getLogger(getClass()); + private final PersistentNode node; + private final CuratorFramework client; + private final long ttlMs; + private final int touchScheduleFactor; + private final ScheduledExecutorService executorService; + private final AtomicReference<Future<?>> futureRef = new AtomicReference<>(); + private final String childPath; + + /** + * @param client the client + * @param path path for the parent ZNode + * @param ttlMs max ttl for the node in milliseconds + * @param initData data for the node + */ + public PersistentTtlNode(CuratorFramework client, String path, long ttlMs, byte[] initData) + { + this(client, Executors.newSingleThreadScheduledExecutor(ThreadUtils.newThreadFactory("PersistentTtlNode")), path, ttlMs, initData, DEFAULT_CHILD_NODE_NAME, DEFAULT_TOUCH_SCHEDULE_FACTOR); + } + + /** + * @param client the client + * @param path path for the parent ZNode --- End diff -- missing javadoc for executor service. probably worthwhile to caution people that if the executor service is overloaded then it may impact the ability to do ttl updates. we should also give users a way to realize that this is the case when their nodes start expiring. probably not a very common use case though.
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. ---