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> |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 26 | |
| 27 | #include "simple-interval-generator.h" |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 28 | #include "logging.h" |
| 29 | |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 30 | INIT_LOGGER ("FetchManager"); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace boost; |
| 33 | using namespace std; |
| 34 | using namespace Ccnx; |
| 35 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 36 | static const Name BROADCAST_DOMAIN = Name ("/ndn/broadcast/chronoshare"); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 37 | //The disposer object function |
| 38 | struct fetcher_disposer { void operator() (Fetcher *delete_this) { delete delete_this; } }; |
| 39 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 40 | static const string SCHEDULE_FETCHES_TAG = "ScheduleFetches"; |
| 41 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 42 | FetchManager::FetchManager (CcnxWrapperPtr ccnx, const Mapping &mapping, uint32_t parallelFetches/* = 3*/) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 43 | : m_ccnx (ccnx) |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 44 | , m_mapping (mapping) |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 45 | , m_maxParallelFetches (parallelFetches) |
| 46 | , m_currentParallelFetches (0) |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 47 | , m_scheduler (new Scheduler) |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame^] | 48 | , m_executor (new Executor(1)) |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 49 | { |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 50 | m_scheduler->start (); |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame^] | 51 | m_executor->start(); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 52 | |
| 53 | m_scheduleFetchesTask = Scheduler::schedulePeriodicTask (m_scheduler, |
Alexander Afanasyev | f975623 | 2013-01-28 16:42:20 -0800 | [diff] [blame] | 54 | make_shared<SimpleIntervalGenerator> (300), // no need to check to often. if needed, will be rescheduled |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 55 | bind (&FetchManager::ScheduleFetches, this), SCHEDULE_FETCHES_TAG); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 56 | } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 58 | FetchManager::~FetchManager () |
| 59 | { |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 60 | m_scheduler->shutdown (); |
| 61 | |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame^] | 62 | m_executor->shutdown(); |
| 63 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 64 | m_fetchList.clear_and_dispose (fetcher_disposer ()); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 65 | } |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 67 | void |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 68 | FetchManager::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 Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 71 | { |
Zhenkai Zhu | 454bae2 | 2013-01-24 19:27:54 -0800 | [diff] [blame] | 72 | // Assumption for the following code is minSeqNo <= maxSeqNo |
| 73 | if (minSeqNo > maxSeqNo) |
| 74 | { |
| 75 | return; |
| 76 | } |
| 77 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 78 | // 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] | 79 | Name forwardingHint; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 80 | forwardingHint = m_mapping (deviceName); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 81 | |
| 82 | Fetcher &fetcher = *(new Fetcher (m_ccnx, |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame^] | 83 | m_executor, |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 84 | segmentCallback, |
| 85 | finishCallback, |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 86 | bind (&FetchManager::DidFetchComplete, this, _1), |
| 87 | bind (&FetchManager::DidNoDataTimeout, this, _1), |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 88 | deviceName, baseName, minSeqNo, maxSeqNo, |
| 89 | boost::posix_time::seconds (30), |
| 90 | forwardingHint)); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 91 | |
| 92 | switch (priority) |
| 93 | { |
| 94 | case PRIORITY_HIGH: |
| 95 | m_fetchList.push_front (fetcher); |
| 96 | break; |
| 97 | |
| 98 | case PRIORITY_NORMAL: |
| 99 | default: |
| 100 | m_fetchList.push_back (fetcher); |
| 101 | break; |
| 102 | } |
| 103 | |
Alexander Afanasyev | f8b22bf | 2013-01-26 17:28:11 -0800 | [diff] [blame] | 104 | _LOG_DEBUG ("Reschedule fetcher task"); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 105 | m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, 0); |
| 106 | // 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] | 107 | } |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 109 | void |
| 110 | FetchManager::ScheduleFetches () |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 111 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 112 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 113 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 114 | 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] | 115 | 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] | 116 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 117 | for (FetchList::iterator item = m_fetchList.begin (); |
| 118 | m_currentParallelFetches < m_maxParallelFetches && item != m_fetchList.end (); |
| 119 | item++) |
| 120 | { |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 121 | if (item->IsActive ()) |
Alexander Afanasyev | f8b22bf | 2013-01-26 17:28:11 -0800 | [diff] [blame] | 122 | { |
| 123 | _LOG_DEBUG ("Item is active"); |
| 124 | continue; |
| 125 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 127 | if (currentTime < item->GetNextScheduledRetry ()) |
Alexander Afanasyev | f8b22bf | 2013-01-26 17:28:11 -0800 | [diff] [blame] | 128 | { |
Alexander Afanasyev | f975623 | 2013-01-28 16:42:20 -0800 | [diff] [blame] | 129 | if (item->GetNextScheduledRetry () < nextSheduleCheck) |
| 130 | nextSheduleCheck = item->GetNextScheduledRetry (); |
| 131 | |
Alexander Afanasyev | f8b22bf | 2013-01-26 17:28:11 -0800 | [diff] [blame] | 132 | _LOG_DEBUG ("Item is delayed"); |
| 133 | continue; |
| 134 | } |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 135 | |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 136 | _LOG_DEBUG ("Start fetching of " << item->GetName ()); |
| 137 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 138 | m_currentParallelFetches ++; |
| 139 | item->RestartPipeline (); |
| 140 | } |
Alexander Afanasyev | f975623 | 2013-01-28 16:42:20 -0800 | [diff] [blame] | 141 | |
| 142 | m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, (nextSheduleCheck - currentTime).seconds ()); |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 145 | void |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 146 | FetchManager::DidNoDataTimeout (Fetcher &fetcher) |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 147 | { |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 148 | _LOG_DEBUG ("No data timeout for " << fetcher.GetName () << " with forwarding hint: " << fetcher.GetForwardingHint ()); |
| 149 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 150 | { |
| 151 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 152 | m_currentParallelFetches --; |
| 153 | // no need to do anything with the m_fetchList |
| 154 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 155 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 156 | if (fetcher.GetForwardingHint () == BROADCAST_DOMAIN) |
| 157 | { |
| 158 | // try again directly (hopefully with different forwarding hint |
| 159 | |
| 160 | /// @todo Handle potential exception |
| 161 | Name forwardingHint; |
| 162 | forwardingHint = m_mapping (fetcher.GetDeviceName ()); |
| 163 | fetcher.SetForwardingHint (forwardingHint); |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | fetcher.SetForwardingHint (BROADCAST_DOMAIN); |
| 168 | } |
| 169 | |
| 170 | double delay = fetcher.GetRetryPause (); |
| 171 | if (delay < 1) // first time |
| 172 | { |
| 173 | delay = 1; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | delay = std::min (2*delay, 300.0); // 5 minutes max |
| 178 | } |
| 179 | |
| 180 | fetcher.SetRetryPause (delay); |
| 181 | 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] | 182 | |
| 183 | m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, 0); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 187 | FetchManager::DidFetchComplete (Fetcher &fetcher) |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 188 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 189 | { |
| 190 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 191 | m_currentParallelFetches --; |
| 192 | m_fetchList.erase_and_dispose (FetchList::s_iterator_to (fetcher), fetcher_disposer ()); |
| 193 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 194 | |
Alexander Afanasyev | f975623 | 2013-01-28 16:42:20 -0800 | [diff] [blame] | 195 | m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, 0); |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 196 | } |