8303413: (fs) Ignore polling interval sensitivity modifiers in PollingWatchService

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2023-03-03 18:33:58 +00:00
parent 99443142cc
commit a04b1049ff
2 changed files with 31 additions and 107 deletions
src/java.base/share/classes/sun/nio/fs
test/jdk/java/nio/file/WatchService

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -62,8 +62,8 @@ import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
class PollingWatchService
extends AbstractWatchService
{
// default polling interval in seconds
private static final int DEFAULT_POLLING_INTERVAL = 2;
// polling interval in seconds
private static final int POLLING_INTERVAL = 2;
// map of registrations
private final Map<Object, PollingWatchKey> map = new HashMap<>();
@ -118,19 +118,13 @@ class PollingWatchService
if (eventSet.isEmpty())
throw new IllegalArgumentException("No events to register");
// Extended modifiers may be used to specify the sensitivity level
int sensitivity = DEFAULT_POLLING_INTERVAL;
// no modifiers supported at this time
for (WatchEvent.Modifier modifier : modifiers) {
if (modifier == null)
throw new NullPointerException();
if (ExtendedOptions.SENSITIVITY_HIGH.matches(modifier)) {
sensitivity = ExtendedOptions.SENSITIVITY_HIGH.parameter();
} else if (ExtendedOptions.SENSITIVITY_MEDIUM.matches(modifier)) {
sensitivity = ExtendedOptions.SENSITIVITY_MEDIUM.parameter();
} else if (ExtendedOptions.SENSITIVITY_LOW.matches(modifier)) {
sensitivity = ExtendedOptions.SENSITIVITY_LOW.parameter();
} else {
if (!ExtendedOptions.SENSITIVITY_HIGH.matches(modifier) &&
!ExtendedOptions.SENSITIVITY_MEDIUM.matches(modifier) &&
!ExtendedOptions.SENSITIVITY_LOW.matches(modifier)) {
throw new UnsupportedOperationException("Modifier not supported");
}
}
@ -142,12 +136,11 @@ class PollingWatchService
// registration is done in privileged block as it requires the
// attributes of the entries in the directory.
try {
int value = sensitivity;
return AccessController.doPrivileged(
new PrivilegedExceptionAction<PollingWatchKey>() {
@Override
public PollingWatchKey run() throws IOException {
return doPrivilegedRegister(path, eventSet, value);
return doPrivilegedRegister(path, eventSet);
}
});
} catch (PrivilegedActionException pae) {
@ -161,8 +154,7 @@ class PollingWatchService
// registers directory returning a new key if not already registered or
// existing key if already registered
private PollingWatchKey doPrivilegedRegister(Path path,
Set<? extends WatchEvent.Kind<?>> events,
int sensitivityInSeconds)
Set<? extends WatchEvent.Kind<?>> events)
throws IOException
{
// check file is a directory and get its file key if possible
@ -191,7 +183,7 @@ class PollingWatchService
watchKey.disable();
}
}
watchKey.enable(events, sensitivityInSeconds);
watchKey.enable(events);
return watchKey;
}
@ -300,8 +292,8 @@ class PollingWatchService
valid = false;
}
// enables periodic polling
void enable(Set<? extends WatchEvent.Kind<?>> events, long period) {
// enables periodic polling with interval POLLING_INTERVAL
void enable(Set<? extends WatchEvent.Kind<?>> events) {
synchronized (this) {
// update the events
this.events = events;
@ -309,7 +301,8 @@ class PollingWatchService
// create the periodic task to poll directories
Runnable thunk = new Runnable() { public void run() { poll(); }};
this.poller = scheduledExecutor
.scheduleAtFixedRate(thunk, period, period, TimeUnit.SECONDS);
.scheduleAtFixedRate(thunk, POLLING_INTERVAL,
POLLING_INTERVAL, TimeUnit.SECONDS);
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,122 +22,53 @@
*/
/* @test
* @bug 4313887
* @bug 4313887 8303413
* @summary Sanity test for JDK-specific sensitivity level watch event modifier
* @modules jdk.unsupported
* @library .. /test/lib
* @build jdk.test.lib.Platform
* @build jdk.test.lib.RandomFactory
* @run main/timeout=240 SensitivityModifier
* @key randomness
*/
import java.nio.file.Files;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import static java.nio.file.StandardWatchEventKinds.*;
import java.io.OutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import com.sun.nio.file.SensitivityWatchEventModifier;
import jdk.test.lib.Platform;
import jdk.test.lib.RandomFactory;
public class SensitivityModifier {
// on macOS and other platforms, watch services might be based on polling
// requiring a longer timeout to detect events before returning
static final long POLL_TIMEOUT_SECONDS =
Platform.isLinux() || Platform.isWindows() ? 1 : 2;
static final Random RAND = RandomFactory.getRandom();
static final Map<Path,Integer> pathToTime = new HashMap<>();
static void register(Path[] dirs, WatchService watcher) throws IOException {
pathToTime.clear();
static WatchKey register(Path dir, WatchService watcher)
throws IOException {
SensitivityWatchEventModifier[] sensitivities =
SensitivityWatchEventModifier.values();
for (int i=0; i<dirs.length; i++) {
SensitivityWatchEventModifier sensitivity =
sensitivities[RAND.nextInt(sensitivities.length)];
Path dir = dirs[i];
dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY },
sensitivity);
pathToTime.put(dir, sensitivity.sensitivityValueInSeconds());
}
SensitivityWatchEventModifier sensitivity =
sensitivities[RAND.nextInt(sensitivities.length)];
return dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY },
sensitivity);
}
@SuppressWarnings("unchecked")
static void doTest(Path top) throws Exception {
FileSystem fs = top.getFileSystem();
static void doTest(Path dir) throws Exception {
FileSystem fs = dir.getFileSystem();
try (WatchService watcher = fs.newWatchService()) {
// register the directory (random sensitivity)
WatchKey key = register(dir, watcher);
// create directories and files
int nDirs = 5 + RAND.nextInt(20);
int nFiles = 50 + RAND.nextInt(50);
Path[] dirs = new Path[nDirs];
Path[] files = new Path[nFiles];
for (int i=0; i<nDirs; i++) {
dirs[i] = Files.createDirectory(top.resolve("dir" + i));
}
for (int i=0; i<nFiles; i++) {
Path dir = dirs[RAND.nextInt(nDirs)];
files[i] = Files.createFile(dir.resolve("file" + i));
}
// check validity
if (!key.isValid())
throw new RuntimeException("Registration is invalid");
// register the directories (random sensitivity)
register(dirs, watcher);
// sleep a bit here to ensure that modification to the first file
// can be detected by polling implementations (ie: last modified time
// may not change otherwise).
try { Thread.sleep(1000); } catch (InterruptedException e) { }
// modify files and check that events are received
for (int i=0; i<10; i++) {
Path file = files[RAND.nextInt(nFiles)];
System.out.println("Modify: " + file);
try (OutputStream out = Files.newOutputStream(file)) {
out.write(new byte[100]);
}
System.out.println("Waiting for event(s)...");
boolean eventReceived = false;
WatchKey key = watcher.take();
do {
for (WatchEvent<?> event: key.pollEvents()) {
if (event.kind() != ENTRY_MODIFY)
throw new RuntimeException("Unexpected event: " + event);
Path name = ((WatchEvent<Path>)event).context();
if (name.equals(file.getFileName())) {
eventReceived = true;
break;
}
}
key.reset();
key = watcher.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} while (key != null);
// we should have received at least one ENTRY_MODIFY event
if (eventReceived) {
System.out.println("Event OK");
} else {
Path parent = file.getParent();
String msg = String.format("No ENTRY_MODIFY event received for %s (dir: %s, sensitivity: %d)",
file, parent, pathToTime.get(parent));
throw new RuntimeException(msg);
}
// re-register the directories to force changing their sensitivity
// level
register(dirs, watcher);
}
// cancel the registration
key.cancel();
}
}