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:
Giancarlo Panichi 2016-11-09 19:01:43 +00:00
parent 88c8e22ecb
commit e040d787f0
12 changed files with 240 additions and 28 deletions

12
pom.xml
View File

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

View File

@ -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;
@ -96,8 +97,9 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
ASLSession aslSession = SessionUtil.getASLSession(session); ASLSession aslSession = SessionUtil.getASLSession(session);
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);
AccountingCallerInterface accountingCaller; AccountingCache accountingCache = SessionUtil
if (Constants.DEBUG_MODE) { .getAccountingCache(session,token);
accountingCaller = new AccountingCallerTester(); String key = new String(accountingType.name() + "_"
} else { + seriesRequest.toString());
accountingCaller = new AccountingCaller(); 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( AccountingStateData accountingStateData = new AccountingStateData(
accountingType, seriesRequest, seriesResponse); accountingType, seriesRequest, seriesResponse);
SessionUtil.setAccountingStateData(session, accountingType, SessionUtil.setAccountingStateData(session, accountingType,
@ -217,7 +231,7 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
} }
} }
/** /**
* *
* {@inheritDoc} * {@inheritDoc}
@ -230,9 +244,9 @@ 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;
} catch (ServiceException e) { } catch (ServiceException e) {
@ -245,7 +259,6 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
} }
} }
@Override @Override
public ItemDescription saveCSVOnWorkspace(AccountingType accountingType) public ItemDescription saveCSVOnWorkspace(AccountingType accountingType)

View File

@ -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,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 { try {
logger.info("Current context is " + aslSession.getScope()); logger.info("Current context is " + aslSession.getScope());
ArrayList<String> contexts = new ArrayList<>(); ArrayList<String> contexts = new ArrayList<>();
contexts.add(aslSession.getScope()); contexts.add(aslSession.getScope());
GroupManager gm = new LiferayGroupManager(); GroupManager gm = new LiferayGroupManager();
long currentGroupId = gm long currentGroupId = gm
.getGroupIdFromInfrastructureScope(aslSession.getScope()); .getGroupIdFromInfrastructureScope(aslSession.getScope());
@ -156,9 +171,8 @@ 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;
} catch (Exception e) { } catch (Exception e) {

View File

@ -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();
@ -63,7 +63,7 @@ public class AccountingCaller implements AccountingCallerInterface {
try { try {
logger.debug("getSeries(): [AccountingType=" + accountingType logger.debug("getSeries(): [AccountingType=" + accountingType
+ " , seriesRequest=" + seriesRequest + "]"); + " , seriesRequest=" + seriesRequest + "]");
AccountingQueryBuilder queryBuilder = getAccountQueryBuilder( AccountingQueryBuilder queryBuilder = getAccountQueryBuilder(
accountingType, seriesRequest); accountingType, seriesRequest);

View 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);
}
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -19,7 +19,7 @@ public class Constants {
public static final String DEFAULT_USER = "giancarlo.panichi"; public static final String DEFAULT_USER = "giancarlo.panichi";
public final static String DEFAULT_SCOPE = "/gcube/devNext/NextNext"; 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_TOKEN = "6af6eaff-35bd-4405-b747-f63246d0212a-98187548";
// public final static String DEFAULT_SCOPE = "/gcube/devsec/devVRE"; // public final static String DEFAULT_SCOPE = "/gcube/devsec/devVRE";
// public final static String DEFAULT_TOKEN = // public final static String DEFAULT_TOKEN =
// "16e65d4f-11e0-4e4a-84b9-351688fccc12-98187548"; // "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 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 };;
@ -37,6 +39,6 @@ public class Constants {
//IS Resource //IS Resource
public static final String ACCOUNTING_NAME = "AccountingManager"; public static final String ACCOUNTING_NAME = "AccountingManager";
public static final String ACCOUNTING_CATEGORY = "AccountingProfile"; public static final String ACCOUNTING_CATEGORY = "AccountingProfile";
} }

View File

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