Refs #12184: Create different pools for Scheduled Thread in accounting stack

Task-Url: https://support.d4science.org/issues/12184

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@169818 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2018-07-18 14:19:47 +00:00
parent c2041599cc
commit 7f3ce5e96f
6 changed files with 44 additions and 24 deletions

View File

@ -2,12 +2,14 @@ package org.gcube.accounting.aggregator;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.gcube.accounting.datamodel.validations.validators.RegexReplace;
import org.gcube.accounting.persistence.AccountingPersistenceConfiguration;
import org.gcube.documentstore.persistence.ExecutorUtils;
import org.gcube.documentstore.records.DSMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -19,6 +21,24 @@ public class RegexRulesAggregator implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(RegexRulesAggregator.class);
private static final ScheduledExecutorService REGEX_REDISCOVERY_POOL;
static {
REGEX_REDISCOVERY_POOL = Executors.newScheduledThreadPool(50, new ThreadFactory() {
private int counter = 0;
private static final String prefix = "RegexRediscoveryThread";
public Thread newThread(Runnable r) {
return new Thread(r, prefix + "-" + counter++);
}
});
}
protected TimeUnit timeUnit = TimeUnit.MINUTES;
protected long delay = TimeUnit.MINUTES.toMinutes(15);
@ -46,8 +66,10 @@ public class RegexRulesAggregator implements Runnable {
readConfiguration();
}
public List<RegexReplace> getRegexReplacelist() {
return regexReplaceList;
public List<RegexReplace> getRegexReplaceList() {
synchronized(regexReplaceList) {
return regexReplaceList;
}
}
public RegexReplace addRegexReplace(String serviceClass, String serviceName, String regex, String replace) {
@ -99,7 +121,7 @@ public class RegexRulesAggregator implements Runnable {
public void start() {
if(reloadAggregatorRules == null) {
reloadAggregatorRules = ExecutorUtils.scheduler.scheduleAtFixedRate(this, delay, delay, timeUnit);
reloadAggregatorRules = REGEX_REDISCOVERY_POOL.scheduleAtFixedRate(this, delay, delay, timeUnit);
}
}

View File

@ -19,21 +19,6 @@ public class CalledMethodRegexReplaceValidator implements FieldAction {
private static Logger logger = LoggerFactory.getLogger(CalledMethodRegexReplaceValidator.class);
@Deprecated
public static List<RegexReplace> getRegexReplacelist() {
return RegexRulesAggregator.getInstance().getRegexReplacelist();
}
@Deprecated
public static RegexReplace addRegexReplace(String serviceClass, String serviceName, String regex, String replace) {
return RegexRulesAggregator.getInstance().addRegexReplace(serviceClass, serviceName, regex, replace);
}
@Deprecated
public static RegexReplace addRegexReplace(RegexReplace regexReplace) {
return RegexRulesAggregator.getInstance().addRegexReplace(regexReplace);
}
/**
* {@inheritDoc}
*/
@ -64,7 +49,9 @@ public class CalledMethodRegexReplaceValidator implements FieldAction {
return value;
}
for(RegexReplace regexReplace : getRegexReplacelist()) {
List<RegexReplace> regexReplaceList = RegexRulesAggregator.getInstance().getRegexReplaceList();
for(RegexReplace regexReplace : regexReplaceList) {
if(serviceClass.compareTo(regexReplace.getServiceClass())==0 && serviceName.compareTo(regexReplace.getServiceName())==0) {
Matcher matcher = regexReplace.regexPattern.matcher(stringValue);
if(matcher.matches()) {

View File

@ -17,12 +17,16 @@ import org.gcube.documentstore.persistence.PersistenceBackendConfiguration;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class AccountingPersistenceConfiguration extends PersistenceBackendConfiguration {
private static final Logger logger = LoggerFactory.getLogger(AccountingPersistenceConfiguration.class);
protected static final String TARGET_SCOPE = "targetScope";
protected static final String SERVICE_ENDPOINT_CATEGORY = "Accounting";
@ -38,8 +42,13 @@ public class AccountingPersistenceConfiguration extends PersistenceBackendConfig
public AccountingPersistenceConfiguration(Class<?> clz) throws Exception {
super();
ServiceEndpoint serviceEndpoint = getServiceEndpoint(SERVICE_ENDPOINT_CATEGORY, SERVICE_ENDPOINT_NAME, clz);
setValues(serviceEndpoint, clz);
try {
ServiceEndpoint serviceEndpoint = getServiceEndpoint(SERVICE_ENDPOINT_CATEGORY, SERVICE_ENDPOINT_NAME, clz);
setValues(serviceEndpoint, clz);
}catch (Exception e) {
logger.error("Error while instancing {} in context {}", this.getClass().getSimpleName(),
BasicUsageRecord.getContextFromToken(), e);
}
}
protected ServiceEndpoint getServiceEndpoint(String serviceEndpointCategory, String serviceEndpointName, Class<?> clz){

View File

@ -63,6 +63,8 @@ public class AccountingPersistenceFactory {
//flush all and shutdown connection and thread
PersistenceBackendFactory.flushAll();
PersistenceBackendFactory.shutdown();
}
/**

View File

@ -15,7 +15,7 @@ public class RegexRulesAggregatorTest extends ScopedTest {
@Test
public void test() {
RegexRulesAggregator regexRulesAggregator = RegexRulesAggregator.getInstance();
List<RegexReplace> list = regexRulesAggregator.getRegexReplacelist();
List<RegexReplace> list = regexRulesAggregator.getRegexReplaceList();
for(RegexReplace regexReplace : list) {
logger.debug("{} {} {} {}", regexReplace.getServiceClass(), regexReplace.getServiceName(), regexReplace.getRegex(), regexReplace.getReplace());
}

View File

@ -174,7 +174,7 @@ public class ServiceUsageRecordTest extends ScopedTest {
logger.debug("{}", usageRecord);
RegexRulesAggregator regexRulesAggregator = RegexRulesAggregator.getInstance();
List<RegexReplace> list = regexRulesAggregator.getRegexReplacelist();
List<RegexReplace> list = regexRulesAggregator.getRegexReplaceList();
for(RegexReplace regexReplace : list) {
logger.debug("{} {} {} {}", regexReplace.getServiceClass(), regexReplace.getServiceName(), regexReplace.getRegex(), regexReplace.getReplace());
}