From e4b4bd02410c3fecfb27d7028a6b68a0040438a9 Mon Sep 17 00:00:00 2001 From: Leandro Ezequiel Barrios Date: Thu, 5 Sep 2024 13:53:21 -0300 Subject: [PATCH 1/7] Ignored test --- .../config/impl/FileSpanExporterConfigurationTestCase.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java b/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java index ce448dc8d0af..69db362e1835 100644 --- a/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java +++ b/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java @@ -43,6 +43,7 @@ import io.qameta.allure.Story; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -182,7 +183,7 @@ public void whenValueCorrespondingToPathGetAbsoluteValue() { assertThat(keyFileLocationPath.isAbsolute(), is(TRUE)); } - @Test + @Test @Ignore("To be fixed in W-16676258") public void configurationFileChanged() throws Exception { File file = createTempFile("tracing", "test"); Path testFile = get(file.getPath()); From f5698fbd84e3a1e674309b9dc0beee2ecfa10967 Mon Sep 17 00:00:00 2001 From: Leandro Ezequiel Barrios Date: Thu, 5 Sep 2024 15:56:02 -0300 Subject: [PATCH 2/7] Fixed --- .../TracingConfigurationFileWatcher.java | 53 ++++++++++--------- ...FileSpanExporterConfigurationTestCase.java | 2 +- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java b/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java index beb6e0601a04..70f07aa3b93c 100644 --- a/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java +++ b/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java @@ -6,11 +6,19 @@ */ package org.mule.runtime.tracer.common.watcher; +import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; import static org.mule.runtime.tracer.exporter.config.api.OpenTelemetrySpanExporterConfigurationProperties.MULE_OPEN_TELEMETRY_EXPORTER_CONFIGURATION_WATCHER_DEFAULT_DELAY_PROPERTY; import static org.slf4j.LoggerFactory.getLogger; import java.io.File; +import java.io.IOException; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; +import java.nio.file.WatchService; +import java.util.concurrent.TimeUnit; import org.slf4j.Logger; @@ -23,44 +31,42 @@ public class TracingConfigurationFileWatcher extends Thread { public final long DEFAULT_DELAY = Long .getLong(MULE_OPEN_TELEMETRY_EXPORTER_CONFIGURATION_WATCHER_DEFAULT_DELAY_PROPERTY, 60000l); - private final String filename; private final Runnable doOnChange; + private final File file; + private final WatchService watchService; protected long delay = DEFAULT_DELAY; - private final File file; - private long lastModified; - private boolean warnedAlready; public TracingConfigurationFileWatcher(String filename, Runnable doOnChange) { super("FileSpanExporterConfigurationWatcher"); - this.filename = filename; this.file = new File(filename); this.doOnChange = doOnChange; - this.lastModified = file.lastModified(); + + try { + this.watchService = FileSystems.getDefault().newWatchService(); + file.toPath().getParent().register(watchService, ENTRY_MODIFY); + } catch (IOException e) { + throw new RuntimeException(e); + } + this.setDaemon(true); } - protected void checkAndConfigure() { - boolean fileExists; - try { - fileExists = file.exists(); - } catch (SecurityException var4) { - LOGGER.warn("The tracing config file " + filename + " was possibly removed."); - this.interrupt(); + protected void checkAndConfigure() throws InterruptedException { + WatchKey key = watchService.poll(delay, TimeUnit.MILLISECONDS); + if (key == null) { return; } - if (fileExists) { - long fileLastMod = file.lastModified(); - if (fileLastMod > lastModified) { - this.lastModified = fileLastMod; - this.doOnChange(); - this.warnedAlready = false; + for (WatchEvent event : key.pollEvents()) { + if (ENTRY_MODIFY.equals(event.kind())) { + Path changedFile = (Path) event.context(); + if (changedFile.equals(file.toPath().getFileName())) { + this.doOnChange(); + } } - } else if (!this.warnedAlready) { - LOGGER.warn("Configuration for file exporter was not found. It was possibly removed."); - this.warnedAlready = true; } + key.reset(); } @Override @@ -68,8 +74,7 @@ public void run() { while (!interrupted()) { try { checkAndConfigure(); - sleep(delay); - } catch (InterruptedException var2) { + } catch (InterruptedException ignored) { return; } } diff --git a/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java b/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java index 69db362e1835..9e64e4f90030 100644 --- a/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java +++ b/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java @@ -183,7 +183,7 @@ public void whenValueCorrespondingToPathGetAbsoluteValue() { assertThat(keyFileLocationPath.isAbsolute(), is(TRUE)); } - @Test @Ignore("To be fixed in W-16676258") + @Test public void configurationFileChanged() throws Exception { File file = createTempFile("tracing", "test"); Path testFile = get(file.getPath()); From 1928dadff10d5b8ba5e8d0073157a76c7ebec257 Mon Sep 17 00:00:00 2001 From: Leandro Ezequiel Barrios Date: Thu, 5 Sep 2024 16:08:50 -0300 Subject: [PATCH 3/7] .. --- .../common/watcher/TracingConfigurationFileWatcher.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java b/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java index 70f07aa3b93c..ca5281176162 100644 --- a/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java +++ b/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java @@ -6,6 +6,7 @@ */ package org.mule.runtime.tracer.common.watcher; +import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE; import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; import static org.mule.runtime.tracer.exporter.config.api.OpenTelemetrySpanExporterConfigurationProperties.MULE_OPEN_TELEMETRY_EXPORTER_CONFIGURATION_WATCHER_DEFAULT_DELAY_PROPERTY; @@ -38,7 +39,7 @@ public class TracingConfigurationFileWatcher extends Thread { protected long delay = DEFAULT_DELAY; public TracingConfigurationFileWatcher(String filename, Runnable doOnChange) { - super("FileSpanExporterConfigurationWatcher"); + super("TracingConfigurationFileWatcher"); this.file = new File(filename); this.doOnChange = doOnChange; @@ -66,6 +67,7 @@ protected void checkAndConfigure() throws InterruptedException { } } } + key.reset(); } From 68bf9535a309cf9d72baba91630ab68becd9a1aa Mon Sep 17 00:00:00 2001 From: Leandro Ezequiel Barrios Date: Thu, 5 Sep 2024 16:10:29 -0300 Subject: [PATCH 4/7] Removed unused ignore --- .../config/impl/FileSpanExporterConfigurationTestCase.java | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java b/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java index 9e64e4f90030..ce448dc8d0af 100644 --- a/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java +++ b/modules/tracing/mule-tracer-exporter-configuration-impl/src/test/java/org/mule/runtime/tracer/exporter/config/impl/FileSpanExporterConfigurationTestCase.java @@ -43,7 +43,6 @@ import io.qameta.allure.Story; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; From c5158090396b3435da3cdd8667c3b91e88dd8e89 Mon Sep 17 00:00:00 2001 From: Leandro Ezequiel Barrios Date: Thu, 5 Sep 2024 16:39:36 -0300 Subject: [PATCH 5/7] Order of imports --- .../common/watcher/TracingConfigurationFileWatcher.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java b/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java index ca5281176162..5b3cb5768721 100644 --- a/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java +++ b/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java @@ -6,10 +6,8 @@ */ package org.mule.runtime.tracer.common.watcher; -import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE; -import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; import static org.mule.runtime.tracer.exporter.config.api.OpenTelemetrySpanExporterConfigurationProperties.MULE_OPEN_TELEMETRY_EXPORTER_CONFIGURATION_WATCHER_DEFAULT_DELAY_PROPERTY; - +import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; import static org.slf4j.LoggerFactory.getLogger; import java.io.File; @@ -20,7 +18,6 @@ import java.nio.file.WatchKey; import java.nio.file.WatchService; import java.util.concurrent.TimeUnit; - import org.slf4j.Logger; /** @@ -31,7 +28,7 @@ public class TracingConfigurationFileWatcher extends Thread { private static final Logger LOGGER = getLogger(TracingConfigurationFileWatcher.class); public final long DEFAULT_DELAY = Long .getLong(MULE_OPEN_TELEMETRY_EXPORTER_CONFIGURATION_WATCHER_DEFAULT_DELAY_PROPERTY, - 60000l); + 60000l); private final Runnable doOnChange; private final File file; private final WatchService watchService; From ca1b9238f23a5c30c31608eda7c00b9322311854 Mon Sep 17 00:00:00 2001 From: Leandro Ezequiel Barrios Date: Thu, 5 Sep 2024 16:55:43 -0300 Subject: [PATCH 6/7] Format --- .../common/watcher/TracingConfigurationFileWatcher.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java b/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java index 5b3cb5768721..aa947562d6e9 100644 --- a/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java +++ b/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java @@ -7,7 +7,9 @@ package org.mule.runtime.tracer.common.watcher; import static org.mule.runtime.tracer.exporter.config.api.OpenTelemetrySpanExporterConfigurationProperties.MULE_OPEN_TELEMETRY_EXPORTER_CONFIGURATION_WATCHER_DEFAULT_DELAY_PROPERTY; + import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; + import static org.slf4j.LoggerFactory.getLogger; import java.io.File; @@ -18,6 +20,7 @@ import java.nio.file.WatchKey; import java.nio.file.WatchService; import java.util.concurrent.TimeUnit; + import org.slf4j.Logger; /** @@ -26,9 +29,8 @@ public class TracingConfigurationFileWatcher extends Thread { private static final Logger LOGGER = getLogger(TracingConfigurationFileWatcher.class); - public final long DEFAULT_DELAY = Long - .getLong(MULE_OPEN_TELEMETRY_EXPORTER_CONFIGURATION_WATCHER_DEFAULT_DELAY_PROPERTY, - 60000l); + public final long DEFAULT_DELAY = + Long.getLong(MULE_OPEN_TELEMETRY_EXPORTER_CONFIGURATION_WATCHER_DEFAULT_DELAY_PROPERTY, 60000l); private final Runnable doOnChange; private final File file; private final WatchService watchService; From bb78f14d806c7bc913e1d3d7704bba2490d9247d Mon Sep 17 00:00:00 2001 From: Leandro Ezequiel Barrios Date: Tue, 10 Sep 2024 12:01:03 -0300 Subject: [PATCH 7/7] close WatchService on interrupt --- .../common/watcher/TracingConfigurationFileWatcher.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java b/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java index aa947562d6e9..4d5f1119508e 100644 --- a/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java +++ b/modules/tracing/mule-tracer-common/src/main/java/org/mule/runtime/tracer/common/watcher/TracingConfigurationFileWatcher.java @@ -77,6 +77,12 @@ public void run() { checkAndConfigure(); } catch (InterruptedException ignored) { return; + } finally { + try { + watchService.close(); + } catch (IOException e) { + LOGGER.error("Error while closing watch service", e); + } } } }