1 | package com.bowman.cardserv;
|
---|
2 |
|
---|
3 | import com.bowman.cardserv.interfaces.*;
|
---|
4 | import com.bowman.cardserv.util.*;
|
---|
5 |
|
---|
6 | import java.util.*;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Created by IntelliJ IDEA.
|
---|
10 | * User: bowman
|
---|
11 | * Date: Oct 8, 2005
|
---|
12 | * Time: 5:54:37 PM
|
---|
13 | */
|
---|
14 | public class DefaultCache implements CacheHandler {
|
---|
15 |
|
---|
16 | protected ProxyLogger logger;
|
---|
17 | protected MessageCacheMap pendingEcms;
|
---|
18 | protected MessageCacheMap ecmMap;
|
---|
19 | protected CacheListener listener, monitor;
|
---|
20 |
|
---|
21 | protected long maxAge;
|
---|
22 | private long maxCacheWait;
|
---|
23 | private int maxWaitPercent;
|
---|
24 |
|
---|
25 | private int timeouts, instantHits, waitHits, remoteHits, pendingPeak, contested;
|
---|
26 |
|
---|
27 | private Set caids = new HashSet();
|
---|
28 |
|
---|
29 | public DefaultCache() {
|
---|
30 | logger = ProxyLogger.getLabeledLogger(getClass().getName());
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void configUpdated(ProxyXmlConfig xml) throws ConfigException {
|
---|
34 | maxAge = xml.getTimeValue("cw-max-age", "s");
|
---|
35 | String maxWaitStr = xml.getStringValue("max-cache-wait");
|
---|
36 | if(maxWaitStr.endsWith("%")) {
|
---|
37 | maxCacheWait = -1;
|
---|
38 | maxWaitStr = maxWaitStr.substring(0, maxWaitStr.length() - 1);
|
---|
39 | maxWaitPercent = Integer.parseInt(maxWaitStr);
|
---|
40 | if(maxWaitPercent < 1 || maxWaitPercent > 100)
|
---|
41 | throw new ConfigException(xml.getFullName(), "max-cache-wait", "Must be a time value or 1-100%");
|
---|
42 | } else {
|
---|
43 | maxWaitPercent = -1;
|
---|
44 | maxCacheWait = xml.getTimeValue("max-cache-wait", "s");
|
---|
45 | }
|
---|
46 | if(pendingEcms == null) pendingEcms = new MessageCacheMap(maxAge);
|
---|
47 | else pendingEcms.setMaxAge(maxAge);
|
---|
48 | if(ecmMap == null) ecmMap = new MessageCacheMap(maxAge);
|
---|
49 | else ecmMap.setMaxAge(maxAge);
|
---|
50 | pendingPeak = 0;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public long getMaxCacheWait(long maxCwWait) {
|
---|
54 | if(maxCacheWait == -1) return (long)((maxWaitPercent / 100.0) * maxCwWait);
|
---|
55 | else return Math.min(maxCwWait, maxCacheWait);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void start() {
|
---|
59 | timeouts = 0;
|
---|
60 | instantHits = 0;
|
---|
61 | waitHits = 0;
|
---|
62 | contested = 0;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public CamdNetMessage processRequest(int successFactor, CamdNetMessage request, boolean alwaysWait, long maxCwWait) {
|
---|
66 | long start = System.currentTimeMillis();
|
---|
67 | long maxWait = getMaxCacheWait(maxCwWait);
|
---|
68 |
|
---|
69 | synchronized(this) {
|
---|
70 |
|
---|
71 | boolean waited = false, alerted = false;
|
---|
72 | long delay;
|
---|
73 |
|
---|
74 | if(!ecmMap.containsKey(request)) {
|
---|
75 |
|
---|
76 | if(alwaysWait) {
|
---|
77 | // no cached or pending transaction but cache-only mode is set, so assume another proxy will provide cw
|
---|
78 | if(!pendingEcms.containsKey(request)) addRequest(successFactor, request, alwaysWait);
|
---|
79 | }
|
---|
80 |
|
---|
81 | if(listener != null) {
|
---|
82 | // allow a set cache listener to introduce a lock for any other arbitrary reason
|
---|
83 | if(!pendingEcms.containsKey(request) && listener.lockRequest(successFactor, request))
|
---|
84 | addRequest(successFactor, request, true); // alwayswait = true <-- prevent clusteredcache from sending the lock
|
---|
85 | }
|
---|
86 |
|
---|
87 | while(pendingEcms.containsKey(request)) {
|
---|
88 |
|
---|
89 | String origin = ((CamdNetMessage)pendingEcms.get(request)).getOriginAddress();
|
---|
90 |
|
---|
91 | if(origin != null && origin.equals(request.getRemoteAddress())) {
|
---|
92 | // skip cache lock if the origin of the cache update is the same as that of the request
|
---|
93 | break;
|
---|
94 | }
|
---|
95 |
|
---|
96 | try {
|
---|
97 | wait(500);
|
---|
98 | } catch(InterruptedException e) {
|
---|
99 | break;
|
---|
100 | }
|
---|
101 | delay = System.currentTimeMillis() - start;
|
---|
102 | logger.finest("Waited for " + request.hashCodeStr() + " in cache: " + delay + " ms");
|
---|
103 | waited = true;
|
---|
104 | if(!alerted && delay >= (maxWait / 2)) {
|
---|
105 | alerted = true;
|
---|
106 | if(!ecmMap.containsKey(request)) delayAlert(successFactor, request, alwaysWait, maxWait);
|
---|
107 | }
|
---|
108 | if(delay >= maxWait) break;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | request.setCacheTime(request.getCacheTime() + (System.currentTimeMillis() - start));
|
---|
113 |
|
---|
114 | if(ecmMap.containsKey(request)) {
|
---|
115 | CamdNetMessage reply = new CamdNetMessage((CamdNetMessage)ecmMap.get(request)); // return copy
|
---|
116 | if(request.getServiceId() != 0) reply.setServiceId(request.getServiceId());
|
---|
117 | if(waited) {
|
---|
118 | waitHits++;
|
---|
119 | reply.setInstant(false);
|
---|
120 | } else {
|
---|
121 | instantHits++;
|
---|
122 | reply.setInstant(true);
|
---|
123 | }
|
---|
124 | if(reply.getOriginAddress() != null) remoteHits++;
|
---|
125 | return reply;
|
---|
126 | } else {
|
---|
127 |
|
---|
128 | if(waited) {
|
---|
129 | pendingEcms.remove(request);
|
---|
130 | timeouts++;
|
---|
131 | request.setTimeOut(true);
|
---|
132 | return null;
|
---|
133 | }
|
---|
134 |
|
---|
135 | addRequest(successFactor, request, alwaysWait);
|
---|
136 | }
|
---|
137 | return null;
|
---|
138 | }
|
---|
139 |
|
---|
140 | }
|
---|
141 |
|
---|
142 | protected void delayAlert(int successFactor, CamdNetMessage request, boolean alwaysWait, long maxWait) {
|
---|
143 | // nothing to do in this impl
|
---|
144 | }
|
---|
145 |
|
---|
146 | public synchronized boolean processReply(CamdNetMessage request, CamdNetMessage reply) {
|
---|
147 | if(monitor != null) monitor.onReply(request, reply);
|
---|
148 | if(reply == null || reply.isEmpty()) {
|
---|
149 | removeRequest(request);
|
---|
150 | notifyAll();
|
---|
151 | } else {
|
---|
152 | if(listener != null) listener.onReply(request, reply);
|
---|
153 | if(pendingEcms.containsKey(request)) {
|
---|
154 | addReply(request, reply);
|
---|
155 | logger.finer("Reply received: " + request.hashCodeStr() + " -> " + reply.hashCodeStr() +
|
---|
156 | " (pending: " + pendingEcms.size() + ")");
|
---|
157 | removeRequest(request);
|
---|
158 | notifyAll();
|
---|
159 | return true;
|
---|
160 | }
|
---|
161 | addReply(request, reply);
|
---|
162 | }
|
---|
163 | return false;
|
---|
164 | }
|
---|
165 |
|
---|
166 | public synchronized void processReplies(Map replies) {
|
---|
167 | boolean lockFound = false;
|
---|
168 | CamdNetMessage request, reply;
|
---|
169 | for(Iterator iter = replies.keySet().iterator(); iter.hasNext(); ) {
|
---|
170 | request = (CamdNetMessage)iter.next();
|
---|
171 | reply = (CamdNetMessage)replies.get(request);
|
---|
172 | // if(monitor != null) monitor.onReply(request, reply);
|
---|
173 | if(listener != null) listener.onReply(request, reply);
|
---|
174 | if(pendingEcms.containsKey(request)) {
|
---|
175 | removeRequest(request);
|
---|
176 | lockFound = true;
|
---|
177 | }
|
---|
178 | addReply(request, reply);
|
---|
179 | }
|
---|
180 | if(lockFound) notifyAll();
|
---|
181 | }
|
---|
182 |
|
---|
183 | public CamdNetMessage peekReply(CamdNetMessage request) {
|
---|
184 | return (CamdNetMessage)ecmMap.get(request);
|
---|
185 | }
|
---|
186 |
|
---|
187 | public Properties getUsageStats() {
|
---|
188 | Properties p = new Properties();
|
---|
189 | p.setProperty("pending-ecms", String.valueOf(pendingEcms.size()) + " (peak: " + pendingPeak + ")");
|
---|
190 | p.setProperty("cached-ecms", String.valueOf(ecmMap.size()));
|
---|
191 | p.setProperty("timeouts", String.valueOf(timeouts));
|
---|
192 | p.setProperty("instant-hits", String.valueOf(instantHits));
|
---|
193 | p.setProperty("wait-hits", String.valueOf(waitHits));
|
---|
194 | p.setProperty("contested", String.valueOf(contested));
|
---|
195 | if(remoteHits > 0) p.setProperty("remote-hits", String.valueOf(remoteHits));
|
---|
196 | return p;
|
---|
197 | }
|
---|
198 |
|
---|
199 | protected synchronized boolean contains(CamdNetMessage request) {
|
---|
200 | return ecmMap.containsKey(request);
|
---|
201 | }
|
---|
202 |
|
---|
203 | protected synchronized boolean containsPending(CamdNetMessage request) {
|
---|
204 | return pendingEcms.containsKey(request);
|
---|
205 | }
|
---|
206 |
|
---|
207 | public boolean containsCaid(int caid) {
|
---|
208 | return caids.contains(new Integer(caid));
|
---|
209 | }
|
---|
210 |
|
---|
211 | protected synchronized void addRequest(int successFactor, CamdNetMessage request, boolean alwaysWait) {
|
---|
212 | CamdNetMessage oldRequest = (CamdNetMessage)pendingEcms.put(request, request);
|
---|
213 | if(pendingEcms.size() > pendingPeak) pendingPeak = pendingEcms.size();
|
---|
214 | if(oldRequest == null) {
|
---|
215 | caids.add(new Integer(request.getCaId()));
|
---|
216 | if(monitor != null) monitor.onRequest(successFactor, request);
|
---|
217 | if(listener != null) listener.onRequest(successFactor, request);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | protected synchronized void addReply(CamdNetMessage request, CamdNetMessage reply) {
|
---|
222 | if(reply.isEmpty()) return; // bad reply = unable to decode
|
---|
223 | if(reply.getProfileName() == null) reply.setProfileName(request.getProfileName());
|
---|
224 | if(reply.getNetworkId() == 0) reply.setNetworkId(request.getNetworkId());
|
---|
225 | CamdNetMessage oldReply = (CamdNetMessage)ecmMap.put(request, reply);
|
---|
226 | if(oldReply != null) {
|
---|
227 | if(!oldReply.equals(reply)) {
|
---|
228 | contested++;
|
---|
229 | try {
|
---|
230 | if(reply.addCandidate(oldReply)) // something is different, save the previous reply
|
---|
231 | if(monitor != null) monitor.onContested(request, reply); // a new candidate was added, notify
|
---|
232 | } catch (IllegalStateException e) {
|
---|
233 | logger.warning("Could not retain cache reply for " + request.hashCodeStr() + ": " + e.getMessage());
|
---|
234 | }
|
---|
235 | if(!oldReply.equalsSingleDcw(reply)) { // completely different reply
|
---|
236 | logger.warning("Contested cache reply for " + request.hashCodeStr() + ", both DCW differ - Previous: "
|
---|
237 | + oldReply.toDebugString() + " Current: " + reply.toDebugString() + " (time difference: " +
|
---|
238 | (reply.getTimeStamp() - oldReply.getTimeStamp()) + "ms)");
|
---|
239 | } else { // one cw matches
|
---|
240 | if(oldReply.hasZeroDcw() || reply.hasZeroDcw()) // one was zeroes only, probably harmless so avoid logging warning in this case
|
---|
241 | logger.info("Contested cache reply for " + request.hashCodeStr() + ", different opposing DCW - Previous: " +
|
---|
242 | oldReply.toDebugString() + " Current: " + reply.toDebugString());
|
---|
243 | else logger.warning("Contested cache reply for " + request.hashCodeStr() + ", different opposing DCW - Previous: "
|
---|
244 | + oldReply.toDebugString() + " Current: " + reply.toDebugString());
|
---|
245 | }
|
---|
246 | } else { // same reply, duplicate
|
---|
247 | if(reply.getOriginAddress() != null) logger.fine("Duplicate cache reply for " + request.hashCodeStr() + " late by: " +
|
---|
248 | (reply.getTimeStamp() - oldReply.getTimeStamp()) + " ms");
|
---|
249 | }
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | protected void removeRequest(CamdNetMessage request) {
|
---|
254 | pendingEcms.remove(request);
|
---|
255 | }
|
---|
256 |
|
---|
257 | public void setListener(CacheListener listener) {
|
---|
258 | this.listener = listener;
|
---|
259 | }
|
---|
260 |
|
---|
261 | public CacheListener getListener() {
|
---|
262 | return listener;
|
---|
263 | }
|
---|
264 |
|
---|
265 | public void setMonitor(CacheListener monitor) {
|
---|
266 | this.monitor = monitor;
|
---|
267 | }
|
---|
268 |
|
---|
269 | public CacheListener getMonitor() {
|
---|
270 | return monitor;
|
---|
271 | }
|
---|
272 |
|
---|
273 | }
|
---|