use of java.nio.channels.FileLock in project graal by oracle.
the class NativeImageServer method getServerInstance.
@SuppressWarnings("try")
private Server getServerInstance(LinkedHashSet<Path> classpath, LinkedHashSet<Path> bootClasspath, LinkedHashSet<String> javaArgs) {
Server[] result = { null };
/* Important - Creating new servers is a machine-exclusive operation */
withFileChannel(getMachineDir().resolve("create-server.lock"), lockFileChannel -> {
try (FileLock lock = lockFileChannel(lockFileChannel)) {
/* Ensure that all dead server entries are gone before we start */
cleanupServers(false, false, true);
/* Determine how many ports are allowed to get used for build-servers */
String maxPortsStr = loadProperties(getMachineDir().resolve(machineProperties)).get(pKeyMaxPorts);
if (maxPortsStr == null || maxPortsStr.isEmpty()) {
maxPortsStr = getUserConfigProperties().get(pKeyMaxPorts);
}
int maxPorts;
if (maxPortsStr == null || maxPortsStr.isEmpty()) {
maxPorts = 2;
} else {
maxPorts = Math.max(1, Integer.parseInt(maxPortsStr));
}
/* Maximize reuse by using same VM mem-args for all server-based image builds */
String xmxValueStr = getXmxValue(maxPorts);
replaceArg(javaArgs, oXmx, xmxValueStr);
String xmsValueStr = getXmsValue();
long xmxValue = SubstrateOptionsParser.parseLong(xmxValueStr);
long xmsValue = SubstrateOptionsParser.parseLong(xmsValueStr);
if (Word.unsigned(xmsValue).aboveThan(Word.unsigned(xmxValue))) {
xmsValueStr = Long.toUnsignedString(xmxValue);
}
replaceArg(javaArgs, oXms, xmsValueStr);
Path sessionDir = getSessionDir();
String serverUID = imageServerUID(classpath, bootClasspath, javaArgs);
Path serverDir = sessionDir.resolve(serverDirPrefix + serverUID);
if (Files.isDirectory(serverDir)) {
try {
Server server = new Server(serverDir);
if (!server.isAlive()) {
throw showError("Found defunct server:" + server.getServerInfo());
}
showVerboseMessage(verboseServer, "Reuse running server: " + server);
result[0] = server;
} catch (Exception e) {
throw showError("Found corrupt ServerDir: " + serverDir, e);
}
} else {
int serverPort = acquirePortNumber(maxPorts);
if (serverPort < 0) {
/* Server limit reached */
showVerboseMessage(verboseServer, "Server limit reached -> remove least recently used server");
/* Shutdown least recently used within session */
Server victim = findVictim(getSessionDirs(false));
/* If none found also consider servers from other sessions on machine */
if (victim == null) {
victim = findVictim(getSessionDirs(true));
}
if (victim != null) {
showVerboseMessage(verboseServer, "Selected server to remove " + victim);
victim.shutdown();
serverPort = acquirePortNumber(maxPorts);
if (serverPort < 0) {
showWarning("Cannot acquire new server port despite removing " + victim);
}
} else {
showWarning("Native image server limit exceeded. Use options --server{-list,-shutdown[-all]} to fix the problem.");
}
}
if (serverPort >= 0) {
/* Instantiate new server and write properties file */
Server server = startServer(serverDir, serverPort, classpath, bootClasspath, javaArgs);
if (server != null) {
showVerboseMessage(verboseServer, "Created new server: " + server);
}
result[0] = server;
}
}
} catch (IOException e) {
throw showError("ServerInstance-creation locking failed", e);
}
});
return result[0];
}
use of java.nio.channels.FileLock in project graal by oracle.
the class NativeImageServer method lockFileChannel.
private FileLock lockFileChannel(FileChannel channel) throws IOException {
Thread lockWatcher = new Thread(() -> {
try {
Thread.sleep(TimeUnit.MINUTES.toMillis(10));
try {
showWarning("Timeout while waiting for FileChannel.lock");
/* Trigger AsynchronousCloseException in channel.lock() */
channel.close();
} catch (IOException e) {
throw showError("LockWatcher closing FileChannel of LockFile failed", e);
}
} catch (InterruptedException e) {
/* Sleep interrupted -> Lock acquired") */
}
});
lockWatcher.start();
FileLock lock = channel.lock();
lockWatcher.interrupt();
return lock;
}
use of java.nio.channels.FileLock in project graal by oracle.
the class NativeImageServer method acquirePortNumber.
@SuppressWarnings("try")
private int acquirePortNumber(int maxPorts) {
int firstPortNumber = 26681;
int[] selectedPort = { -1 };
Path machineDir = getMachineDir();
withLockDirFileChannel(machineDir, lockFileChannel -> {
try (FileLock lock = lockFileChannel(lockFileChannel)) {
Path machinePropertiesPath = machineDir.resolve(machineProperties);
TreeSet<Integer> inUseSet = new TreeSet<>();
Properties mp = new Properties();
if (Files.isReadable(machinePropertiesPath)) {
try (InputStream is = Files.newInputStream(machinePropertiesPath)) {
mp.load(is);
String portInUseValue = mp.getProperty(pKeyPortsInUse);
if (portInUseValue != null && !portInUseValue.isEmpty()) {
inUseSet.addAll(Arrays.stream(portInUseValue.split(" ")).map(Integer::parseInt).collect(Collectors.toList()));
}
}
}
for (int candidatePort = firstPortNumber; candidatePort < firstPortNumber + maxPorts; candidatePort++) {
if (!inUseSet.contains(candidatePort)) {
selectedPort[0] = candidatePort;
inUseSet.add(selectedPort[0]);
break;
}
}
mp.setProperty(pKeyPortsInUse, inUseSet.stream().map(String::valueOf).collect(Collectors.joining(" ")));
try (OutputStream os = Files.newOutputStream(machinePropertiesPath)) {
mp.store(os, "");
}
} catch (IOException e) {
throw showError("Acquiring new server port number locking failed", e);
}
});
if (selectedPort[0] < firstPortNumber) {
showVerboseMessage(verboseServer, "Acquired new server port number failed");
}
return selectedPort[0];
}
use of java.nio.channels.FileLock in project h2database by h2database.
the class TestFileSystem method testSimpleExpandTruncateSize.
private void testSimpleExpandTruncateSize() throws Exception {
String f = "memFS:" + getBaseDir() + "/fs/test.data";
FileUtils.createDirectories("memFS:" + getBaseDir() + "/fs");
FileChannel c = FileUtils.open(f, "rw");
c.position(4000);
c.write(ByteBuffer.wrap(new byte[1]));
FileLock lock = c.tryLock();
c.truncate(0);
if (lock != null) {
lock.release();
}
c.close();
FileUtils.deleteRecursive("memFS:", false);
}
use of java.nio.channels.FileLock in project h2database by h2database.
the class FileRetryOnInterrupt method tryLock.
@Override
public synchronized FileLock tryLock(long position, long size, boolean shared) throws IOException {
FileLock l = channel.tryLock(position, size, shared);
if (l == null) {
return null;
}
lock = new FileLockRetry(l, this);
return lock;
}
Aggregations