Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng 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, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng 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 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 20 | #ifndef REPO_HANDLES_WRITE_HANDLE_HPP |
| 21 | #define REPO_HANDLES_WRITE_HANDLE_HPP |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 22 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 23 | #include "command-base-handle.hpp" |
| 24 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 25 | #include <ndn-cxx/util/segment-fetcher.hpp> |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 26 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 27 | #include <queue> |
| 28 | |
| 29 | namespace repo { |
| 30 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 31 | /** |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 32 | * @brief WriteHandle provides basic credit based congestion control. |
| 33 | * |
| 34 | * First repo sends interests of credit number and then credit will be 0. |
| 35 | * |
| 36 | * If a data comes, credit++ and sends a interest then credit--. |
| 37 | * |
| 38 | * If the interest timeout, repo will retry and send interest in retrytimes. |
| 39 | * |
| 40 | * If one interest timeout beyond retrytimes, the fetching process will terminate. |
| 41 | * |
| 42 | * Another case is that if command will insert segmented data without EndBlockId. |
| 43 | * |
| 44 | * The repo will keep fetching data in noendTimeout time. |
| 45 | * |
| 46 | * If data returns with FinalBlockId, this detecting timeout process will terminate. |
| 47 | * |
| 48 | * If client sends a insert check command, the noendTimeout timer will be set to 0. |
| 49 | * |
| 50 | * If repo cannot get FinalBlockId in noendTimeout time, the fetching process will terminate. |
| 51 | */ |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 52 | class WriteHandle : public CommandBaseHandle |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 53 | { |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 54 | public: |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 55 | class Error : public CommandBaseHandle::Error |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 56 | { |
| 57 | public: |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame^] | 58 | using CommandBaseHandle::Error::Error; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 61 | WriteHandle(Face& face, RepoStorage& storageHandle, |
| 62 | ndn::mgmt::Dispatcher& dispatcher, Scheduler& scheduler, |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame^] | 63 | ndn::security::Validator& validator); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | /** |
| 67 | * @brief Information of insert process including variables for response |
| 68 | * and credit based flow control |
| 69 | */ |
| 70 | struct ProcessInfo |
| 71 | { |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 72 | RepoCommandResponse response; |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 73 | std::queue<SegmentNo> nextSegmentQueue; ///< queue of waiting segment |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 74 | /// to be sent when having credits |
| 75 | SegmentNo nextSegment; ///< last segment put into the nextSegmentQueue |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 76 | std::map<SegmentNo, int> retryCounts; ///< to store retrying times of timeout segment |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 77 | int credit; ///< congestion control credits of process |
| 78 | |
| 79 | /** |
| 80 | * @brief the latest time point at which EndBlockId must be determined |
| 81 | * |
| 82 | * Segmented fetch process will terminate if EndBlockId cannot be |
| 83 | * determined before this time point. |
| 84 | * It is initialized to now()+noEndTimeout when segmented fetch process begins, |
| 85 | * and reset to now()+noEndTimeout each time an insert status check command is processed. |
| 86 | */ |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame^] | 87 | time::steady_clock::time_point noEndTime; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | private: // insert command |
| 91 | /** |
| 92 | * @brief handle insert commands |
| 93 | */ |
| 94 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 95 | handleInsertCommand(const Name& prefix, const Interest& interest, |
| 96 | const ndn::mgmt::ControlParameters& parameters, |
| 97 | const ndn::mgmt::CommandContinuation& done); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 98 | |
| 99 | void |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame^] | 100 | onValidationFailed(const Interest& interest, const ndn::security::ValidationError& error); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 101 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 102 | private: // single data fetching |
| 103 | /** |
| 104 | * @brief fetch one data |
| 105 | */ |
| 106 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 107 | onData(const Interest& interest, const Data& data, ProcessId processId); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 108 | |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 109 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 110 | onDataValidated(const Interest& interest, const Data& data, ProcessId processId); |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 111 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 112 | /** |
| 113 | * @brief handle when fetching one data timeout |
| 114 | */ |
| 115 | void |
| 116 | onTimeout(const Interest& interest, ProcessId processId); |
| 117 | |
| 118 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 119 | processSingleInsertCommand(const Interest& interest, RepoCommandParameter& parameter, |
| 120 | const ndn::mgmt::CommandContinuation& done); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 121 | |
| 122 | private: // segmented data fetching |
| 123 | /** |
| 124 | * @brief fetch segmented data |
| 125 | */ |
| 126 | void |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 127 | onSegmentData(ndn::util::SegmentFetcher& fetcher, const Data& data, ProcessId processId); |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 128 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 129 | /** |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 130 | * @brief handle when fetching segmented data timeout |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 131 | */ |
| 132 | void |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 133 | onSegmentTimeout(ndn::util::SegmentFetcher& fetcher, ProcessId processId); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * @brief initiate fetching segmented data |
| 137 | */ |
| 138 | void |
| 139 | segInit(ProcessId processId, const RepoCommandParameter& parameter); |
| 140 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 141 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 142 | processSegmentedInsertCommand(const Interest& interest, RepoCommandParameter& parameter, |
| 143 | const ndn::mgmt::CommandContinuation& done); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 144 | |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 145 | private: |
| 146 | /** |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 147 | * @brief extends noEndTime of process if not noEndTimeout, set StatusCode 405 |
| 148 | * |
| 149 | * called by onCheckValidated() if there is no EndBlockId. If not noEndTimeout, |
| 150 | * extends noEndTime of process. If noEndTimeout, set status code 405 as noEndTimeout. |
| 151 | */ |
| 152 | void |
| 153 | extendNoEndTime(ProcessInfo& process); |
| 154 | |
| 155 | private: // insert state check command |
| 156 | /** |
| 157 | * @brief handle insert check command |
| 158 | */ |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 159 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 160 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 161 | handleCheckCommand(const Name& prefix, const Interest& interest, |
| 162 | const ndn::mgmt::ControlParameters& parameters, |
| 163 | const ndn::mgmt::CommandContinuation& done); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 164 | |
| 165 | void |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame^] | 166 | onCheckValidationFailed(const Interest& interest, const ndn::security::ValidationError& error); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 167 | |
| 168 | private: |
| 169 | void |
| 170 | deleteProcess(ProcessId processId); |
| 171 | |
| 172 | /** |
| 173 | * @brief schedule a event to delete the process |
| 174 | */ |
| 175 | void |
| 176 | deferredDeleteProcess(ProcessId processId); |
| 177 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 178 | RepoCommandResponse |
| 179 | negativeReply(std::string text, int statusCode); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 180 | |
| 181 | private: |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame^] | 182 | ndn::security::Validator& m_validator; |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 183 | std::map<ProcessId, ProcessInfo> m_processes; |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame^] | 184 | time::milliseconds m_interestLifetime = ndn::DEFAULT_INTEREST_LIFETIME; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | } // namespace repo |
| 188 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 189 | #endif // REPO_HANDLES_WRITE_HANDLE_HPP |