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