Skip to content

Commit

Permalink
Merge pull request #44 from anthonydahanne/add-clear-set-timeouts
Browse files Browse the repository at this point in the history
Add clear cache button
  • Loading branch information
boyusun-SAG authored Aug 6, 2018
2 parents b6fddfa + 718d208 commit e3c306e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public interface CacheManagerBusiness {

void removeCache(String alias);

void clearCache(String alias);

String retrieveHumanReadableConfiguration();

void initializeCacheManager(String terracottaServerUrl, String cmName, String tinyPounderDiskPersistenceLocation, String defaultOffheapResource, String diskResource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
Expand Down Expand Up @@ -206,6 +207,19 @@ public void removeCache(String alias) {
}
}

@Override
public void clearCache(String alias) {
try {
Thread.currentThread().setContextClassLoader(kitAwareClassLoaderDelegator.getUrlClassLoader());
Object cache = getCache(alias);
Class ehCacheClass = loadClass("org.ehcache.core.Ehcache");
Method clearCacheMethod = ehCacheClass.getMethod("clear");
clearCacheMethod.invoke(cache);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public String retrieveHumanReadableConfiguration() {
try {
Expand Down Expand Up @@ -256,11 +270,7 @@ public void initializeCacheManager(String terracottaServerUrl, String cmName, St
} catch (Exception e) {
defaultManagementRegistryConfiguration = null;
}
if (kitAwareClassLoaderDelegator.isEEKit()) {
cacheManager = constructCacheManagerBuilder(clusteringServiceConfigurationBuilder, cacheManagerPersistenceConfiguration, defaultManagementRegistryConfiguration);
} else {
cacheManager = constructCacheManagerBuilder(clusteringServiceConfigurationBuilder, cacheManagerPersistenceConfiguration, defaultManagementRegistryConfiguration);
}
cacheManager = constructCacheManagerBuilder(clusteringServiceConfigurationBuilder, cacheManagerPersistenceConfiguration, defaultManagementRegistryConfiguration);
ehCacheManagerClass = loadClass("org.ehcache.core.EhcacheManager");
Method initMethod = ehCacheManagerClass.getMethod("init");
initMethod.invoke(cacheManager);
Expand Down Expand Up @@ -292,7 +302,9 @@ private Object constructCacheManagerPersistenceConfiguration(File tinyPounderDis
return cacheManagerPersistenceConfigurationConstructor.newInstance(tinyPounderDiskPersistenceLocationFolder);
}

private Object constructCacheManagerBuilder(Object enterpriseClusteringServiceConfigurationBuilder, Object cacheManagerPersistenceConfiguration, Object defaultManagementRegistryConfiguration) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
private Object constructCacheManagerBuilder(Object enterpriseClusteringServiceConfigurationBuilder,
Object cacheManagerPersistenceConfiguration,
Object defaultManagementRegistryConfiguration) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Class<?> cacheManagerBuilderClass = loadClass("org.ehcache.config.builders.CacheManagerBuilder");
Method newCacheManagerBuilderMethod = cacheManagerBuilderClass.getMethod("newCacheManagerBuilder");
Class<?> builderClass = loadClass("org.ehcache.config.Builder");
Expand Down Expand Up @@ -323,6 +335,16 @@ private Object constructClusteringServiceConfigurationBuilder(String clusterTier
Method valueOfMethod = memoryUnitClass.getMethod("valueOf", String.class);
Object mB = valueOfMethod.invoke(null, "MB");

Object timeoutsInstance = null;
Class<?> timeoutsClass = null;
try {
timeoutsClass = loadClass("org.ehcache.clustered.client.config.Timeouts");
Constructor timeoutsClassConstructor = timeoutsClass.getConstructor(new Class[]{Duration.class, Duration.class, Duration.class});
timeoutsInstance = timeoutsClassConstructor.newInstance(Duration.ofSeconds(5), Duration.ofSeconds(5), Duration.ofSeconds(5));
} catch (Exception e) {
// nevermind, the api wrt timeouts is fairly recent, 3.5.x, so if running an old ehcache, we won't configure timeouts; oh well...
}

if (eeKit) {

Class<?> enterpriseServerSideConfigurationBuilderClass = loadClass("com.terracottatech.ehcache.clustered.client.config.builders.EnterpriseServerSideConfigurationBuilder");
Expand All @@ -339,6 +361,11 @@ private Object constructClusteringServiceConfigurationBuilder(String clusterTier


Object enterpriseClusteringServiceConfigurationBuilder = enterpriseClusterMethod.invoke(null, clusterUri.resolve(clusterTierManagerName));
if (timeoutsClass != null && timeoutsInstance != null) {
Method timeoutsMethod = enterpriseClusteringServiceConfigurationBuilderClass.getMethod("timeouts", timeoutsClass);
enterpriseClusteringServiceConfigurationBuilder = timeoutsMethod.invoke(enterpriseClusteringServiceConfigurationBuilder, timeoutsInstance);
}

Object enterpriseServerSideConfigurationBuilder = autoCreateMethod.invoke(enterpriseClusteringServiceConfigurationBuilder);
enterpriseServerSideConfigurationBuilder = defaultServerResourceMethod.invoke(enterpriseServerSideConfigurationBuilder, defaultOffheapResource);
enterpriseServerSideConfigurationBuilder = resourcePoolMethod4.invoke(enterpriseServerSideConfigurationBuilder, "resource-pool-a", 128L, mB, defaultOffheapResource);
Expand All @@ -348,20 +375,24 @@ private Object constructClusteringServiceConfigurationBuilder(String clusterTier

Class<?> serverSideConfigurationBuilderClass = loadClass("org.ehcache.clustered.client.config.builders.ServerSideConfigurationBuilder");
Class<?> clusteringServiceConfigurationBuilderClass = loadClass("org.ehcache.clustered.client.config.builders.ClusteringServiceConfigurationBuilder");
Method enterpriseClusterMethod = clusteringServiceConfigurationBuilderClass.getMethod("cluster", URI.class);
Method clusterMethod = clusteringServiceConfigurationBuilderClass.getMethod("cluster", URI.class);

Method autoCreateMethod = clusteringServiceConfigurationBuilderClass.getMethod("autoCreate");
Method defaultServerResourceMethod = serverSideConfigurationBuilderClass.getMethod("defaultServerResource", String.class);
Method resourcePoolMethod4 = serverSideConfigurationBuilderClass.getMethod("resourcePool", String.class, long.class, memoryUnitClass, String.class);
Method resourcePoolMethod3 = serverSideConfigurationBuilderClass.getMethod("resourcePool", String.class, long.class, memoryUnitClass);


Object enterpriseClusteringServiceConfigurationBuilder = enterpriseClusterMethod.invoke(null, clusterUri.resolve(clusterTierManagerName));
Object enterpriseServerSideConfigurationBuilder = autoCreateMethod.invoke(enterpriseClusteringServiceConfigurationBuilder);
enterpriseServerSideConfigurationBuilder = defaultServerResourceMethod.invoke(enterpriseServerSideConfigurationBuilder, defaultOffheapResource);
enterpriseServerSideConfigurationBuilder = resourcePoolMethod4.invoke(enterpriseServerSideConfigurationBuilder, "resource-pool-a", 128L, mB, defaultOffheapResource);
enterpriseServerSideConfigurationBuilder = resourcePoolMethod3.invoke(enterpriseServerSideConfigurationBuilder, "resource-pool-b", 64L, mB);
return enterpriseServerSideConfigurationBuilder;
Object clusteringServiceConfigurationBuilder = clusterMethod.invoke(null, clusterUri.resolve(clusterTierManagerName));
if (timeoutsClass != null && timeoutsInstance != null) {
Method timeoutsMethod = clusteringServiceConfigurationBuilderClass.getMethod("timeouts", timeoutsClass);
clusteringServiceConfigurationBuilder = timeoutsMethod.invoke(clusteringServiceConfigurationBuilder, timeoutsInstance);
}
Object serverSideConfigurationBuilder = autoCreateMethod.invoke(clusteringServiceConfigurationBuilder);
serverSideConfigurationBuilder = defaultServerResourceMethod.invoke(serverSideConfigurationBuilder, defaultOffheapResource);
serverSideConfigurationBuilder = resourcePoolMethod4.invoke(serverSideConfigurationBuilder, "resource-pool-a", 128L, mB, defaultOffheapResource);
serverSideConfigurationBuilder = resourcePoolMethod3.invoke(serverSideConfigurationBuilder, "resource-pool-b", 64L, mB);
return serverSideConfigurationBuilder;
}


Expand Down
13 changes: 12 additions & 1 deletion src/main/java/org/terracotta/tinypounder/TinyPounderMainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,17 @@ private void addCacheControls() {
}
});

Button clearCacheButton = new Button("Clear cache");
clearCacheButton.addClickListener(event -> {
try {
cacheManagerBusiness.clearCache(cacheName);
displayWarningNotification("Cache cleared with success !");
} catch (RuntimeException e) {
displayErrorNotification("Cache could not be cleared !", e);
refreshCacheStuff(listDataProvider);
}
});

Button destroyCacheButton = new Button("Destroy cache");
destroyCacheButton.addClickListener(event -> {
try {
Expand All @@ -1276,7 +1287,7 @@ private void addCacheControls() {
refreshCacheStuff(listDataProvider);
}
});
cacheInfo.addComponentsAndExpand(cacheNameLabel, poundingSlider, removeCacheButton, destroyCacheButton);
cacheInfo.addComponentsAndExpand(cacheNameLabel, poundingSlider, clearCacheButton, removeCacheButton, destroyCacheButton);
cacheList.addComponent(cacheInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public void removeCache(String alias) {
cacheManager.removeCache(alias);
}

@Override
public void clearCache(String alias) {
cacheManager.getCache(alias, Integer.class, String.class);
}

@Override
public String retrieveHumanReadableConfiguration() {
return ((HumanReadable) cacheManager.getRuntimeConfiguration()).readableString();
Expand All @@ -117,7 +122,6 @@ public String retrieveHumanReadableConfiguration() {
public void initializeCacheManager(String terracottaServerUrl, String cmName, String tinyPounderDiskPersistenceLocation, String defaultOffheapResource, String diskResource) {
URI clusterUri = URI.create("terracotta://" + terracottaServerUrl + "/" + cmName);


File tinyPounderDiskPersistenceLocationFolder = new File(tinyPounderDiskPersistenceLocation);
if (tinyPounderDiskPersistenceLocationFolder.exists()) {
try {
Expand All @@ -134,14 +138,9 @@ public void initializeCacheManager(String terracottaServerUrl, String cmName, St
.resourcePool("resource-pool-a", 128, MemoryUnit.MB, "offheap-2")
.resourcePool("resource-pool-b", 64, MemoryUnit.MB)
).with(new CacheManagerPersistenceConfiguration(tinyPounderDiskPersistenceLocationFolder))
// .using(new DefaultManagementRegistryConfiguration()
// .addTags("tiny", "pounder", "client")
// .setCacheManagerAlias(cmName)
// )
;

Cache<Integer, String> cache = cacheManager.getCache(cmName, Integer.class, String.class);
//cache.put();
EhcacheManager cacheManager = (EhcacheManager) cacheManagerBuilder.build();
cacheManager.init();
this.cacheManager = cacheManager;
Expand Down

0 comments on commit e3c306e

Please sign in to comment.