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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceUtil/Random.h>
#include <Ice/Communicator.h>
#include <Ice/LoggerUtil.h>
#include <Ice/LocalException.h>
#include <IceGrid/ObjectCache.h>
#include <IceGrid/NodeSessionI.h>
#include <IceGrid/ServerCache.h>
#include <IceGrid/SessionI.h>
using namespace std;
using namespace IceGrid;
pointer_to_unary_function<int, int> ObjectCache::_rand(IceUtil::random);
namespace IceGrid
{
struct ObjectEntryCI : binary_function<ObjectEntryPtr&, ObjectEntryPtr&, bool>
{
bool
operator()(const ObjectEntryPtr& lhs, const ObjectEntryPtr& rhs)
{
return ::Ice::proxyIdentityLess(lhs->getProxy(), rhs->getProxy());
}
};
struct ObjectLoadCI : binary_function<pair<Ice::ObjectPrx, float>&, pair<Ice::ObjectPrx, float>&, bool>
{
bool operator()(const pair<Ice::ObjectPrx, float>& lhs, const pair<Ice::ObjectPrx, float>& rhs)
{
return lhs.second < rhs.second;
}
};
};
ObjectCache::TypeEntry::TypeEntry() : _allocatablesCount(0)
{
}
void
ObjectCache::TypeEntry::add(const ObjectEntryPtr& obj)
{
//
// No mutex protection here, this is called with the cache locked.
//
_objects.insert(lower_bound(_objects.begin(), _objects.end(), obj, ObjectEntryCI()), obj);
_allocatablesCount += obj->isAllocatable() ? 1 : 0;
if(!_requests.empty())
{
canTryAllocate(obj, false);
}
}
bool
ObjectCache::TypeEntry::remove(const ObjectEntryPtr& obj)
{
//
// No mutex protection here, this is called with the cache locked.
//
_allocatablesCount -= obj->isAllocatable() ? 1 : 0;
vector<ObjectEntryPtr>::iterator q = lower_bound(_objects.begin(), _objects.end(), obj, ObjectEntryCI());
assert(q->get() == obj.get());
_objects.erase(q);
if(!_requests.empty() && !_allocatablesCount)
{
for(list<ObjectAllocationRequestPtr>::const_iterator p = _requests.begin(); p != _requests.end(); ++p)
{
(*p)->cancel(AllocationException("no allocatable objects with type `" + obj->getType() + "' registered"));
}
}
return _objects.empty();
}
void
ObjectCache::TypeEntry::addAllocationRequest(const ObjectAllocationRequestPtr& request)
{
//
// No mutex protection here, this is called with the cache locked.
//
assert(_allocatablesCount);
if(request->pending())
{
_requests.push_back(request);
}
}
bool
ObjectCache::TypeEntry::canTryAllocate(const ObjectEntryPtr& entry, bool fromRelease)
{
//
// No mutex protection here, this is called with the cache locked.
//
list<ObjectAllocationRequestPtr>::iterator p = _requests.begin();
while(p != _requests.end())
{
AllocationRequestPtr request = *p;
try
{
if(request->isCanceled()) // If the request has been canceled, we just remove it.
{
p = _requests.erase(p);
}
else if(entry->tryAllocate(request, fromRelease))
{
p = _requests.erase(p);
return true; // The request successfully allocated the entry!
}
else if(entry->getSession()) // If entry is allocated, we're done
{
return false;
}
else
{
++p;
}
}
catch(const AllocationException&)
{
p = _requests.erase(p);
}
}
return false;
}
bool
ObjectCache::TypeEntry::hasAllocatables() const
{
return _allocatablesCount;
}
ObjectCache::ObjectCache(const Ice::CommunicatorPtr& communicator, AdapterCache& adapterCache) :
_communicator(communicator),
_adapterCache(adapterCache)
{
}
void
ObjectCache::add(const ObjectInfo& info, const string& application, bool allocatable, const AllocatablePtr& parent)
{
const Ice::Identity& id = info.proxy->ice_getIdentity();
Lock sync(*this);
assert(!getImpl(id));
ObjectEntryPtr entry = new ObjectEntry(*this, info, application, allocatable, parent);
addImpl(id, entry);
map<string, TypeEntry>::iterator p = _types.find(entry->getType());
if(p == _types.end())
{
p = _types.insert(p, map<string, TypeEntry>::value_type(entry->getType(), TypeEntry()));
}
p->second.add(entry);
if(_traceLevels && _traceLevels->object > 0)
{
Ice::Trace out(_traceLevels->logger, _traceLevels->objectCat);
out << "added object `" << _communicator->identityToString(id) << "'";
}
}
ObjectEntryPtr
ObjectCache::get(const Ice::Identity& id) const
{
Lock sync(*this);
ObjectEntryPtr entry = getImpl(id);
if(!entry)
{
throw ObjectNotRegisteredException(id);
}
return entry;
}
ObjectEntryPtr
ObjectCache::remove(const Ice::Identity& id)
{
Lock sync(*this);
ObjectEntryPtr entry = removeImpl(id);
assert(entry);
map<string, TypeEntry>::iterator p = _types.find(entry->getType());
assert(p != _types.end());
if(p->second.remove(entry))
{
_types.erase(p);
}
if(_traceLevels && _traceLevels->object > 0)
{
Ice::Trace out(_traceLevels->logger, _traceLevels->objectCat);
out << "removed object `" << _communicator->identityToString(id) << "'";
}
return entry;
}
void
ObjectCache::allocateByType(const string& type, const ObjectAllocationRequestPtr& request)
{
Lock sync(*this);
map<string, TypeEntry>::iterator p = _types.find(type);
if(p == _types.end() || !p->second.hasAllocatables())
{
throw AllocationException("no allocatable objects with type `" + type + "' registered");
}
vector<ObjectEntryPtr> objects = p->second.getObjects();
random_shuffle(objects.begin(), objects.end(), _rand); // TODO: OPTIMIZE
try
{
for(vector<ObjectEntryPtr>::const_iterator q = objects.begin(); q != objects.end(); ++q)
{
if((*q)->tryAllocate(request))
{
return;
}
}
}
catch(const AllocationException&)
{
return;
}
p->second.addAllocationRequest(request);
}
bool
ObjectCache::canTryAllocate(const ObjectEntryPtr& entry)
{
//
// Notify the type entry that an object was released.
//
Lock sync(*this);
map<string, TypeEntry>::iterator p = _types.find(entry->getType());
if(p == _types.end())
{
return false;
}
return p->second.canTryAllocate(entry, true);
}
Ice::ObjectProxySeq
ObjectCache::getObjectsByType(const string& type)
{
Lock sync(*this);
Ice::ObjectProxySeq proxies;
map<string, TypeEntry>::const_iterator p = _types.find(type);
if(p == _types.end())
{
return proxies;
}
const vector<ObjectEntryPtr>& objects = p->second.getObjects();
for(vector<ObjectEntryPtr>::const_iterator q = objects.begin(); q != objects.end(); ++q)
{
proxies.push_back((*q)->getProxy());
}
return proxies;
}
ObjectInfoSeq
ObjectCache::getAll(const string& expression)
{
Lock sync(*this);
ObjectInfoSeq infos;
for(map<Ice::Identity, ObjectEntryPtr>::const_iterator p = _entries.begin(); p != _entries.end(); ++p)
{
if(expression.empty() || IceUtil::match(_communicator->identityToString(p->first), expression, true))
{
infos.push_back(p->second->getObjectInfo());
}
}
return infos;
}
ObjectInfoSeq
ObjectCache::getAllByType(const string& type)
{
Lock sync(*this);
ObjectInfoSeq infos;
map<string, TypeEntry>::const_iterator p = _types.find(type);
if(p == _types.end())
{
return infos;
}
const vector<ObjectEntryPtr>& objects = p->second.getObjects();
for(vector<ObjectEntryPtr>::const_iterator q = objects.begin(); q != objects.end(); ++q)
{
infos.push_back((*q)->getObjectInfo());
}
return infos;
}
ObjectEntry::ObjectEntry(ObjectCache& cache,
const ObjectInfo& info,
const string& application,
bool allocatable,
const AllocatablePtr& parent) :
Allocatable(allocatable, parent),
_cache(cache),
_info(info),
_application(application)
{
}
Ice::ObjectPrx
ObjectEntry::getProxy() const
{
return _info.proxy;
}
string
ObjectEntry::getType() const
{
return _info.type;
}
string
ObjectEntry::getApplication() const
{
return _application;
}
const ObjectInfo&
ObjectEntry::getObjectInfo() const
{
return _info;
}
bool
ObjectEntry::canRemove()
{
return true;
}
void
ObjectEntry::allocated(const SessionIPtr& session)
{
//
// Add the object allocation to the session. The object will be
// released once the session is destroyed.
//
if(!session->addAllocation(this))
{
throw AllocationException("session destroyed");
}
TraceLevelsPtr traceLevels = _cache.getTraceLevels();
if(traceLevels && traceLevels->object > 1)
{
Ice::Trace out(traceLevels->logger, traceLevels->objectCat);
const Ice::Identity id = _info.proxy->ice_getIdentity();
out << "object `" << _cache.communicator()->identityToString(id) << "' allocated by `" << session->getId()
<< "' (" << _count << ")";
}
Glacier2::SessionControlPrx ctl = session->getSessionControl();
if(ctl)
{
try
{
Ice::IdentitySeq seq(1);
seq.push_back(_info.proxy->ice_getIdentity());
ctl->objectIdFilter()->addAccept(seq);
}
catch(const Ice::ObjectNotExistException&)
{
}
}
}
void
ObjectEntry::released(const SessionIPtr& session)
{
//
// Remove the object allocation from the session.
//
session->removeAllocation(this);
Glacier2::SessionControlPrx ctl = session->getSessionControl();
if(ctl)
{
try
{
Ice::IdentitySeq seq(1);
seq.push_back(_info.proxy->ice_getIdentity());
ctl->objectIdFilter()->removeAccept(seq);
}
catch(const Ice::ObjectNotExistException&)
{
}
}
TraceLevelsPtr traceLevels = _cache.getTraceLevels();
if(traceLevels && traceLevels->object > 1)
{
Ice::Trace out(traceLevels->logger, traceLevels->objectCat);
const Ice::Identity id = _info.proxy->ice_getIdentity();
out << "object `" << _cache.communicator()->identityToString(id) << "' released by `" << session->getId()
<< "' (" << _count << ")";
}
}
bool
ObjectEntry::canTryAllocate()
{
return _cache.canTryAllocate(this);
}
|