Skip to content

Commit

Permalink
lib: use StampedLock, and fix check in GenericRestWSServer, #TASK-6565
Browse files Browse the repository at this point in the history
On branch TASK-6565
Changes to be committed:
	modified:   cellbase-lib/src/main/java/org/opencb/cellbase/lib/impl/core/DataReleaseSingleton.java
	modified:   cellbase-server/src/main/java/org/opencb/cellbase/server/rest/GenericRestWSServer.java
  • Loading branch information
jtarraga committed Jul 19, 2024
1 parent c8ebf7f commit 48184f7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.StampedLock;

public final class DataReleaseSingleton {

Expand All @@ -38,7 +38,7 @@ public final class DataReleaseSingleton {
// {key = dbname, value = assembly}
private Map<String, String> assemblyMap = new HashMap<>();
// {key = dbname, value = lock}
private Map<String, ReentrantReadWriteLock> rwLockMap = new HashMap<>();
private Map<String, StampedLock> stampedLockMap = new HashMap<>();

// {key = dbname, value = { key = release, value = { key = data, value = collection } } }
private Map<String, Map<Integer, Map<String, MongoDBCollection>>> cachedData = new HashMap<>();
Expand All @@ -64,7 +64,7 @@ private DataReleaseSingleton(CellBaseManagerFactory managerFactory) {
// manager from the species and the assembly
speciesMap.put(databaseName, vertebrate.getId());
assemblyMap.put(databaseName, assembly.getName());
rwLockMap.put(databaseName, new ReentrantReadWriteLock());
stampedLockMap.put(databaseName, new StampedLock());
cachedData.put(databaseName, new HashMap<>());
}
}
Expand All @@ -89,6 +89,7 @@ public static DataReleaseSingleton getInstance() {
private void loadData(String dbname) throws CellBaseException {
String species = speciesMap.get(dbname);
String assembly = assemblyMap.get(dbname);
// We need the release adaptor to get the collection name
ReleaseMongoDBAdaptor releaseMongoDBAdaptor = managerFactory.getDataReleaseManager(species, assembly).getReleaseDBAdaptor();
List<DataRelease> dataReleases = releaseMongoDBAdaptor.getAll().getResults();
if (CollectionUtils.isNotEmpty(dataReleases)) {
Expand Down Expand Up @@ -118,16 +119,60 @@ public void checkDataRelease(String dbname, int release, String data) throws Cel
String msg = UNKOWN_DATABASE_MSG_PREFIX + dbname;
throw new CellBaseException(msg);
}
rwLockMap.get(dbname).writeLock().lock();
long stamp = stampedLockMap.get(dbname).readLock();
try {
if (!cachedData.containsKey(dbname) || !cachedData.containsKey(release)
|| (StringUtils.isNotEmpty(data) && !cachedData.get(release).containsKey(data))) {
loadData(dbname);
if (isCacheOutdated(dbname, release, data)) {
long writeStamp = stampedLockMap.get(dbname).tryConvertToWriteLock(stamp);
if (writeStamp != 0L) {
// Successfully converted to write lock
stamp = writeStamp;
if (isCacheOutdated(dbname, release, data)) {
loadData(dbname);
checkAfterLoading(dbname, release, data);
}
} else {
// Could not convert to write lock, acquire it explicitly
stampedLockMap.get(dbname).unlockRead(stamp);
writeStamp = stampedLockMap.get(dbname).writeLock();
try {
if (isCacheOutdated(dbname, release, data)) {
loadData(dbname);
checkAfterLoading(dbname, release, data);
}
} finally {
stampedLockMap.get(dbname).unlockWrite(writeStamp);
}
}
}
} finally {
rwLockMap.get(dbname).writeLock().unlock();
stampedLockMap.get(dbname).unlockRead(stamp);
}
}

// Method to get collection name based on the data and the release
public MongoDBCollection getMongoDBCollection(String dbname, String data, int release) throws CellBaseException {
checkDataRelease(dbname, release, data);
return cachedData.get(dbname).get(release).get(data);
}

private boolean isCacheOutdated(String dbname, int release, String data) {
long stamp = stampedLockMap.get(dbname).tryOptimisticRead();
boolean outdated = !cachedData.get(dbname).containsKey(release)
|| (StringUtils.isNotEmpty(data) && !cachedData.get(dbname).get(release).containsKey(data));
if (!stampedLockMap.get(dbname).validate(stamp)) {
// Fallback to read lock if optimistic read was invalidated
stamp = stampedLockMap.get(dbname).readLock();
try {
outdated = !cachedData.get(dbname).containsKey(release)
|| (StringUtils.isNotEmpty(data) && !cachedData.get(dbname).get(release).containsKey(data));
} finally {
stampedLockMap.get(dbname).unlockRead(stamp);
}
}
return outdated;
}

private void checkAfterLoading(String dbname, int release, String data) throws CellBaseException {
// Check after loading
if (!cachedData.get(dbname).containsKey(release)) {
// If the data release is invalid, throw an exception
Expand All @@ -141,10 +186,4 @@ public void checkDataRelease(String dbname, int release, String data) throws Cel
throw new CellBaseException(msg);
}
}

// Method to get collection name based on the data and the release
public MongoDBCollection getMongoDBCollection(String dbname, String data, int release) throws CellBaseException {
checkDataRelease(dbname, release, data);
return cachedData.get(dbname).get(release).get(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ protected int getDataRelease() throws CellBaseException {
if (dataRelease == 0) {
logger.info("Using data release 0 in query: using the default data release '" + defaultDataRelease.getRelease()
+ "' for CellBase version '" + version + "'");
DataReleaseSingleton.getInstance().checkDataRelease(species, assembly, version, dataRelease);
return defaultDataRelease.getRelease();
} else {
DataReleaseSingleton.getInstance().checkDataRelease(species, assembly, version, dataRelease);
return dataRelease;
}
} catch (NumberFormatException e) {
Expand Down

0 comments on commit 48184f7

Please sign in to comment.