blob: 36b1fc7a06963ecdb4900e1b58a3b0bb97bd1125 [file] [log] [blame]
Zhenkai Zhu8224bb62013-01-06 15:58:02 -08001/* -*- 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 *
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080018 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080020 */
21
22#include "sync-core.h"
23
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080024#include "one-time-task.h"
25#include "random-interval-generator.h"
26
Zhenkai Zhue851b952013-01-13 22:29:57 -080027const string SyncCore::RECOVER = "RECOVER";
Zhenkai Zhue573ae82013-01-15 13:15:52 -080028const double SyncCore::WAIT = 0.05;
Zhenkai Zhue851b952013-01-13 22:29:57 -080029const double SyncCore::RANDOM_PERCENT = 0.5;
30
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080031// for debugging
32static void
33printMsg(SyncStateMsgPtr &msg)
34{
35 cout << " ===== start Msg ======" << endl;
36 int size = msg->state_size();
37 if (size > 0)
38 {
39 int index = 0;
40 while (index < size)
41 {
42 SyncState state = msg->state(index);
43 string strName = state.name();
44 string strLocator = state.locator();
45 sqlite3_int64 seq = state.seq();
46 cout << "Name: " << Name((const unsigned char *)strName.c_str(), strName.size());
47 cout << ", Locator: " << Name((const unsigned char *)strLocator.c_str(), strLocator.size());
48 cout << ", seq: " << seq << endl;
49 index ++;
50 }
51 }
52 else
53 {
54 cout << "Msg size 0" << endl;
55 }
56 cout << " ++++++++ end Msg ++++++++ \n\n" << endl;
57}
58
Zhenkai Zhub330aed2013-01-17 13:29:37 -080059SyncCore::SyncCore(SyncLogPtr syncLog, const Name &userName, const Name &localPrefix, const Name &syncPrefix, const StateMsgCallback &callback, const CcnxWrapperPtr &handle, const SchedulerPtr &scheduler)
60 : m_log(syncLog)
61 , m_scheduler(scheduler)
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -080062 , m_stateMsgCallback(callback)
63 , m_userName(userName)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080064 , m_localPrefix(localPrefix)
65 , m_syncPrefix(syncPrefix)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080066 , m_handle(handle)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080067 , m_syncClosure (boost::bind(&SyncCore::handleSyncData, this, _1, _2),
68 boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1))
69 , m_recoverClosure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2),
70 boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1))
Zhenkai Zhue573ae82013-01-15 13:15:52 -080071 , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP))
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080072{
Zhenkai Zhub330aed2013-01-17 13:29:37 -080073 m_rootHash = m_log->RememberStateInStateLog();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080074
Zhenkai Zhue851b952013-01-13 22:29:57 -080075 m_handle->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1));
Alexander Afanasyevdac84922013-01-20 23:32:17 -080076 // m_log->initYP(m_yp);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080077 m_scheduler->start();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080078 sendSyncInterest();
79}
80
81SyncCore::~SyncCore()
82{
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080083 // need to "deregister" closures
Zhenkai Zhue851b952013-01-13 22:29:57 -080084}
85
Alexander Afanasyevdac84922013-01-20 23:32:17 -080086// Name
87// SyncCore::yp(const Name &deviceName)
88// {
89// Name locator;
90// ReadLock lock(m_ypMutex);
91// if (m_yp.find(deviceName) != m_yp.end())
92// {
93// locator = m_yp[deviceName];
94// }
95// else
96// {
97// cout << "self: " << m_userName << ", deviceName: " << deviceName << " not found in yp " << endl;
98// }
99// return locator;
100// }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800101
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800102// void
103// SyncCore::updateLocalPrefix(const Name &localPrefix)
104// {
105// m_localPrefix = localPrefix;
106// // optionally, we can have a sync action to announce the new prefix
107// // we are not doing this for now
108// }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800109
110void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800111SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800112{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800113 m_log->UpdateDeviceSeqNo(m_userName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800114 // choose to update locator everytime
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800115 m_log->UpdateLocator(m_userName, m_localPrefix);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800116 // {
117 // WriteLock lock(m_ypMutex);
118 // m_yp[m_userName] = m_localPrefix;
119 // }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800120 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800121 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800122
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800123 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800124
125 // reply sync Interest with oldHash as last component
126 Name syncName = constructSyncName(oldHash);
127 Bytes syncData;
128 msgToBytes(msg, syncData);
129 m_handle->publishData(syncName, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800130 cout << m_userName << " publishes: " << *oldHash << endl;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800131 printMsg(msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800132
133 // 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;
134 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers
135 ostringstream ss;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800136 ss << *m_rootHash;
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800137 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.1));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800138 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800139 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800140}
141
142void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800143SyncCore::handleInterest(const Name &name)
144{
145 int size = name.size();
146 int prefixSize = m_syncPrefix.size();
147 if (size == prefixSize + 1)
148 {
149 // this is normal sync interest
150 handleSyncInterest(name);
151 }
152 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
153 {
154 // this is recovery interest
155 handleRecoverInterest(name);
156 }
157}
158
159void
160SyncCore::handleRecoverInterest(const Name &name)
161{
162 Bytes hashBytes = name.getComp(name.size() - 1);
163 // this is the hash unkonwn to the sender of the interest
164 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800165 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800166 {
167 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800168 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800169 // DEBUG
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800170 /*
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800171 assert(msg->state_size() > 0);
172 int size = msg->state_size();
173 int index = 0;
174 cout << "Reply recover interest with: " << endl;
175 while (index < size)
176 {
177 SyncState state = msg->state(index);
178 string strName = state.name();
179 string strLoc = state.locator();
180 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;
181 if (state.type() == SyncState::UPDATE)
182 {
183 cout << "Action: update" << endl;
184 }
185 else
186 {
187 cout << "Action: delete" << endl;
188 }
189 index++;
190 }
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800191 */
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800192 // END DEBUG
Zhenkai Zhue851b952013-01-13 22:29:57 -0800193 Bytes syncData;
194 msgToBytes(msg, syncData);
195 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800196 cout << m_userName << " publishes " << hash << endl;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800197 printMsg(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800198 }
199 else
200 {
201 // we don't recognize this hash, can not help
202 }
203}
204
205void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800206SyncCore::handleSyncInterest(const Name &name)
207{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800208 Bytes hashBytes = name.getComp(name.size() - 1);
209 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
210 if (*hash == *m_rootHash)
211 {
212 // we have the same hash; nothing needs to be done
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800213 cout << "same as root hash: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800214 return;
215 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800216 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800217 {
218 // we know something more
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800219 cout << "found hash in sync log" << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800220 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800221
222 Bytes syncData;
223 msgToBytes(msg, syncData);
224 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800225 cout << m_userName << " publishes: " << *hash << endl;
226 printMsg(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800227 }
228 else
229 {
230 // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period
231 ostringstream ss;
232 ss << *hash;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800233 double wait = m_recoverWaitGenerator->nextInterval();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800234 cout << m_userName << ", rootHash: " << *m_rootHash << ", hash: " << *hash << endl;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800235 cout << "recover task scheduled after wait: " << wait << endl;
236 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::recover, this, hash), ss.str(), m_scheduler, wait));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800237 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800238
Zhenkai Zhue851b952013-01-13 22:29:57 -0800239 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800240}
241
242Closure::TimeoutCallbackReturnValue
243SyncCore::handleSyncInterestTimeout(const Name &name)
244{
245 // sendInterestInterest with the current root hash;
246 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800247 return Closure::RESULT_OK;
248}
249
250Closure::TimeoutCallbackReturnValue
251SyncCore::handleRecoverInterestTimeout(const Name &name)
252{
253 // We do not re-express recovery interest for now
254 // if difference is not resolved, the sync interest will trigger
255 // recovery anyway; so it's not so important to have recovery interest
256 // re-expressed
257 return Closure::RESULT_OK;
258}
259
260void
261SyncCore::handleRecoverData(const Name &name, const Bytes &content)
262{
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800263 //cout << "handle recover data" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800264 handleStateData(content);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800265 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800266}
267
268void
269SyncCore::handleSyncData(const Name &name, const Bytes &content)
270{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800271 // suppress recover in interest - data out of order case
272 handleStateData(content);
273
274 // resume outstanding sync interest
275 sendSyncInterest();
276}
277
278void
279SyncCore::handleStateData(const Bytes &content)
280{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800281 SyncStateMsgPtr msg(new SyncStateMsg);
282 bool success = msg->ParseFromArray(head(content), content.size());
283 if(!success)
284 {
285 // ignore misformed SyncData
Zhenkai Zhue851b952013-01-13 22:29:57 -0800286 cerr << "Misformed SyncData"<< endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800287 return;
288 }
289
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800290 cout << m_userName << " receives Msg " << endl;
291 printMsg (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800292 int size = msg->state_size();
293 int index = 0;
294 while (index < size)
295 {
296 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800297 string devStr = state.name();
298 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800299 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800300 if (state.type() == SyncState::UPDATE)
301 {
302 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800303 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800304 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800305 if (state.has_locator())
306 {
307 string locStr = state.locator();
308 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800309 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800310 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800311 // WriteLock lock(m_ypMutex);
312 // m_yp[deviceName] = locatorName;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800313 cout << "self: " << m_userName << ", device: " << deviceName << " < == > " << locatorName << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800314 }
315 }
316 else
317 {
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800318 cout << "nani" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800319 deregister(deviceName);
320 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800321 index++;
322 }
323
Zhenkai Zhue851b952013-01-13 22:29:57 -0800324 // find the actuall difference and invoke callback on the actual difference
325 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800326 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800327 // get diff with both new SeqNo and old SeqNo
328 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800329
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800330 if (diff->state_size() > 0)
331 {
332 m_stateMsgCallback(diff);
333 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800334}
335
336void
337SyncCore::sendSyncInterest()
338{
339 Name syncInterest = constructSyncName(m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800340 m_handle->sendInterest(syncInterest, m_syncClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800341 cout << m_userName << " send SYNC interest: " << *m_rootHash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800342}
343
344void
345SyncCore::recover(const HashPtr &hash)
346{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800347 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800348 {
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800349 cout << m_userName << ", Recover for: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800350 // unfortunately we still don't recognize this hash
351 Bytes bytes;
352 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
353 Name recoverInterest = m_syncPrefix;
354 recoverInterest.appendComp(RECOVER);
355 // append the unknown hash
356 recoverInterest.appendComp(bytes);
357 m_handle->sendInterest(recoverInterest, m_recoverClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800358 cout << m_userName << " send RECOVER Interest: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800359 }
360 else
361 {
362 // we already learned the hash; cheers!
363 }
364}
365
366void
367SyncCore::deregister(const Name &name)
368{
369 // Do nothing for now
370 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800371}
372
373Name
374SyncCore::constructSyncName(const HashPtr &hash)
375{
376 Bytes bytes;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800377 readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes());
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800378 Name syncName = m_syncPrefix;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800379 syncName.appendComp(bytes);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800380 return syncName;
381}
382
383void
384SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
385{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800386 int size = msg->ByteSize();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800387 bytes.resize(size);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800388 msg->SerializeToArray(head(bytes), size);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800389}
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800390
391sqlite3_int64
392SyncCore::seq(const Name &name)
393{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800394 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800395}