Skip to content

Commit 9645cb3

Browse files
committed
Watch file changes in distribution and log
1 parent 33ce328 commit 9645cb3

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/AbstractLocalClusterFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public H create(S spec) {
9494
Path baseWorkingDir;
9595
try {
9696
baseWorkingDir = Files.createTempDirectory(spec.getName());
97+
DebugUtils.watchDirectory(baseWorkingDir);
9798
} catch (IOException e) {
9899
throw new UncheckedIOException(e);
99100
}

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/util/DebugUtils.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414

1515
import java.io.File;
1616
import java.io.IOException;
17+
import java.nio.file.FileSystems;
1718
import java.nio.file.Files;
1819
import java.nio.file.Path;
20+
import java.nio.file.StandardWatchEventKinds;
21+
import java.nio.file.WatchEvent;
22+
import java.nio.file.WatchKey;
23+
import java.nio.file.WatchService;
24+
import java.util.HashMap;
25+
import java.util.Map;
1926
import java.util.stream.Stream;
2027

2128
public class DebugUtils {
@@ -69,4 +76,67 @@ private static void listFilesRecursively(Path directoryPath) {
6976
e.printStackTrace();
7077
}
7178
}
79+
80+
public static void watchDirectory(Path directoryPath) {
81+
82+
Thread thread = new Thread(() -> {
83+
try {
84+
WatchService watchService = FileSystems.getDefault().newWatchService();
85+
Map<WatchKey, Path> keyPathMap = new HashMap<>();
86+
87+
// Register the directory and all its subdirectories
88+
registerAll(directoryPath, watchService, keyPathMap);
89+
90+
LOGGER.warn("Watching directory: {}", directoryPath);
91+
92+
while (true) {
93+
WatchKey key = watchService.take();
94+
Path dir = keyPathMap.get(key);
95+
96+
for (WatchEvent<?> event : key.pollEvents()) {
97+
WatchEvent.Kind<?> kind = event.kind();
98+
Path name = (Path) event.context();
99+
Path child = dir.resolve(name);
100+
101+
LOGGER.warn("Event kind: {}. File affected: {}", kind, child);
102+
103+
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
104+
if (Files.isDirectory(child)) {
105+
registerAll(child, watchService, keyPathMap);
106+
}
107+
}
108+
}
109+
110+
boolean valid = key.reset();
111+
if (valid == false) {
112+
keyPathMap.remove(key);
113+
if (keyPathMap.isEmpty()) {
114+
break;
115+
}
116+
}
117+
}
118+
} catch (IOException | InterruptedException e) {
119+
e.printStackTrace();
120+
}
121+
});
122+
123+
thread.start();
124+
125+
}
126+
127+
private static void registerAll(Path start, WatchService watchService, Map<WatchKey, Path> keyPathMap) throws IOException {
128+
Files.walk(start).filter(Files::isDirectory).forEach(path -> {
129+
try {
130+
WatchKey key = path.register(
131+
watchService,
132+
StandardWatchEventKinds.ENTRY_CREATE,
133+
StandardWatchEventKinds.ENTRY_DELETE,
134+
StandardWatchEventKinds.ENTRY_MODIFY
135+
);
136+
keyPathMap.put(key, path);
137+
} catch (IOException e) {
138+
e.printStackTrace();
139+
}
140+
});
141+
}
72142
}

0 commit comments

Comments
 (0)