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: 269 354 76.0 %
Date: 2026-11-23 02:04:52 Functions: 51 71 71.8 %
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       30596 : DBRequest::DBRequest() : oper(static_cast<DBOperation>(0)) {
      34       30596 : }
      35             : 
      36       30595 : 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       30595 :     assert(key_has_destructor && data_has_destructor);
      42             : #endif
      43             : #endif
      44       30594 : }
      45             : 
      46       15319 : void DBRequest::Swap(DBRequest *rhs) {
      47       15319 :     swap(oper, rhs->oper);
      48       15319 :     swap(key, rhs->key);
      49       15319 :     swap(data, rhs->data);
      50       15319 : }
      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        2356 :     AtomicWithCopy(const AtomicWithCopy& other) : std::atomic<_Tp>(other.load()) {}
      66             :     
      67             :     // Custom assignment operator
      68       10977 :     AtomicWithCopy& operator=(const AtomicWithCopy& other) {
      69       21954 :         this->store(other.load());
      70       10977 :         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       39797 :     explicit ListenerInfo(const string &table_name) :
      81       39797 :         db_state_accounting_(true) {
      82       39797 :         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       39001 :             db_state_accounting_ = false;
      86             :         }
      87       39797 :     }
      88             : 
      89       11077 :     DBTableBase::ListenerId Register(ChangeCallback callback,
      90             :         const string &name) {
      91       11077 :         tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
      92       11077 :         size_t i = bmap_.find_first();
      93       11077 :         if (i == bmap_.npos) {
      94       10977 :             i = callbacks_.size();
      95       10977 :             callbacks_.push_back(callback);
      96       10977 :             names_.push_back(name);
      97       10977 :             state_count_.resize(i + 1);
      98       10977 :             state_count_[i] = AtomicWithCopy<uint64_t>(0);
      99             :         } else {
     100         100 :             bmap_.reset(i);
     101         100 :             if (bmap_.none()) {
     102          80 :                 bmap_.clear();
     103             :             }
     104         100 :             callbacks_[i] = callback;
     105         100 :             names_[i] = name;
     106         100 :             state_count_[i] = 0;
     107             :         }
     108       11077 :         return i;
     109       11077 :     }
     110             : 
     111       11077 :     void Unregister(ListenerId listener) {
     112       11077 :         tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
     113       11077 :         callbacks_[listener] = NULL;
     114       11077 :         names_[listener] = "";
     115             :         // During Unregister Listener should have cleaned up,
     116             :         // DB states from all the entries in this table.
     117       11077 :         assert(state_count_[listener] == 0);
     118       11077 :         if ((size_t) listener == callbacks_.size() - 1) {
     119       21537 :             while (!callbacks_.empty() && callbacks_.back() == NULL) {
     120       10977 :                 callbacks_.pop_back();
     121       10977 :                 names_.pop_back();
     122       10977 :                 state_count_.pop_back();
     123             :             }
     124       10560 :             if (bmap_.size() > callbacks_.size()) {
     125         325 :                 bmap_.resize(callbacks_.size());
     126             :             }
     127             :         } else {
     128         517 :             if ((size_t) listener >= bmap_.size()) {
     129         446 :                 bmap_.resize(listener + 1);
     130             :             }
     131         517 :             bmap_.set(listener);
     132             :         }
     133       11077 :     }
     134             : 
     135             :     // concurrency: called from DBPartition task.
     136       22003 :     void RunNotify(DBTablePartBase *tpart, DBEntryBase *entry) {
     137       22003 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     138       22005 :         for (CallbackList::iterator iter = callbacks_.begin();
     139       70483 :              iter != callbacks_.end(); ++iter) {
     140       48485 :             if (*iter != NULL) {
     141       48286 :                 ChangeCallback cb = *iter;
     142       48279 :                 (cb)(tpart, entry);
     143       48287 :             }
     144             :         }
     145       21998 :     }
     146             : 
     147       31056 :     void AddToDBStateCount(ListenerId listener, int count) {
     148       31056 :         if (db_state_accounting_ && listener != DBTableBase::kInvalidId) {
     149       21223 :             tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     150       21230 :             state_count_[listener] += count;
     151       21233 :         }
     152       31062 :     }
     153             : 
     154        5445 :     uint64_t GetDBStateCount(ListenerId listener) {
     155        5445 :         assert(db_state_accounting_ && listener != DBTableBase::kInvalidId);
     156        5445 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     157       10891 :         return state_count_[listener];
     158        5446 :     }
     159             : 
     160           0 :     void FillListeners(vector<ShowTableListener> *listeners) const {
     161           0 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     162           0 :         ListenerId id = 0;
     163           0 :         for (CallbackList::const_iterator iter = callbacks_.begin();
     164           0 :              iter != callbacks_.end(); ++iter, ++id) {
     165           0 :             if (*iter != NULL) {
     166           0 :                 ShowTableListener item;
     167           0 :                 item.id = id;
     168           0 :                 item.name = names_[id];
     169           0 :                 item.state_count = state_count_[id];
     170           0 :                 listeners->push_back(item);
     171           0 :             }
     172             :         }
     173           0 :     }
     174             : 
     175       59081 :     bool empty() const {
     176       59081 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     177      118161 :         return callbacks_.empty();
     178       59080 :     }
     179             : 
     180           0 :     size_t size() const {
     181           0 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     182           0 :         return (callbacks_.size() - bmap_.count());
     183           0 :     }
     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       39797 : DBTableBase::DBTableBase(DB *db, const string &name)
     195       39797 :         : db_(db), name_(name), info_(new ListenerInfo(name)),
     196       39797 :           enqueue_count_(0), input_count_(0), notify_count_(0) {
     197       39797 :     walker_count_ = 0;
     198       39797 :     walk_request_count_ = 0;
     199       39797 :     walk_complete_count_ = 0;
     200       39797 :     walk_cancel_count_ = 0;
     201       39797 :     walk_again_count_ = 0;
     202       39797 :     walk_count_ = 0;
     203       39797 : }
     204             : 
     205       39797 : DBTableBase::~DBTableBase() {
     206       39797 : }
     207             : 
     208       11077 : DBTableBase::ListenerId DBTableBase::Register(ChangeCallback callback,
     209             :     const string &name) {
     210       11077 :     return info_->Register(callback, name);
     211             : }
     212             : 
     213       11077 : void DBTableBase::Unregister(ListenerId listener) {
     214       11077 :     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       11077 :     if (info_->empty())
     218        9698 :         RetryDelete();
     219       11077 : }
     220             : 
     221       15305 : bool DBTableBase::Enqueue(DBRequest *req) {
     222       15305 :     DBTablePartBase *tpart = GetTablePartition(req->key.get());
     223       15305 :     DBPartition *partition = db_->GetPartition(tpart->index());
     224       15305 :     enqueue_count_++;
     225       15305 :     return partition->EnqueueRequest(tpart, NULL, req);
     226             : }
     227             : 
     228        1283 : void DBTableBase::EnqueueRemove(DBEntryBase *db_entry) {
     229        1283 :     DBTablePartBase *tpart = GetTablePartition(db_entry);
     230        1283 :     DBPartition *partition = db_->GetPartition(tpart->index());
     231        1283 :     partition->EnqueueRemove(tpart, db_entry);
     232        1283 : }
     233             : 
     234       22003 : void DBTableBase::RunNotify(DBTablePartBase *tpart, DBEntryBase *entry) {
     235       22003 :     notify_count_++;
     236       22003 :     info_->RunNotify(tpart, entry);
     237       22003 : }
     238             : 
     239       31062 : void DBTableBase::AddToDBStateCount(ListenerId listener, int count) {
     240       31062 :     info_->AddToDBStateCount(listener, count);
     241       31062 : }
     242             : 
     243        5445 : uint64_t DBTableBase::GetDBStateCount(ListenerId listener) {
     244        5445 :     return info_->GetDBStateCount(listener);
     245             : }
     246             : 
     247         782 : bool DBTableBase::MayDelete() const {
     248         782 :     if (HasListeners()) {
     249           0 :         return false;
     250             :     }
     251         782 :     if (HasWalkers()) {
     252           0 :         return false;
     253             :     }
     254         782 :     if (!empty()) {
     255           0 :         return false;
     256             :     }
     257             : 
     258         782 :     return true;
     259             : }
     260             : 
     261       48004 : bool DBTableBase::HasListeners() const {
     262       48004 :     return !info_->empty();
     263             : }
     264             : 
     265           0 : size_t DBTableBase::GetListenerCount() const {
     266           0 :     return info_->size();
     267             : }
     268             : 
     269           0 : void DBTableBase::FillListeners(vector<ShowTableListener> *listeners) const {
     270           0 :     info_->FillListeners(listeners);
     271           0 : }
     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       39797 :     TableWalker(DBTable *table) : table_(table) {
     294       39797 :         pending_workers_ = 0;
     295       39797 :     }
     296             : 
     297             :     void StartWalk();
     298             : 
     299       21595 :     DBTable *table() {
     300       21595 :         return table_;
     301             :     }
     302             : 
     303        3259 :     void ClearWalkWorks() {
     304        3259 :         worker_tasks_.clear();
     305        3259 :     }
     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       10717 : bool DBTable::WalkWorker::Run() {
     317       10717 :     int count = 0;
     318       10717 :     DBRequestKey *key_resume = walk_ctx_.get();
     319       10712 :     DBTable *table = walker_->table();
     320       10709 :     int max_walk_entry_count = table->GetWalkIterationToYield();
     321             :     DBEntry *entry;
     322             : 
     323       10701 :     if (key_resume != NULL) {
     324        5324 :         std::unique_ptr<const DBEntryBase> start;
     325        5324 :         start = table->AllocEntry(key_resume);
     326             :         // Find matching or next in sort order
     327        5322 :         entry = tbl_partition_->lower_bound(start.get());
     328        5322 :     } else {
     329        5377 :         entry = tbl_partition_->GetFirst();
     330             :     }
     331       10740 :     if (entry == NULL) {
     332           1 :         goto walk_done;
     333             :     }
     334             : 
     335       21463 :     for (DBEntry *next = NULL; entry; entry = next) {
     336       16051 :         next = tbl_partition_->GetNext(entry);
     337       16058 :         if (count == max_walk_entry_count) {
     338             :             // store the context
     339        5321 :             walk_ctx_ = entry->GetDBRequestKey();
     340        5290 :             return false;
     341             :         }
     342             : 
     343             :         // Invoke walker function
     344       10737 :         bool more = table->InvokeWalkCb(tbl_partition_, entry);
     345       10727 :         if (!more) {
     346           0 :             break;
     347             :         }
     348             : 
     349       10727 :         db_walker_wait();
     350       10724 :         count++;
     351             :     }
     352             : 
     353        5412 : walk_done:
     354             :     // Check whether all other walks on the table is completed
     355        5413 :     long num_walkers_on_tpart = walker_->pending_workers_.fetch_sub(1);
     356        5413 :     if (num_walkers_on_tpart == 1) {
     357        1791 :         table->WalkDone();
     358             :     }
     359        5443 :     return true;
     360             : }
     361             : 
     362        5443 : DBTable::WalkWorker::WalkWorker(TableWalker *walker, int db_partition_id)
     363        5443 :     : Task(walker->table()->GetWalkerTaskId(), db_partition_id), walker_(walker) {
     364        5443 :     tbl_partition_ = static_cast<DBTablePartition *>
     365        5443 :         (walker_->table()->GetTablePartition(db_partition_id));
     366        5443 : }
     367             : 
     368        3259 : void DBTable::TableWalker::StartWalk() {
     369        3259 :     CHECK_CONCURRENCY("db::Walker");
     370        3259 :     assert(pending_workers_ == 0);
     371       15683 :     for (int i = 0; i < table_->PartitionCount(); i++) {
     372             :         DBTablePartition *partition = static_cast<DBTablePartition *>(
     373       12424 :             table_->GetTablePartition(i));
     374       12424 :         if (!partition->size()) continue;
     375        5443 :         worker_tasks_.push_back(new WalkWorker(this, i));
     376        5443 :         pending_workers_++;
     377             :     }
     378        3259 :     if (pending_workers_ == 0) {
     379        1468 :         table_->WalkDone();
     380             :     } else {
     381        1791 :         TaskScheduler *scheduler = TaskScheduler::GetInstance();
     382        7234 :         for (auto *task : worker_tasks_) scheduler->Enqueue(task);
     383             :     }
     384        3259 : }
     385             : 
     386             : ///////////////////////////////////////////////////////////
     387             : // Implementation of DBTable methods
     388             : ///////////////////////////////////////////////////////////
     389       39797 : DBTable::DBTable(DB *db, const string &name)
     390             :     : DBTableBase(db, name),
     391       39797 :       walker_(new TableWalker(this)),
     392       79594 :       walker_task_id_(db->task_id()) {
     393             : 
     394             :     static bool init_ = false;
     395             :     static int iter_to_yield_env_ = 0;
     396             : 
     397       39797 :     if (!init_) {
     398             :         // XXX To be used for testing purposes only.
     399          19 :         char *count_str = getenv("DB_ITERATION_TO_YIELD");
     400          19 :         if (count_str) {
     401          19 :             iter_to_yield_env_ = strtol(count_str, NULL, 0);
     402             :         } else {
     403           0 :             iter_to_yield_env_ = kIterationToYield;
     404             :         }
     405          19 :         init_ = true;
     406             :     }
     407       39797 :     max_walk_iteration_to_yield_ = iter_to_yield_env_;
     408       39797 : }
     409             : 
     410       39797 : DBTable::~DBTable() {
     411       39797 :     STLDeleteValues(&partitions_);
     412       39797 : }
     413             : 
     414       39797 : void DBTable::Init() {
     415       81370 :     for (int i = 0; i < PartitionCount(); i++) {
     416       41573 :         partitions_.push_back(AllocPartition(i));
     417             :     }
     418       39797 : }
     419             : 
     420       41573 : DBTablePartition *DBTable::AllocPartition(int index) {
     421       41573 :     return new DBTablePartition(this, index);
     422             : }
     423             : 
     424        3259 : void DBTable::StartWalk() {
     425        3259 :     CHECK_CONCURRENCY("db::Walker");
     426        3259 :     incr_walk_count();
     427        3259 :     walker_->StartWalk();
     428        3259 : }
     429             : 
     430        2108 : DBEntry *DBTable::Add(const DBRequest *req) {
     431        2108 :     return AllocEntry(req->key.get()).release();
     432             : }
     433             : 
     434        1282 : void DBTable::Change(DBEntryBase *entry) {
     435        1282 :     DBTablePartBase *tpart = GetTablePartition(entry);
     436        1282 :     tpart->Notify(entry);
     437        1282 : }
     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       18235 : int DBTable::PartitionCount() const {
     448       18235 :     return DB::PartitionCount();
     449             : }
     450             : 
     451      167368 : static size_t HashToPartition(size_t hash) {
     452      167368 :     return hash % DB::PartitionCount();
     453             : }
     454             : 
     455      252399 : DBTablePartBase *DBTable::GetTablePartition(const int index) {
     456      252399 :     return partitions_[index];
     457             : }
     458             : 
     459           0 : const DBTablePartBase *DBTable::GetTablePartition(const int index) const {
     460           0 :     return partitions_[index];
     461             : }
     462             : 
     463       16265 : DBTablePartBase *DBTable::GetTablePartition(const DBRequestKey *key) {
     464       16265 :     int id = HashToPartition(Hash(key));
     465       16265 :     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      135674 : DBTablePartBase *DBTable::GetTablePartition(const DBEntryBase *entry) {
     475      135674 :     const DBEntry *gentry = static_cast<const DBEntry *>(entry);
     476      135674 :     size_t id = HashToPartition(Hash(gentry));
     477      135668 :     return GetTablePartition(id);
     478             : }
     479             : 
     480           0 : const DBTablePartBase *DBTable::GetTablePartition(
     481             :         const DBEntryBase *entry) const {
     482           0 :     const DBEntry *gentry = static_cast<const DBEntry *>(entry);
     483           0 :     size_t id = HashToPartition(Hash(gentry));
     484           0 :     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           0 : DBEntry *DBTable::FindNoLock(const DBEntry *entry) {
     490           0 :     size_t id = HashToPartition(Hash(entry));
     491             :     DBTablePartition *tbl_partition =
     492           0 :         static_cast<DBTablePartition *>(GetTablePartition(id));
     493           0 :     return tbl_partition->FindNoLock(entry);
     494             : }
     495             : 
     496       12350 : DBEntry *DBTable::Find(const DBEntry *entry) {
     497       12350 :     size_t id = HashToPartition(Hash(entry));
     498             :     DBTablePartition *tbl_partition =
     499       12350 :         static_cast<DBTablePartition *>(GetTablePartition(id));
     500       12350 :     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           0 : DBEntry *DBTable::FindNoLock(const DBRequestKey *key) {
     510           0 :     int id = HashToPartition(Hash(key));
     511             :     DBTablePartition *tbl_partition =
     512           0 :     static_cast<DBTablePartition *>(GetTablePartition(id));
     513           0 :     return tbl_partition->FindNoLock(key);
     514             : }
     515             : 
     516        3043 : DBEntry *DBTable::Find(const DBRequestKey *key, int id) {
     517        3043 :     if (id == -1)
     518        3043 :         id = HashToPartition(Hash(key));
     519             :     DBTablePartition *tbl_partition =
     520        3043 :     static_cast<DBTablePartition *>(GetTablePartition(id));
     521        3043 :     return tbl_partition->Find(key);
     522             : }
     523             : 
     524         130 : const DBEntry *DBTable::Find(const DBRequestKey *key, int id) const {
     525         130 :     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        5314 : size_t DBTable::Size() const {
     535        5314 :     size_t total = 0;
     536        5314 :     for (vector<DBTablePartition *>::const_iterator iter = partitions_.begin();
     537       21779 :          iter != partitions_.end(); iter++) {
     538       16465 :         total += (*iter)->size();
     539             :     }
     540        5314 :     return total;
     541             : }
     542             : 
     543          48 : void DBTable::Input(DBTablePartition *tbl_partition, DBClient *client,
     544             :                     DBRequest *req) {
     545             :     DBRequestKey *key =
     546          48 :         static_cast<DBRequestKey *>(req->key.get());
     547          48 :     DBEntry *entry = NULL;
     548             : 
     549          48 :     entry = tbl_partition->Find(key);
     550          48 :     if (req->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
     551          26 :         if (entry) {
     552           5 :             if (OnChange(entry, req) || entry->IsDeleted()) {
     553             :                 // The entry may currently be marked as deleted.
     554           5 :                 entry->ClearDelete();
     555           5 :                 tbl_partition->Change(entry);
     556             :             }
     557             :         } else {
     558          21 :             if ((entry = Add(req)) != NULL) {
     559          21 :                 tbl_partition->Add(entry);
     560             :             }
     561             :         }
     562          22 :     } else if (req->oper == DBRequest::DB_ENTRY_DELETE) {
     563          22 :         if (entry) {
     564          22 :             if (Delete(entry, req)) {
     565          22 :                 tbl_partition->Delete(entry);
     566             :             }
     567             :         }
     568           0 :     } else if (req->oper == DBRequest::DB_ENTRY_NOTIFY) {
     569           0 :         if (entry) {
     570           0 :             tbl_partition->Notify(entry);
     571             :         }
     572             :     } else {
     573           0 :         assert(0);
     574             :     }
     575          48 : }
     576             : 
     577           0 : void DBTable::DBStateClear(DBTable *table, ListenerId id) {
     578           0 :     DBEntryBase *next = NULL;
     579             : 
     580           0 :     for (int i = 0; i < table->PartitionCount(); ++i) {
     581             :         DBTablePartition *partition = static_cast<DBTablePartition *>(
     582           0 :             table->GetTablePartition(i));
     583             : 
     584           0 :         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           0 : }
     594             : 
     595             : //
     596             : // Callback for table walk triggered by NotifyAllEntries.
     597             : //
     598           0 : bool DBTable::WalkCallback(DBTablePartBase *tpart, DBEntryBase *entry) {
     599           0 :     tpart->Notify(entry);
     600           0 :     return true;
     601             : }
     602             : 
     603             : //
     604             : // Callback for completion of table walk triggered by NotifyAllEntries.
     605             : //
     606           0 : void DBTable::WalkCompleteCallback(DBTableBase *tbl_base) {
     607           0 :     walk_ref_.reset();
     608           0 : }
     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           0 : void DBTable::NotifyAllEntries() {
     624           0 :     CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper", "bgp::RTFilter",
     625             :                       "db::DBTable");
     626             : 
     627           0 :     if (empty())
     628           0 :         return;
     629             : 
     630           0 :     if (walk_ref_ == NULL) {
     631             :         walk_ref_ =
     632           0 :             AllocWalker(boost::bind(&DBTable::WalkCallback, this, _1, _2),
     633           0 :                     boost::bind(&DBTable::WalkCompleteCallback, this, _2));
     634           0 :         WalkTable(walk_ref_);
     635             :     } else {
     636           0 :         WalkAgain(walk_ref_);
     637             :     }
     638             : }
     639             : 
     640        2738 : DBTable::DBTableWalkRef DBTable::AllocWalker(WalkFn walk_fn,
     641             :                                              WalkCompleteFn walk_complete) {
     642        2738 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     643        2738 :     return walk_mgr->AllocWalker(this, walk_fn, walk_complete);
     644             : }
     645             : 
     646        2738 : void DBTable::ReleaseWalker(DBTable::DBTableWalkRef &walk) {
     647        2738 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     648        2738 :     walk_mgr->ReleaseWalker(walk);
     649        2738 :     return;
     650             : }
     651             : 
     652        3506 : void DBTable::WalkTable(DBTable::DBTableWalkRef walk) {
     653        3506 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     654        3506 :     walk_mgr->WalkTable(walk);
     655        3506 :     return;
     656             : }
     657             : 
     658           0 : void DBTable::WalkAgain(DBTable::DBTableWalkRef walk) {
     659           0 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     660           0 :     walk_mgr->WalkAgain(walk);
     661           0 :     return;
     662             : }
     663             : 
     664       10730 : bool DBTable::InvokeWalkCb(DBTablePartBase *part, DBEntryBase *entry) {
     665       10730 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     666       10692 :     return walk_mgr->InvokeWalkCb(part, entry);
     667             : }
     668             : 
     669        3259 : void DBTable::WalkDone() {
     670        3259 :     incr_walk_complete_count();
     671        3259 :     walker_->ClearWalkWorks();
     672        3259 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     673        3259 :     return walk_mgr->WalkDone();
     674             : }

Generated by: LCOV version 1.14