Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "asf-probing-module.hpp" |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 27 | #include "core/random.hpp" |
Ashlesh Gawande | 2a73f35 | 2016-12-01 15:37:03 +0000 | [diff] [blame] | 28 | #include "algorithm.hpp" |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 29 | |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 30 | namespace nfd { |
| 31 | namespace fw { |
| 32 | namespace asf { |
| 33 | |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 34 | constexpr time::milliseconds ProbingModule::DEFAULT_PROBING_INTERVAL; |
| 35 | constexpr time::milliseconds ProbingModule::MIN_PROBING_INTERVAL; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 36 | |
| 37 | static_assert(ProbingModule::DEFAULT_PROBING_INTERVAL < AsfMeasurements::MEASUREMENTS_LIFETIME, |
| 38 | "ProbingModule::DEFAULT_PROBING_INTERVAL must be less than AsfMeasurements::MEASUREMENTS_LIFETIME"); |
| 39 | |
| 40 | ProbingModule::ProbingModule(AsfMeasurements& measurements) |
| 41 | : m_probingInterval(DEFAULT_PROBING_INTERVAL) |
| 42 | , m_measurements(measurements) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 47 | ProbingModule::scheduleProbe(const fib::Entry& fibEntry, const time::milliseconds& interval) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 48 | { |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 49 | Name prefix = fibEntry.getPrefix(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 50 | |
| 51 | // Set the probing flag for the namespace to true after passed interval of time |
| 52 | scheduler::schedule(interval, [this, prefix] { |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 53 | NamespaceInfo* info = m_measurements.getNamespaceInfo(prefix); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 54 | |
| 55 | if (info == nullptr) { |
| 56 | // fib::Entry with the passed prefix has been removed or the fib::Entry has |
| 57 | // a name that is not controlled by the AsfStrategy |
| 58 | return; |
| 59 | } |
| 60 | else { |
| 61 | info->setIsProbingDue(true); |
| 62 | } |
| 63 | }); |
| 64 | } |
| 65 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 66 | Face* |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 67 | ProbingModule::getFaceToProbe(const Face& inFace, |
| 68 | const Interest& interest, |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 69 | const fib::Entry& fibEntry, |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 70 | const Face& faceUsed) |
| 71 | { |
| 72 | FaceInfoFacePairSet rankedFaces( |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 73 | [] (const auto& pairLhs, const auto& pairRhs) -> bool { |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 74 | // Sort by RTT |
| 75 | // If a face has timed-out, rank it behind non-timed-out faces |
| 76 | FaceInfo& lhs = *pairLhs.first; |
| 77 | FaceInfo& rhs = *pairRhs.first; |
| 78 | |
| 79 | return (!lhs.isTimeout() && rhs.isTimeout()) || |
| 80 | (lhs.isTimeout() == rhs.isTimeout() && lhs.getSrtt() < rhs.getSrtt()); |
| 81 | }); |
| 82 | |
| 83 | // Put eligible faces into rankedFaces. If a face does not have an RTT measurement, |
| 84 | // immediately pick the face for probing |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 85 | for (const fib::NextHop& hop : fibEntry.getNextHops()) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 86 | Face& hopFace = hop.getFace(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 87 | |
| 88 | // Don't send probe Interest back to the incoming face or use the same face |
Ashlesh Gawande | 2a73f35 | 2016-12-01 15:37:03 +0000 | [diff] [blame] | 89 | // as the forwarded Interest or use a face that violates scope |
| 90 | if (hopFace.getId() == inFace.getId() || hopFace.getId() == faceUsed.getId() || |
| 91 | wouldViolateScope(inFace, interest, hopFace)) { |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 92 | continue; |
| 93 | } |
| 94 | |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 95 | FaceInfo* info = m_measurements.getFaceInfo(fibEntry, interest, hopFace.getId()); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 96 | // If no RTT has been recorded, probe this face |
| 97 | if (info == nullptr || !info->hasSrttMeasurement()) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 98 | return &hopFace; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Add FaceInfo to container sorted by RTT |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 102 | rankedFaces.insert({info, &hopFace}); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (rankedFaces.empty()) { |
| 106 | // No Face to probe |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
| 110 | return getFaceBasedOnProbability(rankedFaces); |
| 111 | } |
| 112 | |
| 113 | bool |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 114 | ProbingModule::isProbingNeeded(const fib::Entry& fibEntry, const Interest& interest) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 115 | { |
| 116 | // Return the probing status flag for a namespace |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 117 | NamespaceInfo& info = m_measurements.getOrCreateNamespaceInfo(fibEntry, interest); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 118 | |
| 119 | // If a first probe has not been scheduled for a namespace |
| 120 | if (!info.isFirstProbeScheduled()) { |
| 121 | // Schedule first probe between 0 and 5 seconds |
| 122 | uint64_t interval = getRandomNumber(0, 5000); |
| 123 | scheduleProbe(fibEntry, time::milliseconds(interval)); |
| 124 | |
| 125 | info.setHasFirstProbeBeenScheduled(true); |
| 126 | } |
| 127 | |
| 128 | return info.isProbingDue(); |
| 129 | } |
| 130 | |
| 131 | void |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 132 | ProbingModule::afterForwardingProbe(const fib::Entry& fibEntry, const Interest& interest) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 133 | { |
| 134 | // After probing is done, need to set probing flag to false and |
| 135 | // schedule another future probe |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 136 | NamespaceInfo& info = m_measurements.getOrCreateNamespaceInfo(fibEntry, interest); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 137 | info.setIsProbingDue(false); |
| 138 | |
| 139 | scheduleProbe(fibEntry, m_probingInterval); |
| 140 | } |
| 141 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 142 | Face* |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 143 | ProbingModule::getFaceBasedOnProbability(const FaceInfoFacePairSet& rankedFaces) |
| 144 | { |
| 145 | double randomNumber = getRandomNumber(0, 1); |
| 146 | uint64_t rankSum = ((rankedFaces.size() + 1) * rankedFaces.size()) / 2; |
| 147 | |
| 148 | uint64_t rank = 1; |
| 149 | double offset = 0.0; |
| 150 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 151 | for (const auto& pair : rankedFaces) { |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 152 | double probability = getProbingProbability(rank++, rankSum, rankedFaces.size()); |
| 153 | |
| 154 | // Is the random number within the bounds of this face's probability + the previous faces' |
| 155 | // probability? |
| 156 | // |
| 157 | // e.g. (FaceId: 1, p=0.5), (FaceId: 2, p=0.33), (FaceId: 3, p=0.17) |
| 158 | // randomNumber = 0.92 |
| 159 | // |
| 160 | // The face with FaceId: 3 should be picked |
| 161 | // (0.68 < 0.5 + 0.33 + 0.17) == true |
| 162 | // |
| 163 | if (randomNumber <= offset + probability) { |
| 164 | // Found face to probe |
| 165 | return pair.second; |
| 166 | } |
| 167 | |
| 168 | offset += probability; |
| 169 | } |
| 170 | |
| 171 | // Given a set of Faces, this method should always select a Face to probe |
| 172 | BOOST_ASSERT(false); |
| 173 | return nullptr; |
| 174 | } |
| 175 | |
| 176 | double |
| 177 | ProbingModule::getProbingProbability(uint64_t rank, uint64_t rankSum, uint64_t nFaces) |
| 178 | { |
| 179 | // p = n + 1 - j ; n: # faces |
| 180 | // --------- |
| 181 | // sum(ranks) |
| 182 | return static_cast<double>(nFaces + 1 - rank) / rankSum; |
| 183 | } |
| 184 | |
| 185 | double |
| 186 | ProbingModule::getRandomNumber(double start, double end) |
| 187 | { |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 188 | std::uniform_real_distribution<double> dist(start, end); |
| 189 | return dist(getGlobalRng()); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 192 | void |
| 193 | ProbingModule::setProbingInterval(size_t probingInterval) |
| 194 | { |
| 195 | if (time::milliseconds(probingInterval) >= MIN_PROBING_INTERVAL) { |
| 196 | m_probingInterval = time::milliseconds(probingInterval); |
| 197 | } |
| 198 | else { |
| 199 | BOOST_THROW_EXCEPTION(std::invalid_argument("Probing interval should be >= " |
| 200 | + to_string(MIN_PROBING_INTERVAL.count()) |
| 201 | + " milliseconds")); |
| 202 | } |
| 203 | } |
| 204 | |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 205 | } // namespace asf |
| 206 | } // namespace fw |
| 207 | } // namespace nfd |