#1679 - ENH: Support enabling/disabling L2 cache by "region" at runtime

This commit is contained in:
rob bygrave committed 2019-04-24 15:41:07 +12:00
1 parent ae228d6b0e
commit 86436ac3ac
10 files changed
+111 -87

No files matched your search

@@ -61,25 +61,29 @@ public class DefaultServerCacheManager implements SpiCacheManager {
@Override
public void setEnabledRegions(String regions) {
if (regions != null) {
List<String> enabledRegionNames = Arrays.asList(regions.split(","));
List<String> enabledRegions = Arrays.asList(regions.split(","));
List<String> disabledRegions = new ArrayList<>();
List<String> disabled = new ArrayList<>();
List<String> enabled = new ArrayList<>();
for (SpiCacheRegion region : regionMap.values()) {
if (enabledRegions.contains(region.getName())) {
if (!region.isEnabled()) {
region.setEnabled(true);
log.debug("Cache region[{}] enabled", region.getName());
}
} else {
disabledRegions.add(region.getName());
if (region.isEnabled()) {
region.setEnabled(false);
log.debug("Cache region[{}] disabled", region.getName());
for (SpiCacheRegion region : regionMap.values()) {
if (enabledRegionNames.contains(region.getName())) {
enabled.add(region.getName());
if (!region.isEnabled()) {
region.setEnabled(true);
log.debug("Cache region[{}] enabled", region.getName());
}
} else {
disabled.add(region.getName());
if (region.isEnabled()) {
region.setEnabled(false);
log.debug("Cache region[{}] disabled", region.getName());
}
}
}
log.info("Cache regions enabled:{} disabled:{}", enabled, disabled);
}
log.info("Cache regions enabled:{} disabled:{}", enabledRegions, disabledRegions);
}
@Override
@@ -12,7 +12,9 @@ public class CacheOptions {
*/
public static final CacheOptions NO_CACHING = new CacheOptions();
public static final CacheOptions INVALIDATE_QUERY_CACHE = new CacheOptions(true);
private static final String R0 = "r0";
private static final CacheOptions INVALIDATE_QUERY_CACHE_R0 = new CacheOptions(true);
private final boolean invalidateQueryCache;
private final boolean enableBeanCache;
@@ -21,6 +23,14 @@ public class CacheOptions {
private final String[] naturalKey;
private final String region;
public static CacheOptions invalidateQueryCache(String region) {
if (R0.equals(region)) {
return INVALIDATE_QUERY_CACHE_R0;
} else {
return new CacheOptions(true, region);
}
}
/**
* Construct for no caching.
*/
@@ -37,12 +47,16 @@ public class CacheOptions {
* Construct for invalidateQueryCache.
*/
private CacheOptions(boolean invalidateQueryCache) {
this(invalidateQueryCache, R0);
}
private CacheOptions(boolean invalidateQueryCache, String region) {
this.invalidateQueryCache = invalidateQueryCache;
this.enableBeanCache = false;
this.enableQueryCache = false;
this.readOnly = false;
this.naturalKey = null;
this.region = null;
this.region = region;
}
/**
@@ -446,6 +446,7 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
if (encryptKeyManager != null) {
encryptKeyManager.initialise();
}
serverCacheManager.setEnabledRegions(serverConfig.getEnabledL2Regions());
}
/**
@@ -484,7 +485,7 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
*/
private void shutdownInternal(boolean shutdownDataSource, boolean deregisterDriver) {
logger.debug("Shutting down EbeanServer {}", serverName);
logger.debug("Shutting down instance:{}", serverName);
if (shutdown) {
// already shutdown
return;
@@ -790,23 +791,11 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
transactionManager.exitScopedTransaction(returnOrThrowable, opCode);
}
/**
* Returns the current transaction (or null) from the scope.
*/
@Override
public SpiTransaction currentServerTransaction() {
return transactionManager.getActive();
}
/**
* Start a transaction with 'REQUIRED' semantics.
* <p>
* If a transaction already exists that transaction will be used.
* </p>
* <p>
* Note that the transaction is stored in a ThreadLocal variable.
* </p>
*/
@Override
public Transaction beginTransaction() {
return beginTransaction(TxScope.required());
@@ -817,12 +806,6 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
return transactionManager.beginScopedTransaction(txScope);
}
/**
* Start a transaction with a specific Isolation Level.
* <p>
* Note that the transaction is stored in a ThreadLocal variable.
* </p>
*/
@Override
public Transaction beginTransaction(TxIsolation isolation) {
// start an explicit transaction
@@ -837,10 +820,6 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
return t;
}
/**
* Return the current transaction or null if there is not one currently in
* scope.
*/
@Override
public Transaction currentTransaction() {
return transactionManager.getActive();
@@ -851,50 +830,16 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
currentTransaction().flush();
}
/**
* Commit the current transaction.
*/
@Override
public void commitTransaction() {
currentTransaction().commit();
}
/**
* Rollback the current transaction.
*/
@Override
public void rollbackTransaction() {
currentTransaction().rollback();
}
/**
* If the current transaction has already been committed do nothing otherwise
* rollback the transaction.
* <p>
* Useful to put in a finally block to ensure the transaction is ended, rather
* than a rollbackTransaction() in each catch block.
* </p>
* <p>
* Code example:<br />
* <p>
* <pre>
* &lt;code&gt;
* Ebean.startTransaction();
* try {
* // do some fetching and or persisting
*
* // commit at the end
* Ebean.commitTransaction();
*
* } finally {
* // if commit didn't occur then rollback the transaction
* Ebean.endTransaction();
* }
* &lt;/code&gt;
* </pre>
* <p>
* </p>
*/
@Override
public void endTransaction() {
Transaction transaction = transactionManager.getInScope();
@@ -903,13 +848,6 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
}
}
/**
* return the next unique identity value.
* <p>
* Uses the BeanDescriptor deployment information to determine the sequence to
* use.
* </p>
*/
@Override
public Object nextId(Class<?> beanType) {
BeanDescriptor<?> desc = getBeanDescriptor(beanType);
@@ -473,8 +473,8 @@ public class DeployBeanDescriptor<T> {
/**
* Set that this type invalidates query caches.
*/
public void setInvalidateQueryCache() {
this.cacheOptions = CacheOptions.INVALIDATE_QUERY_CACHE;
public void setInvalidateQueryCache(String region) {
this.cacheOptions = CacheOptions.invalidateQueryCache(region);
}
/**
@@ -202,13 +202,12 @@ public class AnnotationClass extends AnnotationParser {
} else {
InvalidateQueryCache invalidateQueryCache = AnnotationUtil.findAnnotationRecursive(cls, InvalidateQueryCache.class);
if (invalidateQueryCache != null) {
descriptor.setInvalidateQueryCache();
descriptor.setInvalidateQueryCache(invalidateQueryCache.region());
}
}
}
Set<NamedQuery> namedQueries = AnnotationUtil.findAnnotationsRecursive(cls, NamedQuery.class);
for (NamedQuery namedQuery : namedQueries) {
for (NamedQuery namedQuery : AnnotationUtil.findAnnotationsRecursive(cls, NamedQuery.class)) {
descriptor.addNamedQuery(namedQuery.name(), namedQuery.query());
}
}