blob: 309dd582c3fb467f3c5ceed169d856be7c03f7da [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 *
18 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
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;
67 ReadLock(m_ypMutex);
68 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);
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 Zhue851b952013-01-13 22:29:57 -0800103 ss << *m_rootHash;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800104 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.05));
105 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800106 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800107}
108
109void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800110SyncCore::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
126void
127SyncCore::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 Zhue573ae82013-01-15 13:15:52 -0800136 // 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 Zhue851b952013-01-13 22:29:57 -0800158 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
168void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800169SyncCore::handleSyncInterest(const Name &name)
170{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800171 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 Zhue573ae82013-01-15 13:15:52 -0800176 cout << "same as root hash: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800177 return;
178 }
179 else if (m_log.LookupSyncLog(*hash) > 0)
180 {
181 // we know something more
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800182 cout << "found hash in sync log" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800183 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 Zhue573ae82013-01-15 13:15:52 -0800194 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 Zhue851b952013-01-13 22:29:57 -0800197 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800198
Zhenkai Zhue851b952013-01-13 22:29:57 -0800199 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800200}
201
202Closure::TimeoutCallbackReturnValue
203SyncCore::handleSyncInterestTimeout(const Name &name)
204{
205 // sendInterestInterest with the current root hash;
206 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800207 return Closure::RESULT_OK;
208}
209
210Closure::TimeoutCallbackReturnValue
211SyncCore::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
220void
221SyncCore::handleRecoverData(const Name &name, const Bytes &content)
222{
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800223 cout << "handle recover data" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800224 handleStateData(content);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800225}
226
227void
228SyncCore::handleSyncData(const Name &name, const Bytes &content)
229{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800230 // suppress recover in interest - data out of order case
231 handleStateData(content);
232
233 // resume outstanding sync interest
234 sendSyncInterest();
235}
236
237void
238SyncCore::handleStateData(const Bytes &content)
239{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800240 SyncStateMsgPtr msg(new SyncStateMsg);
241 bool success = msg->ParseFromArray(head(content), content.size());
242 if(!success)
243 {
244 // ignore misformed SyncData
Zhenkai Zhue851b952013-01-13 22:29:57 -0800245 cerr << "Misformed SyncData"<< endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800246 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 Zhue851b952013-01-13 22:29:57 -0800254 string devStr = state.name();
255 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800256 cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800257 if (state.type() == SyncState::UPDATE)
258 {
259 sqlite3_int64 seqno = state.seq();
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800260 cout << ", Got seq: " << seqno << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800261 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 Zhue573ae82013-01-15 13:15:52 -0800266 cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800267 m_log.UpdateLocator(deviceName, locatorName);
268 WriteLock(m_ypMutex);
269 m_yp[deviceName] = locatorName;
270 }
271 }
272 else
273 {
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800274 cout << "nani" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800275 deregister(deviceName);
276 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800277 index++;
278 }
279
Zhenkai Zhue851b952013-01-13 22:29:57 -0800280 // 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 Zhue573ae82013-01-15 13:15:52 -0800284 cout << "OldHash: " << *oldHash << ", Newhash: " << *m_rootHash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800285 m_stateMsgCallback(diff);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800286}
287
288void
289SyncCore::sendSyncInterest()
290{
291 Name syncInterest = constructSyncName(m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800292 m_handle->sendInterest(syncInterest, m_syncClosure);
293}
294
295void
296SyncCore::recover(const HashPtr &hash)
297{
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800298 cout << "Recover for: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800299 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
316void
317SyncCore::deregister(const Name &name)
318{
319 // Do nothing for now
320 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800321}
322
323Name
324SyncCore::constructSyncName(const HashPtr &hash)
325{
326 Bytes bytes;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800327 readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes());
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800328 Name syncName = m_syncPrefix;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800329 syncName.appendComp(bytes);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800330 return syncName;
331}
332
333void
334SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
335{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800336 int size = msg->ByteSize();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800337 bytes.resize(size);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800338 msg->SerializeToArray(head(bytes), size);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800339}