LCOV - code coverage report
Current view: top level - db - db_table_partition.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 122 148 82.4 %
Date: 2026-11-30 02:00:48 Functions: 19 22 86.4 %
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 "base/logging.h"
       7             : #include "base/task_annotations.h"
       8             : #include "base/time_util.h"
       9             : #include "db/db.h"
      10             : #include "db/db_entry.h"
      11             : #include "db/db_partition.h"
      12             : #include "db/db_table.h"
      13             : #include "db/db_table_partition.h"
      14             : 
      15             : using namespace std;
      16             : 
      17             : // concurrency: called from DBPartition task.
      18     3940667 : void DBTablePartBase::Notify(DBEntryBase *entry) {
      19     3940667 :     if (entry->is_onlist()) {
      20     1420820 :         return;
      21             :     }
      22     2519833 :     entry->set_onlist();
      23     2524945 :     bool was_empty = change_list_.empty();
      24     2524645 :     change_list_.push_back(*entry);
      25     2526229 :     if (was_empty) {
      26     1340368 :         DB *db = parent()->database();
      27     1340340 :         DBPartition *partition = db->GetPartition(index_);
      28     1340317 :         partition->OnTableChange(this);
      29             :     }
      30             : }
      31             : 
      32             : //
      33             : // Concurrency: called from db::DBTable/db::IFMapTable task.
      34             : //
      35             : // Evaluate concurrency issues with DBEntryBase::ClearState when making
      36             : // changes to this method. We expect that either this method or ClearState
      37             : // is responsible for removing the DBEntryBase when they run concurrently,
      38             : // assuming the DBEntryBase is eligible for removal. The dbstate_mutex is
      39             : // used for synchronization.
      40             : //
      41     1340429 : bool DBTablePartBase::RunNotify() {
      42     3868331 :     for (int i = 0; ((i < kMaxIterations) && !change_list_.empty()); ++i) {
      43     2527766 :         DBEntryBase *entry = &change_list_.front();
      44     2527655 :         change_list_.pop_front();
      45             : 
      46     2527564 :         parent()->RunNotify(this, entry);
      47     2527843 :         entry->clear_onlist();
      48             : 
      49             :         // If the entry is marked deleted and all DBStates are removed
      50             :         // and it's not already on the remove queue, it can be removed
      51             :         // from the tree right away.
      52             :         //
      53             :         // Note that IsOnRemoveQ must be called after is_state_empty as
      54             :         // synchronization with DBEntryBase::ClearState happens via the
      55             :         // call to is_state_empty, and ClearState can set the OnRemoveQ
      56             :         // bit in the entry.
      57     2987392 :         if (entry->IsDeleted() && entry->is_state_empty(this) &&
      58      459468 :             !entry->IsOnRemoveQ()) {
      59      459418 :             Remove(entry);
      60             :         }
      61             :     }
      62             : 
      63     1340381 :     if (!change_list_.empty()) {
      64          43 :         DB *db = parent()->database();
      65          43 :         DBPartition *partition = db->GetPartition(index_);
      66          43 :         partition->OnTableChange(this);
      67          43 :         return false;
      68             :     }
      69     1340400 :     return true;
      70             : }
      71             : 
      72     1148964 : void DBTablePartBase::Delete(DBEntryBase *entry) {
      73     1148964 :     if (parent_->HasListeners()) {
      74      851057 :         entry->MarkDelete();
      75      851006 :         Notify(entry);
      76             :     } else {
      77             :         // Remove from change_list
      78      298330 :         if (entry->is_onlist()) {
      79        1426 :             change_list_.erase(change_list_.iterator_to(*entry));
      80             :         }
      81      298329 :         Remove(entry);
      82             :     }
      83     1149269 : }
      84             : 
      85     2076122 : DBTablePartition::DBTablePartition(DBTable *table, int index)
      86     2076122 :     : DBTablePartBase(table, index) {
      87     2076163 : }
      88             : 
      89      648998 : void DBTablePartition::Process(DBClient *client, DBRequest *req) {
      90      648998 :     DBTable *table = static_cast<DBTable *>(parent());
      91      648998 :     table->incr_input_count();
      92      648991 :     table->Input(this, client, req);
      93      648983 : }
      94             : 
      95     1128963 : void DBTablePartition::Add(DBEntry *entry) {
      96     1128963 :     std::scoped_lock lock(mutex_);
      97     1129036 :     std::pair<Tree::iterator, bool> ret = tree_.insert(*entry);
      98     1128910 :     assert(ret.second);
      99     1128910 :     entry->set_table_partition(static_cast<DBTablePartBase *>(this));
     100     1128878 :     Notify(entry);
     101     1128953 :     parent()->AddRemoveCallback(entry, true);
     102     1128941 : }
     103             : 
     104      224239 : void DBTablePartition::Change(DBEntry *entry) {
     105      224239 :     std::scoped_lock lock(mutex_);
     106      224239 :     Notify(entry);
     107      224239 : }
     108             : 
     109     1128871 : void DBTablePartition::Remove(DBEntryBase *db_entry) {
     110     1128871 :     std::scoped_lock lock(mutex_);
     111     1128916 :     DBEntry *entry = static_cast<DBEntry *>(db_entry);
     112     1128916 :     parent()->AddRemoveCallback(entry, false);
     113             : 
     114     1128849 :     bool success = tree_.erase(*entry);
     115     1128557 :     if (!success) {
     116           0 :         LOG(FATAL, "ABORT: DB node erase failed for table " + parent()->name());
     117           0 :         LOG(FATAL, "Invalid node " + db_entry->ToString());
     118           0 :         abort();
     119             :     }
     120     1128557 :     delete entry;
     121             : 
     122             :     // If a table is marked for deletion, then we may trigger the deletion
     123             :     // process when the last prefix is deleted
     124     1128965 :     if (tree_.empty())
     125      134577 :         table()->RetryDelete();
     126     1128937 : }
     127             : 
     128           0 : void DBTablePartition::AddWithoutAlloc(DBEntry *entry) {
     129           0 :     std::scoped_lock lock(mutex_);
     130           0 :     tree_.insert(*entry);
     131           0 :     entry->set_table_partition(static_cast<DBTablePartBase *>(this));
     132           0 :     Notify(entry);
     133           0 :     parent()->AddRemoveCallback(entry, true);
     134           0 : }
     135             : 
     136           0 : void DBTablePartition::RemoveWithoutDelete(DBEntry *entry) {
     137           0 :     std::scoped_lock lock(mutex_);
     138           0 :     bool success = tree_.erase(*entry);
     139           0 :     if (!success) {
     140           0 :         LOG(FATAL, "ABORT: DB node erase failed for table " + parent()->name());
     141           0 :         abort();
     142             :     }
     143           0 : }
     144             : 
     145     2943487 : DBEntry *DBTablePartition::FindInternal(const DBEntry *entry) {
     146     2943487 :     Tree::iterator loc = tree_.find(*entry);
     147     5886587 :     if (loc != tree_.end()) {
     148     1384620 :         return loc.operator->();
     149             :     }
     150     1558666 :     return NULL;
     151             : }
     152             : 
     153       24101 : const DBEntry *DBTablePartition::FindInternal(const DBEntry *entry) const {
     154       24101 :     Tree::const_iterator loc = tree_.find(*entry);
     155       48202 :     if (loc != tree_.end()) {
     156       16726 :         return loc.operator->();
     157             :     }
     158        7375 :     return NULL;
     159             : }
     160             : 
     161         107 : DBEntry *DBTablePartition::FindNoLock(const DBEntry *entry) {
     162         107 :     CHECK_CONCURRENCY("db::DBTable", "db::IFMapTable",
     163             :         "Agent::FlowEvent", "Agent::FlowUpdate");
     164         107 :     return FindInternal(entry);
     165             : }
     166             : 
     167     2219955 : DBEntry *DBTablePartition::Find(const DBEntry *entry) {
     168     2219955 :     std::scoped_lock lock(mutex_);
     169     4440516 :     return FindInternal(entry);
     170     2220046 : }
     171             : 
     172       24101 : const DBEntry *DBTablePartition::Find(const DBEntry *entry) const {
     173       24101 :     std::scoped_lock lock(mutex_);
     174       48202 :     return FindInternal(entry);
     175       24101 : }
     176             : 
     177          18 : DBEntry *DBTablePartition::FindNoLock(const DBRequestKey *key) {
     178          18 :     CHECK_CONCURRENCY("db::DBTable", "db::IFMapTable",
     179             :         "Agent::FlowEvent", "Agent::FlowUpdate");
     180          18 :     DBTable *table = static_cast<DBTable *>(parent());
     181          18 :     std::unique_ptr<DBEntry> entry_ptr = table->AllocEntry(key);
     182          36 :     return FindInternal(entry_ptr.get());
     183          18 : }
     184             : 
     185      723123 : DBEntry *DBTablePartition::Find(const DBRequestKey *key) {
     186      723123 :     DBTable *table = static_cast<DBTable *>(parent());
     187      723123 :     std::unique_ptr<DBEntry> entry_ptr = table->AllocEntry(key);
     188      723123 :     std::scoped_lock lock(mutex_);
     189     1446246 :     return FindInternal(entry_ptr.get());
     190      723123 : }
     191             : 
     192           0 : DBEntry *DBTablePartition::FindNext(const DBRequestKey *key) {
     193           0 :     std::scoped_lock lock(mutex_);
     194           0 :     DBTable *table = static_cast<DBTable *>(parent());
     195           0 :     std::unique_ptr<DBEntry> entry_ptr = table->AllocEntry(key);
     196             : 
     197           0 :     Tree::iterator loc = tree_.upper_bound(*(entry_ptr.get()));
     198           0 :     if (loc != tree_.end()) {
     199           0 :         return loc.operator->();
     200             :     }
     201           0 :     return NULL;
     202           0 : }
     203             : 
     204             : // Returns the matching entry or next in lex order
     205      664128 : DBEntry *DBTablePartition::lower_bound(const DBEntryBase *key) {
     206      664128 :     const DBEntry *entry = static_cast<const DBEntry *>(key);
     207      664128 :     std::scoped_lock lock(mutex_);
     208             : 
     209      664447 :     Tree::iterator it = tree_.lower_bound(*entry);
     210     1328626 :     if (it != tree_.end()) {
     211      664222 :         return (it.operator->());
     212             :     }
     213          90 :     return NULL;
     214      664312 : }
     215             : 
     216     1377964 : DBEntry *DBTablePartition::GetFirst() {
     217     1377964 :     std::scoped_lock lock(mutex_);
     218     1378022 :     Tree::iterator it = tree_.begin();
     219     2756016 :     if (it == tree_.end()) {
     220     1207710 :         return NULL;
     221             :     }
     222      170290 :     return it.operator->();
     223     1378000 : }
     224             : 
     225             : // Returns the next entry (Doesn't search). Threaded walk
     226     1746713 : DBEntry *DBTablePartition::GetNext(const DBEntryBase *key) {
     227     1746713 :     const DBEntry *entry = static_cast<const DBEntry *>(key);
     228     1746713 :     std::scoped_lock lock(mutex_);
     229             : 
     230     1746995 :     Tree::const_iterator it = tree_.iterator_to(*entry);
     231     1746819 :     it++;
     232     3492617 :     if (it != tree_.end()) {
     233     1517675 :         return const_cast<DBEntry *>(it.operator->());
     234             :     }
     235      228521 :     return NULL;
     236     1746196 : }
     237             : 
     238      141913 : DBTable *DBTablePartition::table() {
     239      141913 :     return static_cast<DBTable *>(parent());
     240             : }

Generated by: LCOV version 1.14