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