in java/main/src/main/java/com/epam/deltix/utilities/ResourceLoader.java [802:878]
private void verifyOrDeployResources(final Path deploymentPath) throws IOException {
disposeResourceFiles();
// Check, if we can load the existing resource files
try {
if (verifyExistingResourceFiles(deploymentPath)) {
log("All files already deployed");
return;
}
} catch (final Throwable e) {
log("1st Verify call threw exception: %s", e);
throw e;
}
long startTimeNs = nanoTime();
final long startTimeNs0 = startTimeNs;
int retries = 3;
final long timeout = _retryTimeoutMs;
while (null == setFileLock(FileJanitor.tryCreateLockFile(deploymentPath))) {
final long elapsed = (nanoTime() - startTimeNs) / 1000_000; // To milliseconds
// Yes, sleep at least once regardless of how much time remaining and re-check
randomSleep(timeout - elapsed);
if (elapsed < timeout || --retries >= 0)
continue;
final long now = currentTimeMillis();
// If lockfile is updated between retryTimeout in the past and retryTimeout * 10 in the future(!!), extend timer
final long lockFileAge = now - FileJanitor.getLockFileWriteTime(deploymentPath);
if (lockFileAge < timeout && lockFileAge > -10 * timeout) {
startTimeNs = nanoTime();
retries = 3;
log("Lock timer extended");
continue;
}
final double elapsedTotal = (nanoTime() - startTimeNs0) / 1E6;
log(ERR, "Lock timer expired at: %s, elapsed: %s, lock age: %s ms", dt2str(now), elapsedTotal, lockFileAge);
throw new IOException(fmt("Unable to grab Lock file (timeout: %s ms, elapsed: %s ms, lock age: %s ms)",
timeout, elapsedTotal, lockFileAge));
}
if (logLevelLeast(DBG))
log("Lock taken: %s %s", _lockFile.path, _lockFile.channel());
try {
try {
// Check again after possible lock contention
if (verifyExistingResourceFiles(deploymentPath)) {
log("Verified files after lock");
return;
}
} catch (final Throwable e) {
log("2nd Verify call threw exception: %s", e);
throw e;
}
log("Deploying to: %s", deploymentPath);
lockFileWatchdogInit();
try {
deployResourcesInternal(deploymentPath);
} catch (final Throwable e) {
log("Deployment threw exception: %s", e);
throw e;
}
// Update watchdog one last time
lockFileWatchdogUpdate();
} catch (final Throwable e) {
log("Verify/Deploy throw/rethrow exception: %s", e);
throw e;
}
// NOTE: LockFile may still exist and will be released later by the caller
// finally{} clause that releases the lock is removed from here
}