Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 22 | #include "fetch-manager.h" |
| 23 | #include <boost/make_shared.hpp> |
| 24 | #include <boost/ref.hpp> |
| 25 | #include <boost/throw_exception.hpp> |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame^] | 26 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 27 | |
| 28 | #include "simple-interval-generator.h" |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 29 | #include "logging.h" |
| 30 | |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 31 | INIT_LOGGER ("FetchManager"); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 32 | |
| 33 | using namespace boost; |
| 34 | using namespace std; |
| 35 | using namespace Ccnx; |
| 36 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 37 | static const Name BROADCAST_DOMAIN = Name ("/ndn/broadcast/chronoshare"); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 38 | //The disposer object function |
| 39 | struct fetcher_disposer { void operator() (Fetcher *delete_this) { delete delete_this; } }; |
| 40 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 41 | static const string SCHEDULE_FETCHES_TAG = "ScheduleFetches"; |
| 42 | |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 43 | FetchManager::FetchManager (Ccnx::CcnxWrapperPtr ccnx |
| 44 | , const Mapping &mapping |
| 45 | , uint32_t parallelFetches // = 3 |
| 46 | , const SegmentCallback &defaultSegmentCallback |
| 47 | , const FinishCallback &defaultFinishCallback |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 48 | , const FetchTaskDbPtr &taskDb |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 49 | ) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 50 | : m_ccnx (ccnx) |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 51 | , m_mapping (mapping) |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 52 | , m_maxParallelFetches (parallelFetches) |
| 53 | , m_currentParallelFetches (0) |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 54 | , m_scheduler (new Scheduler) |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 55 | , m_executor (new Executor(1)) |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 56 | , m_defaultSegmentCallback(defaultSegmentCallback) |
| 57 | , m_defaultFinishCallback(defaultFinishCallback) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 58 | , m_taskDb(taskDb) |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 59 | { |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 60 | m_scheduler->start (); |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 61 | m_executor->start(); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | 4aecad8 | 2013-01-31 15:27:08 -0800 | [diff] [blame] | 63 | m_scheduleFetchesTask = Scheduler::schedulePeriodicTask (m_scheduler, |
| 64 | make_shared<SimpleIntervalGenerator> (300), // no need to check to often. if needed, will be rescheduled |
| 65 | bind (&FetchManager::ScheduleFetches, this), SCHEDULE_FETCHES_TAG); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 66 | // resume un-finished fetches if there is any |
| 67 | if (m_taskDb) |
| 68 | { |
| 69 | m_taskDb->foreachTask(bind(&FetchManager::Enqueue, this, _1, _2, _3, _4, _5)); |
| 70 | } |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 71 | } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 73 | FetchManager::~FetchManager () |
| 74 | { |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 75 | m_scheduler->shutdown (); |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 76 | m_executor->shutdown(); |
| 77 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 78 | m_ccnx.reset (); |
| 79 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 80 | m_fetchList.clear_and_dispose (fetcher_disposer ()); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 81 | } |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 82 | |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 83 | // Enqueue using default callbacks |
| 84 | void |
| 85 | FetchManager::Enqueue (const Ccnx::Name &deviceName, const Ccnx::Name &baseName, |
| 86 | uint64_t minSeqNo, uint64_t maxSeqNo, int priority) |
| 87 | { |
| 88 | Enqueue(deviceName, baseName, m_defaultSegmentCallback, m_defaultFinishCallback, minSeqNo, maxSeqNo, priority); |
| 89 | } |
| 90 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 91 | void |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 92 | FetchManager::Enqueue (const Ccnx::Name &deviceName, const Ccnx::Name &baseName, |
| 93 | const SegmentCallback &segmentCallback, const FinishCallback &finishCallback, |
| 94 | uint64_t minSeqNo, uint64_t maxSeqNo, int priority/*PRIORITY_NORMAL*/) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 95 | { |
Zhenkai Zhu | 454bae2 | 2013-01-24 19:27:54 -0800 | [diff] [blame] | 96 | // Assumption for the following code is minSeqNo <= maxSeqNo |
| 97 | if (minSeqNo > maxSeqNo) |
| 98 | { |
| 99 | return; |
| 100 | } |
| 101 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 102 | // we may need to guarantee that LookupLocator will gives an answer and not throw exception... |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 103 | Name forwardingHint; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 104 | forwardingHint = m_mapping (deviceName); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 106 | if (m_taskDb) |
| 107 | { |
| 108 | m_taskDb->addTask(deviceName, baseName, minSeqNo, maxSeqNo, priority); |
| 109 | } |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 110 | |
Alexander Afanasyev | bc8bf23 | 2013-01-29 11:19:17 -0800 | [diff] [blame] | 111 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 112 | |
| 113 | _LOG_TRACE ("++++ Create fetcher: " << baseName); |
| 114 | Fetcher *fetcher = new Fetcher (m_ccnx, |
| 115 | m_executor, |
| 116 | segmentCallback, |
| 117 | finishCallback, |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 118 | bind (&FetchManager::DidFetchComplete, this, _1, _2, _3), |
Alexander Afanasyev | bc8bf23 | 2013-01-29 11:19:17 -0800 | [diff] [blame] | 119 | bind (&FetchManager::DidNoDataTimeout, this, _1), |
| 120 | deviceName, baseName, minSeqNo, maxSeqNo, |
| 121 | boost::posix_time::seconds (30), |
| 122 | forwardingHint); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 123 | |
| 124 | switch (priority) |
| 125 | { |
| 126 | case PRIORITY_HIGH: |
Alexander Afanasyev | bc8bf23 | 2013-01-29 11:19:17 -0800 | [diff] [blame] | 127 | _LOG_TRACE ("++++ Push front fetcher: " << fetcher->GetName ()); |
| 128 | m_fetchList.push_front (*fetcher); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 129 | break; |
| 130 | |
| 131 | case PRIORITY_NORMAL: |
| 132 | default: |
Alexander Afanasyev | bc8bf23 | 2013-01-29 11:19:17 -0800 | [diff] [blame] | 133 | _LOG_TRACE ("++++ Push back fetcher: " << fetcher->GetName ()); |
| 134 | m_fetchList.push_back (*fetcher); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | |
Alexander Afanasyev | bc8bf23 | 2013-01-29 11:19:17 -0800 | [diff] [blame] | 138 | _LOG_DEBUG ("++++ Reschedule fetcher task"); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 139 | m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, 0); |
| 140 | // ScheduleFetches (); // will start a fetch if m_currentParallelFetches is less than max, otherwise does nothing |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 141 | } |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 143 | void |
| 144 | FetchManager::ScheduleFetches () |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 145 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 146 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 147 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 148 | boost::posix_time::ptime currentTime = date_time::second_clock<boost::posix_time::ptime>::universal_time (); |
Alexander Afanasyev | f975623 | 2013-01-28 16:42:20 -0800 | [diff] [blame] | 149 | boost::posix_time::ptime nextSheduleCheck = currentTime + posix_time::seconds (300); // no reason to have anything, but just in case |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 150 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 151 | for (FetchList::iterator item = m_fetchList.begin (); |
| 152 | m_currentParallelFetches < m_maxParallelFetches && item != m_fetchList.end (); |
| 153 | item++) |
| 154 | { |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 155 | if (item->IsActive ()) |
Alexander Afanasyev | f8b22bf | 2013-01-26 17:28:11 -0800 | [diff] [blame] | 156 | { |
| 157 | _LOG_DEBUG ("Item is active"); |
| 158 | continue; |
| 159 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 160 | |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame^] | 161 | if (item->IsTimedWait ()) |
| 162 | { |
| 163 | _LOG_DEBUG ("Item is in timed-wait"); |
| 164 | continue; |
| 165 | } |
| 166 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 167 | if (currentTime < item->GetNextScheduledRetry ()) |
Alexander Afanasyev | f8b22bf | 2013-01-26 17:28:11 -0800 | [diff] [blame] | 168 | { |
Alexander Afanasyev | f975623 | 2013-01-28 16:42:20 -0800 | [diff] [blame] | 169 | if (item->GetNextScheduledRetry () < nextSheduleCheck) |
| 170 | nextSheduleCheck = item->GetNextScheduledRetry (); |
| 171 | |
Alexander Afanasyev | f8b22bf | 2013-01-26 17:28:11 -0800 | [diff] [blame] | 172 | _LOG_DEBUG ("Item is delayed"); |
| 173 | continue; |
| 174 | } |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 175 | |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 176 | _LOG_DEBUG ("Start fetching of " << item->GetName ()); |
| 177 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 178 | m_currentParallelFetches ++; |
Alexander Afanasyev | bc8bf23 | 2013-01-29 11:19:17 -0800 | [diff] [blame] | 179 | _LOG_TRACE ("++++ RESTART PIPELINE: " << item->GetName ()); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 180 | item->RestartPipeline (); |
| 181 | } |
Alexander Afanasyev | f975623 | 2013-01-28 16:42:20 -0800 | [diff] [blame] | 182 | |
| 183 | m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, (nextSheduleCheck - currentTime).seconds ()); |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 186 | void |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 187 | FetchManager::DidNoDataTimeout (Fetcher &fetcher) |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 188 | { |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 189 | _LOG_DEBUG ("No data timeout for " << fetcher.GetName () << " with forwarding hint: " << fetcher.GetForwardingHint ()); |
| 190 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 191 | { |
| 192 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 193 | m_currentParallelFetches --; |
| 194 | // no need to do anything with the m_fetchList |
| 195 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 196 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 197 | if (fetcher.GetForwardingHint () == BROADCAST_DOMAIN) |
| 198 | { |
| 199 | // try again directly (hopefully with different forwarding hint |
| 200 | |
| 201 | /// @todo Handle potential exception |
| 202 | Name forwardingHint; |
| 203 | forwardingHint = m_mapping (fetcher.GetDeviceName ()); |
| 204 | fetcher.SetForwardingHint (forwardingHint); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | fetcher.SetForwardingHint (BROADCAST_DOMAIN); |
| 209 | } |
| 210 | |
| 211 | double delay = fetcher.GetRetryPause (); |
| 212 | if (delay < 1) // first time |
| 213 | { |
| 214 | delay = 1; |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | delay = std::min (2*delay, 300.0); // 5 minutes max |
| 219 | } |
| 220 | |
| 221 | fetcher.SetRetryPause (delay); |
| 222 | fetcher.SetNextScheduledRetry (date_time::second_clock<boost::posix_time::ptime>::universal_time () + posix_time::seconds (delay)); |
Alexander Afanasyev | f975623 | 2013-01-28 16:42:20 -0800 | [diff] [blame] | 223 | |
| 224 | m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, 0); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 228 | FetchManager::DidFetchComplete (Fetcher &fetcher, const Name &deviceName, const Name &baseName) |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 229 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 230 | { |
| 231 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 232 | m_currentParallelFetches --; |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 233 | |
| 234 | if (m_taskDb) |
| 235 | { |
| 236 | m_taskDb->deleteTask(deviceName, baseName); |
| 237 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 238 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 239 | |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame^] | 240 | // like TCP timed-wait |
| 241 | m_scheduler->scheduleOneTimeTask(m_scheduler, 10, boost::bind(&FetchManager::TimedWait, this, ref(fetcher)), boost::lexical_cast<string>(baseName)); |
| 242 | |
Alexander Afanasyev | f975623 | 2013-01-28 16:42:20 -0800 | [diff] [blame] | 243 | m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, 0); |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 244 | } |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame^] | 245 | |
| 246 | void |
| 247 | FetchManager::TimedWait (Fetcher &fetcher) |
| 248 | { |
| 249 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 250 | _LOG_TRACE ("+++++ removing fetcher: " << fetcher.GetName ()); |
| 251 | m_fetchList.erase_and_dispose (FetchList::s_iterator_to (fetcher), fetcher_disposer ()); |
| 252 | } |