Skip to content
Permalink
Browse files
CURATOR-408 Handle graceful close of ZookKeeper client waiting for al…
…l resources to be released
  • Loading branch information
eolivelli committed Jun 11, 2018
1 parent 9383aa3 commit fe2c7c4cd606c0cf4bc4fab15deedc0f4c33ea0e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
@@ -112,14 +112,17 @@ void start() throws Exception
}

@Override
public void close() throws IOException
{
public void close() throws IOException {
close(0);
}

public void close(int waitForShutdownTimeoutMs) throws IOException {
log.debug("Closing");

CloseableUtils.closeQuietly(ensembleProvider);
try
{
zooKeeper.closeAndClear();
zooKeeper.closeAndClear(waitForShutdownTimeoutMs);
}
catch ( Exception e )
{
@@ -213,18 +213,29 @@ public void start() throws Exception

state.start();
}

/**
* Close the client
*/
public void close()
public void close() {
close(0);
}

/**
* Close this client object as the {@link #close() } method.
* This method will wait for internal resources to be released.
*
* @param waitForShutdownTimeoutMs timeout (in milliseconds) to wait for resources to be released.
* Use zero or a negative value to skip the wait.
*/
public void close(int waitForShutdownTimeoutMs)
{
log.debug("Closing");
log.debug("Closing, waitForShutdownTimeoutMs {}", waitForShutdownTimeoutMs);

started.set(false);
try
{
state.close();
state.close(waitForShutdownTimeoutMs);
}
catch ( IOException e )
{
@@ -73,15 +73,15 @@ String getNewConnectionString()
return ((helperConnectionString != null) && !ensembleProvider.getConnectionString().equals(helperConnectionString)) ? helperConnectionString : null;
}

void closeAndClear() throws Exception
void closeAndClear(int timeout) throws Exception
{
internalClose();
internalClose(timeout);
helper = null;
}

void closeAndReset() throws Exception
{
internalClose();
internalClose(0);

// first helper is synchronized when getZooKeeper is called. Subsequent calls
// are not synchronized.
@@ -140,7 +140,7 @@ public int getNegotiatedSessionTimeoutMs()
};
}

private void internalClose() throws Exception
private void internalClose(int waitForShutdownTimeoutMs) throws Exception
{
try
{
@@ -155,7 +155,7 @@ public void process(WatchedEvent event)
}
};
zooKeeper.register(dummyWatcher); // clear the default watcher so that no new events get processed by mistake
zooKeeper.close();
zooKeeper.close(waitForShutdownTimeoutMs);
}
}
catch ( InterruptedException dummy )
@@ -1,3 +1,21 @@
/**
* 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.state;

import com.google.common.collect.Queues;
@@ -59,7 +59,7 @@
<jdk-version>1.7</jdk-version>

<!-- versions -->
<zookeeper-version>3.5.3-beta</zookeeper-version>
<zookeeper-version>3.5.4-beta</zookeeper-version>
<maven-project-info-reports-plugin-version>2.9</maven-project-info-reports-plugin-version>
<maven-bundle-plugin-version>3.2.0</maven-bundle-plugin-version>
<maven-javadoc-plugin-version>2.10.4</maven-javadoc-plugin-version>

0 comments on commit fe2c7c4

Please sign in to comment.