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
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#include <IceStorm/Instance.h>
#include <IceStorm/TraceLevels.h>
#include <IceStorm/Observers.h>
#include <IceStorm/NodeI.h>
#include <IceStorm/InstrumentationI.h>
#include <IceUtil/Timer.h>
#include <Ice/InstrumentationI.h>
#include <Ice/Communicator.h>
#include <Ice/Properties.h>
#include <Ice/TraceUtil.h>
using namespace std;
using namespace IceStorm;
using namespace IceStormElection;
using namespace IceStormInternal;
namespace IceStormInternal
{
extern IceDB::IceContext dbContext;
}
void
TopicReaper::add(const string& name)
{
lock_guard<mutex> lg(_mutex);
_topics.push_back(name);
}
vector<string>
TopicReaper::consumeReapedTopics()
{
lock_guard<mutex> lg(_mutex);
vector<string> reaped;
reaped.swap(_topics);
return reaped;
}
Instance::Instance(const string& instanceName,
const string& name,
shared_ptr<Ice::Communicator> communicator,
shared_ptr<Ice::ObjectAdapter> publishAdapter,
shared_ptr<Ice::ObjectAdapter> topicAdapter,
shared_ptr<Ice::ObjectAdapter> nodeAdapter,
shared_ptr<NodePrx> nodeProxy) :
_instanceName(instanceName),
_serviceName(name),
_communicator(move(communicator)),
_publishAdapter(move(publishAdapter)),
_topicAdapter(move(topicAdapter)),
_nodeAdapter(move(nodeAdapter)),
_nodeProxy(move(nodeProxy)),
_traceLevels(make_shared<TraceLevels>(name, _communicator->getProperties(), _communicator->getLogger())),
// default one minute.
_discardInterval(_communicator->getProperties()->getPropertyAsIntWithDefault(name + ".Discard.Interval", 60)),
// default one second.
_flushInterval(_communicator->getProperties()->getPropertyAsIntWithDefault(name + ".Flush.Timeout", 1000)),
// default one minute.
_sendTimeout(_communicator->getProperties()->getPropertyAsIntWithDefault(name + ".Send.Timeout", 60 * 1000)),
_sendQueueSizeMax(_communicator->getProperties()->getPropertyAsIntWithDefault(name + ".Send.QueueSizeMax", -1)),
_sendQueueSizeMaxPolicy(RemoveSubscriber),
_topicReaper(make_shared<TopicReaper>()),
_observers(make_shared<Observers>(_traceLevels))
{
try
{
auto properties = _communicator->getProperties();
if(properties->getProperty(name + ".TopicManager.AdapterId").empty())
{
string p = properties->getProperty(name + ".ReplicatedTopicManagerEndpoints");
if(!p.empty())
{
const_cast<shared_ptr<Ice::ObjectPrx>&>(_topicReplicaProxy) =
_communicator->stringToProxy("dummy:" + p);
}
p = properties->getProperty(name + ".ReplicatedPublishEndpoints");
if(!p.empty())
{
const_cast<shared_ptr<Ice::ObjectPrx>&>(_publisherReplicaProxy) =
_communicator->stringToProxy("dummy:" + p);
}
}
_timer = new IceUtil::Timer();
string policy = properties->getProperty(name + ".Send.QueueSizeMaxPolicy");
if(policy == "RemoveSubscriber")
{
const_cast<SendQueueSizeMaxPolicy&>(_sendQueueSizeMaxPolicy) = RemoveSubscriber;
}
else if(policy == "DropEvents")
{
const_cast<SendQueueSizeMaxPolicy&>(_sendQueueSizeMaxPolicy) = DropEvents;
}
else if(!policy.empty())
{
Ice::Warning warn(_traceLevels->logger);
warn << "invalid value `" << policy << "' for `" << name << ".Send.QueueSizeMaxPolicy'";
}
//
// If an Ice metrics observer is setup on the communicator, also
// enable metrics for IceStorm.
//
auto o = dynamic_pointer_cast<IceInternal::CommunicatorObserverI>(_communicator->getObserver());
if(o)
{
_observer = make_shared<TopicManagerObserverI>(o->getFacet());
}
}
catch(const std::exception&)
{
shutdown();
destroy();
throw;
}
}
Instance::~Instance()
{
}
void
Instance::setNode(shared_ptr<NodeI> node)
{
_node = move(node);
}
string
Instance::instanceName() const
{
return _instanceName;
}
string
Instance::serviceName() const
{
return _serviceName;
}
shared_ptr<Ice::Communicator>
Instance::communicator() const
{
return _communicator;
}
shared_ptr<Ice::Properties>
Instance::properties() const
{
return _communicator->getProperties();
}
shared_ptr<Ice::ObjectAdapter>
Instance::publishAdapter() const
{
return _publishAdapter;
}
shared_ptr<Ice::ObjectAdapter>
Instance::topicAdapter() const
{
return _topicAdapter;
}
shared_ptr<Ice::ObjectAdapter>
Instance::nodeAdapter() const
{
return _nodeAdapter;
}
shared_ptr<Observers>
Instance::observers() const
{
return _observers;
}
shared_ptr<NodeI>
Instance::node() const
{
return _node;
}
shared_ptr<NodePrx>
Instance::nodeProxy() const
{
return _nodeProxy;
}
shared_ptr<TraceLevels>
Instance::traceLevels() const
{
return _traceLevels;
}
IceUtil::TimerPtr
Instance::timer() const
{
return _timer;
}
shared_ptr<Ice::ObjectPrx>
Instance::topicReplicaProxy() const
{
return _topicReplicaProxy;
}
shared_ptr<Ice::ObjectPrx>
Instance::publisherReplicaProxy() const
{
return _publisherReplicaProxy;
}
shared_ptr<IceStorm::Instrumentation::TopicManagerObserver>
Instance::observer() const
{
return _observer;
}
shared_ptr<IceStorm::TopicReaper>
Instance::topicReaper() const
{
return _topicReaper;
}
chrono::seconds
Instance::discardInterval() const
{
return _discardInterval;
}
chrono::milliseconds
Instance::flushInterval() const
{
return _flushInterval;
}
chrono::milliseconds
Instance::sendTimeout() const
{
return _sendTimeout;
}
int
Instance::sendQueueSizeMax() const
{
return _sendQueueSizeMax;
}
Instance::SendQueueSizeMaxPolicy
Instance::sendQueueSizeMaxPolicy() const
{
return _sendQueueSizeMaxPolicy;
}
void
Instance::shutdown()
{
if(_node)
{
_node->destroy();
assert(_nodeAdapter);
_nodeAdapter->destroy();
}
_topicAdapter->destroy();
_publishAdapter->destroy();
if(_timer)
{
_timer->destroy();
}
}
void
Instance::destroy()
{
// The node instance must be cleared as the node holds the
// replica (TopicManager) which holds the instance causing a
// cyclic reference.
_node = nullptr;
//
// The observer instance must be cleared as it holds the
// TopicManagerImpl which holds the instance causing a
// cyclic reference.
//
_observer = nullptr;
}
PersistentInstance::PersistentInstance(const string& instanceName,
const string& name,
shared_ptr<Ice::Communicator> communicator,
shared_ptr<Ice::ObjectAdapter> publishAdapter,
shared_ptr<Ice::ObjectAdapter> topicAdapter,
shared_ptr<Ice::ObjectAdapter> nodeAdapter,
shared_ptr<NodePrx> nodeProxy) :
Instance(instanceName, name, communicator, move(publishAdapter), move(topicAdapter), move(nodeAdapter),
move(nodeProxy)),
_dbLock(communicator->getProperties()->getPropertyWithDefault(name + ".LMDB.Path", name) + "/icedb.lock"),
_dbEnv(communicator->getProperties()->getPropertyWithDefault(name + ".LMDB.Path", name), 2,
IceDB::getMapSize(communicator->getProperties()->getPropertyAsInt(name + ".LMDB.MapSize")))
{
try
{
dbContext.communicator = move(communicator);
dbContext.encoding.minor = 1;
dbContext.encoding.major = 1;
IceDB::ReadWriteTxn txn(_dbEnv);
_lluMap = LLUMap(txn, "llu", dbContext, MDB_CREATE);
_subscriberMap = SubscriberMap(txn, "subscribers", dbContext, MDB_CREATE, compareSubscriberRecordKey);
txn.commit();
}
catch(const std::exception&)
{
shutdown();
destroy();
throw;
}
}
void
PersistentInstance::destroy()
{
_dbEnv.close();
dbContext.communicator = nullptr;
Instance::destroy();
}
|