Skip to content

Commit

Permalink
Clean-up file watcher keys. (elastic#89429)
Browse files Browse the repository at this point in the history
Clean-up all open watcher keys in FileSettingsService.
  • Loading branch information
grcevski committed Aug 17, 2022
1 parent 28a3b72 commit 425a320
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class FileSettingsService extends AbstractLifecycleComponent implements C

private volatile FileUpdateState fileUpdateState = null;
private volatile WatchKey settingsDirWatchKey = null;
private volatile WatchKey configDirWatchKey = null;

private volatile boolean active = false;
private volatile boolean initialState = true;
Expand Down Expand Up @@ -207,6 +208,17 @@ boolean watching() {
return this.watchService != null;
}

private void cleanupWatchKeys() {
if (settingsDirWatchKey != null) {
settingsDirWatchKey.cancel();
settingsDirWatchKey = null;
}
if (configDirWatchKey != null) {
configDirWatchKey.cancel();
configDirWatchKey = null;
}
}

synchronized void startWatcher(ClusterState clusterState, boolean onStartup) {
if (watching() || active == false) {
refreshExistingFileStateIfNeeded(clusterState);
Expand Down Expand Up @@ -246,10 +258,11 @@ synchronized void startWatcher(ClusterState clusterState, boolean onStartup) {
// We watch the config directory always, even if initially we had an operator directory
// it can be deleted and created later. The config directory never goes away, we only
// register it once for watching.
enableSettingsWatcher(null, operatorSettingsDir().getParent());
configDirWatchKey = enableSettingsWatcher(configDirWatchKey, operatorSettingsDir().getParent());
} catch (Exception e) {
if (watchService != null) {
try {
cleanupWatchKeys();
this.watchService.close();
} catch (Exception ignore) {} finally {
this.watchService = null;
Expand Down Expand Up @@ -323,10 +336,7 @@ synchronized void stopWatcher() {
logger.debug("stopping watcher ...");
if (watching()) {
try {
if (settingsDirWatchKey != null) {
settingsDirWatchKey.cancel();
settingsDirWatchKey = null;
}
cleanupWatchKeys();
fileUpdateState = null;
watchService.close();
if (watcherThreadLatch != null) {
Expand Down

0 comments on commit 425a320

Please sign in to comment.