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
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#include <Ice/Communicator.h>
#include <Ice/LocalException.h>
#include <Ice/LoggerUtil.h>
#include <IceGrid/ReplicaCache.h>
#include <IceGrid/ReplicaSessionI.h>
#include <IceGrid/Topics.h>
using namespace std;
using namespace IceGrid;
ReplicaCache::ReplicaCache(const shared_ptr<Ice::Communicator>& communicator,
const shared_ptr<IceStorm::TopicManagerPrx>& topicManager) :
_communicator(communicator)
{
shared_ptr<IceStorm::TopicPrx> t;
try
{
t = topicManager->create("ReplicaObserverTopic");
}
catch(const IceStorm::TopicExists&)
{
t = topicManager->retrieve("ReplicaObserverTopic");
}
const_cast<shared_ptr<IceStorm::TopicPrx>&>(_topic) =
Ice::uncheckedCast<IceStorm::TopicPrx>(t->ice_endpoints(Ice::EndpointSeq()));
const_cast<shared_ptr<ReplicaObserverPrx>&>(_observers) =
Ice::uncheckedCast<ReplicaObserverPrx>(_topic->getPublisher()->ice_endpoints(Ice::EndpointSeq()));
}
shared_ptr<ReplicaEntry>
ReplicaCache::add(const string& name, const shared_ptr<ReplicaSessionI>& session)
{
unique_lock lock(_mutex);
shared_ptr<ReplicaEntry> entry;
while((entry = getImpl(name)))
{
auto s = entry->getSession();
if(s->isDestroyed())
{
// Wait for the session to be removed.
_condVar.wait(lock);
}
else
{
//
// Check if the replica is still reachable, if not, we
// destroy its session.
//
lock.unlock();
try
{
s->getInternalRegistry()->ice_ping();
throw ReplicaActiveException();
}
catch(const Ice::LocalException&)
{
try
{
s->destroy(Ice::emptyCurrent);
}
catch(const Ice::LocalException&)
{
}
}
lock.lock();
}
}
if(_traceLevels && _traceLevels->replica > 0)
{
Ice::Trace out(_traceLevels->logger, _traceLevels->replicaCat);
out << "replica `" << name << "' up";
}
try
{
_observers->replicaAdded(session->getInternalRegistry());
}
catch(const Ice::NoEndpointException&)
{
// Expected if the replica is being shutdown.
}
catch(const Ice::ObjectAdapterDeactivatedException&)
{
// Expected if the replica is being shutdown.
}
catch(const Ice::LocalException& ex)
{
auto traceLevels = getTraceLevels();
if(traceLevels)
{
Ice::Warning out(traceLevels->logger);
out << "unexpected exception while publishing `replicaAdded' update:\n" << ex;
}
}
return addImpl(name, make_shared<ReplicaEntry>(name, session));
}
shared_ptr<ReplicaEntry>
ReplicaCache::remove(const string& name, bool shutdown)
{
lock_guard lock(_mutex);
auto entry = getImpl(name);
assert(entry);
removeImpl(name);
_condVar.notify_all();
if(_traceLevels && _traceLevels->replica > 0)
{
Ice::Trace out(_traceLevels->logger, _traceLevels->replicaCat);
out << "replica `" << name << "' down";
}
if(!shutdown)
{
try
{
_observers->replicaRemoved(entry->getProxy());
}
catch(const Ice::NoEndpointException&)
{
// Expected if the replica is being shutdown.
}
catch(const Ice::ObjectAdapterDeactivatedException&)
{
// Expected if the replica is being shutdown.
}
catch(const Ice::LocalException& ex)
{
auto traceLevels = getTraceLevels();
if(traceLevels)
{
Ice::Warning out(traceLevels->logger);
out << "unexpected exception while publishing `replicaRemoved' update:\n" << ex;
}
}
}
return entry;
}
shared_ptr<ReplicaEntry>
ReplicaCache::get(const string& name) const
{
lock_guard lock(_mutex);
auto entry = getImpl(name);
if(!entry)
{
throw RegistryNotExistException(name);
}
return entry;
}
void
ReplicaCache::subscribe(const shared_ptr<ReplicaObserverPrx>& observer)
{
try
{
lock_guard lock(_mutex);
InternalRegistryPrxSeq replicas;
for(const auto& entry : _entries)
{
replicas.push_back(entry.second->getProxy());
}
IceStorm::QoS qos;
qos["reliability"] = "ordered";
auto publisher = _topic->subscribeAndGetPublisher(qos, observer->ice_twoway());
Ice::uncheckedCast<ReplicaObserverPrx>(publisher)->replicaInit(replicas);
}
catch(const Ice::NoEndpointException&)
{
// The replica is being shutdown.
}
catch(const Ice::ObjectAdapterDeactivatedException&)
{
// Expected if the replica is being shutdown.
}
catch(const Ice::LocalException& ex)
{
auto traceLevels = getTraceLevels();
if(traceLevels)
{
Ice::Warning out(traceLevels->logger);
out << "unexpected exception while subscribing observer from replica observer topic:\n" << ex;
}
}
}
void
ReplicaCache::unsubscribe(const shared_ptr<ReplicaObserverPrx>& observer)
{
try
{
_topic->unsubscribe(observer);
}
catch(const Ice::ObjectAdapterDeactivatedException&)
{
// The replica is being shutdown.
}
catch(const Ice::NoEndpointException&)
{
// The replica is being shutdown.
}
catch(const Ice::LocalException& ex)
{
auto traceLevels = getTraceLevels();
if(traceLevels)
{
Ice::Warning out(traceLevels->logger);
out << "unexpected exception while unsubscribing observer from replica observer topic:\n" << ex;
}
}
}
shared_ptr<Ice::ObjectPrx>
ReplicaCache::getEndpoints(const string& name, const shared_ptr<Ice::ObjectPrx>& proxy) const
{
Ice::EndpointSeq endpoints;
if(proxy)
{
Ice::EndpointSeq endpts = proxy->ice_getEndpoints();
endpoints.insert(endpoints.end(), endpts.begin(), endpts.end());
}
lock_guard lock(_mutex);
for(const auto& entry : _entries)
{
auto prx = entry.second->getSession()->getEndpoint(name);
if(prx)
{
Ice::EndpointSeq endpts = prx->ice_getEndpoints();
endpoints.insert(endpoints.end(), endpts.begin(), endpts.end());
}
}
return _communicator->stringToProxy("dummy")->ice_endpoints(endpoints);
}
void
ReplicaCache::setInternalRegistry(const shared_ptr<InternalRegistryPrx>& proxy)
{
//
// Setup this replica internal registry proxy.
//
_self = proxy;
}
shared_ptr<InternalRegistryPrx>
ReplicaCache::getInternalRegistry() const
{
//
// This replica internal registry proxy.
//
return _self;
}
ReplicaEntry::ReplicaEntry(const std::string& name, const shared_ptr<ReplicaSessionI>& session) :
_name(name),
_session(session)
{
}
const shared_ptr<ReplicaSessionI>&
ReplicaEntry::getSession() const
{
return _session;
}
shared_ptr<InternalReplicaInfo>
ReplicaEntry::getInfo() const
{
return _session->getInfo();
}
shared_ptr<InternalRegistryPrx>
ReplicaEntry::getProxy() const
{
return _session->getInternalRegistry();
}
shared_ptr<Ice::ObjectPrx>
ReplicaEntry::getAdminProxy() const
{
auto prx = getProxy();
assert(prx);
Ice::Identity adminId = { "RegistryAdmin-" + _name, prx->ice_getIdentity().category };
return prx->ice_identity(move(adminId));
}
|