/
ksync_object.cc
1656 lines (1420 loc) · 51.4 KB
/
ksync_object.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <sys/socket.h>
#include <sys/types.h>
#if defined(__linux__)
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/sockios.h>
#endif
#include <boost/bind.hpp>
#include <boost/assign/list_of.hpp>
#include <base/logging.h>
#include <db/db.h>
#include <db/db_entry.h>
#include <db/db_table.h>
#include <db/db_table_partition.h>
#include <sandesh/sandesh_types.h>
#include <sandesh/sandesh.h>
#include <sandesh/sandesh_trace.h>
#include "ksync_index.h"
#include "ksync_entry.h"
#include "ksync_object.h"
#include "ksync_types.h"
SandeshTraceBufferPtr KSyncErrorTraceBuf(
SandeshTraceBufferCreate("KSync Error", 5000));
KSyncObject::FwdRefTree KSyncObject::fwd_ref_tree_;
KSyncObject::BackRefTree KSyncObject::back_ref_tree_;
KSyncObjectManager *KSyncObjectManager::singleton_ = NULL;
std::auto_ptr<KSyncEntry> KSyncObjectManager::default_defer_entry_;
bool KSyncDebug::debug_;
typedef std::map<uint32_t, std::string> VrouterErrorDescriptionMap;
VrouterErrorDescriptionMap g_error_description =
boost::assign::map_list_of<uint32_t, std::string>
(ENOENT, "Entry not present")
(EBADF, "Key mismatch")
(ENOMEM, "Memory insufficient")
(EBUSY, "Object cannot be modified")
(EEXIST, "Object already present")
(ENODEV, "Object not present")
(EINVAL, "Invalid object parameters")
(ENOSPC, "Object table full");
// to be used only by test code, for triggering
// stale entry timer callback explicitly
void TestTriggerStaleEntryCleanupCb(KSyncObject *obj) {
obj->StaleEntryCleanupCb();
}
KSyncObject::KSyncObject(const std::string &name) : need_index_(false), index_table_(),
delete_scheduled_(false), stale_entry_tree_(),
stale_entry_cleanup_timer_(NULL),
stale_entry_cleanup_intvl_(0),
stale_entries_per_intvl_(0) {
KSyncTraceBuf = SandeshTraceBufferCreate(name, 1000);
}
KSyncObject::KSyncObject(const std::string &name, int max_index) :
need_index_(true), index_table_(max_index),
delete_scheduled_(false), stale_entry_tree_(),
stale_entry_cleanup_timer_(NULL),
stale_entry_cleanup_intvl_(0),
stale_entries_per_intvl_(0) {
KSyncTraceBuf = SandeshTraceBufferCreate(name, 1000);
}
KSyncObject::~KSyncObject() {
assert(tree_.size() == 0);
if (stale_entry_cleanup_timer_ != NULL) {
TimerManager::DeleteTimer(stale_entry_cleanup_timer_);
}
}
void KSyncObject::InitStaleEntryCleanup(boost::asio::io_service &ios,
uint32_t cleanup_time,
uint32_t cleanup_intvl,
uint16_t entries_per_intvl) {
// init should be called only once
assert(stale_entry_cleanup_timer_ == NULL);
stale_entry_cleanup_timer_ = TimerManager::CreateTimer(ios,
"KSync Stale Entry Cleanup Timer",
TaskScheduler::GetInstance()->GetTaskId("Agent::KSync"), 0);
stale_entry_cleanup_timer_->Start(cleanup_time,
boost::bind(&KSyncObject::StaleEntryCleanupCb, this));
stale_entry_cleanup_intvl_ = cleanup_intvl;
stale_entries_per_intvl_ = entries_per_intvl;
}
void KSyncObject::Shutdown() {
assert(fwd_ref_tree_.size() == 0);
assert(back_ref_tree_.size() == 0);
}
KSyncEntry *KSyncObject::Find(const KSyncEntry *key) {
Tree::iterator it = tree_.find(*key);
if (it != tree_.end()) {
return it.operator->();
}
return NULL;
}
KSyncEntry *KSyncObject::Next(const KSyncEntry *entry) const {
tbb::recursive_mutex::scoped_lock lock(lock_);
Tree::const_iterator it;
if (entry == NULL) {
it = tree_.begin();
} else {
it = tree_.iterator_to(*entry);
it++;
}
if (it != tree_.end()) {
return const_cast<KSyncEntry *>(it.operator->());
}
return NULL;
}
KSyncEntry *KSyncObject::CreateImpl(const KSyncEntry *key) {
// should not create an entry while scheduled for deletion
assert(delete_scheduled_ == false);
KSyncEntry *entry;
if (need_index_) {
entry = Alloc(key, index_table_.Alloc());
} else {
entry = Alloc(key, KSyncEntry::kInvalidIndex);
}
std::pair<Tree::iterator, bool> ret = tree_.insert(*entry);
if (ret.second == false) {
// entry with same key already exists in the Ksync tree
// delete the allocated entry and use the entry available
// in ksync tree
delete entry;
entry = ret.first.operator->();
} else {
// add reference only if tree insert for newly allocated
// entry succeeds, otherwise reference for tree insertion
// is already accounted for
intrusive_ptr_add_ref(entry);
}
return entry;
}
void KSyncObject::ClearStale(KSyncEntry *entry) {
// Clear stale marked entry and remove from stale entry tree
entry->stale_ = false;
stale_entry_tree_.erase(entry);
}
// Creates a KSync entry. Calling routine sets no_lookup to TRUE when its
// guaranteed that KSync entry is not present (ex: flow)
KSyncEntry *KSyncObject::Create(const KSyncEntry *key, bool no_lookup) {
tbb::recursive_mutex::scoped_lock lock(lock_);
KSyncEntry *entry = NULL;
if (no_lookup == false)
entry = Find(key);
if (entry == NULL) {
entry = CreateImpl(key);
} else {
if (entry->stale_) {
// Clear stale marked entry
ClearStale(entry);
} else if (entry->GetState() != KSyncEntry::TEMP && !entry->IsDeleted()) {
// If entry is already present, it should be in TEMP state
// or deleted state.
assert(0);
}
}
NotifyEvent(entry, KSyncEntry::ADD_CHANGE_REQ);
return entry;
}
KSyncEntry *KSyncObject::Create(const KSyncEntry *key) {
return Create(key, false);
}
KSyncEntry *KSyncObject::CreateStale(const KSyncEntry *key) {
// Should not be called without initialising stale entry
// cleanup InitStaleEntryCleanup
assert(stale_entry_cleanup_timer_ != NULL);
tbb::recursive_mutex::scoped_lock lock(lock_);
KSyncEntry *entry = Find(key);
if (entry == NULL) {
entry = CreateImpl(key);
} else {
if (entry->GetState() != KSyncEntry::TEMP && !entry->IsDeleted()) {
// If entry is already present, it should be in TEMP state
// or deleted state to form a stale entry
return NULL;
}
// cleanup associated DB entry for KSyncDBObject
// so that DB operation on this KSyncEntry does not happen
// without re-claiming this entry
CleanupOnDel(entry);
}
// mark the entry stale and add to stale entry tree.
entry->stale_ = true;
stale_entry_tree_.insert(entry);
NotifyEvent(entry, KSyncEntry::ADD_CHANGE_REQ);
// try starting the timer if not running already
stale_entry_cleanup_timer_->Start(stale_entry_cleanup_intvl_,
boost::bind(&KSyncObject::StaleEntryCleanupCb, this));
return entry;
}
KSyncEntry *KSyncObject::GetReference(const KSyncEntry *key) {
KSyncEntry *entry = Find(key);
if (entry != NULL)
return entry;
entry = CreateImpl(key);
entry->SetState(KSyncEntry::TEMP);
return entry;
}
void KSyncObject::Change(KSyncEntry *entry) {
SafeNotifyEvent(entry, KSyncEntry::ADD_CHANGE_REQ);
}
void KSyncObject::Delete(KSyncEntry *entry) {
if (entry->stale_) {
ClearStale(entry);
}
SafeNotifyEvent(entry, KSyncEntry::DEL_REQ);
}
void KSyncObject::ChangeKey(KSyncEntry *entry, uint32_t arg) {
tbb::recursive_mutex::scoped_lock lock(lock_);
assert(tree_.erase(*entry) > 0);
uint32_t old_key = GetKey(entry);
UpdateKey(entry, arg);
std::pair<Tree::iterator, bool> ret = tree_.insert(*entry);
if (ret.second == false) {
// entry with the same key already exist, to proceed further
// switch place with the existing entry
KSyncEntry *current = ret.first.operator->();
assert(tree_.erase(*current) > 0);
UpdateKey(current, old_key);
// following tree insertions should always pass
assert(tree_.insert(*current).second == true);
assert(tree_.insert(*entry).second == true);
}
}
uint32_t KSyncObject::GetKey(KSyncEntry *entry) {
assert(false);
return 0;
}
void KSyncObject::FreeInd(KSyncEntry *entry, uint32_t index) {
assert(tree_.erase(*entry) > 0);
if (need_index_ == true && index != KSyncEntry::kInvalidIndex) {
index_table_.Free(index);
}
PreFree(entry);
Free(entry);
}
void KSyncObject::Free(KSyncEntry *entry) {
delete entry;
}
void KSyncObject::SafeNotifyEvent(KSyncEntry *entry,
KSyncEntry::KSyncEvent event) {
tbb::recursive_mutex::scoped_lock lock(lock_);
NotifyEvent(entry, event);
}
///////////////////////////////////////////////////////////////////////////////
// KSyncDBObject routines
///////////////////////////////////////////////////////////////////////////////
KSyncDBObject::KSyncDBObject(const std::string &name) : KSyncObject(name), test_id_(-1) {
table_ = NULL;
}
KSyncDBObject::KSyncDBObject(const std::string &name,
int max_index) : KSyncObject(name, max_index), test_id_(-1) {
table_ = NULL;
}
KSyncDBObject::KSyncDBObject(const std::string &name,
DBTableBase *table) : KSyncObject(name), test_id_(-1) {
table_ = table;
id_ = table->Register(boost::bind(&KSyncDBObject::Notify, this, _1, _2));
}
KSyncDBObject::KSyncDBObject(const std::string &name,
DBTableBase *table, int max_index)
: KSyncObject(name, max_index), test_id_(-1) {
table_ = table;
id_ = table->Register(boost::bind(&KSyncDBObject::Notify, this, _1, _2));
}
KSyncDBObject::~KSyncDBObject() {
if (table_) {
UnregisterDb(table_);
}
}
void KSyncDBObject::RegisterDb(DBTableBase *table) {
assert(table_ == NULL);
table_ = table;
id_ = table->Register(boost::bind(&KSyncDBObject::Notify, this, _1, _2));
}
void KSyncDBObject::UnregisterDb(DBTableBase *table) {
assert(table_ == table);
table_->Unregister(id_);
id_ = -1;
table_ = NULL;
}
KSyncDBObject::DBFilterResp KSyncDBObject::DBEntryFilter(
const DBEntry *entry, const KSyncDBEntry *ksync) {
// Default accept all
return DBFilterAccept;
}
void KSyncDBObject::set_test_id(DBTableBase::ListenerId id) {
test_id_ = id;
}
DBTableBase::ListenerId KSyncDBObject::GetListenerId(DBTableBase *table) {
assert(table_ == table);
if (test_id_ != -1) {
return test_id_;
}
return id_;
}
void KSyncDBObject::CleanupOnDel(KSyncEntry *entry) {
KSyncDBEntry *kentry = static_cast<KSyncDBEntry *>(entry);
if (kentry->GetDBEntry() != NULL) {
// when object is created only because of reference it will be in
// temp state without DB entry, deletion of which doesn't need
// this cleanup
kentry->GetDBEntry()->ClearState(table_, id_);
kentry->SetDBEntry(NULL);
}
if (delete_scheduled()) {
// we are in cleanup process remove all duplicate entries
while (kentry->dup_entry_list_.empty() == false) {
// and clear db entry state
kentry->dup_entry_list_.front()->ClearState(table_, id_);
kentry->dup_entry_list_.pop_front();
}
}
}
// DBTable notification handler.
// Generates events for the KSyncEntry state-machine based DBEntry
// Stores the KSyncEntry allocated as DBEntry-state
void KSyncDBObject::Notify(DBTablePartBase *partition, DBEntryBase *e) {
tbb::recursive_mutex::scoped_lock lock(lock_);
DBEntry *entry = static_cast<DBEntry *>(e);
DBTableBase *table = partition->parent();
assert(table_ == table);
KSyncDBEntry *ksync =
static_cast<KSyncDBEntry *>(entry->GetState(table, id_));
DBFilterResp resp = DBFilterAccept;
// cleanup is in-process, ignore All db notifications.
if (delete_scheduled()) {
return;
}
// Trigger DB Filter callback only for ADD/CHANGE, since we need to handle
// cleanup for delete anyways.
if (!entry->IsDeleted()) {
resp = DBEntryFilter(entry, ksync);
}
if (entry->IsDeleted() || resp == DBFilterDelete ||
resp == DBFilterDelAdd) {
if (ksync != NULL) {
// Check if there is any entry present in dup_entry_list
if (!ksync->dup_entry_list_.empty()) {
// Check if entry getting deleted is actively associated with
// Ksync Entry.
if (entry == ksync->GetDBEntry()) {
// clean up db entry state.
CleanupOnDel(ksync);
ksync->SetDBEntry(ksync->dup_entry_list_.front());
ksync->dup_entry_list_.pop_front();
// DB entry association changed, trigger re-sync.
if (ksync->Sync(ksync->GetDBEntry())) {
NotifyEvent(ksync, KSyncEntry::ADD_CHANGE_REQ);
}
} else {
// iterate through entries and delete the
// corresponding DB ref.
KSyncDBEntry::DupEntryList::iterator it_dup;
for (it_dup = ksync->dup_entry_list_.begin();
it_dup != ksync->dup_entry_list_.end(); ++it_dup) {
if (entry == *it_dup)
break;
}
// something bad has happened if we fail to find the entry.
assert(it_dup != ksync->dup_entry_list_.end());
ksync->dup_entry_list_.erase(it_dup);
entry->ClearState(table_, id_);
}
} else {
if (resp == DBFilterDelAdd) {
// clean up db entry state, so that other ksync entry can
// replace the states appropriately.
// cleanup needs to be triggered before notifying delete
// after that ksync entry might be already free'd
CleanupOnDel(ksync);
}
// We may get duplicate delete notification in
// case of db entry reuse
// add -> change ->delete(Notify) -> change -> delete(Notify)
// delete and change gets suppresed as delete and we get
// a duplicate delete notification
if (ksync->IsDeleted() == false) {
NotifyEvent(ksync, KSyncEntry::DEL_REQ);
}
}
}
if (resp != DBFilterDelAdd) {
// return from here except for DBFilterDelAdd case, where
// ADD needs to be triggered after Delete
return;
}
// reset ksync entry pointer, as ksync and DB entry is already
// dissassociated
ksync = NULL;
}
if (resp == DBFilterIgnore) {
// DB filter tells us to ignore this Add/Change.
return;
}
bool need_sync = false;
if (ksync == NULL) {
KSyncEntry *key, *found;
// TODO : Memory is allocated and freed only for lookup. Fix this.
key = DBToKSyncEntry(entry);
found = Find(key);
if (found == NULL) {
ksync = static_cast<KSyncDBEntry *>(CreateImpl(key));
} else {
ksync = static_cast<KSyncDBEntry *>(found);
if (ksync->stale()) {
// Clear stale marked entry and remove from stale entry tree
ClearStale(ksync);
}
}
delete key;
entry->SetState(table, id_, ksync);
// Allow reuse of KSync Entry if the previous associated DB Entry
// is marked deleted. This can happen when Key for OPER DB entry
// deferes from that used in KSync Object.
DBEntry *old_db_entry = ksync->GetDBEntry();
if (old_db_entry != NULL) {
// cleanup previous state id the old db entry is delete marked.
if (old_db_entry->IsDeleted()) {
CleanupOnDel(ksync);
} else {
// In case Oper DB and Ksync use different Keys, its
// possible to have multiple Oper DB entries pointing to
// same Ksync Entry.
// add the entry to dup_entry_list and return
ksync->dup_entry_list_.push_back(entry);
return;
}
}
ksync->SetDBEntry(entry);
need_sync = true;
} else {
// ignore change on non-associated entry.
if (entry != ksync->GetDBEntry()) {
return;
}
}
if (ksync->IsDeleted()) {
// ksync entry was marked as delete, sync required.
need_sync = true;
}
if (ksync->Sync(entry) || need_sync) {
NotifyEvent(ksync, KSyncEntry::ADD_CHANGE_REQ);
}
}
///////////////////////////////////////////////////////////////////////////////
// KSyncEntry routines
///////////////////////////////////////////////////////////////////////////////
bool KSyncEntry::IsResolved() {
KSyncObject *obj = GetObject();
if (obj->IsIndexValid() && index_ == kInvalidIndex)
return false;
if (IsDataResolved() == false)
return false;
return ((state_ >= IN_SYNC) && (state_ < DEL_DEFER_SYNC));
}
std::string KSyncEntry::VrouterErrorToString(uint32_t error) {
std::map<uint32_t, std::string>::iterator iter =
g_error_description.find(error);
if (iter == g_error_description.end())
return strerror(error);
return iter->second;
}
std::string KSyncEntry::VrouterError(uint32_t error) const {
return VrouterErrorToString(error);
}
void KSyncEntry::ErrorHandler(int err, uint32_t seq_no,
KSyncEvent event) const {
if (err == 0) {
return;
}
std::string error_msg = VrouterError(err);
KSYNC_ERROR(VRouterError, "VRouter operation failed. Error <", err,
":", error_msg, ">. Object <", ToString(),
">. Operation <", AckOperationString(event),
">. Message number :", seq_no);
std::stringstream sstr;
sstr << "VRouter operation failed. Error <" << err << ":" << error_msg <<
">. Object <" << ToString() << ">. Operation <" <<
AckOperationString(event) << ">. Message number :" << seq_no;
KSYNC_ERROR_TRACE(Trace, sstr.str().c_str());
LOG(ERROR, sstr.str().c_str());
KSYNC_ASSERT(err == 0);
}
std::string KSyncEntry::AckOperationString(KSyncEvent event) const {
switch(event) {
case ADD_ACK:
return "Addition";
case CHANGE_ACK:
return "Change";
case DEL_ACK:
return "Deletion";
default:
// AckOperationString should track only acks, if something else is
// passed convert it to EventString
return EventString(event);
}
}
std::string KSyncEntry::StateString() const {
std::stringstream str;
switch (state_) {
case INIT:
str << "Init";
break;
case TEMP:
str << "Temp";
break;
case ADD_DEFER:
str << "Add defer";
break;
case CHANGE_DEFER:
str << "Change defer";
break;
case IN_SYNC:
str << "In sync";
break;
case SYNC_WAIT:
str << "Sync wait";
break;
case NEED_SYNC:
str << "Need sync";
break;
case DEL_DEFER_SYNC:
str << "Delete defer sync";
break;
case DEL_DEFER_REF:
str << "Delete pending due to reference";
break;
case DEL_DEFER_DEL_ACK:
str << "Delete pending due to Delete ack wait";
break;
case DEL_ACK_WAIT:
str << "Delete ack wait";
break;
case RENEW_WAIT:
str << "Renew wait";
break;
case FREE_WAIT:
str << "Free wait";
break;
}
if (stale_) {
str << " (Stale entry) ";
}
str << '(' << state_ << ')';
return str.str();
}
std::string KSyncEntry::EventString(KSyncEvent event) const {
std::stringstream str;
switch (event) {
case ADD_CHANGE_REQ:
str << "Add/Change request";
break;
case ADD_ACK:
str << "Add Ack";
break;
case CHANGE_ACK:
str << "Change ack";
break;
case DEL_REQ:
str << "Delete request";
break;
case DEL_ADD_REQ:
str << "Delete followed by Add request";
break;
case DEL_ACK:
str << "Delete ack";
break;
case RE_EVAL:
str << "Re-evaluate";
break;
case INT_PTR_REL:
str << "Reference release";
break;
}
str << '(' << event << ')';
return str.str();
}
void intrusive_ptr_add_ref(KSyncEntry *p) {
p->refcount_++;
};
// KSync adds a reference to object when its created.
// Delete the object if reference falls to 1 and either
// (i) delete was deferred due to refcount or
// (ii) the ksync entry is in TEMP state.
void intrusive_ptr_release(KSyncEntry *p) {
if (--p->refcount_ == 1) {
KSyncObject *obj = p->GetObject();
switch(p->state_) {
case KSyncEntry::TEMP:
// FALLTHRU
case KSyncEntry::DEL_DEFER_REF:
obj->SafeNotifyEvent(p, KSyncEntry::INT_PTR_REL);
break;
default:
break;
}
}
}
///////////////////////////////////////////////////////////////////////////////
// KSyncEntry state machine.
//
// Brief description of States:
//
// INIT : KSyncEntry created. No events notified to the object yet
// TEMP : Temporary object created either due to reference or on
// process restart.
// Ex: Obj-A refers to Obj-B. If Obj-B is not present when Obj-A
// is to be sent, then Obj-B is created in TEMP state
//
// ADD_DEFER : Object deferred since it has some unmet constraints.
// Ex: Obj-A refers to Obj-B. Obj-B is not yet added to kernel.
// Obj-A will get to ADD_DEFER state
// Obj-A goes into BackRefTree for Obj-B.
// Creation of Obj-B will take Obj-A out of this state
// CHANGE_DEFER : Object already added to kernel, subsequent change deferred
// since it has some unmet constraints.
// Ex: Obj-A refers to Obj-B. Obj-B is not yet added to kernel.
// Obj-A will get to ADD_DEFER state
// Obj-A goes into BackRefTree for Obj-B.
// Add of Obj-B will take Obj-A out of this state
// IN_SYNC : Object in-sync with kernel
// SYNC_WAIT : Add or Change sent to kernel. Waiting for ACK from Kernel
// NEED_SYNC : Object out-of-sync with kernel. Need to send a change message
// to sync kernel state
// DEL_DEFER_SYNC:Object deleted when waiting for ACK from kernel. Delete must
// be sent to kernel on getting ACK
// DEL_DEFER_REF: Object deleted with pending references . Delete must be sent
// to kernel when all pending references goes away.
// DEL_DEFER_DEL_ACK: Object delete with pending delete ack wait, Delete
// needs to be sent after receiving a del ACK.
// DEL_ACK_WAIT : Delete sent to Kernel. Waiting for ACK from kernel.
// Can get renewed if there is request to ADD in this case
// RENEW_WAIT : Object being renewed. Waiting for ACK of delete to renew the
// object
// FREE_WAIT : Object marked to be freed at the end of this function call.
// Can only be a temporary state
//
// Brief description of Events:
// ADD_CHANGE_REQ : Request to Add or Change an entry
// ADD_ACK, : Ack from kernel for ADD request
// CHANGE_ACK : Ack from kernel for CHANGE request
// DEL_REQ : Request to DEL an entry
// DEL_ADD_REQ : Request to DEL an entry followed by ADD for the same
// DEL_ACK : Ack from kernel for DEL request
// RE_EVAL : Event to re-evaluate dependencies.
// Ex: If Obj-A is added into Obj-B back-ref tree
// When Obj-B is created, RE_EVAL is sent to Obj-A
///////////////////////////////////////////////////////////////////////////////
// Utility function to handle Add of KSyncEntry.
// If operation is complete, move state to IN_SYNC. Else move to SYNC_WAIT
KSyncEntry::KSyncState KSyncSM_Add(KSyncObject *obj, KSyncEntry *entry) {
KSyncEntry *dep;
if ((dep = entry->UnresolvedReference()) != NULL) {
obj->BackRefAdd(entry, dep);
return KSyncEntry::ADD_DEFER;
}
entry->SetSeen();
if (entry->Add()) {
return KSyncEntry::IN_SYNC;
} else {
return KSyncEntry::SYNC_WAIT;
}
}
// Utility function to handle Change of KSyncEntry.
// If operation is complete, move state to IN_SYNC. Else move to SYNC_WAIT
KSyncEntry::KSyncState KSyncSM_Change(KSyncObject *obj, KSyncEntry *entry) {
KSyncEntry *dep;
assert(entry->Seen());
if ((dep = entry->UnresolvedReference()) != NULL) {
obj->BackRefAdd(entry, dep);
return KSyncEntry::CHANGE_DEFER;
}
if (entry->Change()) {
return KSyncEntry::IN_SYNC;
} else {
return KSyncEntry::SYNC_WAIT;
}
}
// Utility function to handle Delete of KSyncEntry.
// If there are more references to the object, then move it to DEL_DEFER
// state. Object will be deleted when all references drop. If object is
// still not seen by Kernel yet we don't have to delete it.
//
// If operation is complete, move state to IN_SYNC. Else move to SYNC_WAIT
KSyncEntry::KSyncState KSyncSM_Delete(KSyncEntry *entry) {
if (entry->GetRefCount() > 1) {
return KSyncEntry::DEL_DEFER_REF;
}
assert(entry->GetRefCount() == 1);
if (!entry->Seen() && entry->AllowDeleteStateComp()) {
return KSyncEntry::FREE_WAIT;
}
if (entry->Delete()) {
return KSyncEntry::FREE_WAIT;
} else {
return KSyncEntry::DEL_ACK_WAIT;
}
}
// Utility function to handle Delete followed by ADD of KSyncEntry.
// delete is triggered irrespective of the references to the object
// followed by ADD of the object
//
// If operation is complete, move state to IN_SYNC. Else move to SYNC_WAIT
KSyncEntry::KSyncState KSyncSM_DeleteAdd(KSyncObject *obj, KSyncEntry *entry) {
// DeleteAdd operation is not supported/defined for stale entries
// when such an operation is required for stale entry, it must be
// sufficient to trigger only a delete and let Add happen when
// entry is ready to become non-stale
assert(!entry->stale());
// NOTE this API doesnot support managing references for delete trigger
if (entry->Seen() || !entry->AllowDeleteStateComp()) {
if (!entry->Delete()) {
// move to renew wait to trigger Add on DEL_ACK
return KSyncEntry::RENEW_WAIT;
}
}
return KSyncSM_Add(obj, entry);
}
//
//
// ADD_CHANGE_REQ :
// If entry has unresolved references, move it to ADD_DEFER
// Else, send ADD message and move to SYNC_WAIT state
//
// No other events are expected in this state
KSyncEntry::KSyncState KSyncSM_Init(KSyncObject *obj, KSyncEntry *entry,
KSyncEntry::KSyncEvent event) {
KSyncEntry::KSyncState state = KSyncEntry::INIT;
assert(entry->GetRefCount());
switch (event) {
case KSyncEntry::ADD_CHANGE_REQ:
state = KSyncSM_Add(obj, entry);
break;
default:
assert(0);
break;
}
return state;
}
// ADD_CHANGE_REQ :
// ADD_CHANGE_REQ event for an entry in TEMP state.
// If entry has unresolved references, move it to ADD_DEFER
// Else, send ADD message and move to SYNC_WAIT state
//
// DEL_REQ :
// DEL_REQ event for an entry in TEMP state. Can happen only when reference
// for the TEMP entry is dropped
//
// Explicit DEL_REQ event is not expected. This is enforced by checking
// ref-count
//
// Event notified when refcount for object goes to 1 In TEMP state.
// Entry not sent to kernel, so dont send delete message
// Delete object in this state
//
KSyncEntry::KSyncState KSyncSM_Temp(KSyncObject *obj, KSyncEntry *entry,
KSyncEntry::KSyncEvent event) {
KSyncEntry::KSyncState state = KSyncEntry::TEMP;
assert(entry->GetRefCount());
switch (event) {
case KSyncEntry::ADD_CHANGE_REQ:
case KSyncEntry::DEL_ADD_REQ:
state = KSyncSM_Add(obj, entry);
break;
case KSyncEntry::INT_PTR_REL:
case KSyncEntry::DEL_REQ:
if (entry->GetRefCount() == 1) {
state = KSyncEntry::FREE_WAIT;
}
break;
default:
assert(0);
break;
}
return state;
}
// ADD_CHANGE_REQ :
// ADD_CHANGE_REQ event for an entry in ADD_DEFER state.
// Remove the old dependency constraint.
// Re-evaluate to see if there are any further unmet dependencies
//
// RE_EVAL:
// Triggred from back-ref tree when KSyncEntry waited on is added to Kernel
// Entry would already be removed from backref tree
// Re-evaluate to see if there are any further unmet dependencies
//
// DEL_REQ :
// Delete when entry is not yet added to kernel. Dont delete entry. Move state
// based on ref-count
KSyncEntry::KSyncState KSyncSM_AddDefer(KSyncObject *obj, KSyncEntry *entry,
KSyncEntry::KSyncEvent event) {
KSyncEntry::KSyncState state = KSyncEntry::ADD_DEFER;
assert(entry->GetRefCount());
switch (event) {
case KSyncEntry::ADD_CHANGE_REQ:
obj->BackRefDel(entry);
// FALLTHRU
case KSyncEntry::RE_EVAL:
state = KSyncSM_Add(obj, entry);
break;
// Remove any back-ref entry
// Free entry if there are no more references. Else wait in TEMP
// state for either release of reference or for movement to different
// state.
case KSyncEntry::DEL_REQ:
obj->BackRefDel(entry);
if (entry->AllowDeleteStateComp() == false) {
state = KSyncSM_Delete(entry);
} else if (entry->GetRefCount() > 1) {
state = KSyncEntry::TEMP;
} else {
state = KSyncEntry::FREE_WAIT;
}
break;
case KSyncEntry::DEL_ADD_REQ:
obj->BackRefDel(entry);
state = KSyncSM_DeleteAdd(obj, entry);
break;
case KSyncEntry::INT_PTR_REL:
break;
default:
assert(0);
break;
}
return state;
}
// ADD_CHANGE_REQ :
// ADD_CHANGE_REQ event for an entry in CHANGE_DEFER state.
// Remove the old dependency constraint.
// Re-evaluate to see if there are any further unmet dependencies
//
// RE_EVAL:
// Triggred from back-ref tree when KSyncEntry waited on is added to Kernel
// Entry would already be removed from backref tree
// Re-evaluate to see if there are any further unmet dependencies
//
// DEL_REQ :
// Move state based on ref-count.
KSyncEntry::KSyncState KSyncSM_ChangeDefer(KSyncObject *obj, KSyncEntry *entry,
KSyncEntry::KSyncEvent event) {
KSyncEntry::KSyncState state = KSyncEntry::CHANGE_DEFER;
assert(entry->GetRefCount());
switch (event) {
case KSyncEntry::ADD_CHANGE_REQ:
obj->BackRefDel(entry);
// FALLTHRU
case KSyncEntry::RE_EVAL:
state = KSyncSM_Change(obj, entry);
break;
// Remove any back-ref entry and process delete
case KSyncEntry::DEL_REQ:
obj->BackRefDel(entry);
state = KSyncSM_Delete(entry);
break;
case KSyncEntry::DEL_ADD_REQ:
obj->BackRefDel(entry);
state = KSyncSM_DeleteAdd(obj, entry);
break;
case KSyncEntry::INT_PTR_REL:
break;
default:
assert(0);
break;
}
return state;
}
// Object state IN-SYNC with kernel
//
// ADD_CHANGE_REQ :
// Invoke Change on the object
//