Extension of scheduler, different logic in FetchManager regarding re-requesting (exponential backoff, and flipping broadcast/forwarding hint)
diff --git a/src/fetch-manager.cc b/src/fetch-manager.cc
index d74f210..e185581 100644
--- a/src/fetch-manager.cc
+++ b/src/fetch-manager.cc
@@ -23,6 +23,8 @@
#include <boost/make_shared.hpp>
#include <boost/ref.hpp>
#include <boost/throw_exception.hpp>
+
+#include "simple-interval-generator.h"
#include "logging.h"
INIT_LOGGER ("FetchManager");
@@ -31,21 +33,30 @@
using namespace std;
using namespace Ccnx;
-static const string BROADCAST_DOMAIN = "/ndn/broadcast/chronoshare";
+static const Name BROADCAST_DOMAIN = Name ("/ndn/broadcast/chronoshare");
//The disposer object function
struct fetcher_disposer { void operator() (Fetcher *delete_this) { delete delete_this; } };
+static const string SCHEDULE_FETCHES_TAG = "ScheduleFetches";
+
FetchManager::FetchManager (CcnxWrapperPtr ccnx, const Mapping &mapping, uint32_t parallelFetches/* = 3*/)
: m_ccnx (ccnx)
, m_mapping (mapping)
, m_maxParallelFetches (parallelFetches)
, m_currentParallelFetches (0)
-
+ , m_scheduler (new Scheduler)
{
+ m_scheduler->start ();
+
+ m_scheduleFetchesTask = Scheduler::schedulePeriodicTask (m_scheduler,
+ make_shared<SimpleIntervalGenerator> (1),
+ bind (&FetchManager::ScheduleFetches, this), SCHEDULE_FETCHES_TAG);
}
FetchManager::~FetchManager ()
{
+ m_scheduler->shutdown ();
+
m_fetchList.clear_and_dispose (fetcher_disposer ());
}
@@ -85,7 +96,8 @@
break;
}
- ScheduleFetches (); // will start a fetch if m_currentParallelFetches is less than max, otherwise does nothing
+ m_scheduler->rescheduleTaskAt (m_scheduleFetchesTask, 0);
+ // ScheduleFetches (); // will start a fetch if m_currentParallelFetches is less than max, otherwise does nothing
}
void
@@ -93,6 +105,8 @@
{
unique_lock<mutex> lock (m_parellelFetchMutex);
+ boost::posix_time::ptime currentTime = date_time::second_clock<boost::posix_time::ptime>::universal_time ();
+
for (FetchList::iterator item = m_fetchList.begin ();
m_currentParallelFetches < m_maxParallelFetches && item != m_fetchList.end ();
item++)
@@ -100,6 +114,9 @@
if (item->IsActive ())
continue;
+ if (currentTime < item->GetNextScheduledRetry ())
+ continue;
+
_LOG_DEBUG ("Start fetching of " << item->GetName ());
m_currentParallelFetches ++;
@@ -112,14 +129,38 @@
{
_LOG_DEBUG ("No data timeout for " << fetcher.GetName () << " with forwarding hint: " << fetcher.GetForwardingHint ());
- fetcher.SetForwardingHint (Ccnx::Name (BROADCAST_DOMAIN));
{
unique_lock<mutex> lock (m_parellelFetchMutex);
m_currentParallelFetches --;
// no need to do anything with the m_fetchList
}
- ScheduleFetches ();
+ if (fetcher.GetForwardingHint () == BROADCAST_DOMAIN)
+ {
+ // try again directly (hopefully with different forwarding hint
+
+ /// @todo Handle potential exception
+ Name forwardingHint;
+ forwardingHint = m_mapping (fetcher.GetDeviceName ());
+ fetcher.SetForwardingHint (forwardingHint);
+ }
+ else
+ {
+ fetcher.SetForwardingHint (BROADCAST_DOMAIN);
+ }
+
+ double delay = fetcher.GetRetryPause ();
+ if (delay < 1) // first time
+ {
+ delay = 1;
+ }
+ else
+ {
+ delay = std::min (2*delay, 300.0); // 5 minutes max
+ }
+
+ fetcher.SetRetryPause (delay);
+ fetcher.SetNextScheduledRetry (date_time::second_clock<boost::posix_time::ptime>::universal_time () + posix_time::seconds (delay));
}
void