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

W-16676258: Use WatchService for TracingConfigurationFileWatcher #13826

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@
*/
package org.mule.runtime.tracer.common.watcher;

import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
Copy link
Contributor

@fsgonz fsgonz Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order of static imports.
We follow this convention:
static imports
-> mule -> java -> other ones
import
-> mule classes -> java -> other ones.

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;

Expand All @@ -23,53 +32,51 @@ 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;
super("TracingConfigurationFileWatcher");
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
public void run() {
while (!interrupted()) {
try {
checkAndConfigure();
sleep(delay);
} catch (InterruptedException var2) {
} catch (InterruptedException ignored) {
return;
}
}
Expand Down