This commit is contained in:
Fabio Sinibaldi 2021-10-06 11:26:35 +02:00
parent 52f537a6c4
commit 0700398f25
10 changed files with 134 additions and 99 deletions

View File

@ -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();
}
}

View File

@ -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();

View File

@ -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;

View File

@ -47,7 +47,7 @@ public abstract class AbstractScopedMap<T> implements Engine<T>{
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();
}

View File

@ -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();
}
}

View File

@ -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<MongoClient>{
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<MongoClient>{
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,

View File

@ -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<MongoConnection>{
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
}
}

View File

@ -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<AccessPoint> found=getAP(platform, flag);
// public List<AccessPoint> GetAP(String platform,String flag) {
// return performGetAP(platform,flag);
// }
private static DatabaseConnection performQueryForDB(String platform, String flag) throws ConfigurationException {
List<AccessPoint> 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<AccessPoint> getAP(String platform,String flag) {
private static List<AccessPoint> 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<AccessPoint> found=getAP(platform, flag);
private static MongoConnection performQueryForMongoDB(String platform,String flag) throws ConfigurationException {
List<AccessPoint> 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<String, Property> 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<AccessPoint> client = clientFor(AccessPoint.class);
List<AccessPoint> 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<AccessPoint> client = clientFor(AccessPoint.class);
//
// List<AccessPoint> 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<ServiceEndpoint> 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<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
//
// AccessPoint point=client.submit(query).get(0).profile().accessPoints().asCollection().iterator().next();
//
// return point.address();
// }
}

View File

@ -61,6 +61,10 @@ public class MongoTests {
// manager.iterate(new Document(),f).forEach(printBlock);
}
@Test
public void bruteForce(){
}
@Test
public void queries() throws ConfigurationException {

View File

@ -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();
}
}
}