blob: 29f8664f956a4fe4f6a7d4e19e0aac26f6b77758 [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"
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080023#include "sync-state-helper.h"
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080024#include "logging.h"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080025#include "random-interval-generator.h"
Zhenkai Zhu3b406592013-01-25 21:07:49 -080026#include "one-time-task.h"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080027
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080028#include <boost/lexical_cast.hpp>
Zhenkai Zhu3b406592013-01-25 21:07:49 -080029#include <boost/make_shared.hpp>
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080030
31INIT_LOGGER ("Sync.Core");
32
Zhenkai Zhue851b952013-01-13 22:29:57 -080033const string SyncCore::RECOVER = "RECOVER";
Zhenkai Zhue573ae82013-01-15 13:15:52 -080034const double SyncCore::WAIT = 0.05;
Zhenkai Zhue851b952013-01-13 22:29:57 -080035const double SyncCore::RANDOM_PERCENT = 0.5;
36
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080037using namespace boost;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080038using namespace Ccnx;
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080039
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080040SyncCore::SyncCore(SyncLogPtr syncLog, const Name &userName, const Name &localPrefix, const Name &syncPrefix,
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080041 const StateMsgCallback &callback, CcnxWrapperPtr ccnx)
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080042 : m_ccnx (ccnx)
43 , m_log(syncLog)
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080044 , m_scheduler(new Scheduler ())
45 , m_stateMsgCallback(callback)
46 , m_syncPrefix(syncPrefix)
47 , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP))
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080048{
Zhenkai Zhub330aed2013-01-17 13:29:37 -080049 m_rootHash = m_log->RememberStateInStateLog();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080050
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080051 m_ccnx->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1));
Alexander Afanasyevdac84922013-01-20 23:32:17 -080052 // m_log->initYP(m_yp);
Alexander Afanasyev7326a252013-01-20 23:43:25 -080053 m_log->UpdateLocalLocator (localPrefix);
54
Zhenkai Zhue573ae82013-01-15 13:15:52 -080055 m_scheduler->start();
Zhenkai Zhu3b406592013-01-25 21:07:49 -080056 string tag = userName.toString() + "send-sync-interest";
57 m_sendSyncInterestTask = make_shared<OneTimeTask>(bind(&SyncCore::sendSyncInterest, this), tag, m_scheduler, 4.0);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080058 sendSyncInterest();
59}
60
61SyncCore::~SyncCore()
62{
Alexander Afanasyevfc720362013-01-24 21:49:48 -080063 // m_scheduler->shutdown ();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080064 // need to "deregister" closures
Zhenkai Zhue851b952013-01-13 22:29:57 -080065}
66
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080067void
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080068SyncCore::updateLocalPrefix (const Name &localPrefix)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080069{
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080070 m_log->UpdateLocalLocator (localPrefix);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080071}
72
73void
Zhenkai Zhue851b952013-01-13 22:29:57 -080074SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080075{
Alexander Afanasyev7326a252013-01-20 23:43:25 -080076 m_log->UpdateLocalSeqNo (seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080077 localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080078}
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080079
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080080void
81SyncCore::localStateChanged()
82{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080083 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -080084 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080085
Zhenkai Zhub330aed2013-01-17 13:29:37 -080086 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080087
88 // reply sync Interest with oldHash as last component
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080089 Name syncName = Name (m_syncPrefix)(oldHash->GetHash(), oldHash->GetHashBytes());
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -080090 BytesPtr syncData = serializeMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080091
92 m_ccnx->publishData(syncName, *syncData, FRESHNESS);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080093 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] localStateChanged ");
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -080094 _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes: " << oldHash->shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -080095 // _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080096
97 // 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;
98 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080099 Scheduler::scheduleOneTimeTask (m_scheduler, 0.05,
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800100 bind(&SyncCore::sendSyncInterest, this),
101 lexical_cast<string> (*m_rootHash));
102
Zhenkai Zhud276f1e2013-01-24 17:37:31 -0800103 //sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800104}
105
106void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800107SyncCore::handleInterest(const Name &name)
108{
109 int size = name.size();
110 int prefixSize = m_syncPrefix.size();
111 if (size == prefixSize + 1)
112 {
113 // this is normal sync interest
114 handleSyncInterest(name);
115 }
116 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
117 {
118 // this is recovery interest
119 handleRecoverInterest(name);
120 }
121}
122
123void
124SyncCore::handleRecoverInterest(const Name &name)
125{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800126 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER Interest with name " << name);
127
Zhenkai Zhue851b952013-01-13 22:29:57 -0800128 Bytes hashBytes = name.getComp(name.size() - 1);
129 // this is the hash unkonwn to the sender of the interest
130 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800131 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800132 {
133 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800134 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800135
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800136 BytesPtr syncData = serializeMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800137 m_ccnx->publishData(name, *syncData, FRESHNESS);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800138 _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes " << hash.shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800139 // _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800140 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800141 else
142 {
143 // we don't recognize this hash, can not help
144 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800145}
146
147void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800148SyncCore::handleSyncInterest(const Name &name)
149{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800150 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC Interest with name " << name);
151
Zhenkai Zhue851b952013-01-13 22:29:57 -0800152 Bytes hashBytes = name.getComp(name.size() - 1);
153 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
154 if (*hash == *m_rootHash)
155 {
156 // we have the same hash; nothing needs to be done
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800157 _LOG_TRACE ("same as root hash: " << hash->shortHash ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800158 return;
159 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800160 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800161 {
162 // we know something more
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800163 _LOG_TRACE ("found hash in sync log");
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800164 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800165
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800166 BytesPtr syncData = serializeMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800167 m_ccnx->publishData(name, *syncData, FRESHNESS);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800168 _LOG_TRACE (m_log->GetLocalName () << " publishes: " << hash->shortHash ());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800169 _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800170 }
171 else
172 {
173 // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800174 double wait = m_recoverWaitGenerator->nextInterval();
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800175 _LOG_TRACE (m_log->GetLocalName () << ", rootHash: " << *m_rootHash << ", hash: " << hash->shortHash ());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800176 _LOG_TRACE ("recover task scheduled after wait: " << wait);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800177
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800178 Scheduler::scheduleOneTimeTask (m_scheduler,
179 wait, boost::bind(&SyncCore::recover, this, hash),
180 "r-"+lexical_cast<string> (*hash));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800181 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800182}
183
184Closure::TimeoutCallbackReturnValue
185SyncCore::handleSyncInterestTimeout(const Name &name)
186{
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800187 // sync interest will be resent by scheduler
Zhenkai Zhue851b952013-01-13 22:29:57 -0800188 return Closure::RESULT_OK;
189}
190
191Closure::TimeoutCallbackReturnValue
192SyncCore::handleRecoverInterestTimeout(const Name &name)
193{
194 // We do not re-express recovery interest for now
195 // if difference is not resolved, the sync interest will trigger
196 // recovery anyway; so it's not so important to have recovery interest
197 // re-expressed
198 return Closure::RESULT_OK;
199}
200
201void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800202SyncCore::handleRecoverData(const Name &name, PcoPtr content)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800203{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800204 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER DATA with name: " << name);
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800205 //cout << "handle recover data" << end;
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800206 handleStateData(*content->contentPtr ());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800207 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800208}
209
210void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800211SyncCore::handleSyncData(const Name &name, PcoPtr content)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800212{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800213 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC DATA with name: " << name);
214
Zhenkai Zhue851b952013-01-13 22:29:57 -0800215 // suppress recover in interest - data out of order case
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800216 handleStateData(*content->contentPtr ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800217
218 // resume outstanding sync interest
219 sendSyncInterest();
220}
221
222void
223SyncCore::handleStateData(const Bytes &content)
224{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800225 SyncStateMsgPtr msg(new SyncStateMsg);
226 bool success = msg->ParseFromArray(head(content), content.size());
227 if(!success)
228 {
229 // ignore misformed SyncData
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800230 _LOG_ERROR ("Misformed SyncData");
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800231 return;
232 }
233
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800234 _LOG_TRACE (m_log->GetLocalName () << " receives Msg ");
235 _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800236 int size = msg->state_size();
237 int index = 0;
238 while (index < size)
239 {
240 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800241 string devStr = state.name();
242 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800243 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800244 if (state.type() == SyncState::UPDATE)
245 {
246 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800247 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800248 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800249 if (state.has_locator())
250 {
251 string locStr = state.locator();
252 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800253 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800254 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800255
256 _LOG_TRACE ("self: " << m_log->GetLocalName () << ", device: " << deviceName << " < == > " << locatorName);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800257 }
258 }
259 else
260 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800261 _LOG_ERROR ("Receive SYNC DELETE, but we don't support it yet");
Zhenkai Zhue851b952013-01-13 22:29:57 -0800262 deregister(deviceName);
263 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800264 index++;
265 }
266
Zhenkai Zhue851b952013-01-13 22:29:57 -0800267 // find the actuall difference and invoke callback on the actual difference
268 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800269 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800270 // get diff with both new SeqNo and old SeqNo
271 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800272
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800273 if (diff->state_size() > 0)
274 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800275 m_stateMsgCallback (diff);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800276 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800277}
278
279void
280SyncCore::sendSyncInterest()
281{
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800282 Name syncInterest = Name (m_syncPrefix)(m_rootHash->GetHash(), m_rootHash->GetHashBytes());
283
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800284 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> SYNC Interest for " << m_rootHash->shortHash () << ": " << syncInterest);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800285
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800286 m_ccnx->sendInterest(syncInterest,
287 Closure (boost::bind(&SyncCore::handleSyncData, this, _1, _2),
288 boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1)));
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800289
290 // if there is a pending syncSyncInterest task, reschedule it to be 4 seconds from now
291 // if no such task exists, it will be added
292 _LOG_DEBUG("[" << m_log->GetLocalName () << "] >>> Attempt to schedule sendSyncInterest ");
293 m_scheduler->rescheduleTask(m_sendSyncInterestTask);
294 _LOG_DEBUG("[" << m_log->GetLocalName () << "] >>> Scheduled sendSyncInterest ");
Zhenkai Zhue851b952013-01-13 22:29:57 -0800295}
296
297void
298SyncCore::recover(const HashPtr &hash)
299{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800300 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800301 {
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800302 _LOG_TRACE (m_log->GetLocalName () << ", Recover for: " << hash->shortHash ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800303 // unfortunately we still don't recognize this hash
304 Bytes bytes;
305 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800306
Zhenkai Zhue851b952013-01-13 22:29:57 -0800307 // append the unknown hash
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800308 Name recoverInterest = Name (m_syncPrefix)(RECOVER)(bytes);
309
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800310 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> RECOVER Interests for " << hash->shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800311
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800312 m_ccnx->sendInterest(recoverInterest,
313 Closure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2),
314 boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1)));
315
Zhenkai Zhue851b952013-01-13 22:29:57 -0800316 }
317 else
318 {
319 // we already learned the hash; cheers!
320 }
321}
322
323void
324SyncCore::deregister(const Name &name)
325{
326 // Do nothing for now
327 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800328}
329
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800330sqlite3_int64
331SyncCore::seq(const Name &name)
332{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800333 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800334}