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