-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Introducing a file group watcher #134564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ankit--sethi
wants to merge
12
commits into
elastic:main
from
ankit--sethi:feature/recreate-http-client-when-settings-change
Closed
Introducing a file group watcher #134564
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3624c8d
introduce the notion of a FileGroupWatcher, an orchestrating watcher …
ankit--sethi 8f28855
don't need this buffer
ankit--sethi bd47039
Merge branch 'main' into feature/recreate-http-client-when-settings-c…
ankit--sethi fa0a26b
[CI] Auto commit changes from spotless
676b11d
Merge branch 'main' into feature/recreate-http-client-when-settings-c…
ankit--sethi 6fd33fc
Merge branch 'main' into feature/recreate-http-client-when-settings-c…
ankit--sethi b106143
Merge branch 'main' into feature/recreate-http-client-when-settings-c…
ankit--sethi 9ac32e8
Merge branch 'main' into feature/recreate-http-client-when-settings-c…
ankit--sethi c6685d2
clean up
ankit--sethi fac653a
Merge remote-tracking branch 'origin/feature/recreate-http-client-whe…
ankit--sethi 2ab2619
Merge branch 'main' of github.com:ankit--sethi/elasticsearch into fea…
ankit--sethi 0199010
Merge branch 'main' into feature/recreate-http-client-when-settings-c…
ankit--sethi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
server/src/main/java/org/elasticsearch/watcher/FileGroupChangesListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
| package org.elasticsearch.watcher; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Callback interface that FileGroupWatcher is using to notify listeners about changes. | ||
| */ | ||
| public interface FileGroupChangesListener { | ||
| /** | ||
| * Called only once for any number of files that were changed (creates/updates/deletes) in the watched directory | ||
| */ | ||
| default void onFileChanged(List<Path> file) {} | ||
|
|
||
| } |
118 changes: 118 additions & 0 deletions
118
server/src/main/java/org/elasticsearch/watcher/FileGroupWatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
| package org.elasticsearch.watcher; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * An abstraction that coalesces any number of updates, deletes or creations for any number of files into ONE update | ||
| * per configured Resource Watcher period. To this end, the semantics are such that distinctions between | ||
| * updates, deletes or create actions are all collapsed and everything is referred to as a "change". | ||
| * <p> | ||
| * The implementation relies on FileWatcher for the specifics of monitoring files. This class acts like an | ||
| * orchestrator, initiating and reacting to the results of a collection of file watchers. | ||
| */ | ||
| public class FileGroupWatcher extends AbstractResourceWatcher<FileGroupChangesListener> { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(FileGroupWatcher.class); | ||
|
|
||
| private final List<FileWatcher> watchedFiles; | ||
| private final CoalescingFileChangesListener coalescingListener; | ||
|
|
||
| /** | ||
| * @param files a group of files to be watched | ||
| */ | ||
| public FileGroupWatcher(Collection<Path> files) { | ||
| this.coalescingListener = new CoalescingFileChangesListener(); | ||
| this.watchedFiles = new ArrayList<>(); | ||
| for (Path file : files) { | ||
| final FileWatcher watcher = new FileWatcher(file); | ||
| watcher.addListener(coalescingListener); | ||
| watchedFiles.add(watcher); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void doInit() throws IOException { | ||
| for (FileWatcher watcher : watchedFiles) { | ||
| watcher.doInit(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void doCheckAndNotify() throws IOException { | ||
| if (coalescingListener.filesChanged()) { | ||
| // fallback in case state wasn't cleared correctly in previous iteration | ||
| logger.error("Detected inconsistent state in the File Group Watcher, clearing it and proceeding."); | ||
| coalescingListener.reset(); | ||
| } | ||
| for (FileWatcher watcher : watchedFiles) { | ||
| watcher.doCheckAndNotify(); | ||
| } | ||
| // at this point the results of all file changes has coalesced | ||
| if (coalescingListener.filesChanged()) { | ||
| logger.info("Number of changed files: {}", coalescingListener.getFilesChangedCount()); | ||
| try { | ||
| for (FileGroupChangesListener listener : listeners()) { | ||
| try { | ||
| listener.onFileChanged(coalescingListener.getFiles()); | ||
| } catch (Exception e) { | ||
| logger.warn("Cannot notify file group changes listener {}", listener.getClass().getSimpleName(), e); | ||
| } | ||
| } | ||
| } finally { | ||
| coalescingListener.reset(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static class CoalescingFileChangesListener implements FileChangesListener { | ||
|
|
||
| private final ThreadLocal<List<Path>> filesChanged = ThreadLocal.withInitial(ArrayList::new); | ||
|
|
||
| @Override | ||
| public void onFileChanged(Path file) { | ||
| filesChanged.get().add(file); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFileCreated(Path file) { | ||
| onFileChanged(file); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFileDeleted(Path file) { | ||
| onFileChanged(file); | ||
| } | ||
|
|
||
| public boolean filesChanged() { | ||
| return filesChanged.get().isEmpty() == false; | ||
| } | ||
|
|
||
| public int getFilesChangedCount() { | ||
| return filesChanged.get().size(); | ||
| } | ||
|
|
||
| public void reset() { | ||
| filesChanged.set(new ArrayList<>()); | ||
| } | ||
|
|
||
| public List<Path> getFiles() { | ||
| return new ArrayList<>(filesChanged.get()); | ||
| } | ||
|
|
||
| } | ||
| } | ||
144 changes: 144 additions & 0 deletions
144
server/src/test/java/org/elasticsearch/watcher/FileGroupWatcherTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
| package org.elasticsearch.watcher; | ||
|
|
||
| import org.apache.lucene.tests.util.LuceneTestCase; | ||
| import org.elasticsearch.test.ESTestCase; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.attribute.FileTime; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import static org.hamcrest.Matchers.containsInAnyOrder; | ||
| import static org.hamcrest.Matchers.empty; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.hasSize; | ||
|
|
||
| @LuceneTestCase.SuppressFileSystems("ExtrasFS") | ||
| public class FileGroupWatcherTests extends ESTestCase { | ||
|
|
||
| private static class RecordingFileGroupChangesListener implements FileGroupChangesListener { | ||
| private final List<String> notifications = new ArrayList<>(); | ||
|
|
||
| @Override | ||
| public void onFileChanged(List<Path> files) { | ||
| notifications.add("onFileChanged: " + files.size() + " files"); | ||
| } | ||
|
|
||
| public List<String> notifications() { | ||
| return notifications; | ||
| } | ||
| } | ||
|
|
||
| public void testMultipleFileChanges() throws IOException, InterruptedException { | ||
| Path tempDir = createTempDir(); | ||
| RecordingFileGroupChangesListener listener = new RecordingFileGroupChangesListener(); | ||
| Path file1 = tempDir.resolve("test1.txt"); | ||
| Path file2 = tempDir.resolve("test2.txt"); | ||
| Path file3 = tempDir.resolve("test3.txt"); | ||
|
|
||
| touch(file1); | ||
| touch(file2); | ||
| touch(file3); | ||
|
|
||
| FileGroupWatcher fileGroupWatcher = new FileGroupWatcher(Arrays.asList(file1, file2, file3)); | ||
| fileGroupWatcher.addListener(listener); | ||
| fileGroupWatcher.init(); | ||
|
|
||
| // Modify multiple files - should batch into single notification | ||
| Files.setLastModifiedTime(file1, FileTime.fromMillis(System.currentTimeMillis() + 1)); // + 1 makes sure a change is detected | ||
| Files.setLastModifiedTime(file2, FileTime.fromMillis(System.currentTimeMillis() + 1)); | ||
|
|
||
| fileGroupWatcher.checkAndNotify(); | ||
| assertThat(listener.notifications(), hasSize(1)); | ||
| assertThat(listener.notifications().get(0), equalTo("onFileChanged: 2 files")); | ||
| } | ||
|
|
||
| public void testBatchedCreation() throws IOException { | ||
| Path tempDir = createTempDir(); | ||
| RecordingFileGroupChangesListener listener = new RecordingFileGroupChangesListener(); | ||
| Path file1 = tempDir.resolve("test1.txt"); | ||
| Path file2 = tempDir.resolve("test2.txt"); | ||
|
|
||
| FileGroupWatcher fileGroupWatcher = new FileGroupWatcher(Arrays.asList(file1, file2)); | ||
| fileGroupWatcher.addListener(listener); | ||
| fileGroupWatcher.init(); | ||
|
|
||
| // Create both files | ||
| touch(file1); | ||
| touch(file2); | ||
|
|
||
| fileGroupWatcher.checkAndNotify(); | ||
| assertThat(listener.notifications(), hasSize(1)); | ||
| assertThat(listener.notifications().get(0), equalTo("onFileChanged: 2 files")); | ||
| } | ||
|
|
||
| public void testBatchedDeletion() throws IOException { | ||
| Path tempDir = createTempDir(); | ||
| RecordingFileGroupChangesListener listener = new RecordingFileGroupChangesListener(); | ||
| Path file1 = tempDir.resolve("test1.txt"); | ||
| Path file2 = tempDir.resolve("test2.txt"); | ||
|
|
||
| touch(file1); | ||
| touch(file2); | ||
|
|
||
| FileGroupWatcher fileGroupWatcher = new FileGroupWatcher(Arrays.asList(file1, file2)); | ||
| fileGroupWatcher.addListener(listener); | ||
| fileGroupWatcher.init(); | ||
|
|
||
| Files.delete(file1); | ||
| Files.delete(file2); | ||
|
|
||
| fileGroupWatcher.checkAndNotify(); | ||
| assertThat(listener.notifications(), hasSize(1)); | ||
| assertThat(listener.notifications().get(0), equalTo("onFileChanged: 2 files")); | ||
| } | ||
|
|
||
| public void testMixedOperationsSeparateNotifications() throws IOException { | ||
| Path tempDir = createTempDir(); | ||
| RecordingFileGroupChangesListener listener = new RecordingFileGroupChangesListener(); | ||
| Path existingFile = tempDir.resolve("existing.txt"); | ||
| Path newFile = tempDir.resolve("new.txt"); | ||
| Path deleteFile = tempDir.resolve("delete.txt"); | ||
|
|
||
| touch(existingFile); | ||
| touch(deleteFile); | ||
|
|
||
| FileGroupWatcher fileGroupWatcher = new FileGroupWatcher(Arrays.asList(existingFile, newFile, deleteFile)); | ||
| fileGroupWatcher.addListener(listener); | ||
| fileGroupWatcher.init(); | ||
|
|
||
| Files.setLastModifiedTime(existingFile, FileTime.fromMillis(System.currentTimeMillis() + 1)); // + 1 makes sure a change is detected | ||
| touch(newFile); | ||
| Files.delete(deleteFile); | ||
|
|
||
| fileGroupWatcher.checkAndNotify(); | ||
| assertThat(listener.notifications(), hasSize(1)); | ||
| assertThat(listener.notifications(), containsInAnyOrder("onFileChanged: 3 files")); | ||
| } | ||
|
|
||
| public void testEmptyFileGroup() throws IOException { | ||
| RecordingFileGroupChangesListener listener = new RecordingFileGroupChangesListener(); | ||
|
|
||
| FileGroupWatcher fileGroupWatcher = new FileGroupWatcher(List.of()); | ||
| fileGroupWatcher.addListener(listener); | ||
| fileGroupWatcher.init(); | ||
|
|
||
| fileGroupWatcher.checkAndNotify(); | ||
| assertThat(listener.notifications(), empty()); | ||
| } | ||
|
|
||
| static void touch(Path path) throws IOException { | ||
| Files.newOutputStream(path).close(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds concerning. How can this happen?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it shouldn't ever happen due to the reset happening in the
finallyblock. I was thinking of very rare situations where the JDK spec saysfinallywill not execute. I had initially found this reference:https://stackoverflow.com/a/2417986
After digging a little deeper to write this reply, it turns out this is a JDK documentation mistake that they have now updated. Apparently, the section I quoted above is not actually true!
https://bugs.openjdk.org/browse/JDK-8276156?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel
With things where they are now, I'm fine with removing this if condition if it scares more than helps!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed this if-condition