Skip to content
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

fix(file.util): avoid inotify problems in containers by using polling over FileWatcher #38

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {
}

group 'com.dynatrace.metric.util'
version = '2.2.0'
version = '2.2.1'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ private void setUp(String fileName, Duration pollInterval) {
if (!Files.exists(Paths.get(fileName))) {
logger.info("File based configuration does not exist, serving default config.");
} else {
poller = FilePollerFactory.getDefault(fileName, pollInterval);
poller =
FilePollerFactory.getPollBased(
fileName, pollInterval != null ? pollInterval : Duration.ofSeconds(60));
}
} catch (InvalidPathException e) {
// This happens on Windows, when the *nix filepath is not valid.
logger.info(
() -> String.format("%s is not a valid file path (%s).", fileName, e.getMessage()));
} catch (IOException | IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
logger.warning(
() -> String.format("File polling could not be initialized: %s", e.getMessage()));
}
Expand Down
19 changes: 9 additions & 10 deletions lib/src/main/java/com/dynatrace/file/util/FilePollerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@ class FilePollerFactory {
private FilePollerFactory() {}

/**
* Creates the default {@link FilePoller} based on the current OS.
* Creates the default {@link FilePoller}.
*
* @implNote On Linux and Windows, * the poll mechanism is based on the {@link
* java.nio.file.WatchService WatchService}. For macOS, * where there is no native
* implementation for the {@link java.nio.file.WatchService WatchService} * bindings, this
* method provides a {@link PollBasedFilePoller}, which polls the file of interest *
* periodically (according to the {@code pollInterval}).
arminru marked this conversation as resolved.
Show resolved Hide resolved
* @implNote This method may choose to use a {@link java.nio.file.WatchService}-based
* implementation which can be problematic when used in conjunction with container
* bind-mounts. If the resulting {@link FilePoller} will be used in such a manner, consider
* using {@link FilePollerFactory#getPollBased(String, Duration)}.
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
* @param fileName The name of the file to be watched.
* @param pollInterval The interval in which the {@link PollBasedFilePoller} polls for changes.
* Only applicable on macOS. Will default to 60s if {@code null} is passed.
* @param pollInterval The interval in which the {@link FilePoller} will update, if applicable.
* Interval MAY not apply to {@link java.nio.file.WatchService} based implementations. Will
* default to 60s if {@code null} is passed.
* @return An object that implements the abstract methods on {@link FilePoller}.
* @throws IOException if the initialization of the {@link WatchServiceBasedFilePoller} is not
* successful.
* @throws IOException if the initialization of the {@link FilePoller} is not successful
*/
static FilePoller getDefault(String fileName, Duration pollInterval) throws IOException {
if (IS_MAC_OS) {
Expand Down
Loading