Created the LoadingMapOfDetachedVRE

This commit is contained in:
Francesco Mangiacrapa 2020-06-08 17:18:13 +02:00
parent 5c36c21e1d
commit c4ff6be857
7 changed files with 235 additions and 4 deletions

View File

@ -4,6 +4,9 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="detachedres-library-1.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/detachedres-library/detachedres-library">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="uri-resolver"/>
<property name="java-output-path" value="/uri-resolver/target/classes"/>
</wb-module>

View File

@ -9,7 +9,7 @@
</parent>
<groupId>org.gcube.data.transfer</groupId>
<artifactId>uri-resolver</artifactId>
<version>2.3.2</version>
<version>2.4.0-SNAPSHOT</version>
<packaging>war</packaging>
<description>The URI Resolver is an HTTP URI resolver implemented as an REST service which gives access trough HTTP to different gcube Resolvers and gCube Applications.</description>
@ -85,6 +85,12 @@
<version>[0.0.1,1.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.infrastructure.detachedres</groupId>
<artifactId>detachedres-library</artifactId>
<version>[1.0.0,2.0.0-SNAPSHOT)</version>
</dependency>
<!-- TODO REMOVE THIS IMPORT -->
<dependency>
<groupId>org.gcube.common</groupId>

View File

@ -0,0 +1,191 @@
/**
*
*/
package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.init.UriResolverSmartGearManagerInit;
import org.gcube.infrastructure.detachedres.detachedreslibrary.server.DetachedREsClient;
import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.DetachedREs;
import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.Gateway;
import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.VO;
import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.VRE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
/**
* The Class LoadingMapOfDetachedVRE.
*
* @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy)
* Jun 8, 2020
*/
public class LoadingMapOfDetachedVRE {
private static Logger logger = LoggerFactory.getLogger(LoadingMapOfDetachedVRE.class);
private static LoadingCache<String, VRE> vreNameToVRE;
static{
CacheLoader<String, VRE> loader = new CacheLoader<String, VRE>(){
@Override
public VRE load(String vreName)
throws Exception {
logger.info("Loading the cache for VRE name: {}",vreName);
VRE theVRE = loadFullScopeforScopeName(vreName);
logger.info("Returning {} with scope {} for the VRE name: {}", VRE.class.getSimpleName(), theVRE.getScope(), vreName);
return theVRE;
}
};
RemovalListener<String, VRE> removalListener = new RemovalListener<String, VRE>() {
@Override
public void onRemoval(RemovalNotification<String, VRE> arg0) {
logger.info("cache expired");
//prePopulateCache();
}
};
vreNameToVRE = CacheBuilder.newBuilder().maximumSize(300).expireAfterWrite(
1, TimeUnit.DAYS).removalListener(removalListener).
build(loader);
//Populating the cache at init stage
populateTheCache();
logger.info("Pre-Loaded detached VRE Name to VRE Obj cache with: "+vreNameToVRE.asMap().size()+" item/s");
}
/**
* Populate the cache.
*/
private static void populateTheCache(){
try{
//POPULATE THE CACHE READING THE RESOURCE "CATALOGUE-RESOLVER"
logger.info("Trying to pre-populate the detached VREs cache with mapping (VRE Name, VRE Obj)");
ScopeProvider.instance.set(UriResolverSmartGearManagerInit.getRootContextScope());
DetachedREsClient detachedREsClient = new DetachedREsClient();
DetachedREs detachedREs = detachedREsClient.getDetachedREs();
int totalVREDimissed = 0;
for (Gateway gateway : detachedREs.getGateways().values()) {
logger.trace("Gateway: " + gateway.getName());
int vreDismissedPerGatew = 0;
for (VO vo : gateway.getVos().values()) {
logger.trace("VO: " + vo.getName());
for (VRE vre : vo.getVres().values()) {
if(logger.isTraceEnabled()) {
logger.trace("VRE name: " + vre.getName() +
" scope: "+vre.getScope() +
" VRE catalogue url: " + vre.getCatalogUrl() +
" VRE catalog Portlet URL: "+vre.getCatalogPortletURL());
}
try {
if(vre.getScope()!=null && !vre.getScope().isEmpty()) {
String vreName = vre.getScope().split("/")[3];
vreNameToVRE.put(vreName, vre);
vreDismissedPerGatew++;
}
}catch (Exception e) {
logger.warn("Error on parsing the scope: "+vre.getScope()+ " skipping it");
}
}
}
logger.debug("\nVREs dismissed loaded and cached per " + gateway.getName() + " are: "+vreDismissedPerGatew);
totalVREDimissed+=vreDismissedPerGatew;
}
logger.debug("\nTotal detached VREs are: "+totalVREDimissed);
logger.info("Cache populated with: ");
ConcurrentMap<String, VRE> mapOfVreNames = vreNameToVRE.asMap();
for (String key : mapOfVreNames.keySet()) {
VRE theDetachedVRE = mapOfVreNames.get(key);
logger.info("detached VRE with scope {}, catalogueURL {}, cataloguePortletURL {}",
theDetachedVRE.getScope(), theDetachedVRE.getCatalogUrl(),
theDetachedVRE.getCatalogPortletURL());
}
}catch(Exception e){
//SILENT
}finally{
}
}
/**
* Gets the.
*
* @param vreName the vre name
* @return the vre
* @throws ExecutionException the execution exception
*/
public static VRE get(String vreName) throws ExecutionException{
boolean cacheNotLoaded = true;
final int MAX_ATTEMPTS = 3;
int retry = 1;
while (cacheNotLoaded) {
cacheNotLoaded = vreNameToVRE == null;
if(retry<MAX_ATTEMPTS) {
try {
Thread.sleep(retry * 1000);
} catch (InterruptedException e) {
}
}
retry++;
logger.debug("is cache loaded? {}",!cacheNotLoaded);
}
return vreNameToVRE.get(vreName);
}
/**
* Load full scopefor scope name.
*
* @param vreName the scope name
* @return the scope bean
*/
protected static VRE loadFullScopeforScopeName(String vreName){
//THIS CHECK SHOULD BE NOT NEEDED
VRE theVRE = vreNameToVRE.getIfPresent(vreName);
if(theVRE==null){
populateTheCache();
theVRE = vreNameToVRE.getIfPresent(vreName);
}
return theVRE;
}
/**
* Gets the cache.
*
* @return the cache
*/
public LoadingCache<String, VRE> getCache(){
return vreNameToVRE;
}
}

View File

@ -1,5 +1,4 @@
import org.gcube.datatransfer.resolver.catalogue.resource.CatalogueStaticConfigurations;
import org.junit.Test;
public class CatalogueNameExtractor {

View File

@ -0,0 +1,33 @@
import java.util.concurrent.ExecutionException;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.datatransfer.resolver.caches.LoadingMapOfDetachedVRE;
import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.VRE;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DetachedVREs {
public static Logger LOG = LoggerFactory.getLogger(DetachedVREs.class);
public static String TOKEN = "0e2c7963-8d3e-4ea6-a56d-ffda530dd0fa-98187548";
/**
* Storage hub test.
*
* @throws Exception the exception
*/
@Test
public void testCacheOfDetachedVREs() throws Exception{
try {
SecurityTokenProvider.instance.set(TOKEN);
VRE theDetachedVRE = LoadingMapOfDetachedVRE.get("NextNext");
LOG.info("Detached VRE found {}", theDetachedVRE);
}catch (ExecutionException e) {
e.printStackTrace();
}
}
}

View File

@ -1,6 +1,5 @@
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.gis.entity.GisLayerItem;
import org.gcube.datatransfer.resolver.gis.entity.ServerParameters;
import org.gcube.datatransfer.resolver.services.GisResolver;
/**
@ -14,7 +13,7 @@ import org.gcube.datatransfer.resolver.services.GisResolver;
public class GisResolverTest {
static String scope = "/d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab";
static String gisUUID = "6b99efdf-2202-4b6f-aaa3-7e10e0bf09f4";
static String gisUUID = "";
public static void main(String[] args) {
GisResolver gisResolver = new GisResolver();
ScopeProvider.instance.set(scope);