blob: fa6680dd474cd46bb5cc1fed8e89ec8d72883487 [file] [log] [blame]
Yingdi Yuf7ede412014-08-30 20:37:52 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Ashlesh Gawande9a306fe2019-01-04 11:38:18 -06003 * Copyright (c) 2012-2019 University of California, Los Angeles
Yingdi Yuf7ede412014-08-30 20:37:52 -07004 *
5 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
7 *
8 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
20 * @author Chaoyi Bian <bcy@pku.edu.cn>
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22 * @author Yingdi Yu <yingdi@cs.ucla.edu>
Sonu Mishra0dadc572016-12-12 23:59:41 -080023 * @author Sonu Mishra <https://www.linkedin.com/in/mishrasonu>
Yingdi Yuf7ede412014-08-30 20:37:52 -070024 */
25
26#include "logic.hpp"
27#include "logger.hpp"
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050028#include "bzip2-helper.hpp"
Yingdi Yuf7ede412014-08-30 20:37:52 -070029
Alexander Afanasyev89036292018-02-13 17:19:50 -050030#include <ndn-cxx/util/backports.hpp>
Ashlesh Gawande687cf922017-05-30 15:04:16 -050031#include <ndn-cxx/util/string-helper.hpp>
32
Alexander Afanasyev36eb3ed2017-01-11 12:35:58 -080033INIT_LOGGER(Logic);
Yingdi Yuf7ede412014-08-30 20:37:52 -070034
Yingdi Yuf7ede412014-08-30 20:37:52 -070035#define _LOG_DEBUG_ID(v) _LOG_DEBUG("Instance" << m_instanceId << ": " << v)
Yingdi Yuf7ede412014-08-30 20:37:52 -070036
37namespace chronosync {
38
Yingdi Yuf7ede412014-08-30 20:37:52 -070039const uint8_t EMPTY_DIGEST_VALUE[] = {
40 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
41 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
42 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
43 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
44};
45
Ashlesh Gawande08784d42017-09-06 23:40:21 -050046int Logic::s_instanceCounter = 0;
Yingdi Yuf7ede412014-08-30 20:37:52 -070047
Yingdi Yucd339022014-11-05 17:51:19 -080048const ndn::Name Logic::DEFAULT_NAME;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080049const ndn::Name Logic::EMPTY_NAME;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050050const std::shared_ptr<Validator> Logic::DEFAULT_VALIDATOR;
Yingdi Yuf7ede412014-08-30 20:37:52 -070051const time::steady_clock::Duration Logic::DEFAULT_RESET_TIMER = time::seconds(0);
52const time::steady_clock::Duration Logic::DEFAULT_CANCEL_RESET_TIMER = time::milliseconds(500);
53const time::milliseconds Logic::DEFAULT_RESET_INTEREST_LIFETIME(1000);
54const time::milliseconds Logic::DEFAULT_SYNC_INTEREST_LIFETIME(1000);
55const time::milliseconds Logic::DEFAULT_SYNC_REPLY_FRESHNESS(1000);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080056const time::milliseconds Logic::DEFAULT_RECOVERY_INTEREST_LIFETIME(1000);
Yingdi Yuf7ede412014-08-30 20:37:52 -070057
Ashlesh Gawande08784d42017-09-06 23:40:21 -050058const ConstBufferPtr Logic::EMPTY_DIGEST(new ndn::Buffer(EMPTY_DIGEST_VALUE, 32));
Yingdi Yuf7ede412014-08-30 20:37:52 -070059const ndn::name::Component Logic::RESET_COMPONENT("reset");
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080060const ndn::name::Component Logic::RECOVERY_COMPONENT("recovery");
Yingdi Yuf7ede412014-08-30 20:37:52 -070061
Alexander Afanasyev89036292018-02-13 17:19:50 -050062const size_t NDNLP_EXPECTED_OVERHEAD = 20;
63
64/**
65 * Get maximum packet limit
66 *
67 * By default, it returns `ndn::MAX_NDN_PACKET_SIZE`.
68 * The returned value can be customized using the environment variable `CHRONOSYNC_MAX_PACKET_SIZE`,
69 * but the returned value will be at least 500 and no more than `ndn::MAX_NDN_PACKET_SIZE`.
70 */
71#ifndef CHRONOSYNC_HAVE_TESTS
72static
73#endif // CHRONOSYNC_HAVE_TESTS
74size_t
75getMaxPacketLimit()
76{
77 static size_t limit = 0;
78#ifndef CHRONOSYNC_HAVE_TESTS
79 if (limit != 0) {
80 return limit;
81 }
82#endif // CHRONOSYNC_HAVE_TESTS
83
84 if (getenv("CHRONOSYNC_MAX_PACKET_SIZE") != nullptr) {
85 try {
86 limit = ndn::clamp<size_t>(boost::lexical_cast<size_t>(getenv("CHRONOSYNC_MAX_PACKET_SIZE")),
87 500, ndn::MAX_NDN_PACKET_SIZE);
88 }
89 catch (const boost::bad_lexical_cast&) {
90 limit = ndn::MAX_NDN_PACKET_SIZE;
91 }
92 }
93 else {
94 limit = ndn::MAX_NDN_PACKET_SIZE;
95 }
96
97 return limit;
98}
99
Yingdi Yuf7ede412014-08-30 20:37:52 -0700100Logic::Logic(ndn::Face& face,
101 const Name& syncPrefix,
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800102 const Name& defaultUserPrefix,
Yingdi Yuf7ede412014-08-30 20:37:52 -0700103 const UpdateCallback& onUpdate,
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800104 const Name& defaultSigningId,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500105 std::shared_ptr<Validator> validator,
Yingdi Yuf7ede412014-08-30 20:37:52 -0700106 const time::steady_clock::Duration& resetTimer,
107 const time::steady_clock::Duration& cancelResetTimer,
108 const time::milliseconds& resetInterestLifetime,
109 const time::milliseconds& syncInterestLifetime,
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800110 const time::milliseconds& syncReplyFreshness,
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -0500111 const time::milliseconds& recoveryInterestLifetime,
112 const name::Component& session)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700113 : m_face(face)
114 , m_syncPrefix(syncPrefix)
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800115 , m_defaultUserPrefix(defaultUserPrefix)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700116 , m_interestTable(m_face.getIoService())
Yingdi Yuf7ede412014-08-30 20:37:52 -0700117 , m_isInReset(false)
118 , m_needPeriodReset(resetTimer > time::steady_clock::Duration::zero())
119 , m_onUpdate(onUpdate)
120 , m_scheduler(m_face.getIoService())
Ashlesh Gawande9a306fe2019-01-04 11:38:18 -0600121 , m_rng(ndn::random::getRandomNumberEngine())
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600122 , m_rangeUniformRandom(100, 500)
123 , m_reexpressionJitter(100, 500)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700124 , m_resetTimer(resetTimer)
125 , m_cancelResetTimer(cancelResetTimer)
126 , m_resetInterestLifetime(resetInterestLifetime)
127 , m_syncInterestLifetime(syncInterestLifetime)
128 , m_syncReplyFreshness(syncReplyFreshness)
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800129 , m_recoveryInterestLifetime(recoveryInterestLifetime)
Yingdi Yucd339022014-11-05 17:51:19 -0800130 , m_validator(validator)
Alexander Afanasyev90587b82018-02-11 20:36:53 -0500131 , m_instanceId(s_instanceCounter++)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700132{
Yingdi Yuf7ede412014-08-30 20:37:52 -0700133 _LOG_DEBUG_ID(">> Logic::Logic");
134
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -0500135 addUserNode(m_defaultUserPrefix, defaultSigningId, session);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800136
Yingdi Yuf7ede412014-08-30 20:37:52 -0700137 m_syncReset = m_syncPrefix;
138 m_syncReset.append("reset");
139
140 _LOG_DEBUG_ID("Listen to: " << m_syncPrefix);
Junxiao Shi8e4e76d2019-02-08 15:25:08 -0700141 m_syncRegisteredPrefix = m_face.setInterestFilter(
142 ndn::InterestFilter(m_syncPrefix).allowLoopback(false),
143 bind(&Logic::onSyncInterest, this, _1, _2),
144 bind(&Logic::onSyncRegisterFailed, this, _1, _2));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700145
Qiuhan Dinge246b622014-12-03 21:57:48 -0800146 sendSyncInterest();
Yingdi Yuf7ede412014-08-30 20:37:52 -0700147 _LOG_DEBUG_ID("<< Logic::Logic");
148}
149
150Logic::~Logic()
151{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600152 _LOG_DEBUG_ID(">> Logic::~Logic");
Yingdi Yu9d5679a2015-02-01 00:17:58 -0800153 m_interestTable.clear();
Nick Gordon0b3beab2018-03-02 13:03:28 -0600154 m_scheduler.cancelAllEvents();
155 _LOG_DEBUG_ID("<< Logic::~Logic");
Yingdi Yuf7ede412014-08-30 20:37:52 -0700156}
157
158void
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800159Logic::reset(bool isOnInterest)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700160{
161 m_isInReset = true;
162
163 m_state.reset();
164 m_log.clear();
165
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800166 if (!isOnInterest)
167 sendResetInterest();
Yingdi Yuf7ede412014-08-30 20:37:52 -0700168
Yingdi Yuf7ede412014-08-30 20:37:52 -0700169 sendSyncInterest();
170
Davide Pesaventod057cf12019-03-20 23:38:40 -0400171 m_delayedInterestProcessingId = m_scheduler.schedule(m_cancelResetTimer, [this] { cancelReset(); });
Yingdi Yuf7ede412014-08-30 20:37:52 -0700172}
173
174void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800175Logic::setDefaultUserPrefix(const Name& defaultUserPrefix)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700176{
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800177 if (defaultUserPrefix != EMPTY_NAME) {
178 if (m_nodeList.find(defaultUserPrefix) != m_nodeList.end()) {
179 m_defaultUserPrefix = defaultUserPrefix;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800180 }
181 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700182}
183
184void
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -0500185Logic::addUserNode(const Name& userPrefix, const Name& signingId, const name::Component& session)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700186{
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800187 if (userPrefix == EMPTY_NAME)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700188 return;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800189 if (m_defaultUserPrefix == EMPTY_NAME) {
190 m_defaultUserPrefix = userPrefix;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800191 }
192 if (m_nodeList.find(userPrefix) == m_nodeList.end()) {
193 m_nodeList[userPrefix].userPrefix = userPrefix;
194 m_nodeList[userPrefix].signingId = signingId;
195 Name sessionName = userPrefix;
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -0500196 if (!session.empty()) {
197 sessionName.append(session);
198 }
199 else {
200 sessionName.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count());
201 }
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800202 m_nodeList[userPrefix].sessionName = sessionName;
203 m_nodeList[userPrefix].seqNo = 0;
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800204 reset(false);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800205 }
206}
Yingdi Yuf7ede412014-08-30 20:37:52 -0700207
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800208void
209Logic::removeUserNode(const Name& userPrefix)
210{
211 auto userNode = m_nodeList.find(userPrefix);
212 if (userNode != m_nodeList.end()) {
213 m_nodeList.erase(userNode);
214 if (m_defaultUserPrefix == userPrefix) {
215 if (!m_nodeList.empty()) {
216 m_defaultUserPrefix = m_nodeList.begin()->second.userPrefix;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800217 }
218 else {
219 m_defaultUserPrefix = EMPTY_NAME;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800220 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700221 }
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800222 reset(false);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800223 }
224}
Yingdi Yuf7ede412014-08-30 20:37:52 -0700225
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800226const Name&
227Logic::getSessionName(Name prefix)
228{
229 if (prefix == EMPTY_NAME)
230 prefix = m_defaultUserPrefix;
231 auto node = m_nodeList.find(prefix);
232 if (node != m_nodeList.end())
233 return node->second.sessionName;
234 else
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800235 BOOST_THROW_EXCEPTION(Error("Refer to non-existent node:" + prefix.toUri()));
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800236}
Yingdi Yuf7ede412014-08-30 20:37:52 -0700237
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800238const SeqNo&
239Logic::getSeqNo(Name prefix)
240{
241 if (prefix == EMPTY_NAME)
242 prefix = m_defaultUserPrefix;
243 auto node = m_nodeList.find(prefix);
244 if (node != m_nodeList.end())
245 return node->second.seqNo;
246 else
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800247 BOOST_THROW_EXCEPTION(Logic::Error("Refer to non-existent node:" + prefix.toUri()));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700248
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800249}
250
251void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800252Logic::updateSeqNo(const SeqNo& seqNo, const Name& updatePrefix)
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800253{
254 Name prefix;
255 if (updatePrefix == EMPTY_NAME) {
256 if (m_defaultUserPrefix == EMPTY_NAME)
257 return;
258 prefix = m_defaultUserPrefix;
259 }
260 else
261 prefix = updatePrefix;
262
263 auto it = m_nodeList.find(prefix);
264 if (it != m_nodeList.end()) {
265 NodeInfo& node = it->second;
266 _LOG_DEBUG_ID(">> Logic::updateSeqNo");
267 _LOG_DEBUG_ID("seqNo: " << seqNo << " m_seqNo: " << node.seqNo);
268 if (seqNo < node.seqNo || seqNo == 0)
269 return;
270
271 node.seqNo = seqNo;
272 _LOG_DEBUG_ID("updateSeqNo: m_seqNo " << node.seqNo);
273
274 if (!m_isInReset) {
275 _LOG_DEBUG_ID("updateSeqNo: not in Reset ");
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500276 ConstBufferPtr previousRoot = m_state.getRootDigest();
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800277 {
Davide Pesavento5473abe2017-10-09 01:35:33 -0400278 std::string hash = ndn::toHex(previousRoot->data(), previousRoot->size(), false);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800279 _LOG_DEBUG_ID("Hash: " << hash);
280 }
281
282 bool isInserted = false;
283 bool isUpdated = false;
284 SeqNo oldSeq;
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500285 std::tie(isInserted, isUpdated, oldSeq) = m_state.update(node.sessionName, node.seqNo);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800286
287 _LOG_DEBUG_ID("Insert: " << std::boolalpha << isInserted);
288 _LOG_DEBUG_ID("Updated: " << std::boolalpha << isUpdated);
289 if (isInserted || isUpdated) {
290 DiffStatePtr commit = make_shared<DiffState>();
291 commit->update(node.sessionName, node.seqNo);
292 commit->setRootDigest(m_state.getRootDigest());
293 insertToDiffLog(commit, previousRoot);
294
295 satisfyPendingSyncInterests(prefix, commit);
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500296 // formAndSendExcludeInterest(prefix, *commit, previousRoot);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800297 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700298 }
299 }
300}
301
302ConstBufferPtr
303Logic::getRootDigest() const
304{
305 return m_state.getRootDigest();
306}
307
308void
309Logic::printState(std::ostream& os) const
310{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600311 for (const auto& leaf : m_state.getLeaves()) {
312 os << *leaf << "\n";
313 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700314}
315
316std::set<Name>
317Logic::getSessionNames() const
318{
319 std::set<Name> sessionNames;
Nick Gordon0b3beab2018-03-02 13:03:28 -0600320 for (const auto& leaf : m_state.getLeaves()) {
321 sessionNames.insert(leaf->getSessionName());
322 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700323 return sessionNames;
324}
325
326void
327Logic::onSyncInterest(const Name& prefix, const Interest& interest)
328{
329 _LOG_DEBUG_ID(">> Logic::onSyncInterest");
330 Name name = interest.getName();
331
332 _LOG_DEBUG_ID("InterestName: " << name);
333
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800334 if (name.size() >= 1 && RESET_COMPONENT == name.get(-1)) {
335 processResetInterest(interest);
336 }
337 else if (name.size() >= 2 && RECOVERY_COMPONENT == name.get(-2)) {
338 processRecoveryInterest(interest);
339 }
Ashlesh Gawande1d1092d2018-08-03 14:36:49 -0500340 else {
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500341 processSyncInterest(interest);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700342 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700343
344 _LOG_DEBUG_ID("<< Logic::onSyncInterest");
345}
346
347void
348Logic::onSyncRegisterFailed(const Name& prefix, const std::string& msg)
349{
350 //Sync prefix registration failed
351 _LOG_DEBUG_ID(">> Logic::onSyncRegisterFailed");
352}
353
354void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800355Logic::onSyncData(const Interest& interest, const Data& data)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700356{
357 _LOG_DEBUG_ID(">> Logic::onSyncData");
Ashlesh Gawande1d1092d2018-08-03 14:36:49 -0500358 if (m_validator != nullptr)
359 m_validator->validate(data,
360 bind(&Logic::onSyncDataValidated, this, _1),
361 bind(&Logic::onSyncDataValidationFailed, this, _1));
362 else
363 onSyncDataValidated(data);
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800364
Yingdi Yuf7ede412014-08-30 20:37:52 -0700365 _LOG_DEBUG_ID("<< Logic::onSyncData");
366}
367
368void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800369Logic::onResetData(const Interest& interest, const Data& data)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700370{
371 // This should not happened, drop the received data.
372}
373
374void
375Logic::onSyncTimeout(const Interest& interest)
376{
377 // It is OK. Others will handle the time out situation.
378 _LOG_DEBUG_ID(">> Logic::onSyncTimeout");
379 _LOG_DEBUG_ID("Interest: " << interest.getName());
380 _LOG_DEBUG_ID("<< Logic::onSyncTimeout");
381}
382
383void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500384Logic::onSyncDataValidationFailed(const Data& data)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700385{
386 // SyncReply cannot be validated.
387}
388
389void
Ashlesh Gawande1d1092d2018-08-03 14:36:49 -0500390Logic::onSyncDataValidated(const Data& data)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700391{
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500392 Name name = data.getName();
Yingdi Yuf7ede412014-08-30 20:37:52 -0700393 ConstBufferPtr digest = make_shared<ndn::Buffer>(name.get(-1).value(), name.get(-1).value_size());
394
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500395 try {
396 auto contentBuffer = bzip2::decompress(reinterpret_cast<const char*>(data.getContent().value()),
397 data.getContent().value_size());
Ashlesh Gawande1d1092d2018-08-03 14:36:49 -0500398 processSyncData(name, digest, Block(std::move(contentBuffer)));
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500399 }
400 catch (const std::ios_base::failure& error) {
401 _LOG_WARN("Error decompressing content of " << data.getName() << " (" << error.what() << ")");
402 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700403}
404
405void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500406Logic::processSyncInterest(const Interest& interest, bool isTimedProcessing/*=false*/)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700407{
408 _LOG_DEBUG_ID(">> Logic::processSyncInterest");
409
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500410 Name name = interest.getName();
411 ConstBufferPtr digest = make_shared<ndn::Buffer>(name.get(-1).value(), name.get(-1).value_size());
Yingdi Yuf7ede412014-08-30 20:37:52 -0700412
413 ConstBufferPtr rootDigest = m_state.getRootDigest();
414
415 // If the digest of the incoming interest is the same as root digest
416 // Put the interest into InterestTable
417 if (*rootDigest == *digest) {
418 _LOG_DEBUG_ID("Oh, we are in the same state");
419 m_interestTable.insert(interest, digest, false);
420
421 if (!m_isInReset)
422 return;
423
424 if (!isTimedProcessing) {
425 _LOG_DEBUG_ID("Non timed processing in reset");
426 // Still in reset, our own seq has not been put into state yet
427 // Do not hurry, some others may be also resetting and may send their reply
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600428 time::milliseconds after(m_rangeUniformRandom(m_rng));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700429 _LOG_DEBUG_ID("After: " << after);
Davide Pesaventod057cf12019-03-20 23:38:40 -0400430 m_delayedInterestProcessingId = m_scheduler.schedule(after,
431 [=] { processSyncInterest(interest, true); });
Yingdi Yuf7ede412014-08-30 20:37:52 -0700432 }
433 else {
434 _LOG_DEBUG_ID("Timed processing in reset");
435 // Now we can get out of reset state by putting our own stuff into m_state.
436 cancelReset();
437 }
438
439 return;
440 }
441
442 // If the digest of incoming interest is an "empty" digest
Sonu Mishrae10acbc2017-01-18 14:14:05 -0800443 if (*digest == *EMPTY_DIGEST) {
Yingdi Yuf7ede412014-08-30 20:37:52 -0700444 _LOG_DEBUG_ID("Poor guy, he knows nothing");
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800445 sendSyncData(m_defaultUserPrefix, name, m_state);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700446 return;
447 }
448
449 DiffStateContainer::iterator stateIter = m_log.find(digest);
450 // If the digest of incoming interest can be found from the log
451 if (stateIter != m_log.end()) {
452 _LOG_DEBUG_ID("It is ok, you are so close");
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800453 sendSyncData(m_defaultUserPrefix, name, *(*stateIter)->diff());
Yingdi Yuf7ede412014-08-30 20:37:52 -0700454 return;
455 }
456
457 if (!isTimedProcessing) {
458 _LOG_DEBUG_ID("Let's wait, just wait for a while");
459 // Do not hurry, some incoming SyncReplies may help us to recognize the digest
Yingdi Yu53f5f042015-01-31 16:33:25 -0800460 m_interestTable.insert(interest, digest, true);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700461
462 m_delayedInterestProcessingId =
Davide Pesaventod057cf12019-03-20 23:38:40 -0400463 m_scheduler.schedule(time::milliseconds(m_rangeUniformRandom(m_rng)),
464 [=] { processSyncInterest(interest, true); });
Yingdi Yuf7ede412014-08-30 20:37:52 -0700465 }
466 else {
467 // OK, nobody is helping us, just tell the truth.
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800468 _LOG_DEBUG_ID("OK, nobody is helping us, let us try to recover");
Yingdi Yuf7ede412014-08-30 20:37:52 -0700469 m_interestTable.erase(digest);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800470 sendRecoveryInterest(digest);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700471 }
472
473 _LOG_DEBUG_ID("<< Logic::processSyncInterest");
474}
475
476void
477Logic::processResetInterest(const Interest& interest)
478{
479 _LOG_DEBUG_ID(">> Logic::processResetInterest");
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800480 reset(true);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700481}
482
483void
484Logic::processSyncData(const Name& name,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500485 ConstBufferPtr digest,
Ashlesh Gawande1d1092d2018-08-03 14:36:49 -0500486 const Block& syncReplyBlock)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700487{
488 _LOG_DEBUG_ID(">> Logic::processSyncData");
Yingdi Yuf7ede412014-08-30 20:37:52 -0700489 DiffStatePtr commit = make_shared<DiffState>();
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500490 ConstBufferPtr previousRoot = m_state.getRootDigest();
Yingdi Yuf7ede412014-08-30 20:37:52 -0700491
492 try {
493 m_interestTable.erase(digest); // Remove satisfied interest from PIT
494
495 State reply;
496 reply.wireDecode(syncReplyBlock);
497
498 std::vector<MissingDataInfo> v;
Davide Pesaventod057cf12019-03-20 23:38:40 -0400499 BOOST_FOREACH(ConstLeafPtr leaf, reply.getLeaves().get<ordered>()) {
500 BOOST_ASSERT(leaf != nullptr);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700501
Davide Pesaventod057cf12019-03-20 23:38:40 -0400502 const Name& info = leaf->getSessionName();
503 SeqNo seq = leaf->getSeq();
Yingdi Yuf7ede412014-08-30 20:37:52 -0700504
Davide Pesaventod057cf12019-03-20 23:38:40 -0400505 bool isInserted = false;
506 bool isUpdated = false;
507 SeqNo oldSeq;
508 std::tie(isInserted, isUpdated, oldSeq) = m_state.update(info, seq);
509 if (isInserted || isUpdated) {
510 commit->update(info, seq);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700511
Davide Pesaventod057cf12019-03-20 23:38:40 -0400512 oldSeq++;
513 MissingDataInfo mdi = {info, oldSeq, seq};
514 v.push_back(mdi);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700515 }
Davide Pesaventod057cf12019-03-20 23:38:40 -0400516 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700517
518 if (!v.empty()) {
519 m_onUpdate(v);
520
521 commit->setRootDigest(m_state.getRootDigest());
522 insertToDiffLog(commit, previousRoot);
523 }
524 else {
525 _LOG_DEBUG_ID("What? nothing new");
526 }
527 }
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800528 catch (const State::Error&) {
Yingdi Yuf7ede412014-08-30 20:37:52 -0700529 _LOG_DEBUG_ID("Something really fishy happened during state decoding");
530 // Something really fishy happened during state decoding;
531 commit.reset();
532 return;
533 }
534
Ashlesh Gawande1d1092d2018-08-03 14:36:49 -0500535 if (static_cast<bool>(commit) && !commit->getLeaves().empty()) {
Yingdi Yuf7ede412014-08-30 20:37:52 -0700536 // state changed and it is safe to express a new interest
Junxiao Shic4902122019-02-08 15:13:50 -0700537 auto after = time::milliseconds(m_reexpressionJitter(m_rng));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700538 _LOG_DEBUG_ID("Reschedule sync interest after: " << after);
Davide Pesaventod057cf12019-03-20 23:38:40 -0400539 m_reexpressingInterestId = m_scheduler.schedule(after, [this] { sendSyncInterest(); });
Yingdi Yuf7ede412014-08-30 20:37:52 -0700540 }
541}
542
543void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800544Logic::satisfyPendingSyncInterests(const Name& updatedPrefix, ConstDiffStatePtr commit)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700545{
546 _LOG_DEBUG_ID(">> Logic::satisfyPendingSyncInterests");
547 try {
548 _LOG_DEBUG_ID("InterestTable size: " << m_interestTable.size());
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500549 auto it = m_interestTable.begin();
550 while (it != m_interestTable.end()) {
Yingdi Yuf7ede412014-08-30 20:37:52 -0700551 ConstUnsatisfiedInterestPtr request = *it;
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500552 ++it;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700553 if (request->isUnknown)
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500554 sendSyncData(updatedPrefix, request->interest.getName(), m_state);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700555 else
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500556 sendSyncData(updatedPrefix, request->interest.getName(), *commit);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700557 }
558 m_interestTable.clear();
559 }
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800560 catch (const InterestTable::Error&) {
Yingdi Yuf7ede412014-08-30 20:37:52 -0700561 // ok. not really an error
562 }
563 _LOG_DEBUG_ID("<< Logic::satisfyPendingSyncInterests");
564}
565
566void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500567Logic::insertToDiffLog(DiffStatePtr commit, ConstBufferPtr previousRoot)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700568{
569 _LOG_DEBUG_ID(">> Logic::insertToDiffLog");
570 // Connect to the history
571 if (!m_log.empty())
572 (*m_log.find(previousRoot))->setNext(commit);
573
574 // Insert the commit
575 m_log.erase(commit->getRootDigest());
576 m_log.insert(commit);
577 _LOG_DEBUG_ID("<< Logic::insertToDiffLog");
578}
579
580void
581Logic::sendResetInterest()
582{
583 _LOG_DEBUG_ID(">> Logic::sendResetInterest");
584
585 if (m_needPeriodReset) {
586 _LOG_DEBUG_ID("Need Period Reset");
587 _LOG_DEBUG_ID("ResetTimer: " << m_resetTimer);
588
Davide Pesaventod057cf12019-03-20 23:38:40 -0400589 m_resetInterestId = m_scheduler.schedule(m_resetTimer + ndn::time::milliseconds(m_reexpressionJitter(m_rng)),
590 [this] { sendResetInterest(); });
Yingdi Yuf7ede412014-08-30 20:37:52 -0700591 }
592
593 Interest interest(m_syncReset);
594 interest.setMustBeFresh(true);
Ashlesh Gawande1d1092d2018-08-03 14:36:49 -0500595 interest.setCanBePrefix(false); // no data is expected
Yingdi Yuf7ede412014-08-30 20:37:52 -0700596 interest.setInterestLifetime(m_resetInterestLifetime);
Junxiao Shi8e4e76d2019-02-08 15:25:08 -0700597
598 // Assigning to m_pendingResetInterest cancels the previous reset Interest.
599 // This is harmless since no Data is expected.
600 m_pendingResetInterest = m_face.expressInterest(interest,
Nick Gordon0b3beab2018-03-02 13:03:28 -0600601 bind(&Logic::onResetData, this, _1, _2),
602 bind(&Logic::onSyncTimeout, this, _1), // Nack
603 bind(&Logic::onSyncTimeout, this, _1));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700604 _LOG_DEBUG_ID("<< Logic::sendResetInterest");
605}
606
607void
608Logic::sendSyncInterest()
609{
610 _LOG_DEBUG_ID(">> Logic::sendSyncInterest");
611
612 Name interestName;
613 interestName.append(m_syncPrefix)
614 .append(ndn::name::Component(*m_state.getRootDigest()));
615
Junxiao Shi8e4e76d2019-02-08 15:25:08 -0700616 m_pendingSyncInterestName = interestName;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700617
618#ifdef _DEBUG
619 printDigest(m_state.getRootDigest());
620#endif
621
Davide Pesaventod057cf12019-03-20 23:38:40 -0400622 m_reexpressingInterestId = m_scheduler.schedule(m_syncInterestLifetime / 2 +
623 ndn::time::milliseconds(m_reexpressionJitter(m_rng)),
624 [this] { sendSyncInterest(); });
Yingdi Yuf7ede412014-08-30 20:37:52 -0700625
626 Interest interest(interestName);
627 interest.setMustBeFresh(true);
Ashlesh Gawande1d1092d2018-08-03 14:36:49 -0500628 interest.setCanBePrefix(true);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700629 interest.setInterestLifetime(m_syncInterestLifetime);
630
Junxiao Shi8e4e76d2019-02-08 15:25:08 -0700631 m_pendingSyncInterest = m_face.expressInterest(interest,
632 bind(&Logic::onSyncData, this, _1, _2),
633 bind(&Logic::onSyncTimeout, this, _1), // Nack
634 bind(&Logic::onSyncTimeout, this, _1));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700635
636 _LOG_DEBUG_ID("Send interest: " << interest.getName());
637 _LOG_DEBUG_ID("<< Logic::sendSyncInterest");
638}
639
640void
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500641Logic::trimState(State& partialState, const State& state, size_t nExcludedStates)
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600642{
643 partialState.reset();
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500644
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600645 std::vector<ConstLeafPtr> leaves;
646 for (const ConstLeafPtr& leaf : state.getLeaves()) {
647 leaves.push_back(leaf);
648 }
649
650 std::shuffle(leaves.begin(), leaves.end(), m_rng);
651
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500652 size_t statesToEncode = leaves.size() - std::min(leaves.size() - 1, nExcludedStates);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600653 for (const auto& constLeafPtr : leaves) {
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500654 if (statesToEncode == 0) {
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600655 break;
656 }
657 partialState.update(constLeafPtr->getSessionName(), constLeafPtr->getSeq());
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500658 --statesToEncode;
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600659 }
660}
661
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500662Data
663Logic::encodeSyncReply(const Name& nodePrefix, const Name& name, const State& state)
664{
665 Data syncReply(name);
666 syncReply.setFreshnessPeriod(m_syncReplyFreshness);
667
668 auto finalizeReply = [this, &nodePrefix, &syncReply] (const State& state) {
669 auto contentBuffer = bzip2::compress(reinterpret_cast<const char*>(state.wireEncode().wire()),
670 state.wireEncode().size());
671 syncReply.setContent(contentBuffer);
672
673 if (m_nodeList[nodePrefix].signingId.empty())
674 m_keyChain.sign(syncReply);
675 else
676 m_keyChain.sign(syncReply, security::signingByIdentity(m_nodeList[nodePrefix].signingId));
677 };
678
679 finalizeReply(state);
680
681 size_t nExcludedStates = 1;
682 while (syncReply.wireEncode().size() > getMaxPacketLimit() - NDNLP_EXPECTED_OVERHEAD) {
683 if (nExcludedStates == 1) {
684 // To show this debug message only once
685 _LOG_DEBUG("Sync reply size exceeded maximum packet limit (" << (getMaxPacketLimit() - NDNLP_EXPECTED_OVERHEAD) << ")");
686 }
687 State partialState;
688 trimState(partialState, state, nExcludedStates);
689 finalizeReply(partialState);
690
691 BOOST_ASSERT(state.getLeaves().size() != 0);
692 nExcludedStates *= 2;
693 }
694
695 return syncReply;
696}
697
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600698void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800699Logic::sendSyncData(const Name& nodePrefix, const Name& name, const State& state)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700700{
701 _LOG_DEBUG_ID(">> Logic::sendSyncData");
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600702 if (m_nodeList.find(nodePrefix) == m_nodeList.end())
703 return;
704
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500705 m_face.put(encodeSyncReply(nodePrefix, name, state));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700706
707 // checking if our own interest got satisfied
Junxiao Shi8e4e76d2019-02-08 15:25:08 -0700708 if (m_pendingSyncInterestName == name) {
Yingdi Yuf7ede412014-08-30 20:37:52 -0700709 // remove outstanding interest
Junxiao Shi8e4e76d2019-02-08 15:25:08 -0700710 m_pendingSyncInterest.cancel();
Yingdi Yuf7ede412014-08-30 20:37:52 -0700711
712 // re-schedule sending Sync interest
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600713 time::milliseconds after(m_reexpressionJitter(m_rng));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700714 _LOG_DEBUG_ID("Satisfy our own interest");
715 _LOG_DEBUG_ID("Reschedule sync interest after " << after);
Davide Pesaventod057cf12019-03-20 23:38:40 -0400716 m_reexpressingInterestId = m_scheduler.schedule(after, [this] { sendSyncInterest(); });
Yingdi Yuf7ede412014-08-30 20:37:52 -0700717 }
718 _LOG_DEBUG_ID("<< Logic::sendSyncData");
719}
720
721void
722Logic::cancelReset()
723{
724 _LOG_DEBUG_ID(">> Logic::cancelReset");
725 if (!m_isInReset)
726 return;
727
728 m_isInReset = false;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800729 for (const auto& node : m_nodeList) {
730 updateSeqNo(node.second.seqNo, node.first);
731 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700732 _LOG_DEBUG_ID("<< Logic::cancelReset");
733}
734
735void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500736Logic::printDigest(ConstBufferPtr digest)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700737{
Davide Pesavento5473abe2017-10-09 01:35:33 -0400738 std::string hash = ndn::toHex(digest->data(), digest->size(), false);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700739 _LOG_DEBUG_ID("Hash: " << hash);
740}
741
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800742void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500743Logic::sendRecoveryInterest(ConstBufferPtr digest)
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800744{
745 _LOG_DEBUG_ID(">> Logic::sendRecoveryInterest");
746
747 Name interestName;
748 interestName.append(m_syncPrefix)
749 .append(RECOVERY_COMPONENT)
750 .append(ndn::name::Component(*digest));
751
752 Interest interest(interestName);
753 interest.setMustBeFresh(true);
Ashlesh Gawande1d1092d2018-08-03 14:36:49 -0500754 interest.setCanBePrefix(true);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800755 interest.setInterestLifetime(m_recoveryInterestLifetime);
756
Junxiao Shi8e4e76d2019-02-08 15:25:08 -0700757 m_pendingRecoveryInterests[interestName[-1].toUri()] = m_face.expressInterest(interest,
Nick Gordon0b3beab2018-03-02 13:03:28 -0600758 bind(&Logic::onRecoveryData, this, _1, _2),
759 bind(&Logic::onRecoveryTimeout, this, _1), // Nack
760 bind(&Logic::onRecoveryTimeout, this, _1));
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800761 _LOG_DEBUG_ID("interest: " << interest.getName());
762 _LOG_DEBUG_ID("<< Logic::sendRecoveryInterest");
763}
764
765void
766Logic::processRecoveryInterest(const Interest& interest)
767{
768 _LOG_DEBUG_ID(">> Logic::processRecoveryInterest");
769
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500770 Name name = interest.getName();
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800771 ConstBufferPtr digest = make_shared<ndn::Buffer>(name.get(-1).value(), name.get(-1).value_size());
772
773 ConstBufferPtr rootDigest = m_state.getRootDigest();
774
775 DiffStateContainer::iterator stateIter = m_log.find(digest);
776
777 if (stateIter != m_log.end() || *digest == *EMPTY_DIGEST || *rootDigest == *digest) {
778 _LOG_DEBUG_ID("I can help you recover");
779 sendSyncData(m_defaultUserPrefix, name, m_state);
780 return;
781 }
782 _LOG_DEBUG_ID("<< Logic::processRecoveryInterest");
783}
784
785void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800786Logic::onRecoveryData(const Interest& interest, const Data& data)
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800787{
788 _LOG_DEBUG_ID(">> Logic::onRecoveryData");
Junxiao Shi8e4e76d2019-02-08 15:25:08 -0700789 m_pendingRecoveryInterests.erase(interest.getName()[-1].toUri());
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500790 onSyncDataValidated(data);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800791 _LOG_DEBUG_ID("<< Logic::onRecoveryData");
792}
793
794void
795Logic::onRecoveryTimeout(const Interest& interest)
796{
797 _LOG_DEBUG_ID(">> Logic::onRecoveryTimeout");
Junxiao Shi8e4e76d2019-02-08 15:25:08 -0700798 m_pendingRecoveryInterests.erase(interest.getName()[-1].toUri());
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800799 _LOG_DEBUG_ID("Interest: " << interest.getName());
800 _LOG_DEBUG_ID("<< Logic::onRecoveryTimeout");
801}
802
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800803} // namespace chronosync