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