blob: 1e9a9478f220e8e2fc6746ae0c19ed1c5cf977cc [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)
Zhenkai Zhue573ae82013-01-15 13:15:52 -080067 , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP))
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080068{
Zhenkai Zhub330aed2013-01-17 13:29:37 -080069 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu8339e912013-01-18 18:10:17 -080070 m_syncClosure = new Closure(boost::bind(&SyncCore::handleSyncData, this, _1, _2), boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1));
71 m_recoverClosure = new Closure(boost::bind(&SyncCore::handleRecoverData, this, _1, _2), boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1));
Zhenkai Zhue851b952013-01-13 22:29:57 -080072 m_handle->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1));
Zhenkai Zhub330aed2013-01-17 13:29:37 -080073 m_log->initYP(m_yp);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080074 m_scheduler->start();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080075 sendSyncInterest();
76}
77
78SyncCore::~SyncCore()
79{
Zhenkai Zhue851b952013-01-13 22:29:57 -080080 if (m_syncClosure != 0)
81 {
82 delete m_syncClosure;
83 m_syncClosure = 0;
84 }
85
86 if (m_recoverClosure != 0)
87 {
88 delete m_recoverClosure;
89 m_recoverClosure = 0;
90 }
91}
92
93Name
94SyncCore::yp(const Name &deviceName)
95{
96 Name locator;
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080097 ReadLock lock(m_ypMutex);
Zhenkai Zhue851b952013-01-13 22:29:57 -080098 if (m_yp.find(deviceName) != m_yp.end())
99 {
100 locator = m_yp[deviceName];
101 }
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800102 else
103 {
104 cout << "self: " << m_userName << ", deviceName: " << deviceName << " not found in yp " << endl;
105 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800106 return locator;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800107}
108
109void
110SyncCore::updateLocalPrefix(const Name &localPrefix)
111{
112 m_localPrefix = localPrefix;
113 // optionally, we can have a sync action to announce the new prefix
114 // we are not doing this for now
115}
116
117void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800118SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800119{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800120 m_log->UpdateDeviceSeqNo(m_userName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800121 // choose to update locator everytime
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800122 m_log->UpdateLocator(m_userName, m_localPrefix);
Zhenkai Zhu9f6c41e2013-01-17 14:18:14 -0800123 {
124 WriteLock lock(m_ypMutex);
125 m_yp[m_userName] = m_localPrefix;
126 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800127 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800128 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800129
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800130 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800131
132 // reply sync Interest with oldHash as last component
133 Name syncName = constructSyncName(oldHash);
134 Bytes syncData;
135 msgToBytes(msg, syncData);
136 m_handle->publishData(syncName, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800137 cout << m_userName << " publishes: " << *oldHash << endl;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800138 printMsg(msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800139
140 // 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;
141 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers
142 ostringstream ss;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800143 ss << *m_rootHash;
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800144 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.1));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800145 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800146 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800147}
148
149void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800150SyncCore::handleInterest(const Name &name)
151{
152 int size = name.size();
153 int prefixSize = m_syncPrefix.size();
154 if (size == prefixSize + 1)
155 {
156 // this is normal sync interest
157 handleSyncInterest(name);
158 }
159 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
160 {
161 // this is recovery interest
162 handleRecoverInterest(name);
163 }
164}
165
166void
167SyncCore::handleRecoverInterest(const Name &name)
168{
169 Bytes hashBytes = name.getComp(name.size() - 1);
170 // this is the hash unkonwn to the sender of the interest
171 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800172 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800173 {
174 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800175 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800176 // DEBUG
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800177 /*
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800178 assert(msg->state_size() > 0);
179 int size = msg->state_size();
180 int index = 0;
181 cout << "Reply recover interest with: " << endl;
182 while (index < size)
183 {
184 SyncState state = msg->state(index);
185 string strName = state.name();
186 string strLoc = state.locator();
187 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;
188 if (state.type() == SyncState::UPDATE)
189 {
190 cout << "Action: update" << endl;
191 }
192 else
193 {
194 cout << "Action: delete" << endl;
195 }
196 index++;
197 }
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800198 */
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800199 // END DEBUG
Zhenkai Zhue851b952013-01-13 22:29:57 -0800200 Bytes syncData;
201 msgToBytes(msg, syncData);
202 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800203 cout << m_userName << " publishes " << hash << endl;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800204 printMsg(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800205 }
206 else
207 {
208 // we don't recognize this hash, can not help
209 }
210}
211
212void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800213SyncCore::handleSyncInterest(const Name &name)
214{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800215 Bytes hashBytes = name.getComp(name.size() - 1);
216 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
217 if (*hash == *m_rootHash)
218 {
219 // we have the same hash; nothing needs to be done
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800220 cout << "same as root hash: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800221 return;
222 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800223 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800224 {
225 // we know something more
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800226 cout << "found hash in sync log" << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800227 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800228
229 Bytes syncData;
230 msgToBytes(msg, syncData);
231 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800232 cout << m_userName << " publishes: " << *hash << endl;
233 printMsg(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800234 }
235 else
236 {
237 // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period
238 ostringstream ss;
239 ss << *hash;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800240 double wait = m_recoverWaitGenerator->nextInterval();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800241 cout << m_userName << ", rootHash: " << *m_rootHash << ", hash: " << *hash << endl;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800242 cout << "recover task scheduled after wait: " << wait << endl;
243 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::recover, this, hash), ss.str(), m_scheduler, wait));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800244 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800245
Zhenkai Zhue851b952013-01-13 22:29:57 -0800246 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800247}
248
249Closure::TimeoutCallbackReturnValue
250SyncCore::handleSyncInterestTimeout(const Name &name)
251{
252 // sendInterestInterest with the current root hash;
253 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800254 return Closure::RESULT_OK;
255}
256
257Closure::TimeoutCallbackReturnValue
258SyncCore::handleRecoverInterestTimeout(const Name &name)
259{
260 // We do not re-express recovery interest for now
261 // if difference is not resolved, the sync interest will trigger
262 // recovery anyway; so it's not so important to have recovery interest
263 // re-expressed
264 return Closure::RESULT_OK;
265}
266
267void
268SyncCore::handleRecoverData(const Name &name, const Bytes &content)
269{
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800270 //cout << "handle recover data" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800271 handleStateData(content);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800272 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800273}
274
275void
276SyncCore::handleSyncData(const Name &name, const Bytes &content)
277{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800278 // suppress recover in interest - data out of order case
279 handleStateData(content);
280
281 // resume outstanding sync interest
282 sendSyncInterest();
283}
284
285void
286SyncCore::handleStateData(const Bytes &content)
287{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800288 SyncStateMsgPtr msg(new SyncStateMsg);
289 bool success = msg->ParseFromArray(head(content), content.size());
290 if(!success)
291 {
292 // ignore misformed SyncData
Zhenkai Zhue851b952013-01-13 22:29:57 -0800293 cerr << "Misformed SyncData"<< endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800294 return;
295 }
296
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800297 cout << m_userName << " receives Msg " << endl;
298 printMsg (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800299 int size = msg->state_size();
300 int index = 0;
301 while (index < size)
302 {
303 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800304 string devStr = state.name();
305 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800306 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800307 if (state.type() == SyncState::UPDATE)
308 {
309 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800310 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800311 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800312 if (state.has_locator())
313 {
314 string locStr = state.locator();
315 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800316 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800317 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800318 WriteLock lock(m_ypMutex);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800319 m_yp[deviceName] = locatorName;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800320 cout << "self: " << m_userName << ", device: " << deviceName << " < == > " << locatorName << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800321 }
322 }
323 else
324 {
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800325 cout << "nani" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800326 deregister(deviceName);
327 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800328 index++;
329 }
330
Zhenkai Zhue851b952013-01-13 22:29:57 -0800331 // find the actuall difference and invoke callback on the actual difference
332 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800333 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800334 // get diff with both new SeqNo and old SeqNo
335 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800336
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800337 if (diff->state_size() > 0)
338 {
339 m_stateMsgCallback(diff);
340 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800341}
342
343void
344SyncCore::sendSyncInterest()
345{
346 Name syncInterest = constructSyncName(m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800347 m_handle->sendInterest(syncInterest, m_syncClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800348 cout << m_userName << " send SYNC interest: " << *m_rootHash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800349}
350
351void
352SyncCore::recover(const HashPtr &hash)
353{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800354 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800355 {
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800356 cout << m_userName << ", Recover for: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800357 // unfortunately we still don't recognize this hash
358 Bytes bytes;
359 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
360 Name recoverInterest = m_syncPrefix;
361 recoverInterest.appendComp(RECOVER);
362 // append the unknown hash
363 recoverInterest.appendComp(bytes);
364 m_handle->sendInterest(recoverInterest, m_recoverClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800365 cout << m_userName << " send RECOVER Interest: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800366 }
367 else
368 {
369 // we already learned the hash; cheers!
370 }
371}
372
373void
374SyncCore::deregister(const Name &name)
375{
376 // Do nothing for now
377 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800378}
379
380Name
381SyncCore::constructSyncName(const HashPtr &hash)
382{
383 Bytes bytes;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800384 readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes());
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800385 Name syncName = m_syncPrefix;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800386 syncName.appendComp(bytes);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800387 return syncName;
388}
389
390void
391SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
392{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800393 int size = msg->ByteSize();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800394 bytes.resize(size);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800395 msg->SerializeToArray(head(bytes), size);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800396}
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800397
398sqlite3_int64
399SyncCore::seq(const Name &name)
400{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800401 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800402}