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>
|
||||
|
||||
|
||||
<!-- 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 -->
|
||||
<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.AccountingCallerInterface;
|
||||
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.is.BuildEnableTabs;
|
||||
import org.gcube.portlets.admin.accountingmanager.server.state.AccountingStateData;
|
||||
|
@ -96,8 +97,9 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
|||
ASLSession aslSession = SessionUtil.getASLSession(session);
|
||||
String token = SessionUtil.getToken(aslSession);
|
||||
logger.debug("UserToken: " + token);
|
||||
|
||||
EnableTabs enableTabs =BuildEnableTabs.build(aslSession.getScope());
|
||||
|
||||
EnableTabs enableTabs = BuildEnableTabs
|
||||
.build(aslSession.getScope());
|
||||
return enableTabs;
|
||||
|
||||
} catch (ServiceException e) {
|
||||
|
@ -123,14 +125,26 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
|||
String token = SessionUtil.getToken(aslSession);
|
||||
logger.debug("UserToken: " + token);
|
||||
|
||||
AccountingCallerInterface accountingCaller;
|
||||
if (Constants.DEBUG_MODE) {
|
||||
accountingCaller = new AccountingCallerTester();
|
||||
} else {
|
||||
accountingCaller = new AccountingCaller();
|
||||
AccountingCache accountingCache = SessionUtil
|
||||
.getAccountingCache(session,token);
|
||||
String key = new String(accountingType.name() + "_"
|
||||
+ seriesRequest.toString());
|
||||
SeriesResponse seriesResponse = accountingCache.get(key);
|
||||
|
||||
if (seriesResponse == null) {
|
||||
|
||||
AccountingCallerInterface accountingCaller;
|
||||
if (Constants.DEBUG_MODE) {
|
||||
accountingCaller = new AccountingCallerTester();
|
||||
} else {
|
||||
accountingCaller = new AccountingCaller();
|
||||
}
|
||||
seriesResponse = accountingCaller.getSeries(accountingType,
|
||||
seriesRequest);
|
||||
accountingCache.put(key, seriesResponse);
|
||||
|
||||
}
|
||||
SeriesResponse seriesResponse = accountingCaller.getSeries(
|
||||
accountingType, seriesRequest);
|
||||
|
||||
AccountingStateData accountingStateData = new AccountingStateData(
|
||||
accountingType, seriesRequest, seriesResponse);
|
||||
SessionUtil.setAccountingStateData(session, accountingType,
|
||||
|
@ -217,7 +231,7 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* {@inheritDoc}
|
||||
|
@ -230,9 +244,9 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
|||
String token = SessionUtil.getToken(aslSession);
|
||||
logger.debug("UserToken: " + token);
|
||||
|
||||
Context context=SessionUtil.getContext(aslSession);
|
||||
logger.debug("getContext(): "+context);
|
||||
|
||||
Context context = SessionUtil.getContext(aslSession);
|
||||
logger.debug("getContext(): " + context);
|
||||
|
||||
return context;
|
||||
|
||||
} catch (ServiceException e) {
|
||||
|
@ -245,7 +259,6 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ItemDescription saveCSVOnWorkspace(AccountingType accountingType)
|
||||
|
|
|
@ -11,6 +11,7 @@ import javax.servlet.http.HttpSession;
|
|||
import org.gcube.application.framework.core.session.ASLSession;
|
||||
import org.gcube.application.framework.core.session.SessionManager;
|
||||
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.AccountingStateData;
|
||||
import org.gcube.portlets.admin.accountingmanager.shared.Constants;
|
||||
|
@ -111,13 +112,27 @@ 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 {
|
||||
logger.info("Current context is " + aslSession.getScope());
|
||||
ArrayList<String> contexts = new ArrayList<>();
|
||||
contexts.add(aslSession.getScope());
|
||||
|
||||
|
||||
GroupManager gm = new LiferayGroupManager();
|
||||
long currentGroupId = gm
|
||||
.getGroupIdFromInfrastructureScope(aslSession.getScope());
|
||||
|
@ -156,9 +171,8 @@ public class SessionUtil {
|
|||
}
|
||||
|
||||
// add the current scope too
|
||||
Context context=new Context(contexts);
|
||||
|
||||
|
||||
Context context = new Context(contexts);
|
||||
|
||||
return context;
|
||||
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -35,7 +35,7 @@ import org.slf4j.LoggerFactory;
|
|||
*
|
||||
*/
|
||||
public class AccountingCaller implements AccountingCallerInterface {
|
||||
static Logger logger = LoggerFactory.getLogger(AccountingCaller.class);
|
||||
private static Logger logger = LoggerFactory.getLogger(AccountingCaller.class);
|
||||
|
||||
public AccountingCaller() {
|
||||
super();
|
||||
|
@ -63,7 +63,7 @@ public class AccountingCaller implements AccountingCallerInterface {
|
|||
try {
|
||||
logger.debug("getSeries(): [AccountingType=" + accountingType
|
||||
+ " , seriesRequest=" + seriesRequest + "]");
|
||||
|
||||
|
||||
AccountingQueryBuilder queryBuilder = getAccountQueryBuilder(
|
||||
accountingType, seriesRequest);
|
||||
|
||||
|
|
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(
|
||||
AggregatedJobUsageRecord.class,
|
||||
accountingFilterContext.getContexts(),
|
||||
accountingFilterContext.getContext(),
|
||||
temporalConstraint, filters);
|
||||
} else {
|
||||
logger.error("Invalid Request: " + seriesRequest);
|
||||
|
|
|
@ -125,7 +125,7 @@ public class AccountingQuery4Portlet extends AccountingQueryBuilder {
|
|||
}
|
||||
invocation = new AccountingQueryContext(
|
||||
AggregatedPortletUsageRecord.class,
|
||||
accountingFilterContext.getContexts(),
|
||||
accountingFilterContext.getContext(),
|
||||
temporalConstraint, filters);
|
||||
} else {
|
||||
logger.error("Invalid Request: " + seriesRequest);
|
||||
|
|
|
@ -126,7 +126,7 @@ public class AccountingQuery4Service extends AccountingQueryBuilder {
|
|||
}
|
||||
invocation = new AccountingQueryContext(
|
||||
AggregatedServiceUsageRecord.class,
|
||||
accountingFilterContext.getContexts(),
|
||||
accountingFilterContext.getContext(),
|
||||
temporalConstraint, filters);
|
||||
} else {
|
||||
logger.error("Invalid Request: " + seriesRequest);
|
||||
|
|
|
@ -126,7 +126,7 @@ public class AccountingQuery4Storage extends AccountingQueryBuilder {
|
|||
}
|
||||
invocation = new AccountingQueryContext(
|
||||
AggregatedStorageUsageRecord.class,
|
||||
accountingFilterContext.getContexts(),
|
||||
accountingFilterContext.getContext(),
|
||||
temporalConstraint, filters);
|
||||
} else {
|
||||
logger.error("Invalid Request: " + seriesRequest);
|
||||
|
|
|
@ -125,7 +125,7 @@ public class AccountingQuery4Task extends AccountingQueryBuilder {
|
|||
}
|
||||
invocation = new AccountingQueryContext(
|
||||
AggregatedTaskUsageRecord.class,
|
||||
accountingFilterContext.getContexts(),
|
||||
accountingFilterContext.getContext(),
|
||||
temporalConstraint,
|
||||
filters);
|
||||
} else {
|
||||
|
|
|
@ -19,7 +19,7 @@ public class Constants {
|
|||
public static final String DEFAULT_USER = "giancarlo.panichi";
|
||||
public final static String DEFAULT_SCOPE = "/gcube/devNext/NextNext";
|
||||
public final static String DEFAULT_TOKEN = "6af6eaff-35bd-4405-b747-f63246d0212a-98187548";
|
||||
|
||||
|
||||
// public final static String DEFAULT_SCOPE = "/gcube/devsec/devVRE";
|
||||
// public final static String DEFAULT_TOKEN =
|
||||
// "16e65d4f-11e0-4e4a-84b9-351688fccc12-98187548";
|
||||
|
@ -30,6 +30,8 @@ public class Constants {
|
|||
public static final String EXPORT_SERVLET_ACCOUNTING_TYPE_PARAMETER = "AccountingType";
|
||||
|
||||
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 };;
|
||||
|
||||
|
@ -37,6 +39,6 @@ public class Constants {
|
|||
//IS Resource
|
||||
public static final String ACCOUNTING_NAME = "AccountingManager";
|
||||
public static final String ACCOUNTING_CATEGORY = "AccountingProfile";
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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