Skip to content
Permalink
Browse files
add defaultWaitForShutdownTimeoutMs
  • Loading branch information
eolivelli committed Jun 24, 2018
1 parent fe2c7c4 commit aec134724a500120a11542d43659a676562b03a1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
@@ -52,6 +52,7 @@ public class CuratorZookeeperClient implements Closeable
private final ConnectionState state;
private final AtomicReference<RetryPolicy> retryPolicy = new AtomicReference<RetryPolicy>();
private final int connectionTimeoutMs;
private final int defaultWaitForShutdownTimeoutMs;
private final AtomicBoolean started = new AtomicBoolean(false);
private final AtomicReference<TracerDriver> tracer = new AtomicReference<TracerDriver>(new DefaultTracerDriver());
private final ConnectionHandlingPolicy connectionHandlingPolicy;
@@ -112,7 +113,28 @@ public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvide
* @param connectionHandlingPolicy connection handling policy - use one of the pre-defined policies or write your own
* @since 3.0.0
*/
public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly, ConnectionHandlingPolicy connectionHandlingPolicy)
public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly, ConnectionHandlingPolicy connectionHandlingPolicy) {
this(zookeeperFactory, ensembleProvider, sessionTimeoutMs, connectionTimeoutMs, 0,
watcher, retryPolicy, canBeReadOnly, connectionHandlingPolicy);
}
/**
* @param zookeeperFactory factory for creating {@link ZooKeeper} instances
* @param ensembleProvider the ensemble provider
* @param sessionTimeoutMs session timeout
* @param connectionTimeoutMs connection timeout
* @param defaultWaitForShutdownTimeoutMs default timeout fo close operation
* @param watcher default watcher or null
* @param retryPolicy the retry policy to use
* @param canBeReadOnly if true, allow ZooKeeper client to enter
* read only mode in case of a network partition. See
* {@link ZooKeeper#ZooKeeper(String, int, Watcher, long, byte[], boolean)}
* for details
* @param connectionHandlingPolicy connection handling policy - use one of the pre-defined policies or write your own
* @since 4.0.2
*/
public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider,
int sessionTimeoutMs, int connectionTimeoutMs, int defaultWaitForShutdownTimeoutMs, Watcher watcher,
RetryPolicy retryPolicy, boolean canBeReadOnly, ConnectionHandlingPolicy connectionHandlingPolicy)
{
this.connectionHandlingPolicy = connectionHandlingPolicy;
if ( sessionTimeoutMs < connectionTimeoutMs )
@@ -124,6 +146,7 @@ public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvide
ensembleProvider = Preconditions.checkNotNull(ensembleProvider, "ensembleProvider cannot be null");

this.connectionTimeoutMs = connectionTimeoutMs;
this.defaultWaitForShutdownTimeoutMs = defaultWaitForShutdownTimeoutMs;
state = new ConnectionState(zookeeperFactory, ensembleProvider, sessionTimeoutMs, connectionTimeoutMs, watcher, tracer, canBeReadOnly, connectionHandlingPolicy);
setRetryPolicy(retryPolicy);
}
@@ -215,10 +238,15 @@ public void start() throws Exception
}

/**
* Close the client
* Close the client.
*
* Same as {@link #close(int) } using the default timeout set at construction time.
*
* @see #close(int)
*/
@Override
public void close() {
close(0);
close(defaultWaitForShutdownTimeoutMs);
}

/**
@@ -49,6 +49,7 @@
import java.util.List;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.apache.curator.CuratorZookeeperClient;

import static org.apache.curator.utils.Compatibility.isZK34;

@@ -147,7 +148,7 @@ public static byte[] getLocalAddress()
private ConnectionHandlingPolicy connectionHandlingPolicy = new StandardConnectionHandlingPolicy();
private SchemaSet schemaSet = SchemaSet.getDefaultSchemaSet();
private boolean zk34CompatibilityMode = isZK34();

private int defaultWaitForShutdownTimeoutMs = 0;
/**
* Apply the current values and build a new CuratorFramework
*
@@ -401,6 +402,20 @@ public Builder zk34CompatibilityMode(boolean mode)
return this;
}

/**
* Set a default timeout for {@link CuratorZookeeperClient#close() }.
* The default is 0, which means that this feature is disabled.
*
* @since 4.0.2
* @param defaultWaitForShutdownTimeoutMs default timeout
* @return this
*/
public Builder defaultWaitForShutdownTimeoutMs(int defaultWaitForShutdownTimeoutMs)
{
this.defaultWaitForShutdownTimeoutMs = defaultWaitForShutdownTimeoutMs;
return this;
}

/**
* <p>
* Change the connection handling policy. The default policy is {@link StandardConnectionHandlingPolicy}.
@@ -494,6 +509,11 @@ public int getConnectionTimeoutMs()
return connectionTimeoutMs;
}

public int getDefaultWaitForShutdownTimeoutMs()
{
return defaultWaitForShutdownTimeoutMs;
}

public int getMaxCloseWaitMs()
{
return maxCloseWaitMs;
@@ -122,6 +122,7 @@ public CuratorFrameworkImpl(CuratorFrameworkFactory.Builder builder)
builder.getEnsembleProvider(),
builder.getSessionTimeoutMs(),
builder.getConnectionTimeoutMs(),
builder.getDefaultWaitForShutdownTimeoutMs(),
new Watcher()
{
@Override

0 comments on commit aec1347

Please sign in to comment.