diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/ImplementationProvider.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/ImplementationProvider.java index ae629f6..3f62f97 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/ImplementationProvider.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/ImplementationProvider.java @@ -20,11 +20,7 @@ public class ImplementationProvider { return instance; } - - @Getter - @Setter - private MongoConnectionProvider mongoConnectionProvider=new MongoConnectionProvider(); - + @Getter @Setter private MongoClientProvider mongoClientProvider=new MongoClientProvider(); @@ -38,6 +34,9 @@ public class ImplementationProvider { @Setter private PostgisConnectionProvider dbProvider=new PostgisConnectionProvider(); + @Getter + @Setter + private ISProvider isProvider=new ISProvider(); // @Getter // @Setter @@ -51,13 +50,13 @@ public class ImplementationProvider { public void shutdown() { // Stop JPA // AbstractRecordManager.shutdown(); - mongoConnectionProvider.shutdown(); + mongoClientProvider.shutdown(); } public void startup() { // AbstractRecordManager.setDefaultProvider(emfProvider); - mongoConnectionProvider.init(); + mongoClientProvider.init(); } } diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManager.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManager.java index d5e0c61..a444499 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManager.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManager.java @@ -37,14 +37,12 @@ public abstract class MongoManager { client=ImplementationProvider.get().getMongoClientProvider().getObject(); log.info("Got Mongo Client at "+client.getConnectPoint()); + log.debug("Mongo client is "+client); // NOT AUTHORIZED // log.debug("Existing databases "+client.getDatabaseNames()); } -// private abstract MongoDatabase getDatabase() { -// return client.getDatabase("gna-db"); -// } - + // TODO check if existing DB protected abstract MongoDatabase getDatabase(); diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/postgis/PostgisDBManager.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/postgis/PostgisDBManager.java index ed67b0b..571c84e 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/postgis/PostgisDBManager.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/postgis/PostgisDBManager.java @@ -4,6 +4,7 @@ import lombok.Synchronized; import lombok.extern.slf4j.Slf4j; import org.gcube.application.geoportal.common.model.legacy.BBOX; import org.gcube.application.geoportal.common.model.rest.DatabaseConnection; +import org.gcube.application.geoportal.service.engine.ImplementationProvider; import org.gcube.application.geoportal.service.model.internal.db.PostgisTable; import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException; import org.gcube.application.geoportal.service.model.internal.faults.DataParsingException; @@ -23,7 +24,7 @@ public class PostgisDBManager implements PostgisDBManagerI { // TODO GENERIC DatabaseConnection defaultConfiguration= - ISUtils.queryForDB("postgis", "Concessioni"); + ImplementationProvider.get().getIsProvider().getIS().queryForDB("postgis", "Concessioni"); log.debug("Found configuration : "+defaultConfiguration); config=defaultConfiguration; diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/AbstractScopedMap.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/AbstractScopedMap.java index 4d4cdae..d675162 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/AbstractScopedMap.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/AbstractScopedMap.java @@ -47,7 +47,7 @@ public abstract class AbstractScopedMap implements Engine{ found=scopeMap.get(currentScope); } }else {log.debug(name+" : TTL is null, never disposing..");} - log.debug("Returning {} ",found); + log.debug(name+"Returning {} ",found); return found.getTheObject(); } diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/ISProvider.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/ISProvider.java new file mode 100644 index 0000000..4ed7701 --- /dev/null +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/ISProvider.java @@ -0,0 +1,10 @@ +package org.gcube.application.geoportal.service.engine.providers; + +import org.gcube.application.geoportal.service.utils.ISUtils; + +public class ISProvider { + + public ISUtils getIS(){ + return new ISUtils(); + } +} diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoClientProvider.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoClientProvider.java index a8d15d5..3b61432 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoClientProvider.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoClientProvider.java @@ -5,22 +5,27 @@ import com.mongodb.MongoClientOptions; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import lombok.extern.slf4j.Slf4j; +import org.gcube.application.geoportal.service.ServiceConstants; import org.gcube.application.geoportal.service.engine.ImplementationProvider; import org.gcube.application.geoportal.service.model.internal.db.MongoConnection; import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException; +import org.gcube.application.geoportal.service.utils.ISUtils; @Slf4j public class MongoClientProvider extends AbstractScopedMap{ public MongoClientProvider() { super("MongoClient cache"); - + //NO TTL = Map by context // setTTL(Duration.of(10, ChronoUnit.MINUTES)); } @Override protected MongoClient retrieveObject() throws ConfigurationException { - MongoConnection conn=ImplementationProvider.get().getMongoConnectionProvider().getObject(); + MongoConnection conn=ImplementationProvider.get(). + getIsProvider().getIS().queryForMongoDB( + ServiceConstants.MONGO_SE_PLATFORM, ServiceConstants.MONGO_SE_GNA_FLAG); + log.debug("Connecting to "+conn); MongoCredential credential = MongoCredential.createCredential(conn.getUser(), conn.getDatabase(), @@ -28,7 +33,12 @@ public class MongoClientProvider extends AbstractScopedMap{ - MongoClientOptions options = MongoClientOptions.builder().sslEnabled(true).build(); + MongoClientOptions options = MongoClientOptions.builder(). + threadsAllowedToBlockForConnectionMultiplier(10). + connectionsPerHost(20). + applicationName("geoportal-service"). + sslEnabled(true). + build(); return new MongoClient(new ServerAddress(conn.getHosts().get(0),conn.getPort()), credential, diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoConnectionProvider.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoConnectionProvider.java deleted file mode 100644 index 9833b26..0000000 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoConnectionProvider.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.gcube.application.geoportal.service.engine.providers; - -import org.gcube.application.geoportal.service.ServiceConstants; -import org.gcube.application.geoportal.service.model.internal.db.MongoConnection; -import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException; -import org.gcube.application.geoportal.service.utils.ISUtils; - -import java.time.Duration; -import java.time.temporal.ChronoUnit; - -public class MongoConnectionProvider extends AbstractScopedMap{ - - public MongoConnectionProvider() { - super("MongoDBInfo Cache"); - setTTL(Duration.of(2,ChronoUnit.MINUTES)); - } - - @Override - protected MongoConnection retrieveObject() throws ConfigurationException { - return ISUtils.queryForMongoDB(ServiceConstants.MONGO_SE_PLATFORM, ServiceConstants.MONGO_SE_GNA_FLAG); - } - - @Override - protected void dispose(MongoConnection toDispose) { - - } - - @Override - public void init() { - // TODO Auto-generated method stub - } -} diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/utils/ISUtils.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/utils/ISUtils.java index 75e6baa..d880f92 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/utils/ISUtils.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/utils/ISUtils.java @@ -19,9 +19,17 @@ import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; public class ISUtils { - public static DatabaseConnection queryForDB(String platform, String flag) throws ConfigurationException { + public DatabaseConnection queryForDB(String platform, String flag) throws ConfigurationException { + return ISUtils.performQueryForDB(platform,flag); + } - List found=getAP(platform, flag); +// public List GetAP(String platform,String flag) { +// return performGetAP(platform,flag); +// } + + private static DatabaseConnection performQueryForDB(String platform, String flag) throws ConfigurationException { + + List found=performGetAP(platform, flag); if(found.size()>1) { throw new ConfigurationException("Multiple SE found ["+found.size()+"] for platform : "+platform+" flag : "+flag); }else if (found.isEmpty()){ @@ -38,7 +46,7 @@ public class ISUtils { } - public static List getAP(String platform,String flag) { + private static List performGetAP(String platform,String flag) { SimpleQuery query = queryFor(ServiceEndpoint.class); query.addCondition("$resource/Profile/Category/text() eq '"+ServiceConstants.SE_GNA_DB_CATEGORY+"'") @@ -51,9 +59,13 @@ public class ISUtils { return client.submit(query); } - public static MongoConnection queryForMongoDB(String platform,String flag) throws ConfigurationException { + public MongoConnection queryForMongoDB(String platform,String flag) throws ConfigurationException { + return performQueryForMongoDB(platform,flag); + } - List found=getAP(platform, flag); + private static MongoConnection performQueryForMongoDB(String platform,String flag) throws ConfigurationException { + + List found=performGetAP(platform, flag); if(found.size()>1) { throw new ConfigurationException("Multiple SE found ["+found.size()+"] for platform : "+platform+" flag : "+flag); }else if (found.isEmpty()){ @@ -66,7 +78,7 @@ public class ISUtils { case "host" : { toReturn.getHosts().add(prop.value()); break;} - } + } } toReturn.getHosts().add(point.address()); Map props=point.propertyMap(); @@ -77,51 +89,51 @@ public class ISUtils { return toReturn; } - - - - public static String getToken() throws ConfigurationException { - SimpleQuery query = queryFor(ServiceEndpoint.class); - - query.addCondition("$resource/Profile/Category/text() eq 'Application'") - .addCondition("$resource/Profile/Name/text() eq 'GNA-APP'") - .setResult("$resource/Profile/AccessPoint"); - - DiscoveryClient client = clientFor(AccessPoint.class); - - List found= client.submit(query); - if(found.size()>1) { - throw new ConfigurationException("Multiple Token SE found ["+found.size()+"] for Category : Application name : GNA-APP"); - }else if (found.isEmpty()){ - throw new ConfigurationException("No Token SE found ["+found.size()+"] for Category : Application name : GNA-APP"); - } - - AccessPoint point=found.get(0); - return decryptString(point.password()); - - } - - - public static String decryptString(String toDecrypt){ +// +// +// +// private static String getToken() throws ConfigurationException { +// SimpleQuery query = queryFor(ServiceEndpoint.class); +// +// query.addCondition("$resource/Profile/Category/text() eq 'Application'") +// .addCondition("$resource/Profile/Name/text() eq 'GNA-APP'") +// .setResult("$resource/Profile/AccessPoint"); +// +// DiscoveryClient client = clientFor(AccessPoint.class); +// +// List found= client.submit(query); +// if(found.size()>1) { +// throw new ConfigurationException("Multiple Token SE found ["+found.size()+"] for Category : Application name : GNA-APP"); +// }else if (found.isEmpty()){ +// throw new ConfigurationException("No Token SE found ["+found.size()+"] for Category : Application name : GNA-APP"); +// } +// +// AccessPoint point=found.get(0); +// return decryptString(point.password()); +// +// } +// +// + private static String decryptString(String toDecrypt){ try{ return StringEncrypter.getEncrypter().decrypt(toDecrypt); }catch(Exception e) { throw new RuntimeException("Unable to decrypt : "+toDecrypt,e); } } - - - public static String getgCubeBaseEndpoint(String category,String name) { - - SimpleQuery query = queryFor(ServiceEndpoint.class); - - query.addCondition("$resource/Profile/Category/text() eq '"+category+"'") - .addCondition("$resource/Profile/Name/text() eq '"+name+"'"); - DiscoveryClient client = clientFor(ServiceEndpoint.class); - - AccessPoint point=client.submit(query).get(0).profile().accessPoints().asCollection().iterator().next(); - - return point.address(); - } +// +// +// private static String getgCubeBaseEndpoint(String category,String name) { +// +// SimpleQuery query = queryFor(ServiceEndpoint.class); +// +// query.addCondition("$resource/Profile/Category/text() eq '"+category+"'") +// .addCondition("$resource/Profile/Name/text() eq '"+name+"'"); +// DiscoveryClient client = clientFor(ServiceEndpoint.class); +// +// AccessPoint point=client.submit(query).get(0).profile().accessPoints().asCollection().iterator().next(); +// +// return point.address(); +// } } diff --git a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/MongoTests.java b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/MongoTests.java index bdd1c94..56d6af2 100644 --- a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/MongoTests.java +++ b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/MongoTests.java @@ -61,6 +61,10 @@ public class MongoTests { // manager.iterate(new Document(),f).forEach(printBlock); } + @Test + public void bruteForce(){ + + } @Test public void queries() throws ConfigurationException { diff --git a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/engine/caches/Caches.java b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/engine/caches/Caches.java index ae86729..1891b72 100644 --- a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/engine/caches/Caches.java +++ b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/engine/caches/Caches.java @@ -2,6 +2,8 @@ package org.gcube.application.geoportal.service.engine.caches; import lombok.extern.slf4j.Slf4j; import org.gcube.application.cms.tests.TokenSetter; +import org.gcube.application.geoportal.service.BasicServiceTestUnit; +import org.gcube.application.geoportal.service.engine.mongo.ConcessioniMongoManager; import org.gcube.application.geoportal.service.engine.providers.AbstractScopedMap; import org.gcube.application.geoportal.service.engine.providers.TTLObject; import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException; @@ -11,12 +13,16 @@ import java.time.Duration; import java.time.Instant; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; import static org.junit.Assert.assertTrue; @Slf4j -public class Caches { +public class Caches extends BasicServiceTestUnit { @Test public void testCache() throws ConfigurationException { @@ -53,10 +59,37 @@ public class Caches { } catch (InterruptedException e) { } } - - - - } + + @Test + public void mongoconnections() throws ConfigurationException, InterruptedException { + TokenSetter.set(scope); + ExecutorService service = Executors.newFixedThreadPool(1000); + LocalDateTime start=LocalDateTime.now(); + AtomicLong executed = new AtomicLong(0); + AtomicLong launched = new AtomicLong(0); + //for 100 secs + while(Duration.between(start,LocalDateTime.now()). + compareTo(Duration.of(100, ChronoUnit.SECONDS))<0){ + service.execute(new Runnable() { + @Override + public void run() { + try { + new ConcessioniMongoManager().list(); + } catch (ConfigurationException e) { + e.printStackTrace(); + }finally{ + executed.incrementAndGet(); + } + } + }); + launched.incrementAndGet(); + } + + while (!service.awaitTermination(2, TimeUnit.MINUTES)) { + log.info("Waiting .. completed {}, out of {} ",executed.get(),launched.get()); + if(executed.get()==launched.get()) service.shutdown(); + } + } }