Updated cache behavior
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/admin/accounting-manager@134002 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
88c8e22ecb
commit
e040d787f0
12
pom.xml
12
pom.xml
|
@ -339,7 +339,19 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/javax.cache/cache-api -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.cache</groupId>
|
||||||
|
<artifactId>cache-api</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- ehcache -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.ehcache</groupId>
|
||||||
|
<artifactId>ehcache</artifactId>
|
||||||
|
<version>3.1.3</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- LOGGING -->
|
<!-- LOGGING -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.gcube.portlets.admin.accountingmanager.client.rpc.AccountingManagerSe
|
||||||
import org.gcube.portlets.admin.accountingmanager.server.amservice.AccountingCaller;
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.AccountingCaller;
|
||||||
import org.gcube.portlets.admin.accountingmanager.server.amservice.AccountingCallerInterface;
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.AccountingCallerInterface;
|
||||||
import org.gcube.portlets.admin.accountingmanager.server.amservice.AccountingCallerTester;
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.AccountingCallerTester;
|
||||||
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.cache.AccountingCache;
|
||||||
import org.gcube.portlets.admin.accountingmanager.server.export.CSVManager;
|
import org.gcube.portlets.admin.accountingmanager.server.export.CSVManager;
|
||||||
import org.gcube.portlets.admin.accountingmanager.server.is.BuildEnableTabs;
|
import org.gcube.portlets.admin.accountingmanager.server.is.BuildEnableTabs;
|
||||||
import org.gcube.portlets.admin.accountingmanager.server.state.AccountingStateData;
|
import org.gcube.portlets.admin.accountingmanager.server.state.AccountingStateData;
|
||||||
|
@ -97,7 +98,8 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
||||||
String token = SessionUtil.getToken(aslSession);
|
String token = SessionUtil.getToken(aslSession);
|
||||||
logger.debug("UserToken: " + token);
|
logger.debug("UserToken: " + token);
|
||||||
|
|
||||||
EnableTabs enableTabs =BuildEnableTabs.build(aslSession.getScope());
|
EnableTabs enableTabs = BuildEnableTabs
|
||||||
|
.build(aslSession.getScope());
|
||||||
return enableTabs;
|
return enableTabs;
|
||||||
|
|
||||||
} catch (ServiceException e) {
|
} catch (ServiceException e) {
|
||||||
|
@ -123,14 +125,26 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
||||||
String token = SessionUtil.getToken(aslSession);
|
String token = SessionUtil.getToken(aslSession);
|
||||||
logger.debug("UserToken: " + token);
|
logger.debug("UserToken: " + token);
|
||||||
|
|
||||||
|
AccountingCache accountingCache = SessionUtil
|
||||||
|
.getAccountingCache(session,token);
|
||||||
|
String key = new String(accountingType.name() + "_"
|
||||||
|
+ seriesRequest.toString());
|
||||||
|
SeriesResponse seriesResponse = accountingCache.get(key);
|
||||||
|
|
||||||
|
if (seriesResponse == null) {
|
||||||
|
|
||||||
AccountingCallerInterface accountingCaller;
|
AccountingCallerInterface accountingCaller;
|
||||||
if (Constants.DEBUG_MODE) {
|
if (Constants.DEBUG_MODE) {
|
||||||
accountingCaller = new AccountingCallerTester();
|
accountingCaller = new AccountingCallerTester();
|
||||||
} else {
|
} else {
|
||||||
accountingCaller = new AccountingCaller();
|
accountingCaller = new AccountingCaller();
|
||||||
}
|
}
|
||||||
SeriesResponse seriesResponse = accountingCaller.getSeries(
|
seriesResponse = accountingCaller.getSeries(accountingType,
|
||||||
accountingType, seriesRequest);
|
seriesRequest);
|
||||||
|
accountingCache.put(key, seriesResponse);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
AccountingStateData accountingStateData = new AccountingStateData(
|
AccountingStateData accountingStateData = new AccountingStateData(
|
||||||
accountingType, seriesRequest, seriesResponse);
|
accountingType, seriesRequest, seriesResponse);
|
||||||
SessionUtil.setAccountingStateData(session, accountingType,
|
SessionUtil.setAccountingStateData(session, accountingType,
|
||||||
|
@ -230,8 +244,8 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
||||||
String token = SessionUtil.getToken(aslSession);
|
String token = SessionUtil.getToken(aslSession);
|
||||||
logger.debug("UserToken: " + token);
|
logger.debug("UserToken: " + token);
|
||||||
|
|
||||||
Context context=SessionUtil.getContext(aslSession);
|
Context context = SessionUtil.getContext(aslSession);
|
||||||
logger.debug("getContext(): "+context);
|
logger.debug("getContext(): " + context);
|
||||||
|
|
||||||
return context;
|
return context;
|
||||||
|
|
||||||
|
@ -246,7 +260,6 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemDescription saveCSVOnWorkspace(AccountingType accountingType)
|
public ItemDescription saveCSVOnWorkspace(AccountingType accountingType)
|
||||||
throws ServiceException {
|
throws ServiceException {
|
||||||
|
|
|
@ -11,6 +11,7 @@ import javax.servlet.http.HttpSession;
|
||||||
import org.gcube.application.framework.core.session.ASLSession;
|
import org.gcube.application.framework.core.session.ASLSession;
|
||||||
import org.gcube.application.framework.core.session.SessionManager;
|
import org.gcube.application.framework.core.session.SessionManager;
|
||||||
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
|
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
|
||||||
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.cache.AccountingCache;
|
||||||
import org.gcube.portlets.admin.accountingmanager.server.state.AccountingState;
|
import org.gcube.portlets.admin.accountingmanager.server.state.AccountingState;
|
||||||
import org.gcube.portlets.admin.accountingmanager.server.state.AccountingStateData;
|
import org.gcube.portlets.admin.accountingmanager.server.state.AccountingStateData;
|
||||||
import org.gcube.portlets.admin.accountingmanager.shared.Constants;
|
import org.gcube.portlets.admin.accountingmanager.shared.Constants;
|
||||||
|
@ -111,7 +112,21 @@ public class SessionUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Context getContext(ASLSession aslSession) throws ServiceException {
|
public static AccountingCache getAccountingCache(HttpSession httpSession,
|
||||||
|
String token) throws ServiceException {
|
||||||
|
AccountingCache accountingCache = (AccountingCache) httpSession
|
||||||
|
.getAttribute(Constants.ACCOUNTING_CACHE);
|
||||||
|
if (accountingCache == null) {
|
||||||
|
accountingCache = new AccountingCache(token);
|
||||||
|
httpSession.setAttribute(Constants.ACCOUNTING_CACHE,
|
||||||
|
accountingCache);
|
||||||
|
}
|
||||||
|
return accountingCache;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Context getContext(ASLSession aslSession)
|
||||||
|
throws ServiceException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.info("Current context is " + aslSession.getScope());
|
logger.info("Current context is " + aslSession.getScope());
|
||||||
|
@ -156,8 +171,7 @@ public class SessionUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
// add the current scope too
|
// add the current scope too
|
||||||
Context context=new Context(contexts);
|
Context context = new Context(contexts);
|
||||||
|
|
||||||
|
|
||||||
return context;
|
return context;
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ import org.slf4j.LoggerFactory;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class AccountingCaller implements AccountingCallerInterface {
|
public class AccountingCaller implements AccountingCallerInterface {
|
||||||
static Logger logger = LoggerFactory.getLogger(AccountingCaller.class);
|
private static Logger logger = LoggerFactory.getLogger(AccountingCaller.class);
|
||||||
|
|
||||||
public AccountingCaller() {
|
public AccountingCaller() {
|
||||||
super();
|
super();
|
||||||
|
|
121
src/main/java/org/gcube/portlets/admin/accountingmanager/server/amservice/cache/AccountingCache.java
vendored
Normal file
121
src/main/java/org/gcube/portlets/admin/accountingmanager/server/amservice/cache/AccountingCache.java
vendored
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
package org.gcube.portlets.admin.accountingmanager.server.amservice.cache;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import javax.cache.Cache;
|
||||||
|
import javax.cache.CacheManager;
|
||||||
|
import javax.cache.Caching;
|
||||||
|
import javax.cache.configuration.MutableConfiguration;
|
||||||
|
import javax.cache.expiry.AccessedExpiryPolicy;
|
||||||
|
import javax.cache.expiry.Duration;
|
||||||
|
import javax.cache.spi.CachingProvider;
|
||||||
|
|
||||||
|
import org.gcube.portlets.admin.accountingmanager.shared.data.response.SeriesResponse;
|
||||||
|
import org.gcube.portlets.admin.accountingmanager.shared.exception.ServiceException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class AccountingCache implements Serializable {
|
||||||
|
private static final long serialVersionUID = -4352823042594405108L;
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory
|
||||||
|
.getLogger(AccountingCache.class);
|
||||||
|
|
||||||
|
private static final String ACCOUNTING_CACHE = "AccountingCache";
|
||||||
|
private Cache<String, SeriesResponse> cache;
|
||||||
|
private CacheManager cacheManager;
|
||||||
|
private String cacheName;
|
||||||
|
|
||||||
|
public AccountingCache(String token) throws ServiceException {
|
||||||
|
super();
|
||||||
|
try {
|
||||||
|
cacheName=ACCOUNTING_CACHE+token;
|
||||||
|
|
||||||
|
// Resolve a cache manager
|
||||||
|
CachingProvider cachingProvider = Caching.getCachingProvider();
|
||||||
|
cacheManager = cachingProvider.getCacheManager();
|
||||||
|
|
||||||
|
// configure the cache MutableConfiguration<String, Integer>
|
||||||
|
MutableConfiguration<String, SeriesResponse> config = new MutableConfiguration<String,SeriesResponse>()
|
||||||
|
.setTypes(String.class, SeriesResponse.class)
|
||||||
|
.setExpiryPolicyFactory(
|
||||||
|
AccessedExpiryPolicy
|
||||||
|
.factoryOf(Duration.THIRTY_MINUTES))
|
||||||
|
.setStatisticsEnabled(true);
|
||||||
|
|
||||||
|
// create the cache
|
||||||
|
cache=cacheManager.getCache(cacheName);
|
||||||
|
|
||||||
|
if(cache==null){
|
||||||
|
cache = cacheManager.createCache(cacheName, config);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ResourcePoolsBuilder builder = ResourcePoolsBuilder
|
||||||
|
* .newResourcePoolsBuilder().heap(5L, MemoryUnit.MB);
|
||||||
|
*
|
||||||
|
* CacheConfiguration<String, Integer> cacheConfiguration =
|
||||||
|
* CacheConfigurationBuilder
|
||||||
|
* .newCacheConfigurationBuilder(String.class, Integer.class,
|
||||||
|
* builder) .withSizeOfMaxObjectSize(2L, MemoryUnit.MB) .withExpiry(
|
||||||
|
* Expirations .timeToLiveExpiration(org.ehcache.expiry.Duration
|
||||||
|
* .of(1L, TimeUnit.HOURS))).build();
|
||||||
|
*
|
||||||
|
* cache = cacheManager.createCache(ACCOUNTING_CACHE,
|
||||||
|
* Eh107Configuration
|
||||||
|
* .fromEhcacheCacheConfiguration(cacheConfiguration));
|
||||||
|
*/
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getLocalizedMessage(), e);
|
||||||
|
throw new ServiceException(e.getLocalizedMessage(), e);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void put(String key, SeriesResponse value) {
|
||||||
|
cache.put(key, value);
|
||||||
|
logger.debug("Cached: [" + key + ", " + value + "]");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public SeriesResponse get(String key) {
|
||||||
|
SeriesResponse value = cache.get(key);
|
||||||
|
logger.debug("Cached value: " + value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void finalize() throws Throwable {
|
||||||
|
super.finalize();
|
||||||
|
logger.debug("Release the resource " + cacheName);
|
||||||
|
if (cache != null) {
|
||||||
|
try {
|
||||||
|
cache.close();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.error("Error closing the cache " + cacheName
|
||||||
|
+ ": " + e.getLocalizedMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
cacheManager.destroyCache(ACCOUNTING_CACHE);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.error("Error destroying the cache " + cacheName
|
||||||
|
+ ": " + e.getLocalizedMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
cacheManager.close();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.error(
|
||||||
|
"Error closing cache manager: "
|
||||||
|
+ e.getLocalizedMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -126,7 +126,7 @@ public class AccountingQuery4Job extends AccountingQueryBuilder {
|
||||||
}
|
}
|
||||||
invocation = new AccountingQueryContext(
|
invocation = new AccountingQueryContext(
|
||||||
AggregatedJobUsageRecord.class,
|
AggregatedJobUsageRecord.class,
|
||||||
accountingFilterContext.getContexts(),
|
accountingFilterContext.getContext(),
|
||||||
temporalConstraint, filters);
|
temporalConstraint, filters);
|
||||||
} else {
|
} else {
|
||||||
logger.error("Invalid Request: " + seriesRequest);
|
logger.error("Invalid Request: " + seriesRequest);
|
||||||
|
|
|
@ -125,7 +125,7 @@ public class AccountingQuery4Portlet extends AccountingQueryBuilder {
|
||||||
}
|
}
|
||||||
invocation = new AccountingQueryContext(
|
invocation = new AccountingQueryContext(
|
||||||
AggregatedPortletUsageRecord.class,
|
AggregatedPortletUsageRecord.class,
|
||||||
accountingFilterContext.getContexts(),
|
accountingFilterContext.getContext(),
|
||||||
temporalConstraint, filters);
|
temporalConstraint, filters);
|
||||||
} else {
|
} else {
|
||||||
logger.error("Invalid Request: " + seriesRequest);
|
logger.error("Invalid Request: " + seriesRequest);
|
||||||
|
|
|
@ -126,7 +126,7 @@ public class AccountingQuery4Service extends AccountingQueryBuilder {
|
||||||
}
|
}
|
||||||
invocation = new AccountingQueryContext(
|
invocation = new AccountingQueryContext(
|
||||||
AggregatedServiceUsageRecord.class,
|
AggregatedServiceUsageRecord.class,
|
||||||
accountingFilterContext.getContexts(),
|
accountingFilterContext.getContext(),
|
||||||
temporalConstraint, filters);
|
temporalConstraint, filters);
|
||||||
} else {
|
} else {
|
||||||
logger.error("Invalid Request: " + seriesRequest);
|
logger.error("Invalid Request: " + seriesRequest);
|
||||||
|
|
|
@ -126,7 +126,7 @@ public class AccountingQuery4Storage extends AccountingQueryBuilder {
|
||||||
}
|
}
|
||||||
invocation = new AccountingQueryContext(
|
invocation = new AccountingQueryContext(
|
||||||
AggregatedStorageUsageRecord.class,
|
AggregatedStorageUsageRecord.class,
|
||||||
accountingFilterContext.getContexts(),
|
accountingFilterContext.getContext(),
|
||||||
temporalConstraint, filters);
|
temporalConstraint, filters);
|
||||||
} else {
|
} else {
|
||||||
logger.error("Invalid Request: " + seriesRequest);
|
logger.error("Invalid Request: " + seriesRequest);
|
||||||
|
|
|
@ -125,7 +125,7 @@ public class AccountingQuery4Task extends AccountingQueryBuilder {
|
||||||
}
|
}
|
||||||
invocation = new AccountingQueryContext(
|
invocation = new AccountingQueryContext(
|
||||||
AggregatedTaskUsageRecord.class,
|
AggregatedTaskUsageRecord.class,
|
||||||
accountingFilterContext.getContexts(),
|
accountingFilterContext.getContext(),
|
||||||
temporalConstraint,
|
temporalConstraint,
|
||||||
filters);
|
filters);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -30,6 +30,8 @@ public class Constants {
|
||||||
public static final String EXPORT_SERVLET_ACCOUNTING_TYPE_PARAMETER = "AccountingType";
|
public static final String EXPORT_SERVLET_ACCOUNTING_TYPE_PARAMETER = "AccountingType";
|
||||||
|
|
||||||
public static final String SESSION_ACCOUNTING_STATE = "ACCOUNTING_STATE";
|
public static final String SESSION_ACCOUNTING_STATE = "ACCOUNTING_STATE";
|
||||||
|
public static final String ACCOUNTING_CACHE = "AccountingCache";
|
||||||
|
|
||||||
|
|
||||||
public static final AccountingType[] DEFAULT_TABS = new AccountingType[] { AccountingType.STORAGE };;
|
public static final AccountingType[] DEFAULT_TABS = new AccountingType[] { AccountingType.STORAGE };;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.portlets.admin.accountingmanager;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.cache.AccountingCache;
|
||||||
|
import org.gcube.portlets.admin.accountingmanager.shared.Constants;
|
||||||
|
import org.gcube.portlets.admin.accountingmanager.shared.data.response.SeriesResponse;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author "Giancarlo Panichi" <a
|
||||||
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TestAccountingManangerCache extends TestCase {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory
|
||||||
|
.getLogger(TestAccountingManangerCache.class);
|
||||||
|
|
||||||
|
|
||||||
|
public void testAccountingManagerResource() {
|
||||||
|
if (Constants.TEST_ENABLE) {
|
||||||
|
logger.debug("Test Enabled");
|
||||||
|
|
||||||
|
try {
|
||||||
|
AccountingCache accountingCache=new AccountingCache("token");
|
||||||
|
SeriesResponse s=new SeriesResponse();
|
||||||
|
accountingCache.put("key", s);
|
||||||
|
assertTrue(true);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getLocalizedMessage(),e);
|
||||||
|
assertTrue("Error in cache!", false);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
logger.debug("Test Disabled");
|
||||||
|
assertTrue(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue