diff --git a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/AccountingClientDaemon.java b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/AccountingClientDaemon.java index bacf615..87f6b00 100644 --- a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/AccountingClientDaemon.java +++ b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/AccountingClientDaemon.java @@ -35,6 +35,7 @@ public class AccountingClientDaemon implements Runnable { private volatile boolean running = true; private volatile AccountingCache accountingCache; private ArrayList tasks; + private long timeout=Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS; public AccountingClientDaemon(ServletContextEvent sce, AccountingCache accountingCache) { @@ -78,8 +79,8 @@ public class AccountingClientDaemon implements Runnable { TaskStatus result = null; try { result = futureResult - .get(Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS, - TimeUnit.MINUTES); + .get(timeout, + TimeUnit.MILLISECONDS); logger.info("AccountingClientTask: " + result); } catch (InterruptedException | ExecutionException e) { @@ -88,8 +89,8 @@ public class AccountingClientDaemon implements Runnable { + e.getLocalizedMessage(), e); } catch (TimeoutException e) { logger.error("AccountingClientTask No response after " - + Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS - + " minutes!"); + + timeout + + " milliseconds!"); futureResult.cancel(true); } dones.add(taskInProgress); @@ -98,7 +99,7 @@ public class AccountingClientDaemon implements Runnable { long diff = now.getTimeInMillis() - taskInProgress.getStartTime() .getTimeInMillis(); - if (diff > Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS) { + if (diff > timeout) { futureResult.cancel(true); dones.add(taskInProgress); } diff --git a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/is/BuildThreadPoolInfo.java b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/is/BuildThreadPoolInfo.java new file mode 100644 index 0000000..175434f --- /dev/null +++ b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/is/BuildThreadPoolInfo.java @@ -0,0 +1,64 @@ +package org.gcube.portlets.admin.accountingmanager.server.is; + +import org.gcube.portlets.admin.accountingmanager.server.util.ThreadPoolInfo; +import org.gcube.portlets.admin.accountingmanager.shared.Constants; +import org.gcube.portlets.admin.accountingmanager.shared.exception.ServiceException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Giancarlo Panichi email: g.panichi@isti.cnr.it + * + */ +public class BuildThreadPoolInfo { + + private static Logger logger = LoggerFactory + .getLogger(BuildThreadPoolInfo.class); + + public static ThreadPoolInfo build(String scope) throws ServiceException { + ThreadPoolInfo threadPoolInfo=null; + + if (Constants.DEBUG_MODE) { + logger.info("AccountingManager use default configuration for threadpool [scope="+scope+"]"); + threadPoolInfo=new ThreadPoolInfo(Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS); + } else { + ThreadPoolJAXB threadPoolJAXB=null; + try { + threadPoolJAXB= InformationSystemUtils + .retrieveThreadPoolTimeout(scope); + } catch(ServiceException e){ + logger.info(e.getLocalizedMessage()); + } + + logger.debug("ThreadPool: " + threadPoolJAXB); + if (threadPoolJAXB != null && threadPoolJAXB.getTimeout() != null + && !threadPoolJAXB.getTimeout().isEmpty()) { + long timeout=0; + try { + timeout=Long.parseLong(threadPoolJAXB.getTimeout()); + }catch(NumberFormatException e){ + logger.error("AccountingManager invalid timeout set in threadpool resource",e); + } + + if(timeout==0){ + logger.info("AccountingManager use default configuration for threadpool [scope="+scope+"]"); + threadPoolInfo=new ThreadPoolInfo(Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS); + } else { + logger.info("AccountingManager use timeout="+timeout+" for threadpool [scope="+scope+"]"); + threadPoolInfo=new ThreadPoolInfo(timeout); + } + + } else { + logger.info("AccountingManager use default configuration for threadpool [scope="+scope+"]"); + threadPoolInfo=new ThreadPoolInfo(Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS); + + } + } + + logger.debug("ThreadPoolInfo: " + threadPoolInfo); + return threadPoolInfo; + } + +} diff --git a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/is/InformationSystemUtils.java b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/is/InformationSystemUtils.java index c471546..e00db7b 100644 --- a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/is/InformationSystemUtils.java +++ b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/is/InformationSystemUtils.java @@ -35,7 +35,9 @@ public class InformationSystemUtils { return null; ScopeProvider.instance.set(scope); - + logger.debug("Retrieve enabletab configuration in scope: "+scope); + + SimpleQuery query = ICFactory.queryFor(GenericResource.class); query.addCondition( "$resource/Profile/SecondaryType/text() eq '" @@ -103,5 +105,87 @@ public class InformationSystemUtils { throw new ServiceException(error, e); } } + + + + public static ThreadPoolJAXB retrieveThreadPoolTimeout(String scope) + throws ServiceException { + try { + + if (scope == null || scope.length() == 0) + return null; + logger.debug("Retrieve threadpool configuration in scope: "+scope); + + ScopeProvider.instance.set(scope); + + SimpleQuery query = ICFactory.queryFor(GenericResource.class); + query.addCondition( + "$resource/Profile/SecondaryType/text() eq '" + + Constants.ACCOUNTING_CATEGORY + "'") + .addCondition( + "$resource/Profile/Name/text() eq '" + + Constants.ACCOUNTING_POOL_NAME + "'") + .setResult("$resource"); + + DiscoveryClient client = ICFactory + .clientFor(GenericResource.class); + List accountingResources = client.submit(query); + logger.debug("Resources: " + accountingResources); + + ThreadPoolJAXB threadPool = null; + + for (GenericResource genericResource : accountingResources) { + if (genericResource.scopes() != null) { + ScopeGroup scopes = genericResource.scopes(); + Iterator iterator = scopes.iterator(); + String scopeFound = null; + boolean found = false; + while (iterator.hasNext() && !found) { + scopeFound = iterator.next(); + if (scopeFound.compareTo(scope) == 0) { + found = true; + } + } + if (found) { + try { + JAXBParser parser = new JAXBParser( + ThreadPoolJAXB.class); + logger.debug("Body: " + + genericResource.profile().bodyAsString()); + threadPool = (ThreadPoolJAXB) parser + .parse(genericResource.profile() + .bodyAsString()); + logger.debug("ThreadPool: " + threadPool); + } catch (Throwable e) { + String error = "Error in discovery Accounting Manager thread pool in scope "+ scope+". " + + "Resource parsing failed!"; + logger.error(error); + logger.error("Error {resource="+ genericResource + ", error=" + + e.getLocalizedMessage() + "}"); + e.printStackTrace(); + throw new ServiceException(error, e); + } + break; + + } + + } + } + + return threadPool; + + } catch (ServiceException e) { + throw e; + } catch (Throwable e) { + String error = "Error in discovery Accounting Manager thread pool in scope: " + + scope; + logger.error(error); + logger.error("Error: " + e.getLocalizedMessage()); + e.printStackTrace(); + throw new ServiceException(error, e); + } + } + + } diff --git a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/is/ThreadPoolJAXB.java b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/is/ThreadPoolJAXB.java new file mode 100644 index 0000000..a0c86d2 --- /dev/null +++ b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/is/ThreadPoolJAXB.java @@ -0,0 +1,34 @@ +package org.gcube.portlets.admin.accountingmanager.server.is; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * + * @author Giancarlo Panichi email: g.panichi@isti.cnr.it + * + */ + +@XmlRootElement(name = "threadpool") +@XmlAccessorType(XmlAccessType.FIELD) +public class ThreadPoolJAXB { + @XmlElement + private String timeout; + + public String getTimeout() { + return timeout; + } + + public void setTimeout(String timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "ThreadPoolJAXB [timeout=" + timeout + "]"; + } + +} diff --git a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/util/ThreadPoolInfo.java b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/util/ThreadPoolInfo.java new file mode 100644 index 0000000..5b2d183 --- /dev/null +++ b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/util/ThreadPoolInfo.java @@ -0,0 +1,38 @@ +package org.gcube.portlets.admin.accountingmanager.server.util; + +import java.io.Serializable; + +/** + * + * @author Giancarlo Panichi email: g.panichi@isti.cnr.it + * + */ +public class ThreadPoolInfo implements Serializable { + + private static final long serialVersionUID = 6955469914102879890L; + private long timeout; + + /** + * + * @param timeout + */ + public ThreadPoolInfo(long timeout) { + super(); + this.timeout = timeout; + } + + public long getTimeout() { + return timeout; + } + + public void setTimeout(long timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "ThreadPoolInfo [timeout=" + timeout + "]"; + } + +} diff --git a/src/main/java/org/gcube/portlets/admin/accountingmanager/shared/Constants.java b/src/main/java/org/gcube/portlets/admin/accountingmanager/shared/Constants.java index 2d09ed7..a4fc258 100644 --- a/src/main/java/org/gcube/portlets/admin/accountingmanager/shared/Constants.java +++ b/src/main/java/org/gcube/portlets/admin/accountingmanager/shared/Constants.java @@ -39,6 +39,8 @@ public class Constants { // IS Resource public static final String ACCOUNTING_NAME = "AccountingManager"; public static final String ACCOUNTING_CATEGORY = "AccountingProfile"; + public static final String ACCOUNTING_POOL_NAME = "AccountingManagerTaskPool"; + //TimeOut public static final long SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS = 1800000;