Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Junxiao Shi | fadcde5 | 2022-02-28 00:38:45 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, The University of Memphis |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 4 | * |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 5 | * This file is part of PSync. |
| 6 | * See AUTHORS.md for complete list of PSync authors and contributors. |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 7 | * |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 8 | * PSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU Lesser General Public License as published by the Free Software Foundation, |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 12 | * PSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 14 | * PURPOSE. See the GNU Lesser General Public License for more details. |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 15 | * |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 16 | * You should have received a copy of the GNU Lesser General Public License along with |
| 17 | * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 18 | **/ |
| 19 | |
| 20 | #ifndef PSYNC_FULL_PRODUCER_HPP |
| 21 | #define PSYNC_FULL_PRODUCER_HPP |
| 22 | |
Ashlesh Gawande | 78b94ad | 2018-12-13 15:29:19 -0600 | [diff] [blame] | 23 | #include "PSync/producer-base.hpp" |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 24 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 25 | #include <random> |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 26 | #include <set> |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 27 | |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/util/segment-fetcher.hpp> |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 29 | |
| 30 | namespace psync { |
| 31 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 32 | /** |
| 33 | * @brief Full sync logic to synchronize with other nodes |
| 34 | * where all nodes wants to get all names prefixes synced. |
| 35 | * |
| 36 | * Application should call publishName whenever it wants to |
| 37 | * let consumers know that new data is available for the userPrefix. |
| 38 | * Multiple userPrefixes can be added by using addUserNode. |
| 39 | * Currently, fetching and publishing of data needs to be handled by the application. |
| 40 | */ |
| 41 | class FullProducer : public ProducerBase |
| 42 | { |
| 43 | public: |
| 44 | /** |
| 45 | * @brief constructor |
| 46 | * |
| 47 | * Registers syncPrefix in NFD and sends a sync interest |
| 48 | * |
| 49 | * @param expectedNumEntries expected entries in IBF |
| 50 | * @param face application's face |
| 51 | * @param syncPrefix The prefix of the sync group |
| 52 | * @param userPrefix The prefix of the first user in the group |
| 53 | * @param onUpdateCallBack The call back to be called when there is new data |
| 54 | * @param syncInterestLifetime lifetime of the sync interest |
| 55 | * @param syncReplyFreshness freshness of sync data |
Ashlesh Gawande | d51690a | 2019-11-11 22:51:06 -0600 | [diff] [blame] | 56 | * @param ibltCompression Compression scheme to use for IBF |
| 57 | * @param contentCompression Compression scheme to use for Data content |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 58 | */ |
| 59 | FullProducer(size_t expectedNumEntries, |
| 60 | ndn::Face& face, |
| 61 | const ndn::Name& syncPrefix, |
| 62 | const ndn::Name& userPrefix, |
| 63 | const UpdateCallback& onUpdateCallBack, |
Davide Pesavento | eeca59e | 2021-01-15 16:06:06 -0500 | [diff] [blame] | 64 | ndn::time::milliseconds syncInterestLifetime = SYNC_INTEREST_LIFETIME, |
Ashlesh Gawande | d51690a | 2019-11-11 22:51:06 -0600 | [diff] [blame] | 65 | ndn::time::milliseconds syncReplyFreshness = SYNC_REPLY_FRESHNESS, |
Ashlesh Gawande | e23b53b | 2020-02-16 13:47:38 -0800 | [diff] [blame] | 66 | CompressionScheme ibltCompression = CompressionScheme::DEFAULT, |
| 67 | CompressionScheme contentCompression = CompressionScheme::DEFAULT); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 68 | |
| 69 | ~FullProducer(); |
| 70 | |
| 71 | /** |
| 72 | * @brief Publish name to let others know |
| 73 | * |
| 74 | * addUserNode needs to be called before this to add the prefix |
| 75 | * if not already added via the constructor. |
| 76 | * If seq is null then the seq of prefix is incremented by 1 else |
| 77 | * the supplied sequence is set in the IBF. |
| 78 | * |
| 79 | * @param prefix the prefix to be updated |
| 80 | * @param seq the sequence number of the prefix |
| 81 | */ |
| 82 | void |
Davide Pesavento | c407dee | 2022-07-21 23:56:05 -0400 | [diff] [blame^] | 83 | publishName(const ndn::Name& prefix, std::optional<uint64_t> seq = std::nullopt); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 84 | |
Ashlesh Gawande | 584e120 | 2019-05-19 16:15:33 -0700 | [diff] [blame] | 85 | PSYNC_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 86 | /** |
| 87 | * @brief Send sync interest for full synchronization |
| 88 | * |
| 89 | * Forms the interest name: /<sync-prefix>/<own-IBF> |
| 90 | * Cancels any pending sync interest we sent earlier on the face |
| 91 | * Sends the sync interest |
| 92 | */ |
| 93 | void |
| 94 | sendSyncInterest(); |
| 95 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 96 | /** |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 97 | * @brief Process sync interest from other parties |
| 98 | * |
| 99 | * Get differences b/w our IBF and IBF in the sync interest. |
| 100 | * If we cannot get the differences successfully then send an application nack. |
| 101 | * |
| 102 | * If we have some things in our IBF that the other side does not have, reply with the content or |
| 103 | * If no. of different items is greater than threshold or equals zero then send a nack. |
| 104 | * Otherwise add the sync interest into a map with interest name as key and PendingEntryInfoFull |
| 105 | * as value. |
| 106 | * |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 107 | * @param prefixName prefix for sync group which we registered |
| 108 | * @param interest the interest we got |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 109 | */ |
| 110 | void |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 111 | onSyncInterest(const ndn::Name& prefixName, const ndn::Interest& interest); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 112 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 113 | /** |
| 114 | * @brief Send sync data |
| 115 | * |
| 116 | * Check if the data will satisfy our own pending interest, |
| 117 | * remove it first if it does, and then renew the sync interest |
| 118 | * Otherwise just send the data |
| 119 | * |
| 120 | * @param name name to be set as data name |
| 121 | * @param block the content of the data |
| 122 | */ |
| 123 | void |
| 124 | sendSyncData(const ndn::Name& name, const ndn::Block& block); |
| 125 | |
| 126 | /** |
| 127 | * @brief Process sync data |
| 128 | * |
| 129 | * Call deletePendingInterests to delete any pending sync interest with |
| 130 | * interest name would have been satisfied once NFD got the data. |
| 131 | * |
| 132 | * For each prefix/seq in data content check that we don't already have the |
| 133 | * prefix/seq and updateSeq(prefix, seq) |
| 134 | * |
| 135 | * Notify the application about the updates |
| 136 | * sendSyncInterest because the last one was satisfied by the incoming data |
| 137 | * |
| 138 | * @param interest interest for which we got the data |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 139 | * @param bufferPtr sync data content |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 140 | */ |
| 141 | void |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 142 | onSyncData(const ndn::Interest& interest, const ndn::ConstBufferPtr& bufferPtr); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 143 | |
Ashlesh Gawande | d51690a | 2019-11-11 22:51:06 -0600 | [diff] [blame] | 144 | private: |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 145 | /** |
| 146 | * @brief Satisfy pending sync interests |
| 147 | * |
| 148 | * For pending sync interests SI, if IBF of SI has any difference from our own IBF: |
| 149 | * send data back. |
| 150 | * If we can't decode differences from the stored IBF, then delete it. |
| 151 | */ |
| 152 | void |
| 153 | satisfyPendingInterests(); |
| 154 | |
| 155 | /** |
| 156 | * @brief Delete pending sync interests that match given name |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 157 | */ |
| 158 | void |
| 159 | deletePendingInterests(const ndn::Name& interestName); |
| 160 | |
| 161 | /** |
| 162 | * @brief Check if hash(prefix + 1) is in negative |
| 163 | * |
| 164 | * Sometimes what happens is that interest from other side |
| 165 | * gets to us before the data |
| 166 | */ |
| 167 | bool |
| 168 | isFutureHash(const ndn::Name& prefix, const std::set<uint32_t>& negative); |
| 169 | |
Junxiao Shi | fadcde5 | 2022-02-28 00:38:45 +0000 | [diff] [blame] | 170 | #ifdef PSYNC_WITH_TESTS |
| 171 | public: |
| 172 | size_t nIbfDecodeFailuresAboveThreshold = 0; |
| 173 | size_t nIbfDecodeFailuresBelowThreshold = 0; |
| 174 | #endif // PSYNC_WITH_TESTS |
| 175 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 176 | private: |
Davide Pesavento | db78956 | 2020-12-19 23:01:08 -0500 | [diff] [blame] | 177 | struct PendingEntryInfo |
| 178 | { |
| 179 | detail::IBLT iblt; |
| 180 | ndn::scheduler::ScopedEventId expirationEvent; |
| 181 | }; |
| 182 | |
| 183 | std::map<ndn::Name, PendingEntryInfo> m_pendingEntries; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 184 | ndn::time::milliseconds m_syncInterestLifetime; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 185 | UpdateCallback m_onUpdate; |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 186 | ndn::scheduler::ScopedEventId m_scheduledSyncInterestId; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 187 | std::uniform_int_distribution<> m_jitter; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 188 | ndn::Name m_outstandingInterestName; |
Junxiao Shi | e5fdcc3 | 2019-01-23 16:48:54 +0000 | [diff] [blame] | 189 | ndn::ScopedRegisteredPrefixHandle m_registeredPrefix; |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 190 | std::shared_ptr<ndn::util::SegmentFetcher> m_fetcher; |
Alexander Afanasyev | 16d493e | 2022-04-23 22:37:19 -0400 | [diff] [blame] | 191 | uint64_t m_incomingFace = 0; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | } // namespace psync |
| 195 | |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 196 | #endif // PSYNC_FULL_PRODUCER_HPP |