From 718d208b0dae9a5071893288c77f84fc91e63f43 Mon Sep 17 00:00:00 2001 From: Anthony Dahanne Date: Mon, 6 Aug 2018 19:12:17 -0400 Subject: [PATCH] Add clear cache button Set timeouts to 5 seconds --- .../tinypounder/CacheManagerBusiness.java | 2 + .../CacheManagerBusinessReflectionImpl.java | 57 ++++++++++++++----- .../tinypounder/TinyPounderMainUI.java | 13 ++++- .../CacheManagerBusinessApiImpl.java | 11 ++-- 4 files changed, 63 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/terracotta/tinypounder/CacheManagerBusiness.java b/src/main/java/org/terracotta/tinypounder/CacheManagerBusiness.java index 583a92f..bbb628b 100644 --- a/src/main/java/org/terracotta/tinypounder/CacheManagerBusiness.java +++ b/src/main/java/org/terracotta/tinypounder/CacheManagerBusiness.java @@ -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); diff --git a/src/main/java/org/terracotta/tinypounder/CacheManagerBusinessReflectionImpl.java b/src/main/java/org/terracotta/tinypounder/CacheManagerBusinessReflectionImpl.java index 0314695..fded92f 100644 --- a/src/main/java/org/terracotta/tinypounder/CacheManagerBusinessReflectionImpl.java +++ b/src/main/java/org/terracotta/tinypounder/CacheManagerBusinessReflectionImpl.java @@ -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; @@ -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 { @@ -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); @@ -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"); @@ -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"); @@ -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); @@ -348,7 +375,7 @@ 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); @@ -356,12 +383,16 @@ private Object constructClusteringServiceConfigurationBuilder(String clusterTier 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; } diff --git a/src/main/java/org/terracotta/tinypounder/TinyPounderMainUI.java b/src/main/java/org/terracotta/tinypounder/TinyPounderMainUI.java index a967540..8552006 100644 --- a/src/main/java/org/terracotta/tinypounder/TinyPounderMainUI.java +++ b/src/main/java/org/terracotta/tinypounder/TinyPounderMainUI.java @@ -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 { @@ -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); } diff --git a/src/test/java/org/terracotta/tinypounder/CacheManagerBusinessApiImpl.java b/src/test/java/org/terracotta/tinypounder/CacheManagerBusinessApiImpl.java index 8dde6fc..b3159f1 100644 --- a/src/test/java/org/terracotta/tinypounder/CacheManagerBusinessApiImpl.java +++ b/src/test/java/org/terracotta/tinypounder/CacheManagerBusinessApiImpl.java @@ -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(); @@ -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 { @@ -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 cache = cacheManager.getCache(cmName, Integer.class, String.class); -//cache.put(); EhcacheManager cacheManager = (EhcacheManager) cacheManagerBuilder.build(); cacheManager.init(); this.cacheManager = cacheManager;