blob: 088f8e371ddb103cc6268b8848f33d932f80beb4 [file] [log] [blame]
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 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 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080022
Chaoyi Bian11f294f2012-03-08 14:28:06 -080023#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080024#include "sync-diff-leaf.h"
25#include "sync-full-leaf.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070026#include "sync-log.h"
27
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080028#include <boost/make_shared.hpp>
29#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070030#include <boost/lexical_cast.hpp>
31#include <boost/date_time/posix_time/posix_time.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080032#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080033
34using namespace std;
35using namespace boost;
36
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070037INIT_LOGGER ("SyncLogic");
38
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080039namespace Sync
40{
41
Alexander Afanasyev750d1872012-03-12 15:33:56 -070042SyncLogic::SyncLogic (const std::string &syncPrefix,
43 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070044 LogicRemoveCallback onRemove)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080045 : m_syncPrefix (syncPrefix)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070046 , m_onUpdate (onUpdate)
47 , m_onRemove (onRemove)
Alexander Afanasyev3f93c2b2012-03-13 00:02:31 -070048 , m_ccnxHandle(new CcnxWrapper())
Alexander Afanasyev45fba082012-03-12 18:05:24 -070049 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070050 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (10,50))
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080051{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070052 _LOG_FUNCTION (syncPrefix);
53
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070054 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070055 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070056
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070057 m_scheduler.schedule (posix_time::seconds (0),
58 bind (&SyncLogic::sendSyncInterest, this),
59 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080060}
61
Alexander Afanasyevc1030192012-03-08 22:21:28 -080062SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080063{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070064 _LOG_FUNCTION (this);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070065 // cout << "SyncLogic::~SyncLogic ()" << endl;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080066
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070067 m_ccnxHandle.reset ();
68}
Alexander Afanasyev387ac952012-03-11 23:49:27 -070069
70void
71SyncLogic::respondSyncInterest (const string &interest)
72{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070073 _LOG_TRACE ("<< I " << interest);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070074 //cout << "Respond Sync Interest" << endl;
Alexander Afanasyev387ac952012-03-11 23:49:27 -070075 string hash = interest.substr(interest.find_last_of("/") + 1);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070076 // cout << "Received Sync Interest: " << hash << endl;
77
Alexander Afanasyev387ac952012-03-11 23:49:27 -070078 DigestPtr digest = make_shared<Digest> ();
79 try
80 {
81 istringstream is (hash);
82 is >> *digest;
83 }
84 catch (Error::DigestCalculationError &e)
85 {
86 // log error. ignoring it for now, later we should log it
87 return;
88 }
89
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070090 processSyncInterest (digest, interest, false);
Alexander Afanasyev387ac952012-03-11 23:49:27 -070091}
92
93void
94SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
95{
Alexander Afanasyev750d1872012-03-12 15:33:56 -070096 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -070097
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -070098 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070099 if (digest->isZero () && !m_state.getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700100 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700101 _LOG_TRACE (">> D " << interestName << "/state" << " (zero)");
102
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700103 m_ccnxHandle->publishData (interestName + "/state",
104 lexical_cast<string> (m_state),
105 m_syncResponseFreshness);
106 return;
107 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700108
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700109 if (*m_state.getDigest() == *digest)
110 {
111 // cout << interestName << "\n";
112 if (digest->isZero ())
113 {
114 _LOG_TRACE ("Digest is zero, adding /state to PIT");
115 m_syncInterestTable.insert (interestName + "/state");
116 }
117 else
118 {
119 _LOG_TRACE ("Same state. Adding to PIT");
120 m_syncInterestTable.insert (interestName);
121 }
122 return;
123 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700124
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700125 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
126
127 if (stateInDiffLog != m_log.end ())
128 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700129 _LOG_TRACE (">> D " << interestName);
130
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700131 m_ccnxHandle->publishData (interestName,
132 lexical_cast<string> (*(*stateInDiffLog)->diff ()),
133 m_syncResponseFreshness);
134 return;
135 }
136
137 if (!timedProcessing)
138 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700139 _LOG_DEBUG ("hmm");
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700140 m_scheduler.schedule (posix_time::milliseconds (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
141 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
142 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700143
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700144 }
145 else
146 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700147 _LOG_TRACE (">> D " << interestName << "/state" << " (timed processing)");
148
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700149 m_ccnxHandle->publishData (interestName + "/state",
150 lexical_cast<string> (m_state),
151 m_syncResponseFreshness);
152 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800153}
154
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800155void
156SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800157{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700158 _LOG_TRACE ("<< D " << name);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700159 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700160
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700161 try
162 {
163 recursive_mutex::scoped_lock lock (m_stateMutex);
164
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700165 string last = name.substr(name.find_last_of("/") + 1);
166 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700167
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700168 if (last == "state")
169 {
170 FullState full;
171 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700172 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700173 {
174 NameInfoConstPtr info = leaf->getInfo ();
175 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700176
177 bool inserted = false;
178 bool updated = false;
179 SeqNo oldSeq;
180 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800181
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700182 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700183 {
184 diffLog->update (info, seq);
185 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700186 }
187 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700188 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700189 else
190 {
191 DiffState diff;
192 ss >> diff;
193 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
194 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700195 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
196 BOOST_ASSERT (diffLeaf != 0);
197
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700198 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700199 if (diffLeaf->getOperation() == UPDATE)
200 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700201 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800202
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700203 bool inserted = false;
204 bool updated = false;
205 SeqNo oldSeq;
206 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800207
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700208 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700209 {
210 diffLog->update (info, seq);
211 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700212 }
213 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700214 else if (diffLeaf->getOperation() == REMOVE)
215 {
216 if (m_state.remove (info))
217 {
218 diffLog->remove (info);
219 m_onRemove (info->toString ());
220 }
221 }
222 else
223 {
224 BOOST_ASSERT (false); // just in case
225 }
226 }
227 }
228
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700229 diffLog->setDigest(m_state.getDigest());
Alexander Afanasyeva36b7512012-03-12 16:03:03 -0700230 if (m_log.size () > 0)
231 {
232 m_log.get<sequenced> ().front ()->setNext (diffLog);
233 }
Alexander Afanasyevf0519d22012-03-13 14:00:44 -0700234 m_log.erase (m_state.getDigest());
235 /// @todo Optimization
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700236 m_log.insert (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700237 }
238 catch (Error::SyncXmlDecodingFailure &e)
239 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700240 diffLog.reset ();
241 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700242 }
243
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700244 // if state has changed, then it is safe to express a new interest
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700245 if (diffLog->getLeaves ().size () > 0)
246 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700247 sendSyncInterest ();
248 }
249 else
250 {
251 // should not reexpress the same interest. Need at least wait for data lifetime
252 // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization
253 m_scheduler.cancel (REEXPRESSING_INTEREST);
254 m_scheduler.schedule (posix_time::seconds (m_syncResponseFreshness),
255 bind (&SyncLogic::sendSyncInterest, this),
256 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700257 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800258}
259
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800260void
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700261SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800262{
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800263 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700264 if (pis.size () > 0)
265 {
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700266 stringstream ss;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700267 ss << *diffLog;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700268 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
269 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700270 _LOG_TRACE (">> D " << *ii);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700271 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
272 }
273 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800274}
275
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800276void
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700277SyncLogic::processPendingSyncInterests (DiffStatePtr diffLog)
278{
279 //cout << "Process Pending Interests" <<endl;
280 diffLog->setDigest (m_state.getDigest());
281 if (m_log.size () > 0)
282 {
283 m_log.get<sequenced> ().front ()->setNext (diffLog);
284 }
285 m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid
286 /// @todo Optimization
287 m_log.insert (diffLog);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700288 _LOG_DEBUG (*diffLog->getDigest () << " " << m_log.size ());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700289}
290
291void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700292SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
293{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700294 DiffStatePtr diff;
295 {
296 //cout << "Add local names" <<endl;
297 recursive_mutex::scoped_lock lock (m_stateMutex);
298 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700299
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700300 SeqNo seqN (session, seq);
301 m_state.update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700302
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700303 diff = make_shared<DiffState>();
304 diff->update(info, seqN);
305 processPendingSyncInterests (diff);
306 }
307
308 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700309}
310
311void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700312SyncLogic::remove(const string &prefix)
313{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700314 DiffStatePtr diff;
315 {
316 recursive_mutex::scoped_lock lock (m_stateMutex);
317 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
318 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700319
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700320 diff = make_shared<DiffState>();
321 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700322
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700323 processPendingSyncInterests (diff);
324 }
325
326 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700327}
328
329void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800330SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800331{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700332 // cout << "Sending Sync Interest" << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700333 recursive_mutex::scoped_lock lock (m_stateMutex);
334
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800335 ostringstream os;
Alexander Afanasyev8a3d9132012-03-12 23:54:32 -0700336 os << m_syncPrefix << "/" << *m_state.getDigest();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800337
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700338 _LOG_TRACE (">> I " << os.str ());
339
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800340 m_ccnxHandle->sendInterest (os.str (),
341 bind (&SyncLogic::processSyncData, this, _1, _2));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700342
343 m_scheduler.cancel (REEXPRESSING_INTEREST);
344 m_scheduler.schedule (posix_time::seconds (4),
345 bind (&SyncLogic::sendSyncInterest, this),
346 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800347}
348
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800349}