blob: d85093ba7bd678d4c2f8993458723ca94408da62 [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,
39 const StateMsgCallback &callback, CcnxWrapperPtr ccnx, SchedulerPtr scheduler)
40 : m_ccnx (ccnx)
41 , m_log(syncLog)
Zhenkai Zhub330aed2013-01-17 13:29:37 -080042 , m_scheduler(scheduler)
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -080043 , m_stateMsgCallback(callback)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080044 , m_syncPrefix(syncPrefix)
Zhenkai Zhue573ae82013-01-15 13:15:52 -080045 , 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 Afanasyevd6c2a902013-01-19 21:24:30 -080059 // need to "deregister" closures
Zhenkai Zhue851b952013-01-13 22:29:57 -080060}
61
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080062void
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080063SyncCore::updateLocalPrefix (const Name &localPrefix)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080064{
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080065 m_log->UpdateLocalLocator (localPrefix);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080066}
67
68void
Zhenkai Zhue851b952013-01-13 22:29:57 -080069SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080070{
Alexander Afanasyev7326a252013-01-20 23:43:25 -080071 m_log->UpdateLocalSeqNo (seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080072 localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080073}
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080074
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080075void
76SyncCore::localStateChanged()
77{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080078 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -080079 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080080
Zhenkai Zhub330aed2013-01-17 13:29:37 -080081 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080082
83 // reply sync Interest with oldHash as last component
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080084 Name syncName = Name (m_syncPrefix)(oldHash->GetHash(), oldHash->GetHashBytes());
85 BytesPtr syncData = serializeMsg (msg);
86
87 m_ccnx->publishData(syncName, *syncData, FRESHNESS);
88 _LOG_TRACE (m_log->GetLocalName () << " publishes: " << *oldHash);
89 _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080090
91 // 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;
92 // 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 -080093 Scheduler::scheduleOneTimeTask (m_scheduler, 0.05,
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080094 bind(&SyncCore::sendSyncInterest, this),
95 lexical_cast<string> (*m_rootHash));
96
Zhenkai Zhue573ae82013-01-15 13:15:52 -080097 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080098}
99
100void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800101SyncCore::handleInterest(const Name &name)
102{
103 int size = name.size();
104 int prefixSize = m_syncPrefix.size();
105 if (size == prefixSize + 1)
106 {
107 // this is normal sync interest
108 handleSyncInterest(name);
109 }
110 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
111 {
112 // this is recovery interest
113 handleRecoverInterest(name);
114 }
115}
116
117void
118SyncCore::handleRecoverInterest(const Name &name)
119{
120 Bytes hashBytes = name.getComp(name.size() - 1);
121 // this is the hash unkonwn to the sender of the interest
122 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800123 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800124 {
125 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800126 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800127
128 BytesPtr syncData = serializeMsg (msg);
129 m_ccnx->publishData(name, *syncData, FRESHNESS);
130 _LOG_TRACE (m_log->GetLocalName () << " publishes " << hash);
131 _LOG_TRACE (msg);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800132 }
133 else
134 {
Zhenkai Zhue851b952013-01-13 22:29:57 -0800135 // we don't recognize this hash, can not help
136 }
137}
138
139void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800140SyncCore::handleSyncInterest(const Name &name)
141{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800142 Bytes hashBytes = name.getComp(name.size() - 1);
143 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
144 if (*hash == *m_rootHash)
145 {
146 // we have the same hash; nothing needs to be done
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800147 _LOG_TRACE ("same as root hash: " << *hash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800148 return;
149 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800150 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800151 {
152 // we know something more
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800153 _LOG_TRACE ("found hash in sync log");
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800154 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800155
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800156 BytesPtr syncData = serializeMsg (msg);
157 m_ccnx->publishData(name, *syncData, FRESHNESS);
158 _LOG_TRACE (m_log->GetLocalName () << " publishes: " << *hash);
159 _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800160 }
161 else
162 {
163 // 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 -0800164 double wait = m_recoverWaitGenerator->nextInterval();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800165 _LOG_TRACE (m_log->GetLocalName () << ", rootHash: " << *m_rootHash << ", hash: " << *hash);
166 _LOG_TRACE ("recover task scheduled after wait: " << wait);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800167
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800168 Scheduler::scheduleOneTimeTask (m_scheduler,
169 wait, boost::bind(&SyncCore::recover, this, hash),
170 "r-"+lexical_cast<string> (*hash));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800171 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800172}
173
174Closure::TimeoutCallbackReturnValue
175SyncCore::handleSyncInterestTimeout(const Name &name)
176{
177 // sendInterestInterest with the current root hash;
178 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800179 return Closure::RESULT_OK;
180}
181
182Closure::TimeoutCallbackReturnValue
183SyncCore::handleRecoverInterestTimeout(const Name &name)
184{
185 // We do not re-express recovery interest for now
186 // if difference is not resolved, the sync interest will trigger
187 // recovery anyway; so it's not so important to have recovery interest
188 // re-expressed
189 return Closure::RESULT_OK;
190}
191
192void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800193SyncCore::handleRecoverData(const Name &name, PcoPtr content)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800194{
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800195 //cout << "handle recover data" << end;
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800196 handleStateData(*content->contentPtr ());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800197 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800198}
199
200void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800201SyncCore::handleSyncData(const Name &name, PcoPtr content)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800202{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800203 // suppress recover in interest - data out of order case
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800204 handleStateData(*content->contentPtr ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800205
206 // resume outstanding sync interest
207 sendSyncInterest();
208}
209
210void
211SyncCore::handleStateData(const Bytes &content)
212{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800213 SyncStateMsgPtr msg(new SyncStateMsg);
214 bool success = msg->ParseFromArray(head(content), content.size());
215 if(!success)
216 {
217 // ignore misformed SyncData
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800218 _LOG_ERROR ("Misformed SyncData");
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800219 return;
220 }
221
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800222 _LOG_TRACE (m_log->GetLocalName () << " receives Msg ");
223 _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800224 int size = msg->state_size();
225 int index = 0;
226 while (index < size)
227 {
228 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800229 string devStr = state.name();
230 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800231 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800232 if (state.type() == SyncState::UPDATE)
233 {
234 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800235 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800236 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800237 if (state.has_locator())
238 {
239 string locStr = state.locator();
240 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800241 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800242 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800243
244 _LOG_TRACE ("self: " << m_log->GetLocalName () << ", device: " << deviceName << " < == > " << locatorName);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800245 }
246 }
247 else
248 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800249 _LOG_ERROR ("Receive SYNC DELETE, but we don't support it yet");
Zhenkai Zhue851b952013-01-13 22:29:57 -0800250 deregister(deviceName);
251 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800252 index++;
253 }
254
Zhenkai Zhue851b952013-01-13 22:29:57 -0800255 // find the actuall difference and invoke callback on the actual difference
256 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800257 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800258 // get diff with both new SeqNo and old SeqNo
259 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800260
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800261 if (diff->state_size() > 0)
262 {
263 m_stateMsgCallback(diff);
264 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800265}
266
267void
268SyncCore::sendSyncInterest()
269{
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800270 Name syncInterest = Name (m_syncPrefix)(m_rootHash->GetHash(), m_rootHash->GetHashBytes());
271
272 m_ccnx->sendInterest(syncInterest,
273 Closure (boost::bind(&SyncCore::handleSyncData, this, _1, _2),
274 boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1)));
275
276 _LOG_TRACE (m_log->GetLocalName () << " send SYNC interest: " << *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800277}
278
279void
280SyncCore::recover(const HashPtr &hash)
281{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800282 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800283 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800284 _LOG_TRACE (m_log->GetLocalName () << ", Recover for: " << *hash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800285 // unfortunately we still don't recognize this hash
286 Bytes bytes;
287 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800288
Zhenkai Zhue851b952013-01-13 22:29:57 -0800289 // append the unknown hash
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800290 Name recoverInterest = Name (m_syncPrefix)(RECOVER)(bytes);
291
292 m_ccnx->sendInterest(recoverInterest,
293 Closure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2),
294 boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1)));
295
296 _LOG_TRACE (m_log->GetLocalName () << " send RECOVER Interest: " << *hash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800297 }
298 else
299 {
300 // we already learned the hash; cheers!
301 }
302}
303
304void
305SyncCore::deregister(const Name &name)
306{
307 // Do nothing for now
308 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800309}
310
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800311sqlite3_int64
312SyncCore::seq(const Name &name)
313{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800314 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800315}