From f7a521c48ec4ef3a78033a4b6ff83eba7594cda8 Mon Sep 17 00:00:00 2001 From: cleverchuk Date: Tue, 14 Jan 2025 15:50:17 -0500 Subject: [PATCH] NH-98975: don't check filename in event just reload config --- .../initialize/ConfigurationLoader.java | 3 +- .../livereload/ConfigurationFileWatcher.java | 32 ++++++------------- .../ConfigurationFileWatcherTest.java | 23 +++---------- 3 files changed, 14 insertions(+), 44 deletions(-) diff --git a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoader.java b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoader.java index 3b5ada27..446bdce5 100644 --- a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoader.java +++ b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoader.java @@ -133,11 +133,10 @@ private static void attachConfigurationFileWatcher() { ConfigurationFileWatcher.restartWatch( Paths.get(configurationFileDir), - runtimeConfigFilename, watchPeriod, watchService, watchScheduler, - filePath -> { + () -> { try { logger.info("Configuration file change detected. Reloading configuration"); loadConfigurations(); diff --git a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/config/livereload/ConfigurationFileWatcher.java b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/config/livereload/ConfigurationFileWatcher.java index e188a2dc..c5357bc2 100644 --- a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/config/livereload/ConfigurationFileWatcher.java +++ b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/config/livereload/ConfigurationFileWatcher.java @@ -27,14 +27,11 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import java.util.function.Consumer; public final class ConfigurationFileWatcher { private final Path directory; - private final String filename; - - private final Consumer fileChangeListener; + private final Runnable fileChangeListener; private final WatchService watchService; @@ -48,13 +45,11 @@ public final class ConfigurationFileWatcher { private ConfigurationFileWatcher( Path directory, - String filename, long watchPeriod, WatchService watchService, ScheduledExecutorService executorService, - Consumer fileChangeListener) { + Runnable fileChangeListener) { this.directory = directory; - this.filename = filename; this.watchPeriod = watchPeriod; this.executorService = executorService; this.fileChangeListener = fileChangeListener; @@ -63,22 +58,16 @@ private ConfigurationFileWatcher( public static void restartWatch( Path directory, - String filename, long watchPeriod, WatchService watchService, ScheduledExecutorService scheduledExecutorService, - Consumer fileChangeListener) { + Runnable fileChangeListener) { if (INSTANCE != null) { INSTANCE.cancelWatch(); } INSTANCE = new ConfigurationFileWatcher( - directory, - filename, - watchPeriod, - watchService, - scheduledExecutorService, - fileChangeListener); + directory, watchPeriod, watchService, scheduledExecutorService, fileChangeListener); INSTANCE.startWatch(); } @@ -91,18 +80,15 @@ private void watch() { continue; } - Object filePath = event.context(); - if (filePath instanceof Path && ((Path) filePath).endsWith(this.filename)) { - fileChangeListener.accept(((Path) filePath)); - } + fileChangeListener.run(); } boolean valid = key.reset(); if (!valid) { LoggerFactory.getLogger() .info( String.format( - "Watch ended for directory(%s) and file(%s) because the directory became inaccessible", - directory, filename)); + "Watch ended for directory(%s) because the directory became inaccessible", + directory)); } } } @@ -121,8 +107,8 @@ private void startWatch() { LoggerFactory.getLogger() .info( String.format( - "Failed to start watch for directory(%s) and file(%s) due to error - %s", - directory, filename, exception)); + "Failed to start watch for directory(%s) due to error - %s", + directory, exception)); } } diff --git a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/initialize/config/livereload/ConfigurationFileWatcherTest.java b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/initialize/config/livereload/ConfigurationFileWatcherTest.java index cac17bf9..bc13d439 100644 --- a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/initialize/config/livereload/ConfigurationFileWatcherTest.java +++ b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/initialize/config/livereload/ConfigurationFileWatcherTest.java @@ -23,7 +23,6 @@ import java.util.Collections; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; -import java.util.function.Consumer; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; @@ -42,7 +41,7 @@ class ConfigurationFileWatcherTest { @Mock private ScheduledExecutorService scheduledExecutorServiceMock; - @Mock private Consumer fileChangeListerMock; + @Mock private Runnable fileChangeListerMock; @Mock private ScheduledFuture scheduledFutureMock; @@ -50,8 +49,6 @@ class ConfigurationFileWatcherTest { @Captor private ArgumentCaptor runnableArgumentCaptor; - private final String filename = "file.json"; - private final long watchPeriod = 1; @Test @@ -66,12 +63,7 @@ void verifyThatOverflowEventsAreIgnored() throws IOException { .thenAnswer(invocation -> scheduledFutureMock); ConfigurationFileWatcher.restartWatch( - dirMock, - filename, - watchPeriod, - watchServiceMock, - scheduledExecutorServiceMock, - fileChangeListerMock); + dirMock, watchPeriod, watchServiceMock, scheduledExecutorServiceMock, fileChangeListerMock); runnableArgumentCaptor.getValue().run(); verify(watchServiceMock).poll(); @@ -85,7 +77,6 @@ void verifyThatFileChangeListenerIsInvokedWhenFileIsModified() throws IOExceptio when(watchKeyMock.pollEvents()).thenReturn(Collections.singletonList(watchEventMock)); when(watchEventMock.kind()).thenAnswer(invocation -> StandardWatchEventKinds.ENTRY_MODIFY); - when(watchEventMock.context()).thenReturn(Paths.get(filename)); when(watchKeyMock.reset()).thenReturn(false); when(dirMock.register(any(), any())).thenReturn(watchKeyMock); @@ -94,19 +85,13 @@ void verifyThatFileChangeListenerIsInvokedWhenFileIsModified() throws IOExceptio .thenAnswer(invocation -> scheduledFutureMock); ConfigurationFileWatcher.restartWatch( - dirMock, - filename, - watchPeriod, - watchServiceMock, - scheduledExecutorServiceMock, - fileChangeListerMock); + dirMock, watchPeriod, watchServiceMock, scheduledExecutorServiceMock, fileChangeListerMock); runnableArgumentCaptor.getValue().run(); verify(watchServiceMock).poll(); verify(watchKeyMock).pollEvents(); - verify(watchEventMock).context(); - verify(fileChangeListerMock).accept(any()); + verify(fileChangeListerMock).run(); } }