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