blob: b07ba6232758e0b1026a84d671b6df16c7171ac2 [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
114 * @param syncReplyFreshness The FreshnessPeriod of sync reply
115 * @param resetInterestLifetime The lifetime of sync interest
116 * @param resetInterestLifetime The lifetime of Reset Interest
117 * @param cancelResetTimer The timer to exit from Reset state
118 */
119 Logic(ndn::Face& face,
120 const Name& syncPrefix,
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800121 const Name& defaultUserPrefix,
Yingdi Yuf7ede412014-08-30 20:37:52 -0700122 const UpdateCallback& onUpdate,
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800123 const Name& defaultSigningId = DEFAULT_NAME,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800124 std::shared_ptr<ndn::Validator> validator = DEFAULT_VALIDATOR,
Yingdi Yuf7ede412014-08-30 20:37:52 -0700125 const time::steady_clock::Duration& resetTimer = DEFAULT_RESET_TIMER,
126 const time::steady_clock::Duration& cancelResetTimer = DEFAULT_CANCEL_RESET_TIMER,
127 const time::milliseconds& resetInterestLifetime = DEFAULT_RESET_INTEREST_LIFETIME,
128 const time::milliseconds& syncInterestLifetime = DEFAULT_SYNC_INTEREST_LIFETIME,
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800129 const time::milliseconds& syncReplyFreshness = DEFAULT_SYNC_REPLY_FRESHNESS,
130 const time::milliseconds& recoveryInterestLifetime = DEFAULT_RECOVERY_INTEREST_LIFETIME);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700131
132 ~Logic();
133
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800134 /**
135 * @brief Reset the sync tree (and restart synchronization again)
136 *
137 * @param isOnInterest a flag that tells whether the reset is called by reset interest.
138 */
Yingdi Yuf7ede412014-08-30 20:37:52 -0700139 void
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800140 reset(bool isOnInterest = false);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700141
142 /**
143 * @brief Set user prefix
144 *
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800145 * This method will also change the default user and signing Id of that user.
Yingdi Yuf7ede412014-08-30 20:37:52 -0700146 *
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800147 * @param defaultUserPrefix The prefix of user.
Yingdi Yuf7ede412014-08-30 20:37:52 -0700148 */
149 void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800150 setDefaultUserPrefix(const Name& defaultUserPrefix);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700151
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800152 /// @brief Get the name of default user.
Yingdi Yuf7ede412014-08-30 20:37:52 -0700153 const Name&
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800154 getDefaultUserPrefix() const
Yingdi Yuf7ede412014-08-30 20:37:52 -0700155 {
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800156 return m_defaultUserPrefix;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700157 }
158
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800159 /**
160 * @brief Add user node into the local session.
161 *
162 * This method also reset after adding
163 *
164 * @param userPrefix prefix of the added node
165 * @param signingId signing Id of the added node
166 */
167 void
168 addUserNode(const Name& userPrefix, const Name& signingId = DEFAULT_NAME);
169
170 /// @brief remove the node from the local session
171 void
172 removeUserNode(const Name& userPrefix);
173
174 /**
175 * @brief Get the name of the local session.
176 *
177 * This method gets the session name according to prefix, if prefix is not specified,
178 * it returns the session name of default user.
179 *
180 * @param prefix prefix of the node
181 */
182 const Name&
183 getSessionName(Name prefix = EMPTY_NAME);
184
185 /**
186 * @brief Get current seqNo of the local session.
187 *
188 * This method gets the seqNo according to prefix, if prefix is not specified,
189 * it returns the seqNo of default user.
190 *
191 * @param prefix prefix of the node
192 */
Yingdi Yuf7ede412014-08-30 20:37:52 -0700193 const SeqNo&
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800194 getSeqNo(Name prefix = EMPTY_NAME);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700195
196 /**
197 * @brief Update the seqNo of the local session
198 *
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800199 * The method updates the existing seqNo with the supplied seqNo and prefix.
Yingdi Yuf7ede412014-08-30 20:37:52 -0700200 *
201 * @param seq The new seqNo.
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800202 * @param updatePrefix The prefix of node to update.
Yingdi Yuf7ede412014-08-30 20:37:52 -0700203 */
204 void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800205 updateSeqNo(const SeqNo& seq, const Name& updatePrefix = EMPTY_NAME);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700206
207 /// @brief Get root digest of current sync tree
208 ndn::ConstBufferPtr
209 getRootDigest() const;
210
211 /// @brief Get the name of all sessions
212 std::set<Name>
213 getSessionNames() const;
214
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700215CHRONOSYNC_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yingdi Yuf7ede412014-08-30 20:37:52 -0700216 void
217 printState(std::ostream& os) const;
218
219 ndn::Scheduler&
220 getScheduler()
221 {
222 return m_scheduler;
223 }
224
225 State&
226 getState()
227 {
228 return m_state;
229 }
230
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800231
Yingdi Yuf7ede412014-08-30 20:37:52 -0700232private:
233 /**
234 * @brief Callback to handle Sync Interest
235 *
236 * This method checks whether an incoming interest is a normal one or a reset
237 * and dispatches the incoming interest to corresponding processing methods.
238 *
239 * @param prefix The prefix of the sync group.
240 * @param interest The incoming sync interest.
241 */
242 void
243 onSyncInterest(const Name& prefix, const Interest& interest);
244
245 /**
246 * @brief Callback to handle Sync prefix registration failure
247 *
248 * This method does nothing for now.
249 *
250 * @param prefix The prefix of the sync group.
251 * @param msg The error message.
252 */
253 void
254 onSyncRegisterFailed(const Name& prefix, const std::string& msg);
255
256 /**
257 * @brief Callback to handle Sync Reply
258 *
259 * This method calls validator to validate Sync Reply.
260 * For now, validation is disabled, Logic::onSyncDataValidated is called
261 * directly.
262 *
263 * @param interest The Sync Interest
264 * @param data The reply to the Sync Interest
265 */
266 void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800267 onSyncData(const Interest& interest, const Data& data);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700268
269 /**
270 * @brief Callback to handle reply to Reset Interest.
271 *
272 * This method does nothing, since reply to Reset Interest is not useful for now.
273 *
274 * @param interest The Reset Interest
275 * @param data The reply to the Reset Interest
276 */
277 void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800278 onResetData(const Interest& interest, const Data& data);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700279
280 /**
281 * @brief Callback to handle Sync Interest timeout.
282 *
283 * This method does nothing, since Logic per se handles timeout explicitly.
284 *
285 * @param interest The Sync Interest
286 */
287 void
288 onSyncTimeout(const Interest& interest);
289
290 /**
291 * @brief Callback to invalid Sync Reply.
292 *
293 * This method does nothing but drops the invalid reply.
294 *
295 * @param data The invalid Sync Reply
296 */
297 void
298 onSyncDataValidationFailed(const shared_ptr<const Data>& data);
299
300 /**
301 * @brief Callback to valid Sync Reply.
302 *
303 * This method simply passes the valid reply to processSyncData.
304 *
305 * @param data The valid Sync Reply.
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800306 * @param firstData Whether the data is new or that obtained using exclude filter
Yingdi Yuf7ede412014-08-30 20:37:52 -0700307 */
308 void
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800309 onSyncDataValidated(const shared_ptr<const Data>& data, bool firstData = true);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700310
311 /**
312 * @brief Process normal Sync Interest
313 *
314 * This method extracts the digest from the incoming Sync Interest,
315 * compares it against current local digest, and process the Sync
316 * Interest according to the comparison result. See docs/design.rst
317 * for more details.
318 *
319 * @param interest The incoming interest
320 * @param isTimedProcessing True if the interest needs an immediate reply,
321 * otherwise hold the interest for a while before
322 * making a reply (to avoid unnecessary recovery)
323 */
324 void
325 processSyncInterest(const shared_ptr<const Interest>& interest,
326 bool isTimedProcessing = false);
327
328 /**
329 * @brief Process reset Sync Interest
330 *
331 * This method simply call Logic::reset()
332 *
333 * @param interest The incoming interest.
334 */
335 void
336 processResetInterest(const Interest& interest);
337
338 /**
339 * @brief Process Sync Reply.
340 *
341 * This method extracts state update information from Sync Reply and applies
342 * it to the Sync Tree and re-express Sync Interest.
343 *
344 * @param name The data name of the Sync Reply.
345 * @param digest The digest in the data name.
346 * @param syncReplyBlock The content of the Sync Reply.
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800347 * @param firstData Whether the data is new or obtained using exclude filter
Yingdi Yuf7ede412014-08-30 20:37:52 -0700348 */
349 void
350 processSyncData(const Name& name,
351 ndn::ConstBufferPtr digest,
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800352 const Block& syncReplyBlock,
353 bool firstData);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700354
355 /**
356 * @brief Insert state diff into log
357 *
358 * @param diff The diff .
359 * @param previousRoot The root digest before state changes.
360 */
361 void
362 insertToDiffLog(DiffStatePtr diff,
363 ndn::ConstBufferPtr previousRoot);
364
365 /**
366 * @brief Reply to all pending Sync Interests with a particular commit (or diff)
367 *
368 * @param commit The diff.
369 */
370 void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800371 satisfyPendingSyncInterests(const Name& updatedPrefix, ConstDiffStatePtr commit);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700372
373 /// @brief Helper method to send normal Sync Interest
374 void
375 sendSyncInterest();
376
377 /// @brief Helper method to send reset Sync Interest
378 void
379 sendResetInterest();
380
381 /// @brief Helper method to send Sync Reply
382 void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800383 sendSyncData(const Name& nodePrefix, const Name& name, const State& state);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700384
385 /**
386 * @brief Unset reset status
387 *
388 * By invoking this method, one can add its own state into the Sync Tree, thus
389 * jumping out of the reset status
390 */
391 void
392 cancelReset();
393
394 void
395 printDigest(ndn::ConstBufferPtr digest);
396
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800397 /**
398 * @brief Helper method to send Recovery Interest
399 *
400 * @param digest The digest to be included in the recovery interest
401 */
402 void
403 sendRecoveryInterest(ndn::ConstBufferPtr digest);
404
405 /**
406 * @brief Process Recovery Interest
407 *
408 * This method extracts the digest from the incoming Recovery Interest.
409 * If it recognizes this incoming digest, then it sends its full state
410 * as reply.
411 *
412 * @param interest The incoming interest
413 */
414 void
415 processRecoveryInterest(const Interest& interest);
416
417 /**
418 * @brief Callback to handle Recovery Reply
419 *
420 * This method calls Logic::onSyncDataValidated directly.
421 *
422 * @param interest The Recovery Interest
423 * @param data The reply to the Recovery Interest
424 */
425 void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800426 onRecoveryData(const Interest& interest, const Data& data);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800427
428 /**
429 * @brief Callback to handle Recovery Interest timeout.
430 *
431 * This method does nothing.
432 *
433 * @param interest The Recovery Interest
434 */
435 void
436 onRecoveryTimeout(const Interest& interest);
437
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800438 /**
439 * @brief Helper method to send Exclude Interest
440 *
441 * @param interest The interest whose exclude filter will be augmented
442 * @param data The data whose implicit digest will be inserted into exclude filter
443 */
444 void
445 sendExcludeInterest(const Interest& interest, const Data& data);
446
447 /**
448 * @brief Helper method to form the exclude Interest and calls sendExcludeInterest
449 *
450 * @param interest The interest whose exclude filter will be augmented
451 * @param nodePrefix The prefix of the sender node
452 * @param commit The commit whose contents will be used to obtain the implicit
453 digest to be excluded
454 * @param previousRoot The digest to be included in the interest
455 */
456 void
457 formAndSendExcludeInterest(const Name& nodePrefix,
458 const State& commit,
459 ndn::ConstBufferPtr previousRoot);
460
Yingdi Yucd339022014-11-05 17:51:19 -0800461public:
462 static const ndn::Name DEFAULT_NAME;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800463 static const ndn::Name EMPTY_NAME;
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800464 static const std::shared_ptr<ndn::Validator> DEFAULT_VALIDATOR;
Yingdi Yucd339022014-11-05 17:51:19 -0800465
Yingdi Yuf7ede412014-08-30 20:37:52 -0700466private:
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800467 typedef std::unordered_map<ndn::Name, NodeInfo> NodeList;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700468
469 static const ndn::ConstBufferPtr EMPTY_DIGEST;
470 static const ndn::name::Component RESET_COMPONENT;
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800471 static const ndn::name::Component RECOVERY_COMPONENT;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700472
473 // Communication
474 ndn::Face& m_face;
475 Name m_syncPrefix;
476 const ndn::RegisteredPrefixId* m_syncRegisteredPrefixId;
477 Name m_syncReset;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800478 Name m_defaultUserPrefix;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700479
480 // State
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800481 NodeList m_nodeList;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700482 State m_state;
483 DiffStateContainer m_log;
484 InterestTable m_interestTable;
485 Name m_outstandingInterestName;
486 const ndn::PendingInterestId* m_outstandingInterestId;
487 bool m_isInReset;
488 bool m_needPeriodReset;
489
490 // Callback
491 UpdateCallback m_onUpdate;
492
493 // Event
494 ndn::Scheduler m_scheduler;
495 ndn::EventId m_delayedInterestProcessingId;
496 ndn::EventId m_reexpressingInterestId;
497 ndn::EventId m_resetInterestId;
498
499 // Timer
500 boost::mt19937 m_randomGenerator;
501 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
502 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter;
503 /// @brief Timer to send next reset 0 for no reset
504 time::steady_clock::Duration m_resetTimer;
505 /// @brief Timer to cancel reset state
506 time::steady_clock::Duration m_cancelResetTimer;
507 /// @brief Lifetime of reset interest
508 time::milliseconds m_resetInterestLifetime;
509 /// @brief Lifetime of sync interest
510 time::milliseconds m_syncInterestLifetime;
511 /// @brief FreshnessPeriod of SyncReply
512 time::milliseconds m_syncReplyFreshness;
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800513 /// @brief Lifetime of recovery interest
514 time::milliseconds m_recoveryInterestLifetime;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700515
Yingdi Yucd339022014-11-05 17:51:19 -0800516 // Security
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800517 ndn::Name m_defaultSigningId;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700518 ndn::KeyChain m_keyChain;
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800519 std::shared_ptr<ndn::Validator> m_validator;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700520
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800521
Yingdi Yuf7ede412014-08-30 20:37:52 -0700522#ifdef _DEBUG
523 int m_instanceId;
524 static int m_instanceCounter;
525#endif
526};
527
528
529} // namespace chronosync
530
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800531#endif // CHRONOSYNC_LOGIC_HPP