blob: 87b0898c51ea3b1b6efabbee510916e50b5755bb [file] [log] [blame]
Alexander Afanasyevdfe58192013-01-17 17:34:04 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
20 */
21
22#include "fetcher.h"
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080023#include "fetch-manager.h"
Alexander Afanasyev66f4c492013-01-20 23:32:50 -080024#include "ccnx-pco.h"
Alexander Afanasyev678f3502013-01-23 17:09:37 -080025#include "logging.h"
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080026
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080027#include <boost/make_shared.hpp>
28#include <boost/ref.hpp>
29#include <boost/throw_exception.hpp>
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080030#include <boost/date_time/posix_time/posix_time.hpp>
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080031
Alexander Afanasyev678f3502013-01-23 17:09:37 -080032INIT_LOGGER ("Fetcher");
33
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080034using namespace boost;
35using namespace std;
Alexander Afanasyev50547892013-01-19 22:03:45 -080036using namespace Ccnx;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080037
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080038Fetcher::Fetcher (Ccnx::CcnxWrapperPtr ccnx,
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080039 ExecutorPtr executor,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080040 const SegmentCallback &segmentCallback,
41 const FinishCallback &finishCallback,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080042 OnFetchCompleteCallback onFetchComplete, OnFetchFailedCallback onFetchFailed,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080043 const Ccnx::Name &deviceName, const Ccnx::Name &name, int64_t minSeqNo, int64_t maxSeqNo,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080044 boost::posix_time::time_duration timeout/* = boost::posix_time::seconds (30)*/,
Alexander Afanasyev49a18522013-01-18 17:49:04 -080045 const Ccnx::Name &forwardingHint/* = Ccnx::Name ()*/)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080046 : m_ccnx (ccnx)
47
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080048 , m_segmentCallback (segmentCallback)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080049 , m_onFetchComplete (onFetchComplete)
50 , m_onFetchFailed (onFetchFailed)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080051 , m_finishCallback (finishCallback)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080052
Alexander Afanasyev83531a42013-01-19 16:21:54 -080053 , m_active (false)
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080054 , m_timedwait (false)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080055 , m_name (name)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080056 , m_deviceName (deviceName)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080057 , m_forwardingHint (forwardingHint)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080058 , m_maximumNoActivityPeriod (timeout)
59
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080060 , m_minSendSeqNo (minSeqNo-1)
61 , m_maxInOrderRecvSeqNo (minSeqNo-1)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080062 , m_minSeqNo (minSeqNo)
63 , m_maxSeqNo (maxSeqNo)
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080064
Yingdi Yuf0b3de32013-07-11 12:59:00 -070065 , m_pipeline (1) // initial "congestion window"
Alexander Afanasyev50547892013-01-19 22:03:45 -080066 , m_activePipeline (0)
Yingdi Yuf0b3de32013-07-11 12:59:00 -070067
68 , m_rto(1) // for temporary congestion control, should be removed when NDN provide transport functionality.
69 , m_maxRto(static_cast<double>(timeout.total_seconds())/2)
70 , m_slowStart(true)
71 , m_threshold(4)
72 , m_roundCount(0)
73
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080074 , m_retryPause (0)
Alexander Afanasyevf8b22bf2013-01-26 17:28:11 -080075 , m_nextScheduledRetry (date_time::second_clock<boost::posix_time::ptime>::universal_time ())
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080076 , m_executor (executor) // must be 1
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080077{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080078}
79
80Fetcher::~Fetcher ()
81{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080082}
Alexander Afanasyev83531a42013-01-19 16:21:54 -080083
84void
85Fetcher::RestartPipeline ()
86{
87 m_active = true;
Alexander Afanasyev50547892013-01-19 22:03:45 -080088 m_minSendSeqNo = m_maxInOrderRecvSeqNo;
Alexander Afanasyev650ba282013-01-20 20:14:06 -080089 // cout << "Restart: " << m_minSendSeqNo << endl;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080090 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -080091
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080092 m_executor->execute (bind (&Fetcher::FillPipeline, this));
Alexander Afanasyev50547892013-01-19 22:03:45 -080093}
94
95void
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080096Fetcher::SetForwardingHint (const Ccnx::Name &forwardingHint)
97{
98 m_forwardingHint = forwardingHint;
99}
100
101void
Alexander Afanasyev50547892013-01-19 22:03:45 -0800102Fetcher::FillPipeline ()
103{
104 for (; m_minSendSeqNo < m_maxSeqNo && m_activePipeline < m_pipeline; m_minSendSeqNo++)
105 {
Alexander Afanasyev2ec2c382013-01-29 20:09:00 -0800106 unique_lock<mutex> lock (m_seqNoMutex);
107
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800108 if (m_outOfOrderRecvSeqNo.find (m_minSendSeqNo+1) != m_outOfOrderRecvSeqNo.end ())
109 continue;
110
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800111 if (m_inActivePipeline.find (m_minSendSeqNo+1) != m_inActivePipeline.end ())
112 continue;
113
114 m_inActivePipeline.insert (m_minSendSeqNo+1);
115
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800116 _LOG_DEBUG (" >>> i " << Name (m_forwardingHint)(m_name) << ", seq = " << (m_minSendSeqNo + 1 ));
Alexander Afanasyev678f3502013-01-23 17:09:37 -0800117
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800118 // cout << ">>> " << m_minSendSeqNo+1 << endl;
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800119 m_ccnx->sendInterest (Name (m_forwardingHint)(m_name)(m_minSendSeqNo+1),
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800120 Closure (bind(&Fetcher::OnData, this, m_minSendSeqNo+1, _1, _2),
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800121 bind(&Fetcher::OnTimeout, this, m_minSendSeqNo+1, _1, _2, _3)),
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700122 Selectors().interestLifetime (m_rto)); // Alex: this lifetime should be changed to RTO
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800123 _LOG_DEBUG (" >>> i ok");
Alexander Afanasyev50547892013-01-19 22:03:45 -0800124
125 m_activePipeline ++;
126 }
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800127}
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800128
129void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800130Fetcher::OnData (uint64_t seqno, const Ccnx::Name &name, PcoPtr data)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800131{
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800132 m_executor->execute (bind (&Fetcher::OnData_Execute, this, seqno, name, data));
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800133}
134
135void
136Fetcher::OnData_Execute (uint64_t seqno, Ccnx::Name name, Ccnx::PcoPtr data)
137{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800138 _LOG_DEBUG (" <<< d " << name.getPartialName (0, name.size () - 1) << ", seq = " << seqno);
139
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800140 if (m_forwardingHint == Name ())
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800141 {
Zhenkai Zhud5d99be2013-03-13 19:15:56 -0700142 // TODO: check verified!!!!
143 if (true)
Zhenkai Zhu5957c0d2013-02-08 13:47:52 -0800144 {
145 if (!m_segmentCallback.empty ())
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800146 {
147 m_segmentCallback (m_deviceName, m_name, seqno, data);
148 }
Zhenkai Zhu5957c0d2013-02-08 13:47:52 -0800149 }
150 else
151 {
152 _LOG_ERROR("Can not verify signature content. Name = " << data->name());
153 // probably needs to do more in the future
154 }
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800155 // we don't have to tell FetchManager about this
156 }
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800157 else
158 {
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700159 // in this case we don't care whether "data" is verified, in fact, we expect it is unverified
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800160 try {
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800161 PcoPtr pco = make_shared<ParsedContentObject> (*data->contentPtr ());
Zhenkai Zhu79264a42013-02-07 21:49:42 -0800162
163 // we need to verify this pco and apply callback only when verified
Zhenkai Zhud5d99be2013-03-13 19:15:56 -0700164 // TODO: check verified !!!
165 if (true)
Zhenkai Zhu5957c0d2013-02-08 13:47:52 -0800166 {
167 if (!m_segmentCallback.empty ())
168 {
169 m_segmentCallback (m_deviceName, m_name, seqno, pco);
170 }
171 }
172 else
173 {
174 _LOG_ERROR("Can not verify signature content. Name = " << pco->name());
175 // probably needs to do more in the future
176 }
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800177 }
178 catch (MisformedContentObjectException &e)
179 {
180 cerr << "MisformedContentObjectException..." << endl;
181 // no idea what should do...
182 // let's ignore for now
183 }
184 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800185
Alexander Afanasyev50547892013-01-19 22:03:45 -0800186 m_activePipeline --;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800187 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -0800188
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700189 {
190 unique_lock<mutex> lock (m_pipelineMutex);
191 if(m_slowStart){
192 m_pipeline++;
193 if(m_pipeline == m_threshold)
194 m_slowStart = false;
195 }
196 else{
197 m_roundCount++;
198 _LOG_DEBUG ("roundCount: " << m_roundCount);
199 if(m_roundCount == m_pipeline){
200 m_pipeline++;
201 m_roundCount = 0;
202 }
203 }
204 }
205
206 _LOG_DEBUG ("slowStart: " << boolalpha << m_slowStart << " pipeline: " << m_pipeline << " threshold: " << m_threshold);
207
208
Alexander Afanasyev50547892013-01-19 22:03:45 -0800209 ////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev2ec2c382013-01-29 20:09:00 -0800210 unique_lock<mutex> lock (m_seqNoMutex);
211
Alexander Afanasyev50547892013-01-19 22:03:45 -0800212 m_outOfOrderRecvSeqNo.insert (seqno);
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800213 m_inActivePipeline.erase (seqno);
214 _LOG_DEBUG ("Total segments received: " << m_outOfOrderRecvSeqNo.size ());
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800215 set<int64_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin ();
Alexander Afanasyev50547892013-01-19 22:03:45 -0800216 for (; inOrderSeqNo != m_outOfOrderRecvSeqNo.end ();
217 inOrderSeqNo++)
218 {
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800219 _LOG_TRACE ("Checking " << *inOrderSeqNo << " and " << m_maxInOrderRecvSeqNo+1);
Alexander Afanasyev50547892013-01-19 22:03:45 -0800220 if (*inOrderSeqNo == m_maxInOrderRecvSeqNo+1)
221 {
222 m_maxInOrderRecvSeqNo = *inOrderSeqNo;
223 }
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800224 else if (*inOrderSeqNo < m_maxInOrderRecvSeqNo+1) // not possible anymore, but just in case
225 {
226 continue;
227 }
Alexander Afanasyev50547892013-01-19 22:03:45 -0800228 else
229 break;
230 }
231 m_outOfOrderRecvSeqNo.erase (m_outOfOrderRecvSeqNo.begin (), inOrderSeqNo);
232 ////////////////////////////////////////////////////////////////////////////
233
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800234 _LOG_TRACE ("Max in order received: " << m_maxInOrderRecvSeqNo << ", max seqNo to request: " << m_maxSeqNo);
235
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800236 if (m_maxInOrderRecvSeqNo == m_maxSeqNo)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800237 {
Alexander Afanasyevbc8bf232013-01-29 11:19:17 -0800238 _LOG_TRACE ("Fetch finished: " << m_name);
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800239 m_active = false;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800240 // invoke callback
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800241 if (!m_finishCallback.empty ())
242 {
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800243 _LOG_TRACE ("Notifying callback");
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800244 m_finishCallback(m_deviceName, m_name);
245 }
246
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800247 // tell FetchManager that we have finish our job
Zhenkai Zhu1f1def52013-01-29 00:01:03 -0800248 // m_onFetchComplete (*this);
249 // using executor, so we won't be deleted if there is scheduled FillPipeline call
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800250 if (!m_onFetchComplete.empty ())
251 {
Zhenkai Zhu354d46d2013-02-06 13:49:48 -0800252 m_timedwait = true;
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800253 m_executor->execute (bind (m_onFetchComplete, ref(*this), m_deviceName, m_name));
254 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800255 }
256 else
257 {
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800258 m_executor->execute (bind (&Fetcher::FillPipeline, this));
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800259 }
260}
261
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800262void
263Fetcher::OnTimeout (uint64_t seqno, const Ccnx::Name &name, const Closure &closure, Selectors selectors)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800264{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800265 _LOG_DEBUG (this << ", " << m_executor.get ());
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800266 m_executor->execute (bind (&Fetcher::OnTimeout_Execute, this, seqno, name, closure, selectors));
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800267}
268
269void
Alexander Afanasyev67d757c2013-01-28 23:22:12 -0800270Fetcher::OnTimeout_Execute (uint64_t seqno, Ccnx::Name name, Ccnx::Closure closure, Ccnx::Selectors selectors)
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800271{
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800272 _LOG_DEBUG (" <<< :( timeout " << name.getPartialName (0, name.size () - 1) << ", seq = " << seqno);
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800273
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800274 // cout << "Fetcher::OnTimeout: " << name << endl;
275 // cout << "Last: " << m_lastPositiveActivity << ", config: " << m_maximumNoActivityPeriod
276 // << ", now: " << date_time::second_clock<boost::posix_time::ptime>::universal_time()
277 // << ", oldest: " << (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod) << endl;
278
279 if (m_lastPositiveActivity <
280 (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod))
281 {
Alexander Afanasyev2ec2c382013-01-29 20:09:00 -0800282 bool done = false;
283 {
284 unique_lock<mutex> lock (m_seqNoMutex);
285 m_inActivePipeline.erase (seqno);
286 m_activePipeline --;
287
288 if (m_activePipeline == 0)
289 {
290 done = true;
291 }
292 }
293
294 if (done)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800295 {
Alexander Afanasyev2ec2c382013-01-29 20:09:00 -0800296 {
297 unique_lock<mutex> lock (m_seqNoMutex);
298 _LOG_DEBUG ("Telling that fetch failed");
299 _LOG_DEBUG ("Active pipeline size should be zero: " << m_inActivePipeline.size ());
300 }
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800301
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800302 m_active = false;
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800303 if (!m_onFetchFailed.empty ())
304 {
305 m_onFetchFailed (ref (*this));
306 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800307 // this is not valid anymore, but we still should be able finish work
308 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800309 }
310 else
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800311 {
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800312 _LOG_DEBUG ("Asking to reexpress seqno: " << seqno);
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700313 {
314 unique_lock<mutex> lock (m_rtoMutex);
315 _LOG_DEBUG ("Interest Timeout");
316 m_rto = m_rto * 2;
317 if(m_rto > m_maxRto)
318 {
319 m_rto = m_maxRto;
320 _LOG_DEBUG ("RTO is max: " << m_rto);
321 }
322 else
323 {
324 _LOG_DEBUG ("RTO is doubled: " << m_rto);
325 }
326 }
327
328 {
329 unique_lock<mutex> lock (m_pipelineMutex);
330 m_threshold = m_pipeline / 2;
331 m_pipeline = 1;
332 m_roundCount = 0;
333 m_slowStart = true;
334 }
335
336 m_ccnx->sendInterest (name, closure, selectors.interestLifetime (m_rto));
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800337 }
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800338}