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 | 0c13951 | 2018-11-03 18:23:38 -0400 | [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 | |
| 20 | #include "write-handle.hpp" |
| 21 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 22 | #include <ndn-cxx/util/random.hpp> |
| 23 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 24 | namespace repo { |
| 25 | |
| 26 | static const int RETRY_TIMEOUT = 3; |
| 27 | static const int DEFAULT_CREDIT = 12; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 28 | static const milliseconds NOEND_TIMEOUT(10000_ms); |
| 29 | static const milliseconds PROCESS_DELETE_TIME(10000_ms); |
| 30 | static const milliseconds DEFAULT_INTEREST_LIFETIME(4000_ms); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 31 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 32 | WriteHandle::WriteHandle(Face& face, RepoStorage& storageHandle, ndn::mgmt::Dispatcher& dispatcher, |
| 33 | Scheduler& scheduler, Validator& validator) |
| 34 | : CommandBaseHandle(face, storageHandle, scheduler, validator) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 35 | , m_validator(validator) |
| 36 | , m_retryTime(RETRY_TIMEOUT) |
| 37 | , m_credit(DEFAULT_CREDIT) |
| 38 | , m_noEndTimeout(NOEND_TIMEOUT) |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 39 | , m_interestLifetime(DEFAULT_INTEREST_LIFETIME) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 40 | { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 41 | dispatcher.addControlCommand<RepoCommandParameter>(ndn::PartialName("insert"), |
| 42 | makeAuthorization(), |
| 43 | std::bind(&WriteHandle::validateParameters<InsertCommand>, this, _1), |
| 44 | std::bind(&WriteHandle::handleInsertCommand, this, _1, _2, _3, _4)); |
| 45 | |
| 46 | dispatcher.addControlCommand<RepoCommandParameter>(ndn::PartialName("insert check"), |
| 47 | makeAuthorization(), |
| 48 | std::bind(&WriteHandle::validateParameters<InsertCheckCommand>, this, _1), |
| 49 | std::bind(&WriteHandle::handleCheckCommand, this, _1, _2, _3, _4)); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void |
| 53 | WriteHandle::deleteProcess(ProcessId processId) |
| 54 | { |
| 55 | m_processes.erase(processId); |
| 56 | } |
| 57 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 58 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 59 | WriteHandle::handleInsertCommand(const Name& prefix, const Interest& interest, |
| 60 | const ndn::mgmt::ControlParameters& parameter, |
| 61 | const ndn::mgmt::CommandContinuation& done) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 62 | { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 63 | RepoCommandParameter* repoParameter = |
| 64 | dynamic_cast<RepoCommandParameter*>(const_cast<ndn::mgmt::ControlParameters*>(¶meter)); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 65 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 66 | if (repoParameter->hasStartBlockId() || repoParameter->hasEndBlockId()) { |
| 67 | if (repoParameter->hasSelectors()) { |
| 68 | done(negativeReply("BlockId present. BlockId is not supported in this protocol", 402)); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 69 | return; |
| 70 | } |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 71 | processSegmentedInsertCommand(interest, *repoParameter, done); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 72 | } |
| 73 | else { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 74 | processSingleInsertCommand(interest, *repoParameter, done); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 75 | } |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 76 | if (repoParameter->hasInterestLifetime()) |
| 77 | m_interestLifetime = repoParameter->getInterestLifetime(); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 81 | WriteHandle::onData(const Interest& interest, const Data& data, ProcessId processId) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 82 | { |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 83 | m_validator.validate(data, |
| 84 | bind(&WriteHandle::onDataValidated, this, interest, _1, processId), |
| 85 | bind(&WriteHandle::onDataValidationFailed, this, _1, _2)); |
| 86 | } |
| 87 | |
| 88 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 89 | WriteHandle::onDataValidated(const Interest& interest, const Data& data, ProcessId processId) |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 90 | { |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 91 | if (m_processes.count(processId) == 0) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | ProcessInfo& process = m_processes[processId]; |
| 96 | RepoCommandResponse& response = process.response; |
| 97 | |
| 98 | if (response.getInsertNum() == 0) { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 99 | storageHandle.insertData(data); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 100 | response.setInsertNum(1); |
| 101 | } |
| 102 | |
| 103 | deferredDeleteProcess(processId); |
| 104 | } |
| 105 | |
| 106 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 107 | WriteHandle::onDataValidationFailed(const Data& data, const ValidationError& error) |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 108 | { |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 109 | std::cerr << error << std::endl; |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 113 | WriteHandle::onSegmentData(const Interest& interest, const Data& data, ProcessId processId) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 114 | { |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 115 | m_validator.validate(data, |
| 116 | bind(&WriteHandle::onSegmentDataValidated, this, interest, _1, processId), |
| 117 | bind(&WriteHandle::onDataValidationFailed, this, _1, _2)); |
| 118 | } |
| 119 | |
| 120 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 121 | WriteHandle::onSegmentDataValidated(const Interest& interest, const Data& data, ProcessId processId) |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 122 | { |
Davide Pesavento | 0c13951 | 2018-11-03 18:23:38 -0400 | [diff] [blame] | 123 | auto it = m_processes.find(processId); |
| 124 | if (it == m_processes.end()) { |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 125 | return; |
| 126 | } |
Davide Pesavento | 0c13951 | 2018-11-03 18:23:38 -0400 | [diff] [blame] | 127 | RepoCommandResponse& response = it->second.response; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 128 | |
| 129 | //refresh endBlockId |
Davide Pesavento | 0c13951 | 2018-11-03 18:23:38 -0400 | [diff] [blame] | 130 | auto finalBlock = data.getFinalBlock(); |
| 131 | if (finalBlock && finalBlock->isSegment()) { |
| 132 | auto finalSeg = finalBlock->toSegment(); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 133 | if (response.hasEndBlockId()) { |
Davide Pesavento | 0c13951 | 2018-11-03 18:23:38 -0400 | [diff] [blame] | 134 | if (finalSeg < response.getEndBlockId()) { |
| 135 | response.setEndBlockId(finalSeg); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | else { |
Davide Pesavento | 0c13951 | 2018-11-03 18:23:38 -0400 | [diff] [blame] | 139 | response.setEndBlockId(finalSeg); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | //insert data |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 144 | if (storageHandle.insertData(data)) { |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 145 | response.setInsertNum(response.getInsertNum() + 1); |
| 146 | } |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 147 | |
| 148 | onSegmentDataControl(processId, interest); |
| 149 | } |
| 150 | |
| 151 | void |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 152 | WriteHandle::onTimeout(const Interest& interest, ProcessId processId) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 153 | { |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 154 | std::cerr << "Timeout" << std::endl; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 155 | m_processes.erase(processId); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | WriteHandle::onSegmentTimeout(const Interest& interest, ProcessId processId) |
| 160 | { |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 161 | std::cerr << "SegTimeout" << std::endl; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 162 | |
| 163 | onSegmentTimeoutControl(processId, interest); |
| 164 | } |
| 165 | |
| 166 | void |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 167 | WriteHandle::segInit(ProcessId processId, const RepoCommandParameter& parameter) |
| 168 | { |
| 169 | ProcessInfo& process = m_processes[processId]; |
| 170 | process.credit = 0; |
| 171 | |
| 172 | map<SegmentNo, int>& processRetry = process.retryCounts; |
| 173 | |
| 174 | Name name = parameter.getName(); |
| 175 | SegmentNo startBlockId = parameter.getStartBlockId(); |
| 176 | |
| 177 | uint64_t initialCredit = m_credit; |
| 178 | |
| 179 | if (parameter.hasEndBlockId()) { |
| 180 | initialCredit = |
Shuo Chen | 39fe8da | 2014-04-30 00:00:35 +0800 | [diff] [blame] | 181 | std::min(initialCredit, parameter.getEndBlockId() - parameter.getStartBlockId() + 1); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 182 | } |
| 183 | else { |
| 184 | // set noEndTimeout timer |
| 185 | process.noEndTime = ndn::time::steady_clock::now() + |
| 186 | m_noEndTimeout; |
| 187 | } |
| 188 | process.credit = initialCredit; |
| 189 | SegmentNo segment = startBlockId; |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 190 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 191 | for (; segment < startBlockId + initialCredit; ++segment) { |
| 192 | Name fetchName = name; |
| 193 | fetchName.appendSegment(segment); |
| 194 | Interest interest(fetchName); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 195 | interest.setInterestLifetime(m_interestLifetime); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 196 | face.expressInterest(interest, |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 197 | bind(&WriteHandle::onSegmentData, this, _1, _2, processId), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 198 | bind(&WriteHandle::onSegmentTimeout, this, _1, processId), // Nack |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 199 | bind(&WriteHandle::onSegmentTimeout, this, _1, processId)); |
| 200 | process.credit--; |
| 201 | processRetry[segment] = 0; |
| 202 | } |
| 203 | |
| 204 | queue<SegmentNo>& nextSegmentQueue = process.nextSegmentQueue; |
| 205 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 206 | process.nextSegment = segment; |
| 207 | nextSegmentQueue.push(segment); |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | WriteHandle::onSegmentDataControl(ProcessId processId, const Interest& interest) |
| 212 | { |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 213 | if (m_processes.count(processId) == 0) { |
| 214 | return; |
| 215 | } |
| 216 | ProcessInfo& process = m_processes[processId]; |
| 217 | RepoCommandResponse& response = process.response; |
| 218 | int& processCredit = process.credit; |
| 219 | //onSegmentDataControl is called when a data returns. |
| 220 | //When data returns, processCredit++ |
| 221 | processCredit++; |
| 222 | SegmentNo& nextSegment = process.nextSegment; |
| 223 | queue<SegmentNo>& nextSegmentQueue = process.nextSegmentQueue; |
| 224 | map<SegmentNo, int>& retryCounts = process.retryCounts; |
| 225 | |
| 226 | //read whether notime timeout |
| 227 | if (!response.hasEndBlockId()) { |
| 228 | |
| 229 | ndn::time::steady_clock::TimePoint& noEndTime = process.noEndTime; |
| 230 | ndn::time::steady_clock::TimePoint now = ndn::time::steady_clock::now(); |
| 231 | |
| 232 | if (now > noEndTime) { |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 233 | std::cerr << "noEndtimeout: " << processId << std::endl; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 234 | //m_processes.erase(processId); |
| 235 | //StatusCode should be refreshed as 405 |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 236 | response.setCode(405); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 237 | //schedule a delete event |
| 238 | deferredDeleteProcess(processId); |
| 239 | return; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | //read whether this process has total ends, if ends, remove control info from the maps |
| 244 | if (response.hasEndBlockId()) { |
| 245 | uint64_t nSegments = |
| 246 | response.getEndBlockId() - response.getStartBlockId() + 1; |
| 247 | if (response.getInsertNum() >= nSegments) { |
| 248 | //m_processes.erase(processId); |
| 249 | //All the data has been inserted, StatusCode is refreshed as 200 |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 250 | response.setCode(200); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 251 | deferredDeleteProcess(processId); |
| 252 | return; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | //check whether there is any credit |
| 257 | if (processCredit == 0) |
| 258 | return; |
| 259 | |
| 260 | |
| 261 | //check whether sent queue empty |
| 262 | if (nextSegmentQueue.empty()) { |
| 263 | //do not do anything |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | //pop the queue |
| 268 | SegmentNo sendingSegment = nextSegmentQueue.front(); |
| 269 | nextSegmentQueue.pop(); |
| 270 | |
| 271 | //check whether sendingSegment exceeds |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 272 | if (response.hasEndBlockId() && sendingSegment > response.getEndBlockId()) { |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 273 | //do not do anything |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | //read whether this is retransmitted data; |
| 278 | SegmentNo fetchedSegment = |
| 279 | interest.getName().get(interest.getName().size() - 1).toSegment(); |
| 280 | |
| 281 | BOOST_ASSERT(retryCounts.count(fetchedSegment) != 0); |
| 282 | |
| 283 | //find this fetched data, remove it from this map |
| 284 | //rit->second.erase(oit); |
| 285 | retryCounts.erase(fetchedSegment); |
| 286 | //express the interest of the top of the queue |
| 287 | Name fetchName(interest.getName().getPrefix(-1)); |
| 288 | fetchName.appendSegment(sendingSegment); |
| 289 | Interest fetchInterest(fetchName); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 290 | fetchInterest.setInterestLifetime(m_interestLifetime); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 291 | face.expressInterest(fetchInterest, |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 292 | bind(&WriteHandle::onSegmentData, this, _1, _2, processId), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 293 | bind(&WriteHandle::onSegmentTimeout, this, _1, processId), // Nack |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 294 | bind(&WriteHandle::onSegmentTimeout, this, _1, processId)); |
| 295 | //When an interest is expressed, processCredit-- |
| 296 | processCredit--; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 297 | if (retryCounts.count(sendingSegment) == 0) { |
| 298 | //not found |
| 299 | retryCounts[sendingSegment] = 0; |
| 300 | } |
| 301 | else { |
| 302 | //found |
| 303 | retryCounts[sendingSegment] = retryCounts[sendingSegment] + 1; |
| 304 | } |
| 305 | //increase the next seg and put it into the queue |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 306 | if (!response.hasEndBlockId() || (nextSegment + 1) <= response.getEndBlockId()) { |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 307 | nextSegment++; |
| 308 | nextSegmentQueue.push(nextSegment); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void |
| 313 | WriteHandle::onSegmentTimeoutControl(ProcessId processId, const Interest& interest) |
| 314 | { |
| 315 | if (m_processes.count(processId) == 0) { |
| 316 | return; |
| 317 | } |
| 318 | ProcessInfo& process = m_processes[processId]; |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 319 | // RepoCommandResponse& response = process.response; |
| 320 | // SegmentNo& nextSegment = process.nextSegment; |
| 321 | // queue<SegmentNo>& nextSegmentQueue = process.nextSegmentQueue; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 322 | map<SegmentNo, int>& retryCounts = process.retryCounts; |
| 323 | |
| 324 | SegmentNo timeoutSegment = interest.getName().get(-1).toSegment(); |
| 325 | |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 326 | std::cerr << "timeoutSegment: " << timeoutSegment << std::endl; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 327 | |
| 328 | BOOST_ASSERT(retryCounts.count(timeoutSegment) != 0); |
| 329 | |
| 330 | //read the retry time. If retry out of time, fail the process. if not, plus |
| 331 | int& retryTime = retryCounts[timeoutSegment]; |
| 332 | if (retryTime >= m_retryTime) { |
| 333 | //fail this process |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 334 | std::cerr << "Retry timeout: " << processId << std::endl; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 335 | m_processes.erase(processId); |
| 336 | return; |
| 337 | } |
| 338 | else { |
| 339 | //Reput it in the queue, retryTime++ |
| 340 | retryTime++; |
| 341 | Interest retryInterest(interest.getName()); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 342 | retryInterest.setInterestLifetime(m_interestLifetime); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 343 | face.expressInterest(retryInterest, |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 344 | bind(&WriteHandle::onSegmentData, this, _1, _2, processId), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 345 | bind(&WriteHandle::onSegmentTimeout, this, _1, processId), // Nack |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 346 | bind(&WriteHandle::onSegmentTimeout, this, _1, processId)); |
| 347 | } |
| 348 | |
| 349 | } |
| 350 | |
| 351 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 352 | WriteHandle::handleCheckCommand(const Name& prefix, const Interest& interest, |
| 353 | const ndn::mgmt::ControlParameters& parameter, |
| 354 | const ndn::mgmt::CommandContinuation& done) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 355 | { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 356 | const RepoCommandParameter& repoParameter = dynamic_cast<const RepoCommandParameter&>(parameter); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 357 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 358 | //check whether this process exists |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 359 | ProcessId processId = repoParameter.getProcessId(); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 360 | if (m_processes.count(processId) == 0) { |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 361 | std::cerr << "no such processId: " << processId << std::endl; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 362 | done(negativeReply("No such this process is in progress", 404)); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 363 | return; |
| 364 | } |
| 365 | |
| 366 | ProcessInfo& process = m_processes[processId]; |
| 367 | |
| 368 | RepoCommandResponse& response = process.response; |
| 369 | |
| 370 | //Check whether it is single data fetching |
| 371 | if (!response.hasStartBlockId() && |
| 372 | !response.hasEndBlockId()) { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 373 | //reply(interest, response); |
| 374 | done(response); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 375 | return; |
| 376 | } |
| 377 | |
| 378 | //read if noEndtimeout |
| 379 | if (!response.hasEndBlockId()) { |
| 380 | extendNoEndTime(process); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 381 | done(response); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 382 | return; |
| 383 | } |
| 384 | else { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 385 | done(response); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | void |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 390 | WriteHandle::deferredDeleteProcess(ProcessId processId) |
| 391 | { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 392 | scheduler.scheduleEvent(PROCESS_DELETE_TIME, |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 393 | bind(&WriteHandle::deleteProcess, this, processId)); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 397 | WriteHandle::processSingleInsertCommand(const Interest& interest, RepoCommandParameter& parameter, |
| 398 | const ndn::mgmt::CommandContinuation& done) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 399 | { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 400 | ProcessId processId = ndn::random::generateWord64(); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 401 | |
| 402 | ProcessInfo& process = m_processes[processId]; |
| 403 | |
| 404 | RepoCommandResponse& response = process.response; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 405 | response.setCode(100); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 406 | response.setProcessId(processId); |
| 407 | response.setInsertNum(0); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 408 | response.setBody(response.wireEncode()); |
| 409 | done(response); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 410 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 411 | response.setCode(300); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 412 | |
| 413 | Interest fetchInterest(parameter.getName()); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 414 | fetchInterest.setInterestLifetime(m_interestLifetime); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 415 | if (parameter.hasSelectors()) { |
| 416 | fetchInterest.setSelectors(parameter.getSelectors()); |
| 417 | } |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 418 | face.expressInterest(fetchInterest, |
| 419 | bind(&WriteHandle::onData, this, _1, _2, processId), |
| 420 | bind(&WriteHandle::onTimeout, this, _1, processId), // Nack |
| 421 | bind(&WriteHandle::onTimeout, this, _1, processId)); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 425 | WriteHandle::processSegmentedInsertCommand(const Interest& interest, RepoCommandParameter& parameter, |
| 426 | const ndn::mgmt::CommandContinuation& done) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 427 | { |
| 428 | if (parameter.hasEndBlockId()) { |
| 429 | //normal fetch segment |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 430 | SegmentNo startBlockId = parameter.hasStartBlockId() ? parameter.getStartBlockId() : 0; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 431 | SegmentNo endBlockId = parameter.getEndBlockId(); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 432 | if (startBlockId > endBlockId) { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 433 | done(negativeReply("Malformed Command", 403)); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 437 | ProcessId processId = ndn::random::generateWord64(); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 438 | ProcessInfo& process = m_processes[processId]; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 439 | RepoCommandResponse& response = process.response; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 440 | response.setCode(100); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 441 | response.setProcessId(processId); |
| 442 | response.setInsertNum(0); |
| 443 | response.setStartBlockId(startBlockId); |
| 444 | response.setEndBlockId(endBlockId); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 445 | response.setBody(response.wireEncode()); |
| 446 | done(response); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 447 | |
| 448 | //300 means data fetching is in progress |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 449 | response.setCode(300); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 450 | |
| 451 | segInit(processId, parameter); |
| 452 | } |
| 453 | else { |
| 454 | //no EndBlockId, so fetch FinalBlockId in data, if timeout, stop |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 455 | ProcessId processId = ndn::random::generateWord64(); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 456 | ProcessInfo& process = m_processes[processId]; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 457 | RepoCommandResponse& response = process.response; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 458 | response.setCode(100); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 459 | response.setProcessId(processId); |
| 460 | response.setInsertNum(0); |
| 461 | response.setStartBlockId(parameter.getStartBlockId()); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 462 | response.setBody(response.wireEncode()); |
| 463 | done(response); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 464 | |
| 465 | //300 means data fetching is in progress |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 466 | response.setCode(300); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 467 | |
| 468 | segInit(processId, parameter); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | void |
| 473 | WriteHandle::extendNoEndTime(ProcessInfo& process) |
| 474 | { |
| 475 | ndn::time::steady_clock::TimePoint& noEndTime = process.noEndTime; |
| 476 | ndn::time::steady_clock::TimePoint now = ndn::time::steady_clock::now(); |
| 477 | RepoCommandResponse& response = process.response; |
| 478 | if (now > noEndTime) { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 479 | response.setCode(405); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | //extends noEndTime |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 483 | process.noEndTime = ndn::time::steady_clock::now() + m_noEndTimeout; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 484 | |
| 485 | } |
| 486 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 487 | RepoCommandResponse |
| 488 | WriteHandle::negativeReply(std::string text, int statusCode) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 489 | { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 490 | RepoCommandResponse response(statusCode, text); |
| 491 | response.setBody(response.wireEncode()); |
| 492 | |
| 493 | return response; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 496 | } // namespace repo |