blob: 788d63566892d2fc5662cef17e05e867a7f8b71e [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)
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080048 , m_executor (new Executor(1))
Alexander Afanasyev8811b352013-01-02 12:51:15 -080049{
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080050 m_scheduler->start ();
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080051 m_executor->start();
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080052
53 m_scheduleFetchesTask = Scheduler::schedulePeriodicTask (m_scheduler,
Alexander Afanasyevf9756232013-01-28 16:42:20 -080054 make_shared<SimpleIntervalGenerator> (300), // no need to check to often. if needed, will be rescheduled
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080055 bind (&FetchManager::ScheduleFetches, this), SCHEDULE_FETCHES_TAG);
Alexander Afanasyev49a18522013-01-18 17:49:04 -080056}
Alexander Afanasyeva199f972013-01-02 19:37:26 -080057
Alexander Afanasyev49a18522013-01-18 17:49:04 -080058FetchManager::~FetchManager ()
59{
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080060 m_scheduler->shutdown ();
61
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080062 m_executor->shutdown();
63
Alexander Afanasyev83531a42013-01-19 16:21:54 -080064 m_fetchList.clear_and_dispose (fetcher_disposer ());
Alexander Afanasyev49a18522013-01-18 17:49:04 -080065}
Alexander Afanasyev8811b352013-01-02 12:51:15 -080066
Alexander Afanasyev49a18522013-01-18 17:49:04 -080067void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080068FetchManager::Enqueue (const Ccnx::Name &deviceName, const Ccnx::Name &baseName,
69 const SegmentCallback &segmentCallback, const FinishCallback &finishCallback,
70 uint64_t minSeqNo, uint64_t maxSeqNo, int priority/*PRIORITY_NORMAL*/)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080071{
Zhenkai Zhu454bae22013-01-24 19:27:54 -080072 // Assumption for the following code is minSeqNo <= maxSeqNo
73 if (minSeqNo > maxSeqNo)
74 {
75 return;
76 }
77
Alexander Afanasyev83531a42013-01-19 16:21:54 -080078 // we may need to guarantee that LookupLocator will gives an answer and not throw exception...
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080079 Name forwardingHint;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080080 forwardingHint = m_mapping (deviceName);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080081
Alexander Afanasyevbc8bf232013-01-29 11:19:17 -080082 unique_lock<mutex> lock (m_parellelFetchMutex);
83
84 _LOG_TRACE ("++++ Create fetcher: " << baseName);
85 Fetcher *fetcher = new Fetcher (m_ccnx,
86 m_executor,
87 segmentCallback,
88 finishCallback,
89 bind (&FetchManager::DidFetchComplete, this, _1),
90 bind (&FetchManager::DidNoDataTimeout, this, _1),
91 deviceName, baseName, minSeqNo, maxSeqNo,
92 boost::posix_time::seconds (30),
93 forwardingHint);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080094
95 switch (priority)
96 {
97 case PRIORITY_HIGH:
Alexander Afanasyevbc8bf232013-01-29 11:19:17 -080098 _LOG_TRACE ("++++ Push front fetcher: " << fetcher->GetName ());
99 m_fetchList.push_front (*fetcher);
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800100 break;
101
102 case PRIORITY_NORMAL:
103 default:
Alexander Afanasyevbc8bf232013-01-29 11:19:17 -0800104 _LOG_TRACE ("++++ Push back fetcher: " << fetcher->GetName ());
105 m_fetchList.push_back (*fetcher);
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800106 break;
107 }
108
Alexander Afanasyevbc8bf232013-01-29 11:19:17 -0800109 _LOG_DEBUG ("++++ Reschedule fetcher task");
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800110 m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, 0);
111 // ScheduleFetches (); // will start a fetch if m_currentParallelFetches is less than max, otherwise does nothing
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800112}
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800113
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800114void
115FetchManager::ScheduleFetches ()
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800116{
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800117 unique_lock<mutex> lock (m_parellelFetchMutex);
118
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800119 boost::posix_time::ptime currentTime = date_time::second_clock<boost::posix_time::ptime>::universal_time ();
Alexander Afanasyevf9756232013-01-28 16:42:20 -0800120 boost::posix_time::ptime nextSheduleCheck = currentTime + posix_time::seconds (300); // no reason to have anything, but just in case
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800121
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800122 for (FetchList::iterator item = m_fetchList.begin ();
123 m_currentParallelFetches < m_maxParallelFetches && item != m_fetchList.end ();
124 item++)
125 {
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800126 if (item->IsActive ())
Alexander Afanasyevf8b22bf2013-01-26 17:28:11 -0800127 {
128 _LOG_DEBUG ("Item is active");
129 continue;
130 }
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800131
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800132 if (currentTime < item->GetNextScheduledRetry ())
Alexander Afanasyevf8b22bf2013-01-26 17:28:11 -0800133 {
Alexander Afanasyevf9756232013-01-28 16:42:20 -0800134 if (item->GetNextScheduledRetry () < nextSheduleCheck)
135 nextSheduleCheck = item->GetNextScheduledRetry ();
136
Alexander Afanasyevf8b22bf2013-01-26 17:28:11 -0800137 _LOG_DEBUG ("Item is delayed");
138 continue;
139 }
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800140
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800141 _LOG_DEBUG ("Start fetching of " << item->GetName ());
142
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800143 m_currentParallelFetches ++;
Alexander Afanasyevbc8bf232013-01-29 11:19:17 -0800144 _LOG_TRACE ("++++ RESTART PIPELINE: " << item->GetName ());
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800145 item->RestartPipeline ();
146 }
Alexander Afanasyevf9756232013-01-28 16:42:20 -0800147
148 m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, (nextSheduleCheck - currentTime).seconds ());
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800149}
150
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800151void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800152FetchManager::DidNoDataTimeout (Fetcher &fetcher)
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800153{
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800154 _LOG_DEBUG ("No data timeout for " << fetcher.GetName () << " with forwarding hint: " << fetcher.GetForwardingHint ());
155
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800156 {
157 unique_lock<mutex> lock (m_parellelFetchMutex);
158 m_currentParallelFetches --;
159 // no need to do anything with the m_fetchList
160 }
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800161
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800162 if (fetcher.GetForwardingHint () == BROADCAST_DOMAIN)
163 {
164 // try again directly (hopefully with different forwarding hint
165
166 /// @todo Handle potential exception
167 Name forwardingHint;
168 forwardingHint = m_mapping (fetcher.GetDeviceName ());
169 fetcher.SetForwardingHint (forwardingHint);
170 }
171 else
172 {
173 fetcher.SetForwardingHint (BROADCAST_DOMAIN);
174 }
175
176 double delay = fetcher.GetRetryPause ();
177 if (delay < 1) // first time
178 {
179 delay = 1;
180 }
181 else
182 {
183 delay = std::min (2*delay, 300.0); // 5 minutes max
184 }
185
186 fetcher.SetRetryPause (delay);
187 fetcher.SetNextScheduledRetry (date_time::second_clock<boost::posix_time::ptime>::universal_time () + posix_time::seconds (delay));
Alexander Afanasyevf9756232013-01-28 16:42:20 -0800188
189 m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, 0);
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800190}
191
192void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800193FetchManager::DidFetchComplete (Fetcher &fetcher)
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800194{
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800195 {
196 unique_lock<mutex> lock (m_parellelFetchMutex);
197 m_currentParallelFetches --;
Alexander Afanasyevbc8bf232013-01-29 11:19:17 -0800198 _LOG_TRACE ("+++++ removing fetcher: " << fetcher.GetName ());
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800199 m_fetchList.erase_and_dispose (FetchList::s_iterator_to (fetcher), fetcher_disposer ());
200 }
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800201
Alexander Afanasyevf9756232013-01-28 16:42:20 -0800202 m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, 0);
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800203}