summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/ReplicaCache.cpp
blob: 56cfac1d3e7d73e114ada81149c2536207e3e9e6 (plain)
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
//
// 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 Ice::CommunicatorPtr& communicator, const IceStorm::TopicManagerPrx& topicManager) :
    _communicator(communicator)
{
    IceStorm::TopicPrx t;
    try
    {
        t = topicManager->create("ReplicaObserverTopic");
    }
    catch(const IceStorm::TopicExists&)
    {
        t = topicManager->retrieve("ReplicaObserverTopic");
    }

    const_cast<IceStorm::TopicPrx&>(_topic) = IceStorm::TopicPrx::uncheckedCast(t->ice_endpoints(Ice::EndpointSeq()));
    const_cast<ReplicaObserverPrx&>(_observers) = ReplicaObserverPrx::uncheckedCast(_topic->getPublisher()->ice_endpoints(Ice::EndpointSeq()));
}

ReplicaEntryPtr
ReplicaCache::add(const string& name, const ReplicaSessionIPtr& session)
{
    Lock sync(*this);

    ReplicaEntryPtr entry;
    while((entry = getImpl(name)))
    {
        ReplicaSessionIPtr s = entry->getSession();
        if(s->isDestroyed())
        {
            wait(); // Wait for the session to be removed.
        }
        else
        {
            //
            // Check if the replica is still reachable, if not, we
            // destroy its session.
            //
            sync.release();
            try
            {
                s->getInternalRegistry()->ice_ping();
                throw ReplicaActiveException();
            }
            catch(const Ice::LocalException&)
            {
                try
                {
                    s->destroy(Ice::emptyCurrent);
                }
                catch(const Ice::LocalException&)
                {
                }
            }
            sync.acquire();
        }
    }

    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)
    {
        TraceLevelsPtr traceLevels = getTraceLevels();
        if(traceLevels)
        {
            Ice::Warning out(traceLevels->logger);
            out << "unexpected exception while publishing `replicaAdded' update:\n" << ex;
        }
    }

    return addImpl(name, new ReplicaEntry(name, session));
}

ReplicaEntryPtr
ReplicaCache::remove(const string& name, bool shutdown)
{
    Lock sync(*this);

    ReplicaEntryPtr entry = getImpl(name);
    assert(entry);
    removeImpl(name);
    notifyAll();

    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)
        {
            TraceLevelsPtr traceLevels = getTraceLevels();
            if(traceLevels)
            {
                Ice::Warning out(traceLevels->logger);
                out << "unexpected exception while publishing `replicaRemoved' update:\n" << ex;
            }
        }
    }

    return entry;
}

ReplicaEntryPtr
ReplicaCache::get(const string& name) const
{
    Lock sync(*this);
    ReplicaEntryPtr entry = getImpl(name);
    if(!entry)
    {
        throw RegistryNotExistException(name);
    }
    return entry;
}

void
ReplicaCache::subscribe(const ReplicaObserverPrx& observer)
{
    try
    {
        Lock sync(*this);
        InternalRegistryPrxSeq replicas;
        for(map<string, ReplicaEntryPtr>::const_iterator p = _entries.begin(); p != _entries.end(); ++p)
        {
            replicas.push_back(p->second->getProxy());
        }

        IceStorm::QoS qos;
        qos["reliability"] = "ordered";
        Ice::ObjectPrx publisher = _topic->subscribeAndGetPublisher(qos, observer->ice_twoway());
        ReplicaObserverPrx::uncheckedCast(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)
    {
        TraceLevelsPtr 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 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)
    {
        TraceLevelsPtr traceLevels = getTraceLevels();
        if(traceLevels)
        {
            Ice::Warning out(traceLevels->logger);
            out << "unexpected exception while unsubscribing observer from replica observer topic:\n" << ex;
        }
    }
}

Ice::ObjectPrx
ReplicaCache::getEndpoints(const string& name, const Ice::ObjectPrx& proxy) const
{
    Ice::EndpointSeq endpoints;

    if(proxy)
    {
        Ice::EndpointSeq endpts = proxy->ice_getEndpoints();
        endpoints.insert(endpoints.end(), endpts.begin(), endpts.end());
    }

    Lock sync(*this);
    for(map<string, ReplicaEntryPtr>::const_iterator p = _entries.begin(); p != _entries.end(); ++p)
    {
        Ice::ObjectPrx prx = p->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 InternalRegistryPrx& proxy)
{
    //
    // Setup this replica internal registry proxy.
    //
    _self = proxy;
}

InternalRegistryPrx
ReplicaCache::getInternalRegistry() const
{
    //
    // This replica internal registry proxy.
    //
    return _self;
}

ReplicaEntry::ReplicaEntry(const std::string& name, const ReplicaSessionIPtr& session) :
    _name(name),
    _session(session)
{
}

ReplicaEntry::~ReplicaEntry()
{
}

const ReplicaSessionIPtr&
ReplicaEntry::getSession() const
{
    return _session;
}

InternalReplicaInfoPtr
ReplicaEntry::getInfo() const
{
    return _session->getInfo();
}

InternalRegistryPrx
ReplicaEntry::getProxy() const
{
    return _session->getInternalRegistry();
}

Ice::ObjectPrx
ReplicaEntry::getAdminProxy() const
{
    Ice::ObjectPrx prx = getProxy();
    assert(prx);
    Ice::Identity adminId;
    adminId.name = "RegistryAdmin-" + _name;
    adminId.category = prx->ice_getIdentity().category;
    return prx->ice_identity(adminId);
}