1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
package com.c2kernel.persistency;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.entity.agent.JobList;
import com.c2kernel.entity.proxy.EntityProxyManager;
import com.c2kernel.entity.proxy.ProxyMessage;
import com.c2kernel.events.History;
import com.c2kernel.persistency.outcome.Outcome;
import com.c2kernel.persistency.outcome.Viewpoint;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Logger;
import com.c2kernel.utils.SoftCache;
import com.c2kernel.utils.WeakCache;
/**
* instantiates ClusterStorages listed in properties file All read/write requests to storage pass through this object, which
* can query the capabilities of each declared storage, and channel requests accordingly. Transaction based.
*
* * @version $Revision: 1.62 $ $Date: 2006/02/01 13:27:46 $
* @author $Author: abranson $
*/
public class ClusterStorageManager {
HashMap<String, ClusterStorage> allStores = new HashMap<String, ClusterStorage>();
String[] clusterPriority = new String[0];
HashMap<String, ArrayList<ClusterStorage>> clusterWriters = new HashMap<String, ArrayList<ClusterStorage>>();
HashMap<String, ArrayList<ClusterStorage>> clusterReaders = new HashMap<String, ArrayList<ClusterStorage>>();
// we don't need a soft cache for the top level cache - the proxies and entities clear that when reaped
HashMap<Integer, Map<String, C2KLocalObject>> memoryCache = new HashMap<Integer, Map<String, C2KLocalObject>>();
/**
* Initialises all ClusterStorage handlers listed by class name in the property "ClusterStorages"
* This property is usually process specific, and so should be in the server/client.conf and not the connect file.
*/
public ClusterStorageManager() throws ClusterStorageException {
Object clusterStorageProp = Gateway.getProperty("ClusterStorage");
if (clusterStorageProp == null || clusterStorageProp.equals("")) {
throw new ClusterStorageException("ClusterStorageManager.init() - no ClusterStorages defined. No persistency!");
}
ArrayList<ClusterStorage> rootStores;
if (clusterStorageProp instanceof String)
rootStores = instantiateStores((String)clusterStorageProp);
else if (clusterStorageProp instanceof ArrayList<?>) {
ArrayList<?> propStores = (ArrayList<?>)clusterStorageProp;
rootStores = new ArrayList<ClusterStorage>();
for (Object thisStore : propStores) {
if (thisStore instanceof ClusterStorage)
rootStores.add((ClusterStorage)thisStore);
else
throw new ClusterStorageException("Supplied ClusterStorage "+thisStore.toString()+" was not an instance of ClusterStorage");
}
}
else {
throw new ClusterStorageException("Unknown class of ClusterStorage property: "+clusterStorageProp.getClass().getName());
}
int clusterNo = 0;
for (ClusterStorage newStorage : rootStores) {
try {
newStorage.open();
} catch (ClusterStorageException ex) {
Logger.error(ex);
throw new ClusterStorageException("ClusterStorageManager.init() - Error initialising storage handler " + newStorage.getClass().getName() +
": " + ex.getMessage());
}
Logger.msg(5, "ClusterStorageManager.init() - Cluster storage " + newStorage.getClass().getName() +
" initialised successfully.");
allStores.put(newStorage.getId(), newStorage);
clusterPriority[clusterNo++] = newStorage.getId();
}
clusterReaders.put(ClusterStorage.ROOT, rootStores); // all storages are queried for clusters at the root level
}
public ArrayList<ClusterStorage> instantiateStores(String allClusters) throws ClusterStorageException {
ArrayList<ClusterStorage> rootStores = new ArrayList<ClusterStorage>();
StringTokenizer tok = new StringTokenizer(allClusters, ",");
clusterPriority = new String[tok.countTokens()];
while (tok.hasMoreTokens()) {
ClusterStorage newStorage = null;
String newStorageClass = tok.nextToken();
try {
try {
newStorage = (ClusterStorage)(Class.forName(newStorageClass).newInstance());
} catch (ClassNotFoundException ex2) {
newStorage = (ClusterStorage)(Class.forName("com.c2kernel.persistency."+newStorageClass).newInstance());
}
} catch (ClassNotFoundException ex) {
throw new ClusterStorageException("ClusterStorageManager.init() - The cluster storage handler class " + newStorageClass +
" could not be found.");
} catch (InstantiationException ex) {
throw new ClusterStorageException("ClusterStorageManager.init() - The cluster storage handler class " + newStorageClass +
" could not be instantiated.");
} catch (IllegalAccessException ex) {
throw new ClusterStorageException("ClusterStorageManager.init() - The cluster storage handler class " + newStorageClass +
" was not allowed to be instantiated.");
}
rootStores.add(newStorage);
}
return rootStores;
}
public void close() {
for (ClusterStorage thisStorage : allStores.values()) {
try {
thisStorage.close();
} catch (ClusterStorageException ex) {
Logger.error(ex);
}
}
}
/**
* Returns the loaded storage that declare that they can handle writing or reading the specified cluster name (e.g.
* Collection, Property) Must specify if the request is a read or a write.
*/
private ArrayList<ClusterStorage> findStorages(String clusterType, boolean forWrite) {
// choose the right cache for readers or writers
HashMap<String, ArrayList<ClusterStorage>> cache;
if (forWrite)
cache = clusterWriters;
else
cache = clusterReaders;
// check to see if we've been asked to do this before
if (cache.containsKey(clusterType))
return cache.get(clusterType);
// not done yet, we'll have to query them all
Logger.msg(7, "ClusterStorageManager.findStorages() - finding storage for "+clusterType+" forWrite:"+forWrite);
ArrayList<ClusterStorage> useableStorages = new ArrayList<ClusterStorage>();
for (String element : clusterPriority) {
ClusterStorage thisStorage = allStores.get(element);
short requiredSupport = forWrite ? ClusterStorage.WRITE : ClusterStorage.READ;
if ((thisStorage.queryClusterSupport(clusterType) & requiredSupport) == requiredSupport) {
Logger.msg(7, "ClusterStorageManager.findStorages() - Got "+thisStorage.getName());
useableStorages.add(thisStorage);
}
}
cache.put(clusterType, useableStorages);
return useableStorages;
}
/**
* Retrieves the ids of the next level of a cluster
* Does not look in any currently open transactions.
*/
public String[] getClusterContents(Integer sysKey, String path) throws ClusterStorageException {
ArrayList<String> contents = new ArrayList<String>();
// get all readers
Logger.msg(8, "ClusterStorageManager.getClusterContents() - Finding contents of "+path);
ArrayList<ClusterStorage> readers = findStorages(ClusterStorage.getClusterType(path), false);
// try each in turn until we get a result
for (ClusterStorage thisReader : readers) {
try {
String[] thisArr = thisReader.getClusterContents(sysKey, path);
if (thisArr != null) {
for (int j = 0; j < thisArr.length; j++)
if (!contents.contains(thisArr[j])) {
Logger.msg(9, "ClusterStorageManager.getClusterContents() - "+thisReader.getName()+" reports "+thisArr[j]);
contents.add(thisArr[j]);
}
}
} catch (ClusterStorageException e) {
Logger.error("ClusterStorageManager.getClusterContents() - reader " + thisReader.getName() +
" could not retrieve contents of " + sysKey + "/" + path + ": " + e.getMessage());
}
}
String[] retArr = new String[0];
retArr = contents.toArray(retArr);
return retArr;
}
/** Internal get method. Retrieves clusters from ClusterStorages & maintains the memory cache */
public C2KLocalObject get(Integer sysKeyIntObj, String path) throws ClusterStorageException, ObjectNotFoundException {
C2KLocalObject result = null;
// check cache first
Map<String, C2KLocalObject> sysKeyMemCache = null;
sysKeyMemCache = memoryCache.get(sysKeyIntObj);
if (sysKeyMemCache != null) {
synchronized(sysKeyMemCache) {
C2KLocalObject obj = sysKeyMemCache.get(path);
if (obj != null) {
Logger.msg(7, "ClusterStorageManager.get() - found "+sysKeyIntObj+"/"+path+" in memcache");
return obj;
}
}
}
// special case - loading viewpoint contents
if (path.startsWith(ClusterStorage.VIEWPOINT) &&
path.endsWith("/data")) {
StringTokenizer tok = new StringTokenizer(path,"/");
if (tok.countTokens() == 4) { // to not catch viewpoints called 'data'
Outcome data = null;
Viewpoint view = (Viewpoint)get(sysKeyIntObj, path.substring(0, path.lastIndexOf("/")));
if (view != null)
data = view.getOutcome();
return data;
}
}
// deal out top level remote maps
if (path.indexOf('/') == -1) {
if (path.equals(ClusterStorage.HISTORY))
result = new History(sysKeyIntObj, null);
if (path.equals(ClusterStorage.JOB))
result = new JobList(sysKeyIntObj, null);
if (result!=null) {
synchronized(sysKeyMemCache) {
sysKeyMemCache.put(path, result);
}
return result;
}
}
// else try each reader in turn until we find it
ArrayList<ClusterStorage> readers = findStorages(ClusterStorage.getClusterType(path), false);
for (ClusterStorage thisReader : readers) {
try {
result = thisReader.get(sysKeyIntObj, path);
Logger.msg(7, "ClusterStorageManager.get() - reading "+path+" from "+thisReader.getName() + " for intkey=" + sysKeyIntObj);
if (result != null) { // got it!
// store it in the cache
if (sysKeyMemCache == null) { // create cache if needed
boolean useWeak = Gateway.getProperty("Storage.useWeakCache","false").equals("true");
Logger.msg(7,"ClusterStorageManager.put() - Creating "+(useWeak?"Weak":"Strong")+" cache for entity "+sysKeyIntObj);
sysKeyMemCache = useWeak?new WeakCache<String, C2KLocalObject>():new SoftCache<String, C2KLocalObject>(0);
synchronized (memoryCache) {
memoryCache.put(sysKeyIntObj, sysKeyMemCache);
}
}
synchronized(sysKeyMemCache) {
sysKeyMemCache.put(path, result);
}
// then return it
return result;
}
} catch (ClusterStorageException e) {
Logger.msg(7, "ClusterStorageManager.get() - reader " + thisReader.getName() + " could not retrieve " + sysKeyIntObj +
"/" + path + ": " + e.getMessage());
}
}
throw new ObjectNotFoundException("ClusterStorageManager.get() - Path " + path + " not found in " + sysKeyIntObj, "");
}
/** Internal put method. Creates or overwrites a cluster in all writers. Used when committing transactions. */
public void put(Integer sysKeyIntObj, C2KLocalObject obj) throws ClusterStorageException {
String path = ClusterStorage.getPath(obj);
ArrayList<ClusterStorage> writers = findStorages(ClusterStorage.getClusterType(path), true);
for (ClusterStorage thisWriter : writers) {
try {
Logger.msg(7, "ClusterStorageManager.put() - writing "+path+" to "+thisWriter.getName());
thisWriter.put(sysKeyIntObj, obj);
} catch (ClusterStorageException e) {
Logger.error("ClusterStorageManager.put() - writer " + thisWriter.getName() + " could not store " +
sysKeyIntObj + "/" + path + ": " + e.getMessage());
throw e;
}
}
// put in mem cache if that worked
Map<String, C2KLocalObject> sysKeyMemCache;
if (memoryCache.containsKey(sysKeyIntObj))
sysKeyMemCache = memoryCache.get(sysKeyIntObj);
else {
boolean useWeak = Gateway.getProperty("Storage.useWeakCache","false").equals("true");
Logger.msg(7,"ClusterStorageManager.put() - Creating "+(useWeak?"Weak":"Strong")+" cache for entity "+sysKeyIntObj);
sysKeyMemCache = useWeak?new WeakCache<String, C2KLocalObject>():new SoftCache<String, C2KLocalObject>(0);
synchronized (memoryCache) {
memoryCache.put(sysKeyIntObj, sysKeyMemCache);
}
}
synchronized(sysKeyMemCache) {
sysKeyMemCache.put(path, obj);
}
if (Logger.doLog(9)) dumpCacheContents(9);
// transmit proxy event
EntityProxyManager.sendProxyEvent( new ProxyMessage(sysKeyIntObj.intValue(), path, ProxyMessage.ADDED));
}
/** Deletes a cluster from all writers */
public void remove(Integer sysKeyIntObj, String path) throws ClusterStorageException {
ArrayList<ClusterStorage> writers = findStorages(ClusterStorage.getClusterType(path), true);
for (ClusterStorage thisWriter : writers) {
try {
Logger.msg(7, "ClusterStorageManager.delete() - removing "+path+" from "+thisWriter.getName());
thisWriter.delete(sysKeyIntObj, path);
} catch (ClusterStorageException e) {
Logger.error("ClusterStorageManager.delete() - writer " + thisWriter.getName() + " could not delete " + sysKeyIntObj +
"/" + path + ": " + e.getMessage());
throw e;
}
}
if (memoryCache.containsKey(sysKeyIntObj)) {
Map<String, C2KLocalObject> sysKeyMemCache = memoryCache.get(sysKeyIntObj);
synchronized (sysKeyMemCache) {
sysKeyMemCache.remove(path);
}
}
// transmit proxy event
EntityProxyManager.sendProxyEvent( new ProxyMessage(sysKeyIntObj.intValue(), path, ProxyMessage.DELETED));
}
public void clearCache(Integer sysKeyIntObj, String path) {
Logger.msg(7, "CSM.clearCache() - removing "+sysKeyIntObj+"/"+path);
if (memoryCache.containsKey(sysKeyIntObj)) {
Map<String, C2KLocalObject> sysKeyMemCache = memoryCache.get(sysKeyIntObj);
synchronized(sysKeyMemCache) {
for (Iterator<String> iter = sysKeyMemCache.keySet().iterator(); iter.hasNext();) {
String thisPath = iter.next();
if (thisPath.startsWith(path)) {
Logger.msg(7, "CSM.clearCache() - removing "+sysKeyIntObj+"/"+thisPath);
iter.remove();
}
}
}
}
}
public void clearCache(Integer sysKeyIntObj) {
Logger.msg(5, "CSM.clearCache() - removing entire cache of "+sysKeyIntObj);
if (memoryCache.containsKey(sysKeyIntObj)) {
synchronized (memoryCache) {
if (Logger.doLog(6)) {
Map<String, C2KLocalObject> sysKeyMemCache = memoryCache.get(sysKeyIntObj);
int size = sysKeyMemCache.size();
Logger.msg(6, "CSM.clearCache() - "+size+" objects to remove.");
}
memoryCache.remove(sysKeyIntObj);
}
}
else
Logger.msg(6, "CSM.clearCache() - No objects cached");
}
public void clearCache() {
synchronized (memoryCache) {
memoryCache.clear();
}
Logger.msg(5, "CSM.clearCache() - cleared entire cache, "+memoryCache.size()+" entities.");
}
public void dumpCacheContents(int logLevel) {
if (!Logger.doLog(logLevel)) return;
synchronized(memoryCache) {
for (Integer sysKey : memoryCache.keySet()) {
Logger.msg(logLevel, "Cached Objects of Entity "+sysKey);
Map<String, C2KLocalObject> sysKeyMemCache = memoryCache.get(sysKey);
try {
synchronized(sysKeyMemCache) {
for (Object name : sysKeyMemCache.keySet()) {
String path = (String) name;
try {
Logger.msg(logLevel, " Path "+path+": "+sysKeyMemCache.get(path).getClass().getName());
} catch (NullPointerException e) {
Logger.msg(logLevel, " Path "+path+": reaped");
}
}
}
} catch (ConcurrentModificationException ex) {
Logger.msg(logLevel, "Cache modified - aborting");
}
}
Logger.msg(logLevel, "Total number of cached entities: "+memoryCache.size());
}
}
@Deprecated
public Object query(String id, Object query) throws ClusterStorageException {
ClusterStorage requiredStorage = allStores.get(id);
if (requiredStorage == null)
throw new ClusterStorageException("Storage "+id+" not found.");
return requiredStorage.query(query);
}
@Deprecated
public String queryToXML(String id, String query, boolean genericFormat) throws ClusterStorageException {
ClusterStorage requiredStorage = allStores.get(id);
if (requiredStorage == null)
throw new ClusterStorageException("Storage "+id+" not found.");
return requiredStorage.queryToXML(query, genericFormat);
}
}
|