1 7 package org.jboss.cache; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.jboss.cache.config.EvictionRegionConfig; 12 import org.jboss.cache.eviction.EvictedEventNode; 13 import org.jboss.cache.eviction.EvictionPolicy; 14 import org.jboss.cache.eviction.EvictionPolicyConfig; 15 import org.jboss.cache.eviction.NodeEventType; 16 import org.jboss.cache.util.Util; 17 18 import java.util.concurrent.BlockingQueue ; 19 import java.util.concurrent.LinkedBlockingQueue ; 20 import java.util.concurrent.TimeUnit ; 21 22 27 public class RegionImpl implements Region 28 { 29 private static final Log log = LogFactory.getLog(RegionImpl.class); 30 31 private RegionManager regionManager; 32 private Fqn fqn; 33 private boolean active; 34 private ClassLoader classLoader; 35 private BlockingQueue <EvictedEventNode> nodeEventQueue = null; 36 private int capacityWarnThreshold = 0; 37 private EvictionRegionConfig configuration = new EvictionRegionConfig(); 38 private EvictionPolicy policy; 39 40 43 public RegionImpl(Fqn fqn, RegionManager regionManager) 44 { 45 this.fqn = fqn; 46 this.regionManager = regionManager; 47 this.active = !regionManager.isDefaultInactive(); 48 } 49 50 53 public RegionImpl(EvictionPolicy policy, EvictionRegionConfig config, Fqn fqn, RegionManager regionManager) 54 { 55 this(fqn, regionManager); 56 this.configuration = config; 57 this.policy = policy; 58 createQueue(); 59 } 60 61 public void registerContextClassLoader(ClassLoader classLoader) 62 { 63 this.classLoader = classLoader; 64 } 65 66 public void unregisterContextClassLoader() 67 { 68 this.classLoader = null; 69 } 70 71 public void activate() 72 { 73 regionManager.activate(fqn); 74 active = true; 75 } 76 77 public void deactivate() 78 { 79 regionManager.deactivate(fqn); 80 active = false; 81 } 82 83 public boolean isActive() 84 { 85 return active; 86 } 87 88 public ClassLoader getClassLoader() 89 { 90 return classLoader; 91 } 92 93 public Fqn getFqn() 94 { 95 return fqn; 96 } 97 98 public void setActive(boolean b) 99 { 100 active = b; 101 } 102 103 105 public void markNodeCurrentlyInUse(Fqn fqn, long timeout) 106 { 107 EvictedEventNode markUse = new EvictedEventNode(fqn, NodeEventType.MARK_IN_USE_EVENT); 108 markUse.setInUseTimeout(timeout); 109 putNodeEvent(markUse); 110 } 111 112 public void unmarkNodeCurrentlyInUse(Fqn fqn) 113 { 114 EvictedEventNode markNoUse = new EvictedEventNode(fqn, NodeEventType.UNMARK_USE_EVENT); 115 putNodeEvent(markNoUse); 116 } 117 118 public String toString() 119 { 120 return "RegionImpl{" + 121 "fqn=" + fqn + 122 "; active=" + active + 123 "; eviction=" + (getEvictionPolicy() != null) + 124 "; timerThreadRegistered=" + (getEvictionPolicy() != null && regionManager.getEvictionTimerTask().isRegionRegisteredForProcessing(this)) + 125 '}'; 126 } 127 128 public int compareTo(Region other) 129 { 130 return getFqn().compareTo(other.getFqn()); 131 } 132 133 public void putNodeEvent(EvictedEventNode event) 134 { 135 try 136 { 137 if (nodeEventQueue == null) createQueue(); if (nodeEventQueue.size() > capacityWarnThreshold) 139 { 140 log.warn("putNodeEvent(): eviction node event queue size is at 98% threshold value of capacity: " + configuration.getEventQueueSize() + 141 " Region: " + fqn + 142 " You will need to reduce the wakeUpIntervalSeconds parameter."); 143 } 144 145 nodeEventQueue.put(event); 146 } 147 catch (InterruptedException e) 148 { 149 log.debug("give up put", e); 150 } 151 } 152 153 157 public EvictedEventNode takeLastEventNode() 158 { 159 try 160 { 161 return nodeEventQueue.poll(0, TimeUnit.SECONDS); 162 } 163 catch (InterruptedException e) 164 { 165 log.debug("trace", e); 166 } 167 return null; 168 } 169 170 public int nodeEventQueueSize() 171 { 172 return nodeEventQueue.size(); 173 } 174 175 public void resetEvictionQueues() 176 { 177 nodeEventQueue.clear(); 178 } 179 180 private synchronized void createQueue() 181 { 182 if (nodeEventQueue == null) 183 { 184 if (configuration == null) 185 { 186 throw new IllegalArgumentException ("null eviction configuration"); 187 } 188 int size = configuration.getEventQueueSize(); 189 capacityWarnThreshold = (98 * size) / 100 - 100; 190 if (capacityWarnThreshold <= 0) 191 { 192 throw new RuntimeException ("Capacity warn threshold used in eviction is smaller than 1."); 193 } 194 nodeEventQueue = new LinkedBlockingQueue <EvictedEventNode>(size); 195 } 196 } 197 198 public EvictionRegionConfig getEvictionRegionConfig() 199 { 200 return this.configuration; 201 } 202 203 public EvictionPolicyConfig getEvictionPolicyConfig() 204 { 205 return configuration == null ? null : configuration.getEvictionPolicyConfig(); 206 } 207 208 public EvictionPolicy getEvictionPolicy() 209 { 210 return policy; 211 } 212 213 public void setEvictionPolicy(EvictionPolicyConfig evictionPolicyConfig) 214 { 215 configuration.setEvictionPolicyConfig(evictionPolicyConfig); 216 policy = createPolicy(evictionPolicyConfig.getEvictionPolicyClass()); 217 regionManager.getEvictionTimerTask().addRegionToProcess(this); 218 if (nodeEventQueue == null) createQueue(); 219 } 220 221 private EvictionPolicy createPolicy(String className) 222 { 223 if (className == null) 224 { 225 throw new IllegalArgumentException ("null className"); 226 } 227 try 228 { 229 if (log.isTraceEnabled()) log.trace("Instantiating " + className); 230 EvictionPolicy ep = (EvictionPolicy) Util.loadClass(className).newInstance(); 231 ep.configure(regionManager.cache); 232 return ep; 233 } 234 catch (Exception e) 235 { 236 log.fatal("Unable to instantiate eviction policy class " + className, e); 237 return null; 238 } 239 } 240 } 241 | Popular Tags |