Bug fixing LoadingMapOfDetachedVRE

This commit is contained in:
Francesco Mangiacrapa 2020-06-10 14:46:02 +02:00
parent c4ff6be857
commit d96186d0e9
6 changed files with 81 additions and 75 deletions

View File

@ -3,7 +3,7 @@
*/ */
package org.gcube.datatransfer.resolver.caches; package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.ConcurrentMap; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -23,15 +23,16 @@ import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener; import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification; import com.google.common.cache.RemovalNotification;
/** /**
* The Class LoadingMapOfDetachedVRE. * The Class LoadingMapOfDetachedVRE.
* *
* @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy) * @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy)
* Jun 8, 2020 * Jun 10, 2020
*/ */
public class LoadingMapOfDetachedVRE { public class LoadingMapOfDetachedVRE {
private static Logger logger = LoggerFactory.getLogger(LoadingMapOfDetachedVRE.class); private static Logger LOG = LoggerFactory.getLogger(LoadingMapOfDetachedVRE.class);
private static LoadingCache<String, VRE> vreNameToVRE; private static LoadingCache<String, VRE> vreNameToVRE;
static{ static{
@ -42,9 +43,14 @@ public class LoadingMapOfDetachedVRE {
public VRE load(String vreName) public VRE load(String vreName)
throws Exception { throws Exception {
logger.info("Loading the cache for VRE name: {}",vreName); LOG.info("Loading the cache for VRE name: {}",vreName);
VRE theVRE = loadFullScopeforScopeName(vreName); VRE theVRE = loadVREObjForVREName(vreName);
logger.info("Returning {} with scope {} for the VRE name: {}", VRE.class.getSimpleName(), theVRE.getScope(), vreName); if(theVRE!=null)
LOG.info("Returning {} with scope {} for the VRE name: {}", VRE.class.getSimpleName(), theVRE.getScope(), vreName);
else {
LOG.info("No VRE obj for VRE name {}",vreName);
}
return theVRE; return theVRE;
} }
@ -55,20 +61,18 @@ public class LoadingMapOfDetachedVRE {
@Override @Override
public void onRemoval(RemovalNotification<String, VRE> arg0) { public void onRemoval(RemovalNotification<String, VRE> arg0) {
logger.info("cache expired"); LOG.info("cache expired");
//prePopulateCache();
} }
}; };
vreNameToVRE = CacheBuilder.newBuilder().maximumSize(300).expireAfterWrite( vreNameToVRE = CacheBuilder.newBuilder().maximumSize(300).expireAfterWrite(
1, TimeUnit.DAYS).removalListener(removalListener). 12, TimeUnit.HOURS).removalListener(removalListener).
build(loader); build(loader);
//Populating the cache at init stage //Populating the cache at init stage
populateTheCache(); populateTheCache();
logger.info("Pre-Loaded detached VRE Name to VRE Obj cache with: "+vreNameToVRE.asMap().size()+" item/s"); LOG.info("Pre-Loaded detached VRE Name to VRE Obj cache with: "+vreNameToVRE.asMap().size()+" item/s");
} }
@ -76,22 +80,23 @@ public class LoadingMapOfDetachedVRE {
* Populate the cache. * Populate the cache.
*/ */
private static void populateTheCache(){ private static void populateTheCache(){
try{ try{
//POPULATE THE CACHE READING THE RESOURCE "CATALOGUE-RESOLVER" //Populating the cache by using the detachedres-library
logger.info("Trying to pre-populate the detached VREs cache with mapping (VRE Name, VRE Obj)"); LOG.info("Trying to pre-populate the detached VREs cache with mapping (VRE Name, VRE Obj)");
ScopeProvider.instance.set(UriResolverSmartGearManagerInit.getRootContextScope()); ScopeProvider.instance.set(UriResolverSmartGearManagerInit.getRootContextScope());
DetachedREsClient detachedREsClient = new DetachedREsClient(); DetachedREsClient detachedREsClient = new DetachedREsClient();
DetachedREs detachedREs = detachedREsClient.getDetachedREs(); DetachedREs detachedREs = detachedREsClient.getDetachedREs();
int totalVREDimissed = 0; int totalVREDimissed = 0;
for (Gateway gateway : detachedREs.getGateways().values()) { for (Gateway gateway : detachedREs.getGateways().values()) {
logger.trace("Gateway: " + gateway.getName()); LOG.trace("Gateway: " + gateway.getName());
int vreDismissedPerGatew = 0; int vreDismissedPerGatew = 0;
for (VO vo : gateway.getVos().values()) { for (VO vo : gateway.getVos().values()) {
logger.trace("VO: " + vo.getName()); LOG.trace("VO: " + vo.getName());
for (VRE vre : vo.getVres().values()) { for (VRE vre : vo.getVres().values()) {
if(logger.isTraceEnabled()) { if(LOG.isTraceEnabled()) {
logger.trace("VRE name: " + vre.getName() + LOG.trace("VRE name: " + vre.getName() +
" scope: "+vre.getScope() + " scope: "+vre.getScope() +
" VRE catalogue url: " + vre.getCatalogUrl() + " VRE catalogue url: " + vre.getCatalogUrl() +
" VRE catalog Portlet URL: "+vre.getCatalogPortletURL()); " VRE catalog Portlet URL: "+vre.getCatalogPortletURL());
@ -99,28 +104,30 @@ public class LoadingMapOfDetachedVRE {
try { try {
if(vre.getScope()!=null && !vre.getScope().isEmpty()) { if(vre.getScope()!=null && !vre.getScope().isEmpty()) {
String vreName = vre.getScope().split("/")[3]; String vreName = vre.getScope().split("/")[3];
vreNameToVRE.put(vreName, vre); vreNameToVRE.asMap().put(vreName, vre);
vreDismissedPerGatew++; vreDismissedPerGatew++;
} }
}catch (Exception e) { }catch (Exception e) {
logger.warn("Error on parsing the scope: "+vre.getScope()+ " skipping it"); LOG.warn("Error on parsing the scope: "+vre.getScope()+ " skipping it");
} }
} }
} }
logger.debug("\nVREs dismissed loaded and cached per " + gateway.getName() + " are: "+vreDismissedPerGatew); LOG.debug("VREs dismissed loaded and cached per " + gateway.getName() + " are: "+vreDismissedPerGatew);
totalVREDimissed+=vreDismissedPerGatew; totalVREDimissed+=vreDismissedPerGatew;
} }
logger.debug("\nTotal detached VREs are: "+totalVREDimissed); LOG.debug("Total detached VREs are: "+totalVREDimissed);
logger.info("Cache populated with: "); LOG.info("Cache populated with: ");
ConcurrentMap<String, VRE> mapOfVreNames = vreNameToVRE.asMap(); Map<String, VRE> mapOfVreNames = vreNameToVRE.asMap();
for (String key : mapOfVreNames.keySet()) { for (String key : mapOfVreNames.keySet()) {
VRE theDetachedVRE = mapOfVreNames.get(key); VRE theDetachedVRE = mapOfVreNames.get(key);
logger.info("detached VRE with scope {}, catalogueURL {}, cataloguePortletURL {}", LOG.info("VRE name {}, VRE obj with scope {}, catalogueURL {}, cataloguePortletURL {}",
theDetachedVRE.getScope(), theDetachedVRE.getCatalogUrl(), key,
theDetachedVRE.getScope(),
theDetachedVRE.getCatalogUrl(),
theDetachedVRE.getCatalogPortletURL()); theDetachedVRE.getCatalogPortletURL());
} }
@ -132,8 +139,9 @@ public class LoadingMapOfDetachedVRE {
} }
/** /**
* Gets the. * Gets the VRE obj for input VRE name.
* *
* @param vreName the vre name * @param vreName the vre name
* @return the vre * @return the vre
@ -141,35 +149,25 @@ public class LoadingMapOfDetachedVRE {
*/ */
public static VRE get(String vreName) throws ExecutionException{ public static VRE get(String vreName) throws ExecutionException{
boolean cacheNotLoaded = true; try {
final int MAX_ATTEMPTS = 3; return vreNameToVRE.get(vreName);
int retry = 1; }catch (Exception e) {
while (cacheNotLoaded) { LOG.info("Error on getting VRE obj for vreName {}. Is the key {} not found in the cache?", vreName, vreName);
cacheNotLoaded = vreNameToVRE == null; throw e;
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. * Load VRE obj for VRE name.
* *
* @param vreName the scope name * @param vreName the vre name
* @return the scope bean * @return the vre
*/ */
protected static VRE loadFullScopeforScopeName(String vreName){ protected static VRE loadVREObjForVREName(String vreName){
//THIS CHECK SHOULD BE NOT NEEDED
VRE theVRE = vreNameToVRE.getIfPresent(vreName); VRE theVRE = vreNameToVRE.getIfPresent(vreName);
//THIS CHECK SHOULD NOT BE NEEDED
if(theVRE==null){ if(theVRE==null){
populateTheCache(); populateTheCache();
theVRE = vreNameToVRE.getIfPresent(vreName); theVRE = vreNameToVRE.getIfPresent(vreName);

View File

@ -86,13 +86,6 @@ public class LoadingMapOfScopeCache {
} }
logger.info("Cache populated with: "+scopeNamesToFullScopes.asMap().toString()); logger.info("Cache populated with: "+scopeNamesToFullScopes.asMap().toString());
//logger.info("Pre-Loaded CatalogueApplicationProfiles cache is: "+catalogueApplicationProfiles.asMap().toString());
// if(UriResolverSmartGearManagerInit.getRootContextScope().compareTo("/gcube")==0){
// logger.warn("HARD-CABLING PARTHENOS_Registry scope to resolve PARTHENOS_REGISTRY Links in dev environment");
// scopeNamesToFullScopes.asMap().put("PARTHENOS_Registry", "/d4science.research-infrastructures.eu/ParthenosVO/PARTHENOS_Registry");
// }
}catch(Exception e){ }catch(Exception e){
//SILENT //SILENT
@ -123,9 +116,9 @@ public class LoadingMapOfScopeCache {
*/ */
protected static ScopeBean loadFullScopeforScopeName(String scopeName){ protected static ScopeBean loadFullScopeforScopeName(String scopeName){
//THIS CHECK SHOULD BE NOT NEEDED
ScopeBean fullScope = scopeNamesToFullScopes.getIfPresent(scopeName); ScopeBean fullScope = scopeNamesToFullScopes.getIfPresent(scopeName);
//THIS CHECK SHOULD NOT BE NEEDED
if(fullScope==null){ if(fullScope==null){
populateTheCache(); populateTheCache();
fullScope = scopeNamesToFullScopes.getIfPresent(scopeName); fullScope = scopeNamesToFullScopes.getIfPresent(scopeName);

View File

@ -17,6 +17,7 @@ import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type; import org.gcube.common.scope.impl.ScopeBean.Type;
import org.gcube.datatransfer.resolver.caches.LoadingGeonetworkInstanceCache; import org.gcube.datatransfer.resolver.caches.LoadingGeonetworkInstanceCache;
import org.gcube.datatransfer.resolver.caches.LoadingGisViewerApplicationURLCache; import org.gcube.datatransfer.resolver.caches.LoadingGisViewerApplicationURLCache;
import org.gcube.datatransfer.resolver.caches.LoadingMapOfDetachedVRE;
import org.gcube.datatransfer.resolver.caches.LoadingMapOfScopeCache; import org.gcube.datatransfer.resolver.caches.LoadingMapOfScopeCache;
import org.gcube.datatransfer.resolver.gis.property.ApplicationProfilePropertyReader; import org.gcube.datatransfer.resolver.gis.property.ApplicationProfilePropertyReader;
import org.gcube.datatransfer.resolver.gis.property.PropertyFileNotFoundException; import org.gcube.datatransfer.resolver.gis.property.PropertyFileNotFoundException;
@ -94,6 +95,7 @@ public class UriResolverSmartGearManagerInit implements ApplicationManager {
new LoadingGeonetworkInstanceCache(); new LoadingGeonetworkInstanceCache();
new LoadingGisViewerApplicationURLCache(); new LoadingGisViewerApplicationURLCache();
new LoadingMapOfScopeCache(); new LoadingMapOfScopeCache();
new LoadingMapOfDetachedVRE();
} }
} }
catch (Exception e) { catch (Exception e) {

View File

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

View File

@ -3,7 +3,6 @@ import static org.gcube.resources.discovery.icclient.ICFactory.client;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@ -18,6 +17,7 @@ import org.gcube.datatransfer.resolver.init.UriResolverSmartGearManagerInit;
import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.Query; import org.gcube.resources.discovery.client.queries.api.Query;
import org.gcube.resources.discovery.client.queries.impl.QueryBox; import org.gcube.resources.discovery.client.queries.impl.QueryBox;
import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@ -38,12 +38,12 @@ public class GetAllInfrastructureScopesFromIS {
protected static final String RESOURCE_PROFILE_NAME_TEXT = "/Resource/Profile/Name/text()"; protected static final String RESOURCE_PROFILE_NAME_TEXT = "/Resource/Profile/Name/text()";
/** /**
* The main method. * Test loading map of scope.
*
* @param args the arguments
*/ */
public static void main(String[] args) { @Test
public void testLoadingMapOfScope() {
try { try {
@ -53,16 +53,16 @@ public class GetAllInfrastructureScopesFromIS {
UriResolverSmartGearManagerInit.setRootContextScope(rootScope); UriResolverSmartGearManagerInit.setRootContextScope(rootScope);
LoadingMapOfScopeCache cache = new LoadingMapOfScopeCache(); LoadingMapOfScopeCache cache = new LoadingMapOfScopeCache();
int i = 0; // int i = 0;
for (String string : cache.getCache().asMap().keySet()) { // for (String string : cache.getCache().asMap().keySet()) {
try { // try {
System.out.println(++i+") Scope Name: "+string + " to full scope: "+LoadingMapOfScopeCache.get(string)); // System.out.println(++i+") Scope Name: "+string + " to full scope: "+LoadingMapOfScopeCache.get(string));
} catch (ExecutionException e) { // } catch (ExecutionException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
String scopeName = "devsec"; String scopeName = "abc";
ScopeBean scopeBean = LoadingMapOfScopeCache.get(scopeName); ScopeBean scopeBean = LoadingMapOfScopeCache.get(scopeName);
String fullScope = scopeBean.toString(); String fullScope = scopeBean.toString();
logger.info("Read fullScope: "+fullScope + " for SCOPE name: "+scopeName +" from cache created by: "+GetAllInfrastructureScopes.class.getSimpleName()); logger.info("Read fullScope: "+fullScope + " for SCOPE name: "+scopeName +" from cache created by: "+GetAllInfrastructureScopes.class.getSimpleName());

View File

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