LCOV - code coverage report
Current view: top level - db - db_table.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 334 354 94.4 %
Date: 2026-11-30 02:00:48 Functions: 64 71 90.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include <vector>
       7             : #include <atomic>
       8             : 
       9             : #include <tbb/spin_rw_mutex.h>
      10             : 
      11             : #include <boost/bind/bind.hpp>
      12             : #include <boost/foreach.hpp>
      13             : #include <boost/dynamic_bitset.hpp>
      14             : #include <boost/type_traits.hpp>
      15             : 
      16             : #include "base/compiler.h"
      17             : #include "base/logging.h"
      18             : #include "base/task_annotations.h"
      19             : #include "base/time_util.h"
      20             : #include "db/db.h"
      21             : #include "db/db_partition.h"
      22             : #include "db/db_table.h"
      23             : #include "db/db_table_partition.h"
      24             : #include "db/db_table_walk_mgr.h"
      25             : #include "db/db_types.h"
      26             : 
      27             : class DBEntry;
      28             : class DBEntryBase;
      29             : 
      30             : using namespace std;
      31             : using namespace boost::placeholders;
      32             : 
      33     1283105 : DBRequest::DBRequest() : oper(static_cast<DBOperation>(0)) {
      34     1283083 : }
      35             : 
      36     1284611 : DBRequest::~DBRequest() {
      37             : #if defined(__GNUC__)
      38             : #if (__GNUC_PREREQ(4, 2) > 0)
      39             :     boost::has_virtual_destructor<DBRequestKey>::type key_has_destructor;
      40             :     boost::has_virtual_destructor<DBRequestData>::type data_has_destructor;
      41     1284611 :     assert(key_has_destructor && data_has_destructor);
      42             : #endif
      43             : #endif
      44     1284488 : }
      45             : 
      46      649172 : void DBRequest::Swap(DBRequest *rhs) {
      47      649172 :     swap(oper, rhs->oper);
      48      649171 :     swap(key, rhs->key);
      49      649165 :     swap(data, rhs->data);
      50      649157 : }
      51             : 
      52             : // we need copy to be able to resize vector of atomics
      53             : // therefore we don't need the same value in both instances
      54             : template<typename _Tp>
      55             : struct AtomicWithCopy : public std::atomic<_Tp> {
      56             :     // Inherit constructors
      57             :     using std::atomic<_Tp>::atomic;
      58             : 
      59             :     // Bring in base class operators and methods
      60             :     using std::atomic<_Tp>::operator=;
      61             :     using std::atomic<_Tp>::load;
      62             :     using std::atomic<_Tp>::store;
      63             : 
      64             :     // Custom constructor to handle copy from another AtomicWithCopy (must perform a load/store)
      65     1011394 :     AtomicWithCopy(const AtomicWithCopy& other) : std::atomic<_Tp>(other.load()) {}
      66             :     
      67             :     // Custom assignment operator
      68      793018 :     AtomicWithCopy& operator=(const AtomicWithCopy& other) {
      69     1586031 :         this->store(other.load());
      70      793011 :         return *this;
      71             :     }
      72             : };
      73             : 
      74             : class DBTableBase::ListenerInfo {
      75             : public:
      76             :     typedef vector<ChangeCallback> CallbackList;
      77             :     typedef vector<string> NameList;
      78             :     typedef vector<AtomicWithCopy<uint64_t>> StateCountList;
      79             : 
      80     1530605 :     explicit ListenerInfo(const string &table_name) :
      81     1530605 :         db_state_accounting_(true) {
      82     1530546 :         if (table_name.find("__ifmap_") != string::npos) {
      83             :             // TODO need to have unconditional DB state accounting
      84             :             // for now skipp DB State accounting for ifmap tables
      85     1233601 :             db_state_accounting_ = false;
      86             :         }
      87     1530551 :     }
      88             : 
      89      793542 :     DBTableBase::ListenerId Register(ChangeCallback callback,
      90             :         const string &name) {
      91      793542 :         tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
      92      793590 :         size_t i = bmap_.find_first();
      93      793427 :         if (i == bmap_.npos) {
      94      793008 :             i = callbacks_.size();
      95      793001 :             callbacks_.push_back(callback);
      96      793039 :             names_.push_back(name);
      97      793056 :             state_count_.resize(i + 1);
      98      793025 :             state_count_[i] = AtomicWithCopy<uint64_t>(0);
      99             :         } else {
     100         419 :             bmap_.reset(i);
     101         419 :             if (bmap_.none()) {
     102         317 :                 bmap_.clear();
     103             :             }
     104         419 :             callbacks_[i] = callback;
     105         419 :             names_[i] = name;
     106         419 :             state_count_[i] = 0;
     107             :         }
     108      793643 :         return i;
     109      793679 :     }
     110             : 
     111      793651 :     void Unregister(ListenerId listener) {
     112      793651 :         tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
     113      793650 :         callbacks_[listener] = NULL;
     114      793650 :         names_[listener] = "";
     115             :         // During Unregister Listener should have cleaned up,
     116             :         // DB states from all the entries in this table.
     117      793648 :         assert(state_count_[listener] == 0);
     118      793649 :         if ((size_t) listener == callbacks_.size() - 1) {
     119     1258072 :             while (!callbacks_.empty() && callbacks_.back() == NULL) {
     120      793178 :                 callbacks_.pop_back();
     121      793178 :                 names_.pop_back();
     122      793178 :                 state_count_.pop_back();
     123             :             }
     124      464895 :             if (bmap_.size() > callbacks_.size()) {
     125      225831 :                 bmap_.resize(callbacks_.size());
     126             :             }
     127             :         } else {
     128      328755 :             if ((size_t) listener >= bmap_.size()) {
     129      326957 :                 bmap_.resize(listener + 1);
     130             :             }
     131      328756 :             bmap_.set(listener);
     132             :         }
     133      793651 :     }
     134             : 
     135             :     // concurrency: called from DBPartition task.
     136     2527341 :     void RunNotify(DBTablePartBase *tpart, DBEntryBase *entry) {
     137     2527341 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     138     2527863 :         for (CallbackList::iterator iter = callbacks_.begin();
     139     8743478 :              iter != callbacks_.end(); ++iter) {
     140     6214565 :             if (*iter != NULL) {
     141     6192474 :                 ChangeCallback cb = *iter;
     142     6191648 :                 (cb)(tpart, entry);
     143     6194706 :             }
     144             :         }
     145     2527054 :     }
     146             : 
     147     3867214 :     void AddToDBStateCount(ListenerId listener, int count) {
     148     3867214 :         if (db_state_accounting_ && listener != DBTableBase::kInvalidId) {
     149     3652657 :             tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     150     3653351 :             state_count_[listener] += count;
     151     3654151 :         }
     152     3868366 :     }
     153             : 
     154     1307000 :     uint64_t GetDBStateCount(ListenerId listener) {
     155     1307000 :         assert(db_state_accounting_ && listener != DBTableBase::kInvalidId);
     156     1307016 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     157     2614370 :         return state_count_[listener];
     158     1307113 :     }
     159             : 
     160        6450 :     void FillListeners(vector<ShowTableListener> *listeners) const {
     161        6450 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     162        6574 :         ListenerId id = 0;
     163        6574 :         for (CallbackList::const_iterator iter = callbacks_.begin();
     164       15504 :              iter != callbacks_.end(); ++iter, ++id) {
     165        9002 :             if (*iter != NULL) {
     166        8995 :                 ShowTableListener item;
     167        8971 :                 item.id = id;
     168        8971 :                 item.name = names_[id];
     169        8979 :                 item.state_count = state_count_[id];
     170        8970 :                 listeners->push_back(item);
     171        8928 :             }
     172             :         }
     173        6494 :     }
     174             : 
     175     3757090 :     bool empty() const {
     176     3757090 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     177     7515155 :         return callbacks_.empty();
     178     3757342 :     }
     179             : 
     180       10317 :     size_t size() const {
     181       10317 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     182       20634 :         return (callbacks_.size() - bmap_.count());
     183       10317 :     }
     184             : 
     185             : private:
     186             :     bool db_state_accounting_;
     187             :     CallbackList callbacks_;
     188             :     NameList names_;
     189             :     StateCountList state_count_;
     190             :     mutable tbb::spin_rw_mutex rw_mutex_;
     191             :     boost::dynamic_bitset<> bmap_;      // free list.
     192             : };
     193             : 
     194     1530573 : DBTableBase::DBTableBase(DB *db, const string &name)
     195     1530574 :         : db_(db), name_(name), info_(new ListenerInfo(name)),
     196     1530573 :           enqueue_count_(0), input_count_(0), notify_count_(0) {
     197     1530547 :     walker_count_ = 0;
     198     1530680 :     walk_request_count_ = 0;
     199     1530662 :     walk_complete_count_ = 0;
     200     1530670 :     walk_cancel_count_ = 0;
     201     1530673 :     walk_again_count_ = 0;
     202     1530676 :     walk_count_ = 0;
     203     1530657 : }
     204             : 
     205     1530683 : DBTableBase::~DBTableBase() {
     206     1530683 : }
     207             : 
     208      793579 : DBTableBase::ListenerId DBTableBase::Register(ChangeCallback callback,
     209             :     const string &name) {
     210      793579 :     return info_->Register(callback, name);
     211             : }
     212             : 
     213      793651 : void DBTableBase::Unregister(ListenerId listener) {
     214      793651 :     info_->Unregister(listener);
     215             :     // If a table is marked for deletion, then we may trigger the deletion
     216             :     // process when the last client is removed
     217      793651 :     if (info_->empty())
     218      361331 :         RetryDelete();
     219      793650 : }
     220             : 
     221      647860 : bool DBTableBase::Enqueue(DBRequest *req) {
     222      647860 :     DBTablePartBase *tpart = GetTablePartition(req->key.get());
     223      647855 :     DBPartition *partition = db_->GetPartition(tpart->index());
     224      647855 :     enqueue_count_++;
     225      647855 :     return partition->EnqueueRequest(tpart, NULL, req);
     226             : }
     227             : 
     228      371129 : void DBTableBase::EnqueueRemove(DBEntryBase *db_entry) {
     229      371129 :     DBTablePartBase *tpart = GetTablePartition(db_entry);
     230      371062 :     DBPartition *partition = db_->GetPartition(tpart->index());
     231      371058 :     partition->EnqueueRemove(tpart, db_entry);
     232      371184 : }
     233             : 
     234     2527520 : void DBTableBase::RunNotify(DBTablePartBase *tpart, DBEntryBase *entry) {
     235     2527520 :     notify_count_++;
     236     2527520 :     info_->RunNotify(tpart, entry);
     237     2527832 : }
     238             : 
     239     3867566 : void DBTableBase::AddToDBStateCount(ListenerId listener, int count) {
     240     3867566 :     info_->AddToDBStateCount(listener, count);
     241     3868340 : }
     242             : 
     243     1307048 : uint64_t DBTableBase::GetDBStateCount(ListenerId listener) {
     244     1307048 :     return info_->GetDBStateCount(listener);
     245             : }
     246             : 
     247      288661 : bool DBTableBase::MayDelete() const {
     248      288661 :     if (HasListeners()) {
     249           0 :         return false;
     250             :     }
     251      288661 :     if (HasWalkers()) {
     252           0 :         return false;
     253             :     }
     254      288661 :     if (!empty()) {
     255           0 :         return false;
     256             :     }
     257             : 
     258      288661 :     return true;
     259             : }
     260             : 
     261     2963842 : bool DBTableBase::HasListeners() const {
     262     2963842 :     return !info_->empty();
     263             : }
     264             : 
     265       10317 : size_t DBTableBase::GetListenerCount() const {
     266       10317 :     return info_->size();
     267             : }
     268             : 
     269        6453 : void DBTableBase::FillListeners(vector<ShowTableListener> *listeners) const {
     270        6453 :     info_->FillListeners(listeners);
     271        6584 : }
     272             : 
     273             : class DBTable::WalkWorker : public Task {
     274             : public:
     275             :     WalkWorker(TableWalker *walker, int db_partition_id);
     276             : 
     277             :     virtual bool Run();
     278             : 
     279           0 :     std::string Description() const { return "DBTable::WalkWorker"; }
     280             : 
     281             : private:
     282             :     // Store the last visited node to continue walk
     283             :     std::unique_ptr<DBRequestKey> walk_ctx_;
     284             : 
     285             :     // Table partition for which this worker was created
     286             :     DBTablePartition *tbl_partition_;
     287             : 
     288             :     TableWalker *walker_;
     289             : };
     290             : 
     291             : class DBTable::TableWalker {
     292             : public:
     293     1530643 :     TableWalker(DBTable *table) : table_(table) {
     294     1530619 :         pending_workers_ = 0;
     295     1530678 :     }
     296             : 
     297             :     void StartWalk();
     298             : 
     299     1059169 :     DBTable *table() {
     300     1059169 :         return table_;
     301             :     }
     302             : 
     303      226065 :     void ClearWalkWorks() {
     304      226065 :         worker_tasks_.clear();
     305      226065 :     }
     306             : 
     307             :     DBTable *table_;
     308             :     // check whether iteration is completed on all Table Partition
     309             :     std::atomic<uint16_t> pending_workers_;
     310             :     // For debugging purpose. Few of the tasks in this list could has finished
     311             :     // executing and destroyed. List of workers are useful in debugging with
     312             :     // gdb/gcore to see the current state of the walk and walk_context
     313             :     std::list<Task *> worker_tasks_;
     314             : };
     315             : 
     316      795833 : bool DBTable::WalkWorker::Run() {
     317      795833 :     int count = 0;
     318      795833 :     DBRequestKey *key_resume = walk_ctx_.get();
     319      795827 :     DBTable *table = walker_->table();
     320      795819 :     int max_walk_entry_count = table->GetWalkIterationToYield();
     321             :     DBEntry *entry;
     322             : 
     323      795822 :     if (key_resume != NULL) {
     324      664162 :         std::unique_ptr<const DBEntryBase> start;
     325      664162 :         start = table->AllocEntry(key_resume);
     326             :         // Find matching or next in sort order
     327      664007 :         entry = tbl_partition_->lower_bound(start.get());
     328      664299 :     } else {
     329      131660 :         entry = tbl_partition_->GetFirst();
     330             :     }
     331      795757 :     if (entry == NULL) {
     332         596 :         goto walk_done;
     333             :     }
     334             : 
     335     1590382 :     for (DBEntry *next = NULL; entry; entry = next) {
     336     1459219 :         next = tbl_partition_->GetNext(entry);
     337     1459208 :         if (count == max_walk_entry_count) {
     338             :             // store the context
     339      664247 :             walk_ctx_ = entry->GetDBRequestKey();
     340      663714 :             return false;
     341             :         }
     342             : 
     343             :         // Invoke walker function
     344      794961 :         bool more = table->InvokeWalkCb(tbl_partition_, entry);
     345      795285 :         if (!more) {
     346         103 :             break;
     347             :         }
     348             : 
     349      795182 :         db_walker_wait();
     350      795221 :         count++;
     351             :     }
     352             : 
     353      131163 : walk_done:
     354             :     // Check whether all other walks on the table is completed
     355      131862 :     long num_walkers_on_tpart = walker_->pending_workers_.fetch_sub(1);
     356      131862 :     if (num_walkers_on_tpart == 1) {
     357      106852 :         table->WalkDone();
     358             :     }
     359      131690 :     return true;
     360             : }
     361             : 
     362      131690 : DBTable::WalkWorker::WalkWorker(TableWalker *walker, int db_partition_id)
     363      131690 :     : Task(walker->table()->GetWalkerTaskId(), db_partition_id), walker_(walker) {
     364      131690 :     tbl_partition_ = static_cast<DBTablePartition *>
     365      131690 :         (walker_->table()->GetTablePartition(db_partition_id));
     366      131690 : }
     367             : 
     368      226065 : void DBTable::TableWalker::StartWalk() {
     369      226065 :     CHECK_CONCURRENCY("db::Walker");
     370      226065 :     assert(pending_workers_ == 0);
     371      737703 :     for (int i = 0; i < table_->PartitionCount(); i++) {
     372             :         DBTablePartition *partition = static_cast<DBTablePartition *>(
     373      511638 :             table_->GetTablePartition(i));
     374      511638 :         if (!partition->size()) continue;
     375      131690 :         worker_tasks_.push_back(new WalkWorker(this, i));
     376      131690 :         pending_workers_++;
     377             :     }
     378      226065 :     if (pending_workers_ == 0) {
     379      119213 :         table_->WalkDone();
     380             :     } else {
     381      106852 :         TaskScheduler *scheduler = TaskScheduler::GetInstance();
     382      238542 :         for (auto *task : worker_tasks_) scheduler->Enqueue(task);
     383             :     }
     384      226065 : }
     385             : 
     386             : ///////////////////////////////////////////////////////////
     387             : // Implementation of DBTable methods
     388             : ///////////////////////////////////////////////////////////
     389     1530565 : DBTable::DBTable(DB *db, const string &name)
     390             :     : DBTableBase(db, name),
     391     1530659 :       walker_(new TableWalker(this)),
     392     3061145 :       walker_task_id_(db->task_id()) {
     393             : 
     394             :     static bool init_ = false;
     395             :     static int iter_to_yield_env_ = 0;
     396             : 
     397     1530543 :     if (!init_) {
     398             :         // XXX To be used for testing purposes only.
     399         158 :         char *count_str = getenv("DB_ITERATION_TO_YIELD");
     400         158 :         if (count_str) {
     401         158 :             iter_to_yield_env_ = strtol(count_str, NULL, 0);
     402             :         } else {
     403           0 :             iter_to_yield_env_ = kIterationToYield;
     404             :         }
     405         158 :         init_ = true;
     406             :     }
     407     1530543 :     max_walk_iteration_to_yield_ = iter_to_yield_env_;
     408     1530543 : }
     409             : 
     410     1530683 : DBTable::~DBTable() {
     411     1530683 :     STLDeleteValues(&partitions_);
     412     1530683 : }
     413             : 
     414     1522521 : void DBTable::Init() {
     415     3598282 :     for (int i = 0; i < PartitionCount(); i++) {
     416     2075972 :         partitions_.push_back(AllocPartition(i));
     417             :     }
     418     1522216 : }
     419             : 
     420     2075949 : DBTablePartition *DBTable::AllocPartition(int index) {
     421     2075949 :     return new DBTablePartition(this, index);
     422             : }
     423             : 
     424      226065 : void DBTable::StartWalk() {
     425      226065 :     CHECK_CONCURRENCY("db::Walker");
     426      226065 :     incr_walk_count();
     427      226065 :     walker_->StartWalk();
     428      226065 : }
     429             : 
     430      212752 : DBEntry *DBTable::Add(const DBRequest *req) {
     431      212752 :     return AllocEntry(req->key.get()).release();
     432             : }
     433             : 
     434        1334 : void DBTable::Change(DBEntryBase *entry) {
     435        1334 :     DBTablePartBase *tpart = GetTablePartition(entry);
     436        1334 :     tpart->Notify(entry);
     437        1334 : }
     438             : 
     439           0 : bool DBTable::OnChange(DBEntry *entry, const DBRequest *req) {
     440           0 :     return true;
     441             : }
     442             : 
     443           0 : bool DBTable::Delete(DBEntry *entry, const DBRequest *req) {
     444           0 :     return true;
     445             : }
     446             : 
     447     1412949 : int DBTable::PartitionCount() const {
     448     1412949 :     return DB::PartitionCount();
     449             : }
     450             : 
     451    16289561 : static size_t HashToPartition(size_t hash) {
     452    16289561 :     return hash % DB::PartitionCount();
     453             : }
     454             : 
     455    19746646 : DBTablePartBase *DBTable::GetTablePartition(const int index) {
     456    19746646 :     return partitions_[index];
     457             : }
     458             : 
     459       24101 : const DBTablePartBase *DBTable::GetTablePartition(const int index) const {
     460       24101 :     return partitions_[index];
     461             : }
     462             : 
     463      708998 : DBTablePartBase *DBTable::GetTablePartition(const DBRequestKey *key) {
     464      708998 :     int id = HashToPartition(Hash(key));
     465      708979 :     return GetTablePartition(id);
     466             : }
     467             : 
     468           0 : const DBTablePartBase *DBTable::GetTablePartition(
     469             :         const DBRequestKey *key) const {
     470           0 :     int id = HashToPartition(Hash(key));
     471           0 :     return GetTablePartition(id);
     472             : }
     473             : 
     474    15098447 : DBTablePartBase *DBTable::GetTablePartition(const DBEntryBase *entry) {
     475    15098447 :     const DBEntry *gentry = static_cast<const DBEntry *>(entry);
     476    15098447 :     size_t id = HashToPartition(Hash(gentry));
     477    15088546 :     return GetTablePartition(id);
     478             : }
     479             : 
     480       24101 : const DBTablePartBase *DBTable::GetTablePartition(
     481             :         const DBEntryBase *entry) const {
     482       24101 :     const DBEntry *gentry = static_cast<const DBEntry *>(entry);
     483       24101 :     size_t id = HashToPartition(Hash(gentry));
     484       24101 :     return GetTablePartition(id);
     485             : }
     486             : 
     487             : // Find DB Entry without taking lock. Calling routine must ensure its
     488             : // running in exclusion with DB task
     489         107 : DBEntry *DBTable::FindNoLock(const DBEntry *entry) {
     490         107 :     size_t id = HashToPartition(Hash(entry));
     491             :     DBTablePartition *tbl_partition =
     492         107 :         static_cast<DBTablePartition *>(GetTablePartition(id));
     493         107 :     return tbl_partition->FindNoLock(entry);
     494             : }
     495             : 
     496      383423 : DBEntry *DBTable::Find(const DBEntry *entry) {
     497      383423 :     size_t id = HashToPartition(Hash(entry));
     498             :     DBTablePartition *tbl_partition =
     499      383422 :         static_cast<DBTablePartition *>(GetTablePartition(id));
     500      383422 :     return tbl_partition->Find(entry);
     501             : }
     502             : 
     503           0 : const DBEntry *DBTable::Find(const DBEntry *entry) const {
     504           0 :     return const_cast<DBTable *>(this)->Find(entry);
     505             : }
     506             : 
     507             : // Find DB Entry without taking lock. Calling routine must ensure its
     508             : // running in exclusion with DB task
     509          18 : DBEntry *DBTable::FindNoLock(const DBRequestKey *key) {
     510          18 :     int id = HashToPartition(Hash(key));
     511             :     DBTablePartition *tbl_partition =
     512          18 :     static_cast<DBTablePartition *>(GetTablePartition(id));
     513          18 :     return tbl_partition->FindNoLock(key);
     514             : }
     515             : 
     516       85186 : DBEntry *DBTable::Find(const DBRequestKey *key, int id) {
     517       85186 :     if (id == -1)
     518       85158 :         id = HashToPartition(Hash(key));
     519             :     DBTablePartition *tbl_partition =
     520       85186 :     static_cast<DBTablePartition *>(GetTablePartition(id));
     521       85186 :     return tbl_partition->Find(key);
     522             : }
     523             : 
     524         260 : const DBEntry *DBTable::Find(const DBRequestKey *key, int id) const {
     525         260 :     return const_cast<DBTable *>(this)->Find(key, id);
     526             : }
     527             : 
     528             : //
     529             : // Concurrency: called from task that's mutually exclusive with db::DBTable
     530             : // or db::IFMapTable as applicable.
     531             : //
     532             : // Calculate the size across all partitions.
     533             : //
     534     1884426 : size_t DBTable::Size() const {
     535     1884426 :     size_t total = 0;
     536     1884426 :     for (vector<DBTablePartition *>::const_iterator iter = partitions_.begin();
     537     6974376 :          iter != partitions_.end(); iter++) {
     538     5090009 :         total += (*iter)->size();
     539             :     }
     540     1884387 :     return total;
     541             : }
     542             : 
     543        1218 : void DBTable::Input(DBTablePartition *tbl_partition, DBClient *client,
     544             :                     DBRequest *req) {
     545             :     DBRequestKey *key =
     546        1218 :         static_cast<DBRequestKey *>(req->key.get());
     547        1218 :     DBEntry *entry = NULL;
     548             : 
     549        1218 :     entry = tbl_partition->Find(key);
     550        1218 :     if (req->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
     551         667 :         if (entry) {
     552         362 :             if (OnChange(entry, req) || entry->IsDeleted()) {
     553             :                 // The entry may currently be marked as deleted.
     554          56 :                 entry->ClearDelete();
     555          56 :                 tbl_partition->Change(entry);
     556             :             }
     557             :         } else {
     558         305 :             if ((entry = Add(req)) != NULL) {
     559         305 :                 tbl_partition->Add(entry);
     560             :             }
     561             :         }
     562         551 :     } else if (req->oper == DBRequest::DB_ENTRY_DELETE) {
     563         330 :         if (entry) {
     564         330 :             if (Delete(entry, req)) {
     565         330 :                 tbl_partition->Delete(entry);
     566             :             }
     567             :         }
     568         221 :     } else if (req->oper == DBRequest::DB_ENTRY_NOTIFY) {
     569         221 :         if (entry) {
     570         221 :             tbl_partition->Notify(entry);
     571             :         }
     572             :     } else {
     573           0 :         assert(0);
     574             :     }
     575        1218 : }
     576             : 
     577           1 : void DBTable::DBStateClear(DBTable *table, ListenerId id) {
     578           1 :     DBEntryBase *next = NULL;
     579             : 
     580           2 :     for (int i = 0; i < table->PartitionCount(); ++i) {
     581             :         DBTablePartition *partition = static_cast<DBTablePartition *>(
     582           1 :             table->GetTablePartition(i));
     583             : 
     584           1 :         for (DBEntryBase *entry = partition->GetFirst(); entry; entry = next) {
     585           0 :             next = partition->GetNext(entry);
     586           0 :             DBState *state = entry->GetState(table, id);
     587           0 :             if (state) {
     588           0 :                 entry->ClearState(table, id);
     589           0 :                 delete state;
     590             :             }
     591             :         }
     592             :     }
     593           1 : }
     594             : 
     595             : //
     596             : // Callback for table walk triggered by NotifyAllEntries.
     597             : //
     598      129664 : bool DBTable::WalkCallback(DBTablePartBase *tpart, DBEntryBase *entry) {
     599      129664 :     tpart->Notify(entry);
     600      129666 :     return true;
     601             : }
     602             : 
     603             : //
     604             : // Callback for completion of table walk triggered by NotifyAllEntries.
     605             : //
     606       61307 : void DBTable::WalkCompleteCallback(DBTableBase *tbl_base) {
     607       61307 :     walk_ref_.reset();
     608       61307 : }
     609             : 
     610             : //
     611             : // Concurrency: called from task that's mutually exclusive with db::DBTable
     612             : // or db::IFMapTable as applicable.
     613             : //
     614             : // Trigger notification of all entries to all listeners.
     615             : // Should be used sparingly e.g. to handle significant configuration change.
     616             : //
     617             : // The walk callback just turns around and puts the DBentryBase on the change
     618             : // list.
     619             : //
     620             : // If the walk is already running, it is allowed to complete and WalkAgain API
     621             : // is invoked to trigger walk on current walk completion.
     622             : //
     623       63096 : void DBTable::NotifyAllEntries() {
     624       63096 :     CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper", "bgp::RTFilter",
     625             :                       "db::DBTable");
     626             : 
     627       63095 :     if (empty())
     628        1781 :         return;
     629             : 
     630       61313 :     if (walk_ref_ == NULL) {
     631             :         walk_ref_ =
     632      122614 :             AllocWalker(boost::bind(&DBTable::WalkCallback, this, _1, _2),
     633       61307 :                     boost::bind(&DBTable::WalkCompleteCallback, this, _2));
     634       61307 :         WalkTable(walk_ref_);
     635             :     } else {
     636           6 :         WalkAgain(walk_ref_);
     637             :     }
     638             : }
     639             : 
     640      219175 : DBTable::DBTableWalkRef DBTable::AllocWalker(WalkFn walk_fn,
     641             :                                              WalkCompleteFn walk_complete) {
     642      219175 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     643      219175 :     return walk_mgr->AllocWalker(this, walk_fn, walk_complete);
     644             : }
     645             : 
     646      157608 : void DBTable::ReleaseWalker(DBTable::DBTableWalkRef &walk) {
     647      157608 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     648      157608 :     walk_mgr->ReleaseWalker(walk);
     649      157608 :     return;
     650             : }
     651             : 
     652      226760 : void DBTable::WalkTable(DBTable::DBTableWalkRef walk) {
     653      226760 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     654      226760 :     walk_mgr->WalkTable(walk);
     655      226760 :     return;
     656             : }
     657             : 
     658       29112 : void DBTable::WalkAgain(DBTable::DBTableWalkRef walk) {
     659       29112 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     660       29112 :     walk_mgr->WalkAgain(walk);
     661       29112 :     return;
     662             : }
     663             : 
     664      795324 : bool DBTable::InvokeWalkCb(DBTablePartBase *part, DBEntryBase *entry) {
     665      795324 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     666      795217 :     return walk_mgr->InvokeWalkCb(part, entry);
     667             : }
     668             : 
     669      226065 : void DBTable::WalkDone() {
     670      226065 :     incr_walk_complete_count();
     671      226065 :     walker_->ClearWalkWorks();
     672      226065 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     673      226065 :     return walk_mgr->WalkDone();
     674             : }

Generated by: LCOV version 1.14