Commit 233a21a8 authored by Matija Obreza's avatar Matija Obreza
Browse files

Clear cache by name

parent d0f09b86
/**
* Copyright 2015 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.servlet.controller.admin; package org.genesys2.server.servlet.controller.admin;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -15,6 +31,7 @@ import org.springframework.stereotype.Controller; ...@@ -15,6 +31,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.hazelcast.core.DistributedObject; import com.hazelcast.core.DistributedObject;
import com.hazelcast.core.Hazelcast; import com.hazelcast.core.Hazelcast;
...@@ -22,123 +39,136 @@ import com.hazelcast.core.HazelcastInstance; ...@@ -22,123 +39,136 @@ import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap; import com.hazelcast.core.IMap;
import com.hazelcast.monitor.LocalMapStats; import com.hazelcast.monitor.LocalMapStats;
/**
* Manage caches
*
* @author mobreza
*/
@Controller @Controller
@RequestMapping("/admin/cache") @RequestMapping("/admin/cache")
@PreAuthorize("hasRole('ADMINISTRATOR')") @PreAuthorize("hasRole('ADMINISTRATOR')")
public class CacheController { public class CacheController {
public static final Log LOG = LogFactory.getLog(AdminController.class); public static final Log LOG = LogFactory.getLog(AdminController.class);
@Autowired @Autowired
private MappingService mappingService; private MappingService mappingService;
@Autowired @Autowired
private CacheManager cacheManager; private CacheManager cacheManager;
@RequestMapping(method = RequestMethod.POST, value = "/clearTilesCache")
@RequestMapping(method = RequestMethod.POST, value = "/clearTilesCache") public String clearTilesCache() {
public String clearTilesCache() { final Cache tileServerCache = cacheManager.getCache("tileserver");
final Cache tileServerCache = cacheManager.getCache("tileserver"); System.err.println("tileServerCache=" + tileServerCache.getNativeCache());
System.err.println("tileServerCache=" + tileServerCache.getNativeCache());
@SuppressWarnings("rawtypes")
@SuppressWarnings("rawtypes") final IMap hazelCache = (IMap) tileServerCache.getNativeCache();
final IMap hazelCache = (IMap) tileServerCache.getNativeCache();
LOG.info("Tiles cache size=" + hazelCache.size());
LOG.info("Tiles cache size=" + hazelCache.size()); int count = 0;
int count = 0; for (final Object key : hazelCache.keySet()) {
for (final Object key : hazelCache.keySet()) { LOG.info("\tkey=" + key);
LOG.info("\tkey=" + key); if (++count > 20) {
if (++count > 20) { break;
break; }
} }
} mappingService.clearCache();
mappingService.clearCache(); LOG.info("Tiles cache size=" + hazelCache.size());
LOG.info("Tiles cache size=" + hazelCache.size());
return "redirect:/admin/cache/";
return "redirect:/admin/cache/"; }
}
@RequestMapping(method = RequestMethod.POST, value = "/clearCaches")
@RequestMapping(method = RequestMethod.POST, value = "/clearCaches") public String clearCaches() {
public String clearCaches() { for (String cacheName : cacheManager.getCacheNames()) {
for (String cacheName : cacheManager.getCacheNames()) { clearCache(cacheName);
final Cache cache = cacheManager.getCache(cacheName); }
LOG.info("Clearing cache " + cacheName); return "redirect:/admin/cache/";
cache.clear(); }
}
return "redirect:/admin/cache/"; @RequestMapping(method = RequestMethod.POST, value = "/clearCache")
} public String clearCache(@RequestParam(required = true, value = "name") String cacheName) {
final Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
@RequestMapping("/") LOG.info("Clearing cache " + cacheName);
public String cacheStats(Model model) { cache.clear();
List<CacheStats> cacheMaps = new ArrayList<CacheStats>(); } else {
List<Object> cacheOther = new ArrayList<Object>(); LOG.info("No such cache: " + cacheName);
}
Set<HazelcastInstance> instances = Hazelcast.getAllHazelcastInstances(); return "redirect:/admin/cache/";
for (HazelcastInstance hz : instances) { }
if (LOG.isDebugEnabled())
LOG.debug("\n\nCache stats Instance: " + hz.getName()); @RequestMapping("/")
for (DistributedObject o : hz.getDistributedObjects()) { public String cacheStats(Model model) {
if (o instanceof IMap) { List<CacheStats> cacheMaps = new ArrayList<CacheStats>();
IMap imap = (IMap) o; List<Object> cacheOther = new ArrayList<Object>();
cacheMaps.add(new CacheStats(imap));
// System.out.println(imap.getServiceName() + ": " + Set<HazelcastInstance> instances = Hazelcast.getAllHazelcastInstances();
// imap.getName() + " " + imap.getPartitionKey()); for (HazelcastInstance hz : instances) {
// LocalMapStats localMapStats = imap.getLocalMapStats(); if (LOG.isDebugEnabled())
// System.out.println("created: " + LOG.debug("\n\nCache stats Instance: " + hz.getName());
// localMapStats.getCreationTime()); for (DistributedObject o : hz.getDistributedObjects()) {
// System.out.println("owned entries: " + if (o instanceof IMap) {
// localMapStats.getOwnedEntryCount()); IMap imap = (IMap) o;
// System.out.println("backup entries: " + cacheMaps.add(new CacheStats(imap));
// localMapStats.getBackupEntryCount()); // System.out.println(imap.getServiceName() + ": " +
// System.out.println("locked entries: " + // imap.getName() + " " + imap.getPartitionKey());
// localMapStats.getLockedEntryCount()); // LocalMapStats localMapStats = imap.getLocalMapStats();
// System.out.println("dirty entries: " + // System.out.println("created: " +
// localMapStats.getDirtyEntryCount()); // localMapStats.getCreationTime());
// System.out.println("hits: " + localMapStats.getHits()); // System.out.println("owned entries: " +
// System.out.println("puts: " + // localMapStats.getOwnedEntryCount());
// localMapStats.getPutOperationCount()); // System.out.println("backup entries: " +
// System.out.println("last update: " + // localMapStats.getBackupEntryCount());
// localMapStats.getLastUpdateTime()); // System.out.println("locked entries: " +
// System.out.println("last access:" + // localMapStats.getLockedEntryCount());
// localMapStats.getLastAccessTime()); // System.out.println("dirty entries: " +
} else { // localMapStats.getDirtyEntryCount());
if (LOG.isDebugEnabled()) // System.out.println("hits: " + localMapStats.getHits());
LOG.debug(o.getClass() + " " + o); // System.out.println("puts: " +
cacheOther.add(o); // localMapStats.getPutOperationCount());
} // System.out.println("last update: " +
} // localMapStats.getLastUpdateTime());
} // System.out.println("last access:" +
// localMapStats.getLastAccessTime());
model.addAttribute("cacheMaps", cacheMaps); } else {
model.addAttribute("cacheOther", cacheOther); if (LOG.isDebugEnabled())
LOG.debug(o.getClass() + " " + o);
return "/admin/cache"; cacheOther.add(o);
} }
}
public static final class CacheStats { }
private String serviceName; model.addAttribute("cacheMaps", cacheMaps);
private String name; model.addAttribute("cacheOther", cacheOther);
private LocalMapStats mapStats;
return "/admin/cache";
public CacheStats(IMap<?, ?> imap) { }
this.serviceName = imap.getServiceName();
this.name = imap.getName(); public static final class CacheStats {
this.mapStats = imap.getLocalMapStats();
} private String serviceName;
private String name;
public String getServiceName() { private LocalMapStats mapStats;
return serviceName;
} public CacheStats(IMap<?, ?> imap) {
this.serviceName = imap.getServiceName();
public String getName() { this.name = imap.getName();
return name; this.mapStats = imap.getLocalMapStats();
} }
public LocalMapStats getMapStats() { public String getServiceName() {
return mapStats; return serviceName;
} }
}
public String getName() {
return name;
}
public LocalMapStats getMapStats() {
return mapStats;
}
}
} }
...@@ -32,6 +32,12 @@ ...@@ -32,6 +32,12 @@
<c:out value="${cacheMap.serviceName}" /> <c:out value="${cacheMap.serviceName}" />
<c:out value="${cacheMap.name}" /> <c:out value="${cacheMap.name}" />
</h3> </h3>
<form method="post" action="<c:url value="/admin/cache/clearCache" />">
<input type="hidden" name="name" value="${cacheMap.name}" />
<input type="submit" class="btn btn-default" value="Clear" />
<!-- CSRF protection -->
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
<c:set value="${cacheMap.mapStats}" var="mapStat" /> <c:set value="${cacheMap.mapStats}" var="mapStat" />
<div class="row"> <div class="row">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment