mirror of
https://github.com/Qortal/qortal.git
synced 2025-05-10 11:47:51 +00:00
Avoid duplicate concurrent QDN builds.
This commit is contained in:
parent
21d1750779
commit
c682fa89fd
@ -34,6 +34,9 @@ import java.security.InvalidAlgorithmParameterException;
|
|||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class ArbitraryDataReader {
|
public class ArbitraryDataReader {
|
||||||
|
|
||||||
@ -59,6 +62,10 @@ public class ArbitraryDataReader {
|
|||||||
// The resource being read
|
// The resource being read
|
||||||
ArbitraryDataResource arbitraryDataResource = null;
|
ArbitraryDataResource arbitraryDataResource = null;
|
||||||
|
|
||||||
|
// Track resources that are currently being loaded, to avoid duplicate concurrent builds
|
||||||
|
// TODO: all builds could be handled by the build queue (even synchronous ones), to avoid the need for this
|
||||||
|
private static Map<String, Long> inProgress = Collections.synchronizedMap(new HashMap<>());
|
||||||
|
|
||||||
public ArbitraryDataReader(String resourceId, ResourceIdType resourceIdType, Service service, String identifier) {
|
public ArbitraryDataReader(String resourceId, ResourceIdType resourceIdType, Service service, String identifier) {
|
||||||
// Ensure names are always lowercase
|
// Ensure names are always lowercase
|
||||||
if (resourceIdType == ResourceIdType.NAME) {
|
if (resourceIdType == ResourceIdType.NAME) {
|
||||||
@ -166,6 +173,12 @@ public class ArbitraryDataReader {
|
|||||||
|
|
||||||
this.arbitraryDataResource = this.createArbitraryDataResource();
|
this.arbitraryDataResource = this.createArbitraryDataResource();
|
||||||
|
|
||||||
|
// Don't allow duplicate loads
|
||||||
|
if (!this.canStartLoading()) {
|
||||||
|
LOGGER.debug("Skipping duplicate load of {}", this.arbitraryDataResource);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.preExecute();
|
this.preExecute();
|
||||||
this.deleteExistingFiles();
|
this.deleteExistingFiles();
|
||||||
this.fetch();
|
this.fetch();
|
||||||
@ -193,6 +206,7 @@ public class ArbitraryDataReader {
|
|||||||
|
|
||||||
private void preExecute() throws DataException {
|
private void preExecute() throws DataException {
|
||||||
ArbitraryDataBuildManager.getInstance().setBuildInProgress(true);
|
ArbitraryDataBuildManager.getInstance().setBuildInProgress(true);
|
||||||
|
|
||||||
this.checkEnabled();
|
this.checkEnabled();
|
||||||
this.createWorkingDirectory();
|
this.createWorkingDirectory();
|
||||||
this.createUncompressedDirectory();
|
this.createUncompressedDirectory();
|
||||||
@ -200,6 +214,9 @@ public class ArbitraryDataReader {
|
|||||||
|
|
||||||
private void postExecute() {
|
private void postExecute() {
|
||||||
ArbitraryDataBuildManager.getInstance().setBuildInProgress(false);
|
ArbitraryDataBuildManager.getInstance().setBuildInProgress(false);
|
||||||
|
|
||||||
|
this.arbitraryDataResource = this.createArbitraryDataResource();
|
||||||
|
ArbitraryDataReader.inProgress.remove(this.arbitraryDataResource.getUniqueKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkEnabled() throws DataException {
|
private void checkEnabled() throws DataException {
|
||||||
@ -208,6 +225,17 @@ public class ArbitraryDataReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean canStartLoading() {
|
||||||
|
// Avoid duplicate builds if we're already loading this resource
|
||||||
|
String uniqueKey = this.arbitraryDataResource.getUniqueKey();
|
||||||
|
if (ArbitraryDataReader.inProgress.containsKey(uniqueKey)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ArbitraryDataReader.inProgress.put(uniqueKey, NTP.getTime());
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private void createWorkingDirectory() throws DataException {
|
private void createWorkingDirectory() throws DataException {
|
||||||
try {
|
try {
|
||||||
Files.createDirectories(this.workingPath);
|
Files.createDirectories(this.workingPath);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user