blob: 4f75085918131594f1a99dbb1073a329dc100a2a [file] [log] [blame]
Yingdi Yuf7ede412014-08-30 20:37:52 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Sonu Mishra4d3a2e02017-01-18 20:27:51 -08003 * Copyright (c) 2012-2017 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 Mishra4d3a2e02017-01-18 20:27:51 -080023 * @author Sonu Mishra <https://www.linkedin.com/in/mishrasonu>
Yingdi Yuf7ede412014-08-30 20:37:52 -070024 */
25
26#ifndef CHRONOSYNC_LOGIC_HPP
27#define CHRONOSYNC_LOGIC_HPP
28
29#include "boost-header.h"
30#include <memory>
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080031#include <unordered_map>
Yingdi Yuf7ede412014-08-30 20:37:52 -070032
33#include <ndn-cxx/face.hpp>
34#include <ndn-cxx/util/scheduler.hpp>
35#include <ndn-cxx/security/key-chain.hpp>
Yingdi Yucd339022014-11-05 17:51:19 -080036#include <ndn-cxx/security/validator.hpp>
Yingdi Yuf7ede412014-08-30 20:37:52 -070037
38#include "interest-table.hpp"
39#include "diff-state-container.hpp"
40
41namespace chronosync {
42
43/**
44 * @brief The missing sequence numbers for a session
45 *
46 * This class is used to notify the clients of Logic
47 * the details of state changes.
48 *
49 * Instances of this class is usually used as elements of some containers
50 * such as std::vector, thus it is copyable.
51 */
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080052class NodeInfo
53{
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080054public:
55 Name userPrefix;
56 Name signingId;
57 Name sessionName;
58 SeqNo seqNo;
59};
60
Yingdi Yuf7ede412014-08-30 20:37:52 -070061class MissingDataInfo
62{
63public:
64 /// @brief session name
65 Name session;
66 /// @brief the lowest one of missing sequence numbers
67 SeqNo low;
68 /// @brief the highest one of missing sequence numbers
69 SeqNo high;
70};
71
72/**
73 * @brief The callback function to handle state updates
74 *
75 * The parameter is a set of MissingDataInfo, of which each corresponds to
76 * a session that has changed its state.
77 */
78typedef function<void(const std::vector<MissingDataInfo>&)> UpdateCallback;
79
80/**
81 * @brief Logic of ChronoSync
82 */
83class Logic : noncopyable
84{
85public:
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080086 class Error : public std::runtime_error
87 {
88 public:
89 explicit
90 Error(const std::string& what)
91 : std::runtime_error(what)
92 {
93 }
94 };
95
96public:
Yingdi Yuf7ede412014-08-30 20:37:52 -070097 static const time::steady_clock::Duration DEFAULT_RESET_TIMER;
98 static const time::steady_clock::Duration DEFAULT_CANCEL_RESET_TIMER;
99 static const time::milliseconds DEFAULT_RESET_INTEREST_LIFETIME;
100 static const time::milliseconds DEFAULT_SYNC_INTEREST_LIFETIME;
101 static const time::milliseconds DEFAULT_SYNC_REPLY_FRESHNESS;
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800102 static const time::milliseconds DEFAULT_RECOVERY_INTEREST_LIFETIME;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700103
104 /**
105 * @brief Constructor
106 *
Yingdi Yu9d5679a2015-02-01 00:17:58 -0800107 * @param face The face used to communication, will be shutdown in destructor
Yingdi Yuf7ede412014-08-30 20:37:52 -0700108 * @param syncPrefix The prefix of the sync group
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800109 * @param defaultUserPrefix The prefix of the first user added to this session
Yingdi Yuf7ede412014-08-30 20:37:52 -0700110 * @param onUpdate The callback function to handle state updates
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800111 * @param defaultSigningId The signing Id of the default user
Yingdi Yucd339022014-11-05 17:51:19 -0800112 * @param validator The validator for packet validation
Yingdi Yuf7ede412014-08-30 20:37:52 -0700113 * @param resetTimer The timer to periodically send Reset Interest
Yingdi Yuf7ede412014-08-30 20:37:52 -0700114 * @param cancelResetTimer The timer to exit from Reset state
Ashlesh Gawande097bb442017-05-31 13:38:00 -0500115 * @param resetInterestLifetime The lifetime of Reset Interest
116 * @param syncInterestLifetime The lifetime of sync interest
117 * @param syncReplyFreshness The FreshnessPeriod of sync reply
118 * @param recoveryInterestLifetime The lifetime of recovery interest
Yingdi Yuf7ede412014-08-30 20:37:52 -0700119 */
120 Logic(ndn::Face& face,
121 const Name& syncPrefix,
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800122 const Name& defaultUserPrefix,
Yingdi Yuf7ede412014-08-30 20:37:52 -0700123 const UpdateCallback& onUpdate,
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800124 const Name& defaultSigningId = DEFAULT_NAME,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800125 std::shared_ptr<ndn::Validator> validator = DEFAULT_VALIDATOR,
Yingdi Yuf7ede412014-08-30 20:37:52 -0700126 const time::steady_clock::Duration& resetTimer = DEFAULT_RESET_TIMER,
127 const time::steady_clock::Duration& cancelResetTimer = DEFAULT_CANCEL_RESET_TIMER,
128 const time::milliseconds& resetInterestLifetime = DEFAULT_RESET_INTEREST_LIFETIME,
129 const time::milliseconds& syncInterestLifetime = DEFAULT_SYNC_INTEREST_LIFETIME,
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800130 const time::milliseconds& syncReplyFreshness = DEFAULT_SYNC_REPLY_FRESHNESS,
131 const time::milliseconds& recoveryInterestLifetime = DEFAULT_RECOVERY_INTEREST_LIFETIME);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700132
133 ~Logic();
134
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800135 /**
136 * @brief Reset the sync tree (and restart synchronization again)
137 *
138 * @param isOnInterest a flag that tells whether the reset is called by reset interest.
139 */
Yingdi Yuf7ede412014-08-30 20:37:52 -0700140 void
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800141 reset(bool isOnInterest = false);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700142
143 /**
144 * @brief Set user prefix
145 *
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800146 * This method will also change the default user and signing Id of that user.
Yingdi Yuf7ede412014-08-30 20:37:52 -0700147 *
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800148 * @param defaultUserPrefix The prefix of user.
Yingdi Yuf7ede412014-08-30 20:37:52 -0700149 */
150 void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800151 setDefaultUserPrefix(const Name& defaultUserPrefix);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700152
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800153 /// @brief Get the name of default user.
Yingdi Yuf7ede412014-08-30 20:37:52 -0700154 const Name&
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800155 getDefaultUserPrefix() const
Yingdi Yuf7ede412014-08-30 20:37:52 -0700156 {
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800157 return m_defaultUserPrefix;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700158 }
159
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800160 /**
161 * @brief Add user node into the local session.
162 *
163 * This method also reset after adding
164 *
165 * @param userPrefix prefix of the added node
166 * @param signingId signing Id of the added node
167 */
168 void
169 addUserNode(const Name& userPrefix, const Name& signingId = DEFAULT_NAME);
170
171 /// @brief remove the node from the local session
172 void
173 removeUserNode(const Name& userPrefix);
174
175 /**
176 * @brief Get the name of the local session.
177 *
178 * This method gets the session name according to prefix, if prefix is not specified,
179 * it returns the session name of default user.
180 *
181 * @param prefix prefix of the node
182 */
183 const Name&
184 getSessionName(Name prefix = EMPTY_NAME);
185
186 /**
187 * @brief Get current seqNo of the local session.
188 *
189 * This method gets the seqNo according to prefix, if prefix is not specified,
190 * it returns the seqNo of default user.
191 *
192 * @param prefix prefix of the node
193 */
Yingdi Yuf7ede412014-08-30 20:37:52 -0700194 const SeqNo&
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800195 getSeqNo(Name prefix = EMPTY_NAME);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700196
197 /**
198 * @brief Update the seqNo of the local session
199 *
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800200 * The method updates the existing seqNo with the supplied seqNo and prefix.
Yingdi Yuf7ede412014-08-30 20:37:52 -0700201 *
202 * @param seq The new seqNo.
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800203 * @param updatePrefix The prefix of node to update.
Yingdi Yuf7ede412014-08-30 20:37:52 -0700204 */
205 void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800206 updateSeqNo(const SeqNo& seq, const Name& updatePrefix = EMPTY_NAME);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700207
208 /// @brief Get root digest of current sync tree
209 ndn::ConstBufferPtr
210 getRootDigest() const;
211
212 /// @brief Get the name of all sessions
213 std::set<Name>
214 getSessionNames() const;
215
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700216CHRONOSYNC_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yingdi Yuf7ede412014-08-30 20:37:52 -0700217 void
218 printState(std::ostream& os) const;
219
220 ndn::Scheduler&
221 getScheduler()
222 {
223 return m_scheduler;
224 }
225
226 State&
227 getState()
228 {
229 return m_state;
230 }
231
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800232
Yingdi Yuf7ede412014-08-30 20:37:52 -0700233private:
234 /**
235 * @brief Callback to handle Sync Interest
236 *
237 * This method checks whether an incoming interest is a normal one or a reset
238 * and dispatches the incoming interest to corresponding processing methods.
239 *
240 * @param prefix The prefix of the sync group.
241 * @param interest The incoming sync interest.
242 */
243 void
244 onSyncInterest(const Name& prefix, const Interest& interest);
245
246 /**
247 * @brief Callback to handle Sync prefix registration failure
248 *
249 * This method does nothing for now.
250 *
251 * @param prefix The prefix of the sync group.
252 * @param msg The error message.
253 */
254 void
255 onSyncRegisterFailed(const Name& prefix, const std::string& msg);
256
257 /**
258 * @brief Callback to handle Sync Reply
259 *
260 * This method calls validator to validate Sync Reply.
261 * For now, validation is disabled, Logic::onSyncDataValidated is called
262 * directly.
263 *
264 * @param interest The Sync Interest
265 * @param data The reply to the Sync Interest
266 */
267 void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800268 onSyncData(const Interest& interest, const Data& data);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700269
270 /**
271 * @brief Callback to handle reply to Reset Interest.
272 *
273 * This method does nothing, since reply to Reset Interest is not useful for now.
274 *
275 * @param interest The Reset Interest
276 * @param data The reply to the Reset Interest
277 */
278 void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800279 onResetData(const Interest& interest, const Data& data);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700280
281 /**
282 * @brief Callback to handle Sync Interest timeout.
283 *
284 * This method does nothing, since Logic per se handles timeout explicitly.
285 *
286 * @param interest The Sync Interest
287 */
288 void
289 onSyncTimeout(const Interest& interest);
290
291 /**
292 * @brief Callback to invalid Sync Reply.
293 *
294 * This method does nothing but drops the invalid reply.
295 *
296 * @param data The invalid Sync Reply
297 */
298 void
299 onSyncDataValidationFailed(const shared_ptr<const Data>& data);
300
301 /**
302 * @brief Callback to valid Sync Reply.
303 *
304 * This method simply passes the valid reply to processSyncData.
305 *
306 * @param data The valid Sync Reply.
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800307 * @param firstData Whether the data is new or that obtained using exclude filter
Yingdi Yuf7ede412014-08-30 20:37:52 -0700308 */
309 void
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800310 onSyncDataValidated(const shared_ptr<const Data>& data, bool firstData = true);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700311
312 /**
313 * @brief Process normal Sync Interest
314 *
315 * This method extracts the digest from the incoming Sync Interest,
316 * compares it against current local digest, and process the Sync
317 * Interest according to the comparison result. See docs/design.rst
318 * for more details.
319 *
320 * @param interest The incoming interest
321 * @param isTimedProcessing True if the interest needs an immediate reply,
322 * otherwise hold the interest for a while before
323 * making a reply (to avoid unnecessary recovery)
324 */
325 void
326 processSyncInterest(const shared_ptr<const Interest>& interest,
327 bool isTimedProcessing = false);
328
329 /**
330 * @brief Process reset Sync Interest
331 *
332 * This method simply call Logic::reset()
333 *
334 * @param interest The incoming interest.
335 */
336 void
337 processResetInterest(const Interest& interest);
338
339 /**
340 * @brief Process Sync Reply.
341 *
342 * This method extracts state update information from Sync Reply and applies
343 * it to the Sync Tree and re-express Sync Interest.
344 *
345 * @param name The data name of the Sync Reply.
346 * @param digest The digest in the data name.
347 * @param syncReplyBlock The content of the Sync Reply.
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800348 * @param firstData Whether the data is new or obtained using exclude filter
Yingdi Yuf7ede412014-08-30 20:37:52 -0700349 */
350 void
351 processSyncData(const Name& name,
352 ndn::ConstBufferPtr digest,
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800353 const Block& syncReplyBlock,
354 bool firstData);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700355
356 /**
357 * @brief Insert state diff into log
358 *
359 * @param diff The diff .
360 * @param previousRoot The root digest before state changes.
361 */
362 void
363 insertToDiffLog(DiffStatePtr diff,
364 ndn::ConstBufferPtr previousRoot);
365
366 /**
367 * @brief Reply to all pending Sync Interests with a particular commit (or diff)
368 *
369 * @param commit The diff.
370 */
371 void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800372 satisfyPendingSyncInterests(const Name& updatedPrefix, ConstDiffStatePtr commit);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700373
374 /// @brief Helper method to send normal Sync Interest
375 void
376 sendSyncInterest();
377
378 /// @brief Helper method to send reset Sync Interest
379 void
380 sendResetInterest();
381
382 /// @brief Helper method to send Sync Reply
383 void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800384 sendSyncData(const Name& nodePrefix, const Name& name, const State& state);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700385
386 /**
387 * @brief Unset reset status
388 *
389 * By invoking this method, one can add its own state into the Sync Tree, thus
390 * jumping out of the reset status
391 */
392 void
393 cancelReset();
394
395 void
396 printDigest(ndn::ConstBufferPtr digest);
397
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800398 /**
399 * @brief Helper method to send Recovery Interest
400 *
401 * @param digest The digest to be included in the recovery interest
402 */
403 void
404 sendRecoveryInterest(ndn::ConstBufferPtr digest);
405
406 /**
407 * @brief Process Recovery Interest
408 *
409 * This method extracts the digest from the incoming Recovery Interest.
410 * If it recognizes this incoming digest, then it sends its full state
411 * as reply.
412 *
413 * @param interest The incoming interest
414 */
415 void
416 processRecoveryInterest(const Interest& interest);
417
418 /**
419 * @brief Callback to handle Recovery Reply
420 *
421 * This method calls Logic::onSyncDataValidated directly.
422 *
423 * @param interest The Recovery Interest
424 * @param data The reply to the Recovery Interest
425 */
426 void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800427 onRecoveryData(const Interest& interest, const Data& data);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800428
429 /**
430 * @brief Callback to handle Recovery Interest timeout.
431 *
432 * This method does nothing.
433 *
434 * @param interest The Recovery Interest
435 */
436 void
437 onRecoveryTimeout(const Interest& interest);
438
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800439 /**
440 * @brief Helper method to send Exclude Interest
441 *
442 * @param interest The interest whose exclude filter will be augmented
443 * @param data The data whose implicit digest will be inserted into exclude filter
444 */
445 void
446 sendExcludeInterest(const Interest& interest, const Data& data);
447
448 /**
449 * @brief Helper method to form the exclude Interest and calls sendExcludeInterest
450 *
451 * @param interest The interest whose exclude filter will be augmented
452 * @param nodePrefix The prefix of the sender node
453 * @param commit The commit whose contents will be used to obtain the implicit
454 digest to be excluded
455 * @param previousRoot The digest to be included in the interest
456 */
457 void
458 formAndSendExcludeInterest(const Name& nodePrefix,
459 const State& commit,
460 ndn::ConstBufferPtr previousRoot);
461
Yingdi Yucd339022014-11-05 17:51:19 -0800462public:
463 static const ndn::Name DEFAULT_NAME;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800464 static const ndn::Name EMPTY_NAME;
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800465 static const std::shared_ptr<ndn::Validator> DEFAULT_VALIDATOR;
Yingdi Yucd339022014-11-05 17:51:19 -0800466
Yingdi Yuf7ede412014-08-30 20:37:52 -0700467private:
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800468 typedef std::unordered_map<ndn::Name, NodeInfo> NodeList;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700469
470 static const ndn::ConstBufferPtr EMPTY_DIGEST;
471 static const ndn::name::Component RESET_COMPONENT;
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800472 static const ndn::name::Component RECOVERY_COMPONENT;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700473
474 // Communication
475 ndn::Face& m_face;
476 Name m_syncPrefix;
477 const ndn::RegisteredPrefixId* m_syncRegisteredPrefixId;
478 Name m_syncReset;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800479 Name m_defaultUserPrefix;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700480
481 // State
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800482 NodeList m_nodeList;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700483 State m_state;
484 DiffStateContainer m_log;
485 InterestTable m_interestTable;
486 Name m_outstandingInterestName;
487 const ndn::PendingInterestId* m_outstandingInterestId;
488 bool m_isInReset;
489 bool m_needPeriodReset;
490
491 // Callback
492 UpdateCallback m_onUpdate;
493
494 // Event
495 ndn::Scheduler m_scheduler;
496 ndn::EventId m_delayedInterestProcessingId;
497 ndn::EventId m_reexpressingInterestId;
498 ndn::EventId m_resetInterestId;
499
500 // Timer
501 boost::mt19937 m_randomGenerator;
502 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
503 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter;
504 /// @brief Timer to send next reset 0 for no reset
505 time::steady_clock::Duration m_resetTimer;
506 /// @brief Timer to cancel reset state
507 time::steady_clock::Duration m_cancelResetTimer;
508 /// @brief Lifetime of reset interest
509 time::milliseconds m_resetInterestLifetime;
510 /// @brief Lifetime of sync interest
511 time::milliseconds m_syncInterestLifetime;
512 /// @brief FreshnessPeriod of SyncReply
513 time::milliseconds m_syncReplyFreshness;
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800514 /// @brief Lifetime of recovery interest
515 time::milliseconds m_recoveryInterestLifetime;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700516
Yingdi Yucd339022014-11-05 17:51:19 -0800517 // Security
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800518 ndn::Name m_defaultSigningId;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700519 ndn::KeyChain m_keyChain;
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800520 std::shared_ptr<ndn::Validator> m_validator;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700521
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800522
Yingdi Yuf7ede412014-08-30 20:37:52 -0700523#ifdef _DEBUG
524 int m_instanceId;
525 static int m_instanceCounter;
526#endif
527};
528
529
530} // namespace chronosync
531
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800532#endif // CHRONOSYNC_LOGIC_HPP