Skip to content

Commit 433450a

Browse files
committed
add overridden methods to support exclusive file entitlement
1 parent 8cc616c commit 433450a

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

server/src/main/java/org/elasticsearch/common/file/AbstractFileWatchingService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public abstract class AbstractFileWatchingService extends AbstractLifecycleCompo
6363
private WatchKey settingsDirWatchKey;
6464
private WatchKey configDirWatchKey;
6565

66+
@SuppressWarnings("this-escape")
6667
public AbstractFileWatchingService(Path settingsDir) {
6768
if (filesExists(settingsDir) && filesIsDirectory(settingsDir) == false) {
6869
throw new IllegalArgumentException("settingsDir should be a directory");

server/src/main/java/org/elasticsearch/reservedstate/service/FileSettingsService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,26 +350,32 @@ public synchronized HealthIndicatorResult calculate(boolean verbose, int maxAffe
350350
// the following methods are a workaround to ensure exclusive access for files
351351
// required by child watchers; this is required because we only check the caller's module
352352
// not the entire stack
353+
@Override
353354
protected boolean filesExists(Path path) {
354355
return Files.exists(path);
355356
}
356357

358+
@Override
357359
protected boolean filesIsDirectory(Path path) {
358360
return Files.isDirectory(path);
359361
}
360362

363+
@Override
361364
protected <A extends BasicFileAttributes> A filesReadAttributes(Path path, Class<A> clazz) throws IOException {
362365
return Files.readAttributes(path, clazz);
363366
}
364367

368+
@Override
365369
protected Stream<Path> filesList(Path dir) throws IOException {
366370
return Files.list(dir);
367371
}
368372

373+
@Override
369374
protected Path filesSetLastModifiedTime(Path path, FileTime time) throws IOException {
370375
return Files.setLastModifiedTime(path, time);
371376
}
372377

378+
@Override
373379
protected InputStream filesNewInputStream(Path path) throws IOException {
374380
return Files.newInputStream(path);
375381
}

server/src/test/java/org/elasticsearch/common/file/AbstractFileWatchingServiceTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import org.junit.Before;
2525

2626
import java.io.IOException;
27+
import java.io.InputStream;
2728
import java.nio.file.Files;
2829
import java.nio.file.Path;
2930
import java.nio.file.StandardCopyOption;
3031
import java.nio.file.StandardWatchEventKinds;
3132
import java.nio.file.WatchKey;
33+
import java.nio.file.attribute.BasicFileAttributes;
3234
import java.nio.file.attribute.FileTime;
3335
import java.time.Instant;
3436
import java.time.LocalDateTime;
@@ -40,6 +42,7 @@
4042
import java.util.concurrent.ExecutionException;
4143
import java.util.concurrent.TimeUnit;
4244
import java.util.function.Consumer;
45+
import java.util.stream.Stream;
4346

4447
import static org.elasticsearch.node.Node.NODE_NAME_SETTING;
4548
import static org.elasticsearch.test.hamcrest.OptionalMatchers.isEmpty;
@@ -81,6 +84,39 @@ protected void processInitialFilesMissing() {
8184
called.accept(null);
8285
}
8386
}
87+
88+
// the following methods are a workaround to ensure exclusive access for files
89+
// required by child watchers; this is required because we only check the caller's module
90+
// not the entire stack
91+
@Override
92+
protected boolean filesExists(Path path) {
93+
return Files.exists(path);
94+
}
95+
96+
@Override
97+
protected boolean filesIsDirectory(Path path) {
98+
return Files.isDirectory(path);
99+
}
100+
101+
@Override
102+
protected <A extends BasicFileAttributes> A filesReadAttributes(Path path, Class<A> clazz) throws IOException {
103+
return Files.readAttributes(path, clazz);
104+
}
105+
106+
@Override
107+
protected Stream<Path> filesList(Path dir) throws IOException {
108+
return Files.list(dir);
109+
}
110+
111+
@Override
112+
protected Path filesSetLastModifiedTime(Path path, FileTime time) throws IOException {
113+
return Files.setLastModifiedTime(path, time);
114+
}
115+
116+
@Override
117+
protected InputStream filesNewInputStream(Path path) throws IOException {
118+
return Files.newInputStream(path);
119+
}
84120
}
85121

86122
private Path watchedFile;

server/src/test/java/org/elasticsearch/common/file/MasterNodeFileWatchingServiceTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@
2626
import org.junit.Before;
2727

2828
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.nio.file.Files;
2931
import java.nio.file.Path;
32+
import java.nio.file.attribute.BasicFileAttributes;
33+
import java.nio.file.attribute.FileTime;
3034
import java.util.concurrent.ExecutionException;
3135
import java.util.function.Consumer;
36+
import java.util.stream.Stream;
3237

3338
import static org.hamcrest.Matchers.is;
3439
import static org.mockito.Mockito.mock;
@@ -62,6 +67,39 @@ protected void processFileChanges(Path file) throws InterruptedException, Execut
6267
protected void processInitialFilesMissing() throws InterruptedException, ExecutionException, IOException {
6368
// file always exists, but we don't care about the missing case for master node behavior
6469
}
70+
71+
// the following methods are a workaround to ensure exclusive access for files
72+
// required by child watchers; this is required because we only check the caller's module
73+
// not the entire stack
74+
@Override
75+
protected boolean filesExists(Path path) {
76+
return Files.exists(path);
77+
}
78+
79+
@Override
80+
protected boolean filesIsDirectory(Path path) {
81+
return Files.isDirectory(path);
82+
}
83+
84+
@Override
85+
protected <A extends BasicFileAttributes> A filesReadAttributes(Path path, Class<A> clazz) throws IOException {
86+
return Files.readAttributes(path, clazz);
87+
}
88+
89+
@Override
90+
protected Stream<Path> filesList(Path dir) throws IOException {
91+
return Files.list(dir);
92+
}
93+
94+
@Override
95+
protected Path filesSetLastModifiedTime(Path path, FileTime time) throws IOException {
96+
return Files.setLastModifiedTime(path, time);
97+
}
98+
99+
@Override
100+
protected InputStream filesNewInputStream(Path path) throws IOException {
101+
return Files.newInputStream(path);
102+
}
65103
};
66104
testService.start();
67105
}

0 commit comments

Comments
 (0)