blob: 79226b216aa764da70b036b5057e9b968b1b7a8a [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 Zhuc3fd51e2013-01-22 10:45:54 -0800121 localStateChanged();
122
123}
124
125void
126SyncCore::localStateChanged()
127{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800128 // choose to update locator everytime
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800129 m_log->UpdateLocator(m_userName, m_localPrefix);
Zhenkai Zhu9f6c41e2013-01-17 14:18:14 -0800130 {
131 WriteLock lock(m_ypMutex);
132 m_yp[m_userName] = m_localPrefix;
133 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800134 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800135 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800136
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800137 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800138
139 // reply sync Interest with oldHash as last component
140 Name syncName = constructSyncName(oldHash);
141 Bytes syncData;
142 msgToBytes(msg, syncData);
143 m_handle->publishData(syncName, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800144 cout << m_userName << " publishes: " << *oldHash << endl;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800145 printMsg(msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800146
147 // 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;
148 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers
149 ostringstream ss;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800150 ss << *m_rootHash;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800151 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.05));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800152 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800153 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800154}
155
156void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800157SyncCore::handleInterest(const Name &name)
158{
159 int size = name.size();
160 int prefixSize = m_syncPrefix.size();
161 if (size == prefixSize + 1)
162 {
163 // this is normal sync interest
164 handleSyncInterest(name);
165 }
166 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
167 {
168 // this is recovery interest
169 handleRecoverInterest(name);
170 }
171}
172
173void
174SyncCore::handleRecoverInterest(const Name &name)
175{
176 Bytes hashBytes = name.getComp(name.size() - 1);
177 // this is the hash unkonwn to the sender of the interest
178 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800179 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800180 {
181 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800182 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800183 // DEBUG
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800184 /*
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800185 assert(msg->state_size() > 0);
186 int size = msg->state_size();
187 int index = 0;
188 cout << "Reply recover interest with: " << endl;
189 while (index < size)
190 {
191 SyncState state = msg->state(index);
192 string strName = state.name();
193 string strLoc = state.locator();
194 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;
195 if (state.type() == SyncState::UPDATE)
196 {
197 cout << "Action: update" << endl;
198 }
199 else
200 {
201 cout << "Action: delete" << endl;
202 }
203 index++;
204 }
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800205 */
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800206 // END DEBUG
Zhenkai Zhue851b952013-01-13 22:29:57 -0800207 Bytes syncData;
208 msgToBytes(msg, syncData);
209 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800210 cout << m_userName << " publishes " << hash << endl;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800211 printMsg(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800212 }
213 else
214 {
215 // we don't recognize this hash, can not help
216 }
217}
218
219void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800220SyncCore::handleSyncInterest(const Name &name)
221{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800222 Bytes hashBytes = name.getComp(name.size() - 1);
223 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
224 if (*hash == *m_rootHash)
225 {
226 // we have the same hash; nothing needs to be done
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800227 cout << "same as root hash: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800228 return;
229 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800230 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800231 {
232 // we know something more
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800233 cout << "found hash in sync log" << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800234 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800235
236 Bytes syncData;
237 msgToBytes(msg, syncData);
238 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800239 cout << m_userName << " publishes: " << *hash << endl;
240 printMsg(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800241 }
242 else
243 {
244 // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period
245 ostringstream ss;
246 ss << *hash;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800247 double wait = m_recoverWaitGenerator->nextInterval();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800248 cout << m_userName << ", rootHash: " << *m_rootHash << ", hash: " << *hash << endl;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800249 cout << "recover task scheduled after wait: " << wait << endl;
250 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::recover, this, hash), ss.str(), m_scheduler, wait));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800251 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800252
Zhenkai Zhue851b952013-01-13 22:29:57 -0800253 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800254}
255
256Closure::TimeoutCallbackReturnValue
257SyncCore::handleSyncInterestTimeout(const Name &name)
258{
259 // sendInterestInterest with the current root hash;
260 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800261 return Closure::RESULT_OK;
262}
263
264Closure::TimeoutCallbackReturnValue
265SyncCore::handleRecoverInterestTimeout(const Name &name)
266{
267 // We do not re-express recovery interest for now
268 // if difference is not resolved, the sync interest will trigger
269 // recovery anyway; so it's not so important to have recovery interest
270 // re-expressed
271 return Closure::RESULT_OK;
272}
273
274void
275SyncCore::handleRecoverData(const Name &name, const Bytes &content)
276{
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800277 //cout << "handle recover data" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800278 handleStateData(content);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800279 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800280}
281
282void
283SyncCore::handleSyncData(const Name &name, const Bytes &content)
284{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800285 // suppress recover in interest - data out of order case
286 handleStateData(content);
287
288 // resume outstanding sync interest
289 sendSyncInterest();
290}
291
292void
293SyncCore::handleStateData(const Bytes &content)
294{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800295 SyncStateMsgPtr msg(new SyncStateMsg);
296 bool success = msg->ParseFromArray(head(content), content.size());
297 if(!success)
298 {
299 // ignore misformed SyncData
Zhenkai Zhue851b952013-01-13 22:29:57 -0800300 cerr << "Misformed SyncData"<< endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800301 return;
302 }
303
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800304 cout << m_userName << " receives Msg " << endl;
305 printMsg (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800306 int size = msg->state_size();
307 int index = 0;
308 while (index < size)
309 {
310 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800311 string devStr = state.name();
312 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800313 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800314 if (state.type() == SyncState::UPDATE)
315 {
316 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800317 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800318 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800319 if (state.has_locator())
320 {
321 string locStr = state.locator();
322 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800323 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800324 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800325 WriteLock lock(m_ypMutex);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800326 m_yp[deviceName] = locatorName;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800327 cout << "self: " << m_userName << ", device: " << deviceName << " < == > " << locatorName << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800328 }
329 }
330 else
331 {
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800332 cout << "nani" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800333 deregister(deviceName);
334 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800335 index++;
336 }
337
Zhenkai Zhue851b952013-01-13 22:29:57 -0800338 // find the actuall difference and invoke callback on the actual difference
339 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800340 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800341 // get diff with both new SeqNo and old SeqNo
342 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800343
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800344 if (diff->state_size() > 0)
345 {
346 m_stateMsgCallback(diff);
347 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800348}
349
350void
351SyncCore::sendSyncInterest()
352{
353 Name syncInterest = constructSyncName(m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800354 m_handle->sendInterest(syncInterest, m_syncClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800355 cout << m_userName << " send SYNC interest: " << *m_rootHash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800356}
357
358void
359SyncCore::recover(const HashPtr &hash)
360{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800361 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800362 {
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800363 cout << m_userName << ", Recover for: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800364 // unfortunately we still don't recognize this hash
365 Bytes bytes;
366 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
367 Name recoverInterest = m_syncPrefix;
368 recoverInterest.appendComp(RECOVER);
369 // append the unknown hash
370 recoverInterest.appendComp(bytes);
371 m_handle->sendInterest(recoverInterest, m_recoverClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800372 cout << m_userName << " send RECOVER Interest: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800373 }
374 else
375 {
376 // we already learned the hash; cheers!
377 }
378}
379
380void
381SyncCore::deregister(const Name &name)
382{
383 // Do nothing for now
384 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800385}
386
387Name
388SyncCore::constructSyncName(const HashPtr &hash)
389{
390 Bytes bytes;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800391 readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes());
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800392 Name syncName = m_syncPrefix;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800393 syncName.appendComp(bytes);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800394 return syncName;
395}
396
397void
398SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
399{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800400 int size = msg->ByteSize();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800401 bytes.resize(size);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800402 msg->SerializeToArray(head(bytes), size);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800403}
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800404
405sqlite3_int64
406SyncCore::seq(const Name &name)
407{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800408 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800409}