/
full-producer-arbitrary.cpp
352 lines (292 loc) · 11.5 KB
/
full-producer-arbitrary.cpp
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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2019, The University of Memphis
*
* This file is part of PSync.
* See AUTHORS.md for complete list of PSync authors and contributors.
*
* PSync is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* PSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
#include "PSync/full-producer-arbitrary.hpp"
#include <ndn-cxx/util/logger.hpp>
#include <ndn-cxx/util/segment-fetcher.hpp>
#include <ndn-cxx/security/validator-null.hpp>
#include <cstring>
#include <limits>
#include <functional>
namespace psync {
NDN_LOG_INIT(psync.FullProducerArbitrary);
FullProducerArbitrary::FullProducerArbitrary(const size_t expectedNumEntries,
ndn::Face& face,
const ndn::Name& syncPrefix,
const ArbitraryUpdateCallback& onArbitraryUpdateCallback,
ndn::time::milliseconds syncInterestLifetime,
ndn::time::milliseconds syncReplyFreshness,
const ShouldAddToSyncDataCallback& onShouldAddToSyncDataCallback,
const CanAddName& onCanAddName)
: ProducerBase(expectedNumEntries, syncPrefix, syncReplyFreshness)
, m_face(face)
, m_scheduler(m_face.getIoService())
, m_segmentPublisher(m_face, m_keyChain)
, m_syncInterestLifetime(syncInterestLifetime)
, m_onArbitraryUpdateCallback(onArbitraryUpdateCallback)
, m_onShouldAddToSyncDataCallback(onShouldAddToSyncDataCallback)
, m_onCanAddName(onCanAddName)
{
int jitter = m_syncInterestLifetime.count() * .20;
m_jitter = std::uniform_int_distribution<>(-jitter, jitter);
m_registeredPrefix = m_face.setInterestFilter(
ndn::InterestFilter(m_syncPrefix).allowLoopback(false),
std::bind(&FullProducerArbitrary::onSyncInterest, this, _1, _2),
std::bind(&FullProducerArbitrary::onRegisterFailed, this, _1, _2));
// Should we do this after setInterestFilter success call back
// (Currently following ChronoSync's way)
sendSyncInterest();
}
FullProducerArbitrary::~FullProducerArbitrary()
{
if (m_fetcher) {
m_fetcher->stop();
}
}
void
FullProducerArbitrary::publishName(const ndn::Name& name)
{
if (m_name2hash.find(name) != m_name2hash.end()) {
NDN_LOG_DEBUG("Already published, ignoring: " << name);
return;
}
NDN_LOG_INFO("Publish: " << name);
insertToIBF(name);
satisfyPendingInterests();
}
void
FullProducerArbitrary::sendSyncInterest()
{
// If we send two sync interest one after the other
// since there is no new data in the network yet,
// when data is available it may satisfy both of them
if (m_fetcher) {
m_fetcher->stop();
}
// Sync Interest format for full sync: /<sync-prefix>/<ourLatestIBF>
ndn::Name syncInterestName = m_syncPrefix;
// Append our latest IBF
m_iblt.appendToName(syncInterestName);
m_outstandingInterestName = syncInterestName;
m_scheduledSyncInterestId =
m_scheduler.scheduleEvent(m_syncInterestLifetime / 2 + ndn::time::milliseconds(m_jitter(m_rng)),
[this] { sendSyncInterest(); });
ndn::Interest syncInterest(syncInterestName);
ndn::util::SegmentFetcher::Options options;
options.interestLifetime = m_syncInterestLifetime;
options.maxTimeout = m_syncInterestLifetime;
m_fetcher = ndn::util::SegmentFetcher::start(m_face,
syncInterest,
ndn::security::v2::getAcceptAllValidator(),
options);
m_fetcher->onComplete.connect([this, syncInterest] (ndn::ConstBufferPtr bufferPtr) {
onSyncData(syncInterest, bufferPtr);
});
m_fetcher->onError.connect([] (uint32_t errorCode, const std::string& msg) {
NDN_LOG_ERROR("Cannot fetch sync data, error: " <<
errorCode << " message: " << msg);
});
NDN_LOG_DEBUG("sendFullSyncInterest, nonce: " << syncInterest.getNonce() <<
", hash: " << std::hash<ndn::Name>{}(syncInterestName));
}
void
FullProducerArbitrary::onSyncInterest(const ndn::Name& prefixName, const ndn::Interest& interest)
{
if (m_segmentPublisher.replyFromStore(interest.getName())) {
return;
}
ndn::Name nameWithoutSyncPrefix = interest.getName().getSubName(prefixName.size());
ndn::Name interestName;
if (nameWithoutSyncPrefix.size() == 1) {
// Get /<prefix>/IBF from /<prefix>/IBF
interestName = interest.getName();
}
else if (nameWithoutSyncPrefix.size() == 3) {
// Get /<prefix>/IBF from /<prefix>/IBF/<version>/<segment-no>
interestName = interest.getName().getPrefix(-2);
}
else {
return;
}
ndn::name::Component ibltName = interestName.get(-1);
NDN_LOG_DEBUG("Full Sync Interest Received, nonce: " << interest.getNonce() <<
", hash: " << std::hash<ndn::Name>{}(interestName));
IBLT iblt(m_expectedNumEntries);
try {
iblt.initialize(ibltName);
}
catch (const std::exception& e) {
NDN_LOG_WARN(e.what());
return;
}
IBLT diff = m_iblt - iblt;
std::set<uint32_t> positive;
std::set<uint32_t> negative;
if (!diff.listEntries(positive, negative)) {
NDN_LOG_TRACE("Cannot decode differences, positive: " << positive.size()
<< " negative: " << negative.size() << " m_threshold: "
<< m_threshold);
// Send all data if greater then threshold, else send positive below as usual
// Or send if we can't get neither positive nor negative differences
if (positive.size() + negative.size() >= m_threshold ||
(positive.size() == 0 && negative.size() == 0)) {
State state;
for (const auto& it : m_name2hash) {
state.addContent(it.first);
}
if (!state.getContent().empty()) {
m_segmentPublisher.publish(interest.getName(), interest.getName(),
state.wireEncode(), m_syncReplyFreshness);
}
return;
}
}
State state;
for (const auto& hash : positive) {
ndn::Name name = m_hash2name[hash];
ndn::Name prefix = name.getPrefix(-1);
if (m_name2hash.find(name) != m_name2hash.end()) {
if (!m_onShouldAddToSyncDataCallback ||
m_onShouldAddToSyncDataCallback(prefix.toUri(), negative)) {
state.addContent(name);
}
}
}
if (!state.getContent().empty()) {
NDN_LOG_DEBUG("Sending sync content: " << state);
sendSyncData(interestName, state.wireEncode());
return;
}
else {
NDN_LOG_WARN("State is empty");
}
auto& entry = m_pendingEntries.emplace(interestName, PendingEntryInfoFull{iblt, {}}).first->second;
entry.expirationEvent = m_scheduler.scheduleEvent(interest.getInterestLifetime(),
[this, interest] {
NDN_LOG_TRACE("Erase Pending Interest " << interest.getNonce());
m_pendingEntries.erase(interest.getName());
});
}
void
FullProducerArbitrary::sendSyncData(const ndn::Name& name, const ndn::Block& block)
{
NDN_LOG_DEBUG("Checking if data will satisfy our own pending interest");
ndn::Name nameWithIblt;
m_iblt.appendToName(nameWithIblt);
// Append hash of our IBF so that data name maybe different for each node answering
ndn::Name dataName(ndn::Name(name).appendNumber(std::hash<ndn::Name>{}(nameWithIblt)));
// checking if our own interest got satisfied
if (m_outstandingInterestName == name) {
NDN_LOG_DEBUG("Satisfied our own pending interest");
// remove outstanding interest
if (m_fetcher) {
NDN_LOG_DEBUG("Removing our pending interest from face (stop fetcher)");
m_fetcher->stop();
m_outstandingInterestName = ndn::Name("");
}
NDN_LOG_DEBUG("Sending Sync Data");
// Send data after removing pending sync interest on face
m_segmentPublisher.publish(name, dataName, block, m_syncReplyFreshness);
NDN_LOG_TRACE("Renewing sync interest");
sendSyncInterest();
}
else {
NDN_LOG_DEBUG("Sending Sync Data");
m_segmentPublisher.publish(name, dataName, block, m_syncReplyFreshness);
}
}
void
FullProducerArbitrary::onSyncData(const ndn::Interest& interest, const ndn::ConstBufferPtr& bufferPtr)
{
deletePendingInterests(interest.getName());
State state(ndn::Block(std::move(bufferPtr)));
std::vector<ndn::Name> updates;
NDN_LOG_DEBUG("Sync Data Received: " << state);
for (const auto& name : state.getContent()) {
if (m_name2hash.find(name) == m_name2hash.end()) {
NDN_LOG_DEBUG("Checking whether to add");
if (!m_onCanAddName || m_onCanAddName(name)) {
NDN_LOG_DEBUG("Adding...");
updates.push_back(name);
insertToIBF(name);
}
// We should not call satisfyPendingSyncInterests here because we just
// got data and deleted pending interest by calling deletePendingFullSyncInterests
// But we might have interests not matching to this interest that might not have deleted
// from pending sync interest
}
}
// We just got the data, so send a new sync interest
if (!updates.empty()) {
m_onArbitraryUpdateCallback(updates);
NDN_LOG_TRACE("Renewing sync interest");
sendSyncInterest();
}
else {
NDN_LOG_TRACE("No new update, interest nonce: " << interest.getNonce() <<
" , hash: " << std::hash<ndn::Name>{}(interest.getName()));
}
}
void
FullProducerArbitrary::satisfyPendingInterests()
{
NDN_LOG_DEBUG("Satisfying full sync interest: " << m_pendingEntries.size());
for (auto it = m_pendingEntries.begin(); it != m_pendingEntries.end();) {
const PendingEntryInfoFull& entry = it->second;
IBLT diff = m_iblt - entry.iblt;
std::set<uint32_t> positive;
std::set<uint32_t> negative;
if (!diff.listEntries(positive, negative)) {
NDN_LOG_TRACE("Decode failed for pending interest");
if (positive.size() + negative.size() >= m_threshold ||
(positive.size() == 0 && negative.size() == 0)) {
NDN_LOG_TRACE("pos + neg > threshold or no diff can be found, erase pending interest");
m_pendingEntries.erase(it++);
continue;
}
}
State state;
for (const auto& hash : positive) {
ndn::Name name = m_hash2name[hash];
if (m_name2hash.find(name) != m_name2hash.end()) {
state.addContent(name);
}
}
if (!state.getContent().empty()) {
NDN_LOG_DEBUG("Satisfying sync content: " << state);
sendSyncData(it->first, state.wireEncode());
m_pendingEntries.erase(it++);
}
else {
++it;
}
}
}
void
FullProducerArbitrary::deletePendingInterests(const ndn::Name& interestName) {
for (auto it = m_pendingEntries.begin(); it != m_pendingEntries.end();) {
if (it->first == interestName) {
NDN_LOG_TRACE("Delete pending interest: " << interestName);
m_pendingEntries.erase(it++);
}
else {
++it;
}
}
}
} // namespace psync