blob: e18558127588b2af134d4cafe38b78a978eb31d7 [file] [log] [blame]
Alexander Afanasyev8811b352013-01-02 12:51:15 -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
Alexander Afanasyev49a18522013-01-18 17:49:04 -080022#include "fetch-manager.h"
23#include <boost/make_shared.hpp>
24#include <boost/ref.hpp>
25#include <boost/throw_exception.hpp>
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080026
27#include "simple-interval-generator.h"
Alexander Afanasyevfc720362013-01-24 21:49:48 -080028#include "logging.h"
29
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080030INIT_LOGGER ("FetchManager");
Alexander Afanasyev49a18522013-01-18 17:49:04 -080031
32using namespace boost;
33using namespace std;
34using namespace Ccnx;
35
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080036static const Name BROADCAST_DOMAIN = Name ("/ndn/broadcast/chronoshare");
Alexander Afanasyev83531a42013-01-19 16:21:54 -080037//The disposer object function
38struct fetcher_disposer { void operator() (Fetcher *delete_this) { delete delete_this; } };
39
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080040static const string SCHEDULE_FETCHES_TAG = "ScheduleFetches";
41
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080042FetchManager::FetchManager (CcnxWrapperPtr ccnx, const Mapping &mapping, uint32_t parallelFetches/* = 3*/)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080043 : m_ccnx (ccnx)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080044 , m_mapping (mapping)
Alexander Afanasyev83531a42013-01-19 16:21:54 -080045 , m_maxParallelFetches (parallelFetches)
46 , m_currentParallelFetches (0)
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080047 , m_scheduler (new Scheduler)
Alexander Afanasyev8811b352013-01-02 12:51:15 -080048{
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080049 m_scheduler->start ();
50
51 m_scheduleFetchesTask = Scheduler::schedulePeriodicTask (m_scheduler,
52 make_shared<SimpleIntervalGenerator> (1),
53 bind (&FetchManager::ScheduleFetches, this), SCHEDULE_FETCHES_TAG);
Alexander Afanasyev49a18522013-01-18 17:49:04 -080054}
Alexander Afanasyeva199f972013-01-02 19:37:26 -080055
Alexander Afanasyev49a18522013-01-18 17:49:04 -080056FetchManager::~FetchManager ()
57{
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080058 m_scheduler->shutdown ();
59
Alexander Afanasyev83531a42013-01-19 16:21:54 -080060 m_fetchList.clear_and_dispose (fetcher_disposer ());
Alexander Afanasyev49a18522013-01-18 17:49:04 -080061}
Alexander Afanasyev8811b352013-01-02 12:51:15 -080062
Alexander Afanasyev49a18522013-01-18 17:49:04 -080063void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080064FetchManager::Enqueue (const Ccnx::Name &deviceName, const Ccnx::Name &baseName,
65 const SegmentCallback &segmentCallback, const FinishCallback &finishCallback,
66 uint64_t minSeqNo, uint64_t maxSeqNo, int priority/*PRIORITY_NORMAL*/)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080067{
Zhenkai Zhu454bae22013-01-24 19:27:54 -080068 // Assumption for the following code is minSeqNo <= maxSeqNo
69 if (minSeqNo > maxSeqNo)
70 {
71 return;
72 }
73
Alexander Afanasyev83531a42013-01-19 16:21:54 -080074 // we may need to guarantee that LookupLocator will gives an answer and not throw exception...
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080075 Name forwardingHint;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080076 forwardingHint = m_mapping (deviceName);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080077
78 Fetcher &fetcher = *(new Fetcher (m_ccnx,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080079 segmentCallback,
80 finishCallback,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080081 bind (&FetchManager::DidFetchComplete, this, _1),
82 bind (&FetchManager::DidNoDataTimeout, this, _1),
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080083 deviceName, baseName, minSeqNo, maxSeqNo,
84 boost::posix_time::seconds (30),
85 forwardingHint));
Alexander Afanasyev83531a42013-01-19 16:21:54 -080086
87 switch (priority)
88 {
89 case PRIORITY_HIGH:
90 m_fetchList.push_front (fetcher);
91 break;
92
93 case PRIORITY_NORMAL:
94 default:
95 m_fetchList.push_back (fetcher);
96 break;
97 }
98
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080099 m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, 0);
100 // ScheduleFetches (); // will start a fetch if m_currentParallelFetches is less than max, otherwise does nothing
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800101}
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800102
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800103void
104FetchManager::ScheduleFetches ()
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800105{
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800106 unique_lock<mutex> lock (m_parellelFetchMutex);
107
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800108 boost::posix_time::ptime currentTime = date_time::second_clock<boost::posix_time::ptime>::universal_time ();
109
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800110 for (FetchList::iterator item = m_fetchList.begin ();
111 m_currentParallelFetches < m_maxParallelFetches && item != m_fetchList.end ();
112 item++)
113 {
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800114 if (item->IsActive ())
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800115 continue;
116
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800117 if (currentTime < item->GetNextScheduledRetry ())
118 continue;
119
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800120 _LOG_DEBUG ("Start fetching of " << item->GetName ());
121
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800122 m_currentParallelFetches ++;
123 item->RestartPipeline ();
124 }
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800125}
126
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800127void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800128FetchManager::DidNoDataTimeout (Fetcher &fetcher)
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800129{
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800130 _LOG_DEBUG ("No data timeout for " << fetcher.GetName () << " with forwarding hint: " << fetcher.GetForwardingHint ());
131
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800132 {
133 unique_lock<mutex> lock (m_parellelFetchMutex);
134 m_currentParallelFetches --;
135 // no need to do anything with the m_fetchList
136 }
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800137
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800138 if (fetcher.GetForwardingHint () == BROADCAST_DOMAIN)
139 {
140 // try again directly (hopefully with different forwarding hint
141
142 /// @todo Handle potential exception
143 Name forwardingHint;
144 forwardingHint = m_mapping (fetcher.GetDeviceName ());
145 fetcher.SetForwardingHint (forwardingHint);
146 }
147 else
148 {
149 fetcher.SetForwardingHint (BROADCAST_DOMAIN);
150 }
151
152 double delay = fetcher.GetRetryPause ();
153 if (delay < 1) // first time
154 {
155 delay = 1;
156 }
157 else
158 {
159 delay = std::min (2*delay, 300.0); // 5 minutes max
160 }
161
162 fetcher.SetRetryPause (delay);
163 fetcher.SetNextScheduledRetry (date_time::second_clock<boost::posix_time::ptime>::universal_time () + posix_time::seconds (delay));
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800164}
165
166void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800167FetchManager::DidFetchComplete (Fetcher &fetcher)
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800168{
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800169 {
170 unique_lock<mutex> lock (m_parellelFetchMutex);
171 m_currentParallelFetches --;
172 m_fetchList.erase_and_dispose (FetchList::s_iterator_to (fetcher), fetcher_disposer ());
173 }
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800174
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800175}