blob: 80daac82868fe8a59c8fe315d12b4cc45f0ad936 [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"
26
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080027#include <boost/lexical_cast.hpp>
28
29INIT_LOGGER ("Sync.Core");
30
Zhenkai Zhue851b952013-01-13 22:29:57 -080031const string SyncCore::RECOVER = "RECOVER";
Zhenkai Zhue573ae82013-01-15 13:15:52 -080032const double SyncCore::WAIT = 0.05;
Zhenkai Zhue851b952013-01-13 22:29:57 -080033const double SyncCore::RANDOM_PERCENT = 0.5;
34
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080035using namespace boost;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080036using namespace Ccnx;
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080037
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080038SyncCore::SyncCore(SyncLogPtr syncLog, const Name &userName, const Name &localPrefix, const Name &syncPrefix,
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080039 const StateMsgCallback &callback, CcnxWrapperPtr ccnx)
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080040 : m_ccnx (ccnx)
41 , m_log(syncLog)
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080042 , m_scheduler(new Scheduler ())
43 , m_stateMsgCallback(callback)
44 , m_syncPrefix(syncPrefix)
45 , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP))
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080046{
Zhenkai Zhub330aed2013-01-17 13:29:37 -080047 m_rootHash = m_log->RememberStateInStateLog();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080048
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080049 m_ccnx->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1));
Alexander Afanasyevdac84922013-01-20 23:32:17 -080050 // m_log->initYP(m_yp);
Alexander Afanasyev7326a252013-01-20 23:43:25 -080051 m_log->UpdateLocalLocator (localPrefix);
52
Zhenkai Zhue573ae82013-01-15 13:15:52 -080053 m_scheduler->start();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080054 sendSyncInterest();
55}
56
57SyncCore::~SyncCore()
58{
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080059 m_scheduler->shutdown ();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080060 // need to "deregister" closures
Zhenkai Zhue851b952013-01-13 22:29:57 -080061}
62
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080063void
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080064SyncCore::updateLocalPrefix (const Name &localPrefix)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080065{
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080066 m_log->UpdateLocalLocator (localPrefix);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080067}
68
69void
Zhenkai Zhue851b952013-01-13 22:29:57 -080070SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080071{
Alexander Afanasyev7326a252013-01-20 23:43:25 -080072 m_log->UpdateLocalSeqNo (seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080073 localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080074}
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080075
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080076void
77SyncCore::localStateChanged()
78{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080079 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -080080 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080081
Zhenkai Zhub330aed2013-01-17 13:29:37 -080082 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080083
84 // reply sync Interest with oldHash as last component
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080085 Name syncName = Name (m_syncPrefix)(oldHash->GetHash(), oldHash->GetHashBytes());
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -080086 BytesPtr syncData = serializeMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080087
88 m_ccnx->publishData(syncName, *syncData, FRESHNESS);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080089 _LOG_DEBUG (m_log->GetLocalName () << " localStateChanged ");
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080090 _LOG_TRACE (m_log->GetLocalName () << " publishes: " << *oldHash);
91 _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080092
93 // 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;
94 // 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 -080095 Scheduler::scheduleOneTimeTask (m_scheduler, 0.05,
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080096 bind(&SyncCore::sendSyncInterest, this),
97 lexical_cast<string> (*m_rootHash));
98
Zhenkai Zhud276f1e2013-01-24 17:37:31 -080099 //sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800100}
101
102void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800103SyncCore::handleInterest(const Name &name)
104{
105 int size = name.size();
106 int prefixSize = m_syncPrefix.size();
107 if (size == prefixSize + 1)
108 {
109 // this is normal sync interest
110 handleSyncInterest(name);
111 }
112 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
113 {
114 // this is recovery interest
115 handleRecoverInterest(name);
116 }
117}
118
119void
120SyncCore::handleRecoverInterest(const Name &name)
121{
122 Bytes hashBytes = name.getComp(name.size() - 1);
123 // this is the hash unkonwn to the sender of the interest
124 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800125 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800126 {
127 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800128 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800129
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800130 BytesPtr syncData = serializeMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800131 m_ccnx->publishData(name, *syncData, FRESHNESS);
132 _LOG_TRACE (m_log->GetLocalName () << " publishes " << hash);
133 _LOG_TRACE (msg);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800134 }
135 else
136 {
Zhenkai Zhue851b952013-01-13 22:29:57 -0800137 // we don't recognize this hash, can not help
138 }
139}
140
141void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800142SyncCore::handleSyncInterest(const Name &name)
143{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800144 Bytes hashBytes = name.getComp(name.size() - 1);
145 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
146 if (*hash == *m_rootHash)
147 {
148 // we have the same hash; nothing needs to be done
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800149 _LOG_TRACE ("same as root hash: " << *hash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800150 return;
151 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800152 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800153 {
154 // we know something more
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800155 _LOG_TRACE ("found hash in sync log");
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800156 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800157
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800158 BytesPtr syncData = serializeMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800159 m_ccnx->publishData(name, *syncData, FRESHNESS);
160 _LOG_TRACE (m_log->GetLocalName () << " publishes: " << *hash);
161 _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800162 }
163 else
164 {
165 // 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 -0800166 double wait = m_recoverWaitGenerator->nextInterval();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800167 _LOG_TRACE (m_log->GetLocalName () << ", rootHash: " << *m_rootHash << ", hash: " << *hash);
168 _LOG_TRACE ("recover task scheduled after wait: " << wait);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800169
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800170 Scheduler::scheduleOneTimeTask (m_scheduler,
171 wait, boost::bind(&SyncCore::recover, this, hash),
172 "r-"+lexical_cast<string> (*hash));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800173 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800174}
175
176Closure::TimeoutCallbackReturnValue
177SyncCore::handleSyncInterestTimeout(const Name &name)
178{
179 // sendInterestInterest with the current root hash;
180 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800181 return Closure::RESULT_OK;
182}
183
184Closure::TimeoutCallbackReturnValue
185SyncCore::handleRecoverInterestTimeout(const Name &name)
186{
187 // We do not re-express recovery interest for now
188 // if difference is not resolved, the sync interest will trigger
189 // recovery anyway; so it's not so important to have recovery interest
190 // re-expressed
191 return Closure::RESULT_OK;
192}
193
194void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800195SyncCore::handleRecoverData(const Name &name, PcoPtr content)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800196{
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800197 //cout << "handle recover data" << end;
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800198 handleStateData(*content->contentPtr ());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800199 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800200}
201
202void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800203SyncCore::handleSyncData(const Name &name, PcoPtr content)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800204{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800205 // suppress recover in interest - data out of order case
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800206 handleStateData(*content->contentPtr ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800207
208 // resume outstanding sync interest
209 sendSyncInterest();
210}
211
212void
213SyncCore::handleStateData(const Bytes &content)
214{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800215 SyncStateMsgPtr msg(new SyncStateMsg);
216 bool success = msg->ParseFromArray(head(content), content.size());
217 if(!success)
218 {
219 // ignore misformed SyncData
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800220 _LOG_ERROR ("Misformed SyncData");
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800221 return;
222 }
223
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800224 _LOG_TRACE (m_log->GetLocalName () << " receives Msg ");
225 _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800226 int size = msg->state_size();
227 int index = 0;
228 while (index < size)
229 {
230 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800231 string devStr = state.name();
232 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800233 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800234 if (state.type() == SyncState::UPDATE)
235 {
236 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800237 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800238 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800239 if (state.has_locator())
240 {
241 string locStr = state.locator();
242 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800243 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800244 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800245
246 _LOG_TRACE ("self: " << m_log->GetLocalName () << ", device: " << deviceName << " < == > " << locatorName);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800247 }
248 }
249 else
250 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800251 _LOG_ERROR ("Receive SYNC DELETE, but we don't support it yet");
Zhenkai Zhue851b952013-01-13 22:29:57 -0800252 deregister(deviceName);
253 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800254 index++;
255 }
256
Zhenkai Zhue851b952013-01-13 22:29:57 -0800257 // find the actuall difference and invoke callback on the actual difference
258 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800259 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800260 // get diff with both new SeqNo and old SeqNo
261 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800262
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800263 if (diff->state_size() > 0)
264 {
265 m_stateMsgCallback(diff);
266 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800267}
268
269void
270SyncCore::sendSyncInterest()
271{
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800272 Name syncInterest = Name (m_syncPrefix)(m_rootHash->GetHash(), m_rootHash->GetHashBytes());
273
274 m_ccnx->sendInterest(syncInterest,
275 Closure (boost::bind(&SyncCore::handleSyncData, this, _1, _2),
276 boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1)));
277
278 _LOG_TRACE (m_log->GetLocalName () << " send SYNC interest: " << *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800279}
280
281void
282SyncCore::recover(const HashPtr &hash)
283{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800284 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800285 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800286 _LOG_TRACE (m_log->GetLocalName () << ", Recover for: " << *hash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800287 // unfortunately we still don't recognize this hash
288 Bytes bytes;
289 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800290
Zhenkai Zhue851b952013-01-13 22:29:57 -0800291 // append the unknown hash
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800292 Name recoverInterest = Name (m_syncPrefix)(RECOVER)(bytes);
293
294 m_ccnx->sendInterest(recoverInterest,
295 Closure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2),
296 boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1)));
297
298 _LOG_TRACE (m_log->GetLocalName () << " send RECOVER Interest: " << *hash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800299 }
300 else
301 {
302 // we already learned the hash; cheers!
303 }
304}
305
306void
307SyncCore::deregister(const Name &name)
308{
309 // Do nothing for now
310 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800311}
312
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800313sqlite3_int64
314SyncCore::seq(const Name &name)
315{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800316 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800317}