blob: d90c463b6800fcacca6d4f49a38d5f0b62099cd8 [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
Zhenkai Zhue851b952013-01-13 22:29:57 -080024const string SyncCore::RECOVER = "RECOVER";
Zhenkai Zhue573ae82013-01-15 13:15:52 -080025const double SyncCore::WAIT = 0.05;
Zhenkai Zhue851b952013-01-13 22:29:57 -080026const double SyncCore::RANDOM_PERCENT = 0.5;
27
Zhenkai Zhue573ae82013-01-15 13:15:52 -080028SyncCore::SyncCore(const string &path, const Name &userName, const Name &localPrefix, const Name &syncPrefix, const StateMsgCallback &callback, CcnxWrapperPtr handle)
Zhenkai Zhue851b952013-01-13 22:29:57 -080029 : m_log(path, userName.toString())
Zhenkai Zhue573ae82013-01-15 13:15:52 -080030 , m_scheduler(new Scheduler())
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -080031 , m_stateMsgCallback(callback)
32 , m_userName(userName)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080033 , m_localPrefix(localPrefix)
34 , m_syncPrefix(syncPrefix)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080035 , m_handle(handle)
Zhenkai Zhue573ae82013-01-15 13:15:52 -080036 , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP))
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080037{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080038 m_rootHash = m_log.RememberStateInStateLog();
Zhenkai Zhue851b952013-01-13 22:29:57 -080039 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 Zhue573ae82013-01-15 13:15:52 -080043 m_scheduler->start();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080044 sendSyncInterest();
45}
46
47SyncCore::~SyncCore()
48{
Zhenkai Zhue573ae82013-01-15 13:15:52 -080049 m_scheduler->shutdown();
Zhenkai Zhue851b952013-01-13 22:29:57 -080050 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
63Name
64SyncCore::yp(const Name &deviceName)
65{
66 Name locator;
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080067 ReadLock lock(m_ypMutex);
Zhenkai Zhue851b952013-01-13 22:29:57 -080068 if (m_yp.find(deviceName) != m_yp.end())
69 {
70 locator = m_yp[deviceName];
71 }
72 return locator;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080073}
74
75void
76SyncCore::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
83void
Zhenkai Zhue851b952013-01-13 22:29:57 -080084SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080085{
Zhenkai Zhue851b952013-01-13 22:29:57 -080086 m_log.UpdateDeviceSeqNo(m_userName, seqno);
87 // choose to update locator everytime
88 m_log.UpdateLocator(m_userName, m_localPrefix);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080089 HashPtr oldHash = m_rootHash;
90 m_rootHash = m_log.RememberStateInStateLog();
91
Zhenkai Zhue851b952013-01-13 22:29:57 -080092 SyncStateMsgPtr msg = m_log.FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080093
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);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080099 cout << m_userName << " publishes: " << *oldHash << endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800100
101 // 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;
102 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers
103 ostringstream ss;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800104 ss << *m_rootHash;
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800105 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.1));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800106 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800107 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800108}
109
110void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800111SyncCore::handleInterest(const Name &name)
112{
113 int size = name.size();
114 int prefixSize = m_syncPrefix.size();
115 if (size == prefixSize + 1)
116 {
117 // this is normal sync interest
118 handleSyncInterest(name);
119 }
120 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
121 {
122 // this is recovery interest
123 handleRecoverInterest(name);
124 }
125}
126
127void
128SyncCore::handleRecoverInterest(const Name &name)
129{
130 Bytes hashBytes = name.getComp(name.size() - 1);
131 // this is the hash unkonwn to the sender of the interest
132 Hash hash(head(hashBytes), hashBytes.size());
133 if (m_log.LookupSyncLog(hash) > 0)
134 {
135 // we know the hash, should reply everything
136 SyncStateMsgPtr msg = m_log.FindStateDifferences(*(Hash::Origin), *m_rootHash);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800137 // DEBUG
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800138 /*
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800139 assert(msg->state_size() > 0);
140 int size = msg->state_size();
141 int index = 0;
142 cout << "Reply recover interest with: " << endl;
143 while (index < size)
144 {
145 SyncState state = msg->state(index);
146 string strName = state.name();
147 string strLoc = state.locator();
148 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;
149 if (state.type() == SyncState::UPDATE)
150 {
151 cout << "Action: update" << endl;
152 }
153 else
154 {
155 cout << "Action: delete" << endl;
156 }
157 index++;
158 }
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800159 */
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800160 // END DEBUG
Zhenkai Zhue851b952013-01-13 22:29:57 -0800161 Bytes syncData;
162 msgToBytes(msg, syncData);
163 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800164 cout << m_userName << " publishes " << hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800165 }
166 else
167 {
168 // we don't recognize this hash, can not help
169 }
170}
171
172void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800173SyncCore::handleSyncInterest(const Name &name)
174{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800175 Bytes hashBytes = name.getComp(name.size() - 1);
176 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
177 if (*hash == *m_rootHash)
178 {
179 // we have the same hash; nothing needs to be done
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800180 cout << "same as root hash: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800181 return;
182 }
183 else if (m_log.LookupSyncLog(*hash) > 0)
184 {
185 // we know something more
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800186 cout << "found hash in sync log" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800187 SyncStateMsgPtr msg = m_log.FindStateDifferences(*hash, *m_rootHash);
188
189 Bytes syncData;
190 msgToBytes(msg, syncData);
191 m_handle->publishData(name, syncData, FRESHNESS);
192 }
193 else
194 {
195 // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period
196 ostringstream ss;
197 ss << *hash;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800198 double wait = m_recoverWaitGenerator->nextInterval();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800199 cout << m_userName << ", rootHash: " << *m_rootHash << ", hash: " << *hash << endl;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800200 cout << "recover task scheduled after wait: " << wait << endl;
201 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::recover, this, hash), ss.str(), m_scheduler, wait));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800202 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800203
Zhenkai Zhue851b952013-01-13 22:29:57 -0800204 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800205}
206
207Closure::TimeoutCallbackReturnValue
208SyncCore::handleSyncInterestTimeout(const Name &name)
209{
210 // sendInterestInterest with the current root hash;
211 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800212 return Closure::RESULT_OK;
213}
214
215Closure::TimeoutCallbackReturnValue
216SyncCore::handleRecoverInterestTimeout(const Name &name)
217{
218 // We do not re-express recovery interest for now
219 // if difference is not resolved, the sync interest will trigger
220 // recovery anyway; so it's not so important to have recovery interest
221 // re-expressed
222 return Closure::RESULT_OK;
223}
224
225void
226SyncCore::handleRecoverData(const Name &name, const Bytes &content)
227{
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800228 //cout << "handle recover data" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800229 handleStateData(content);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800230 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800231}
232
233void
234SyncCore::handleSyncData(const Name &name, const Bytes &content)
235{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800236 // suppress recover in interest - data out of order case
237 handleStateData(content);
238
239 // resume outstanding sync interest
240 sendSyncInterest();
241}
242
243void
244SyncCore::handleStateData(const Bytes &content)
245{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800246 SyncStateMsgPtr msg(new SyncStateMsg);
247 bool success = msg->ParseFromArray(head(content), content.size());
248 if(!success)
249 {
250 // ignore misformed SyncData
Zhenkai Zhue851b952013-01-13 22:29:57 -0800251 cerr << "Misformed SyncData"<< endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800252 return;
253 }
254
255 int size = msg->state_size();
256 int index = 0;
257 while (index < size)
258 {
259 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800260 string devStr = state.name();
261 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800262 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800263 if (state.type() == SyncState::UPDATE)
264 {
265 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800266 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800267 m_log.UpdateDeviceSeqNo(deviceName, seqno);
268 if (state.has_locator())
269 {
270 string locStr = state.locator();
271 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800272 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800273 m_log.UpdateLocator(deviceName, locatorName);
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800274 WriteLock lock(m_ypMutex);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800275 m_yp[deviceName] = locatorName;
276 }
277 }
278 else
279 {
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800280 cout << "nani" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800281 deregister(deviceName);
282 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800283 index++;
284 }
285
Zhenkai Zhue851b952013-01-13 22:29:57 -0800286 // find the actuall difference and invoke callback on the actual difference
287 HashPtr oldHash = m_rootHash;
288 m_rootHash = m_log.RememberStateInStateLog();
289 SyncStateMsgPtr diff = m_log.FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800290
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800291 if (diff->state_size() > 0)
292 {
293 m_stateMsgCallback(diff);
294 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800295}
296
297void
298SyncCore::sendSyncInterest()
299{
300 Name syncInterest = constructSyncName(m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800301 m_handle->sendInterest(syncInterest, m_syncClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800302 cout << m_userName << " send SYNC interest: " << *m_rootHash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800303}
304
305void
306SyncCore::recover(const HashPtr &hash)
307{
308 if (!(*hash == *m_rootHash) && m_log.LookupSyncLog(*hash) <= 0)
309 {
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800310 cout << m_userName << ", Recover for: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800311 // unfortunately we still don't recognize this hash
312 Bytes bytes;
313 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
314 Name recoverInterest = m_syncPrefix;
315 recoverInterest.appendComp(RECOVER);
316 // append the unknown hash
317 recoverInterest.appendComp(bytes);
318 m_handle->sendInterest(recoverInterest, m_recoverClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800319 cout << m_userName << " send RECOVER Interest: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800320 }
321 else
322 {
323 // we already learned the hash; cheers!
324 }
325}
326
327void
328SyncCore::deregister(const Name &name)
329{
330 // Do nothing for now
331 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800332}
333
334Name
335SyncCore::constructSyncName(const HashPtr &hash)
336{
337 Bytes bytes;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800338 readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes());
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800339 Name syncName = m_syncPrefix;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800340 syncName.appendComp(bytes);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800341 return syncName;
342}
343
344void
345SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
346{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800347 int size = msg->ByteSize();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800348 bytes.resize(size);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800349 msg->SerializeToArray(head(bytes), size);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800350}
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800351
352sqlite3_int64
353SyncCore::seq(const Name &name)
354{
355 return m_log.SeqNo(name);
356}