Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 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 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "sync-core.h" |
| 23 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 24 | const string SyncCore::RECOVER = "RECOVER"; |
| 25 | const double SyncCore::WAIT = 0.2; |
| 26 | const double SyncCore::RANDOM_PERCENT = 0.5; |
| 27 | |
| 28 | SyncCore::SyncCore(const string &path, const Name &userName, const Name &localPrefix, const Name &syncPrefix, const StateMsgCallback &callback, CcnxWrapperPtr handle, SchedulerPtr scheduler) |
| 29 | : m_log(path, userName.toString()) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 30 | , m_localPrefix(localPrefix) |
| 31 | , m_syncPrefix(syncPrefix) |
| 32 | , m_stateMsgCallback(callback) |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 33 | , m_handle(handle) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 34 | , m_scheduler(scheduler) |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 35 | { |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 36 | m_rootHash = m_log.RememberStateInStateLog(); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 37 | m_syncClosure = new Closure(0, boost::bind(&SyncCore::handleSyncData, this, _1, _2), boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1)); |
| 38 | m_recoverClosure = new Closure(0, boost::bind(&SyncCore::handleRecoverData, this, _1, _2), boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1)); |
| 39 | m_handle->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1)); |
| 40 | m_log.initYP(m_yp); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 41 | m_scheduler->start(); |
| 42 | sendSyncInterest(); |
| 43 | } |
| 44 | |
| 45 | SyncCore::~SyncCore() |
| 46 | { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 47 | m_scheduler->shutdown(); |
| 48 | if (m_syncClosure != 0) |
| 49 | { |
| 50 | delete m_syncClosure; |
| 51 | m_syncClosure = 0; |
| 52 | } |
| 53 | |
| 54 | if (m_recoverClosure != 0) |
| 55 | { |
| 56 | delete m_recoverClosure; |
| 57 | m_recoverClosure = 0; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | Name |
| 62 | SyncCore::yp(const Name &deviceName) |
| 63 | { |
| 64 | Name locator; |
| 65 | ReadLock(m_ypMutex); |
| 66 | if (m_yp.find(deviceName) != m_yp.end()) |
| 67 | { |
| 68 | locator = m_yp[deviceName]; |
| 69 | } |
| 70 | return locator; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
| 74 | SyncCore::updateLocalPrefix(const Name &localPrefix) |
| 75 | { |
| 76 | m_localPrefix = localPrefix; |
| 77 | // optionally, we can have a sync action to announce the new prefix |
| 78 | // we are not doing this for now |
| 79 | } |
| 80 | |
| 81 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 82 | SyncCore::updateLocalState(sqlite3_int64 seqno) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 83 | { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 84 | m_log.UpdateDeviceSeqNo(m_userName, seqno); |
| 85 | // choose to update locator everytime |
| 86 | m_log.UpdateLocator(m_userName, m_localPrefix); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 87 | HashPtr oldHash = m_rootHash; |
| 88 | m_rootHash = m_log.RememberStateInStateLog(); |
| 89 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 90 | SyncStateMsgPtr msg = m_log.FindStateDifferences(*oldHash, *m_rootHash); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 91 | |
| 92 | // reply sync Interest with oldHash as last component |
| 93 | Name syncName = constructSyncName(oldHash); |
| 94 | Bytes syncData; |
| 95 | msgToBytes(msg, syncData); |
| 96 | m_handle->publishData(syncName, syncData, FRESHNESS); |
| 97 | |
| 98 | // no hurry in sending out new Sync Interest; if others send the new Sync Interest first, no problem, we know the new root hash already; |
| 99 | // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers |
| 100 | ostringstream ss; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 101 | ss << *m_rootHash; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 102 | TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.05)); |
| 103 | m_scheduler->addTask(task); |
| 104 | } |
| 105 | |
| 106 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 107 | SyncCore::handleInterest(const Name &name) |
| 108 | { |
| 109 | int size = name.size(); |
| 110 | int prefixSize = m_syncPrefix.size(); |
| 111 | if (size == prefixSize + 1) |
| 112 | { |
| 113 | // this is normal sync interest |
| 114 | handleSyncInterest(name); |
| 115 | } |
| 116 | else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER) |
| 117 | { |
| 118 | // this is recovery interest |
| 119 | handleRecoverInterest(name); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | SyncCore::handleRecoverInterest(const Name &name) |
| 125 | { |
| 126 | Bytes hashBytes = name.getComp(name.size() - 1); |
| 127 | // this is the hash unkonwn to the sender of the interest |
| 128 | Hash hash(head(hashBytes), hashBytes.size()); |
| 129 | if (m_log.LookupSyncLog(hash) > 0) |
| 130 | { |
| 131 | // we know the hash, should reply everything |
| 132 | SyncStateMsgPtr msg = m_log.FindStateDifferences(*(Hash::Origin), *m_rootHash); |
| 133 | Bytes syncData; |
| 134 | msgToBytes(msg, syncData); |
| 135 | m_handle->publishData(name, syncData, FRESHNESS); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | // we don't recognize this hash, can not help |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 144 | SyncCore::handleSyncInterest(const Name &name) |
| 145 | { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 146 | Bytes hashBytes = name.getComp(name.size() - 1); |
| 147 | HashPtr hash(new Hash(head(hashBytes), hashBytes.size())); |
| 148 | if (*hash == *m_rootHash) |
| 149 | { |
| 150 | // we have the same hash; nothing needs to be done |
| 151 | return; |
| 152 | } |
| 153 | else if (m_log.LookupSyncLog(*hash) > 0) |
| 154 | { |
| 155 | // we know something more |
| 156 | SyncStateMsgPtr msg = m_log.FindStateDifferences(*hash, *m_rootHash); |
| 157 | |
| 158 | Bytes syncData; |
| 159 | msgToBytes(msg, syncData); |
| 160 | m_handle->publishData(name, syncData, FRESHNESS); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period |
| 165 | ostringstream ss; |
| 166 | ss << *hash; |
| 167 | IntervalGeneratorPtr generator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP)); |
| 168 | TaskPtr task(new PeriodicTask(boost::bind(&SyncCore::recover, this, hash), ss.str(), m_scheduler, generator, 1)); |
| 169 | m_scheduler->addTask(task); |
| 170 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | Closure::TimeoutCallbackReturnValue |
| 174 | SyncCore::handleSyncInterestTimeout(const Name &name) |
| 175 | { |
| 176 | // sendInterestInterest with the current root hash; |
| 177 | sendSyncInterest(); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 178 | return Closure::RESULT_OK; |
| 179 | } |
| 180 | |
| 181 | Closure::TimeoutCallbackReturnValue |
| 182 | SyncCore::handleRecoverInterestTimeout(const Name &name) |
| 183 | { |
| 184 | // We do not re-express recovery interest for now |
| 185 | // if difference is not resolved, the sync interest will trigger |
| 186 | // recovery anyway; so it's not so important to have recovery interest |
| 187 | // re-expressed |
| 188 | return Closure::RESULT_OK; |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | SyncCore::handleRecoverData(const Name &name, const Bytes &content) |
| 193 | { |
| 194 | handleStateData(content); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void |
| 198 | SyncCore::handleSyncData(const Name &name, const Bytes &content) |
| 199 | { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 200 | // suppress recover in interest - data out of order case |
| 201 | handleStateData(content); |
| 202 | |
| 203 | // resume outstanding sync interest |
| 204 | sendSyncInterest(); |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | SyncCore::handleStateData(const Bytes &content) |
| 209 | { |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 210 | SyncStateMsgPtr msg(new SyncStateMsg); |
| 211 | bool success = msg->ParseFromArray(head(content), content.size()); |
| 212 | if(!success) |
| 213 | { |
| 214 | // ignore misformed SyncData |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 215 | cerr << "Misformed SyncData"<< endl; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 216 | return; |
| 217 | } |
| 218 | |
| 219 | int size = msg->state_size(); |
| 220 | int index = 0; |
| 221 | while (index < size) |
| 222 | { |
| 223 | SyncState state = msg->state(index); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 224 | string devStr = state.name(); |
| 225 | Name deviceName((const unsigned char *)devStr.c_str(), devStr.size()); |
| 226 | if (state.type() == SyncState::UPDATE) |
| 227 | { |
| 228 | sqlite3_int64 seqno = state.seq(); |
| 229 | m_log.UpdateDeviceSeqNo(deviceName, seqno); |
| 230 | if (state.has_locator()) |
| 231 | { |
| 232 | string locStr = state.locator(); |
| 233 | Name locatorName((const unsigned char *)locStr.c_str(), locStr.size()); |
| 234 | m_log.UpdateLocator(deviceName, locatorName); |
| 235 | WriteLock(m_ypMutex); |
| 236 | m_yp[deviceName] = locatorName; |
| 237 | } |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | deregister(deviceName); |
| 242 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 243 | index++; |
| 244 | } |
| 245 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 246 | // find the actuall difference and invoke callback on the actual difference |
| 247 | HashPtr oldHash = m_rootHash; |
| 248 | m_rootHash = m_log.RememberStateInStateLog(); |
| 249 | SyncStateMsgPtr diff = m_log.FindStateDifferences(*oldHash, *m_rootHash); |
| 250 | m_stateMsgCallback(diff); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void |
| 254 | SyncCore::sendSyncInterest() |
| 255 | { |
| 256 | Name syncInterest = constructSyncName(m_rootHash); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 257 | m_handle->sendInterest(syncInterest, m_syncClosure); |
| 258 | } |
| 259 | |
| 260 | void |
| 261 | SyncCore::recover(const HashPtr &hash) |
| 262 | { |
| 263 | if (!(*hash == *m_rootHash) && m_log.LookupSyncLog(*hash) <= 0) |
| 264 | { |
| 265 | // unfortunately we still don't recognize this hash |
| 266 | Bytes bytes; |
| 267 | readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes()); |
| 268 | Name recoverInterest = m_syncPrefix; |
| 269 | recoverInterest.appendComp(RECOVER); |
| 270 | // append the unknown hash |
| 271 | recoverInterest.appendComp(bytes); |
| 272 | m_handle->sendInterest(recoverInterest, m_recoverClosure); |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | // we already learned the hash; cheers! |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void |
| 281 | SyncCore::deregister(const Name &name) |
| 282 | { |
| 283 | // Do nothing for now |
| 284 | // TODO: handle deregistering |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | Name |
| 288 | SyncCore::constructSyncName(const HashPtr &hash) |
| 289 | { |
| 290 | Bytes bytes; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 291 | readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes()); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 292 | Name syncName = m_syncPrefix; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 293 | syncName.appendComp(bytes); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 294 | return syncName; |
| 295 | } |
| 296 | |
| 297 | void |
| 298 | SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes) |
| 299 | { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 300 | int size = msg->ByteSize(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 301 | bytes.resize(size); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 302 | msg->SerializeToArray(head(bytes), size); |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 303 | } |