Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "base/logging.h" 6 : #include "base/task_annotations.h" 7 : #include "base/time_util.h" 8 : #include "db/db.h" 9 : #include "db/db_entry.h" 10 : #include "db/db_partition.h" 11 : #include "db/db_table.h" 12 : #include "db/db_table_partition.h" 13 : 14 : using namespace std; 15 : 16 : // concurrency: called from DBPartition task. 17 3912252 : void DBTablePartBase::Notify(DBEntryBase *entry) { 18 3912252 : if (entry->is_onlist()) { 19 1402000 : return; 20 : } 21 2510097 : entry->set_onlist(); 22 2515364 : bool was_empty = change_list_.empty(); 23 2514814 : change_list_.push_back(*entry); 24 2516217 : if (was_empty) { 25 1329644 : DB *db = parent()->database(); 26 1329622 : DBPartition *partition = db->GetPartition(index_); 27 1329603 : partition->OnTableChange(this); 28 : } 29 : } 30 : 31 : // 32 : // Concurrency: called from db::DBTable/db::IFMapTable task. 33 : // 34 : // Evaluate concurrency issues with DBEntryBase::ClearState when making 35 : // changes to this method. We expect that either this method or ClearState 36 : // is responsible for removing the DBEntryBase when they run concurrently, 37 : // assuming the DBEntryBase is eligible for removal. The dbstate_mutex is 38 : // used for synchronization. 39 : // 40 1329732 : bool DBTablePartBase::RunNotify() { 41 3847834 : for (int i = 0; ((i < kMaxIterations) && !change_list_.empty()); ++i) { 42 2517843 : DBEntryBase *entry = &change_list_.front(); 43 2517632 : change_list_.pop_front(); 44 : 45 2517540 : parent()->RunNotify(this, entry); 46 2517812 : entry->clear_onlist(); 47 : 48 : // If the entry is marked deleted and all DBStates are removed 49 : // and it's not already on the remove queue, it can be removed 50 : // from the tree right away. 51 : // 52 : // Note that IsOnRemoveQ must be called after is_state_empty as 53 : // synchronization with DBEntryBase::ClearState happens via the 54 : // call to is_state_empty, and ClearState can set the OnRemoveQ 55 : // bit in the entry. 56 2977867 : if (entry->IsDeleted() && entry->is_state_empty(this) && 57 459925 : !entry->IsOnRemoveQ()) { 58 459718 : Remove(entry); 59 : } 60 : } 61 : 62 1329725 : if (!change_list_.empty()) { 63 43 : DB *db = parent()->database(); 64 42 : DBPartition *partition = db->GetPartition(index_); 65 42 : partition->OnTableChange(this); 66 42 : return false; 67 : } 68 1329726 : return true; 69 : } 70 : 71 1149337 : void DBTablePartBase::Delete(DBEntryBase *entry) { 72 1149337 : if (parent_->HasListeners()) { 73 851605 : entry->MarkDelete(); 74 851473 : Notify(entry); 75 : } else { 76 : // Remove from change_list 77 298264 : if (entry->is_onlist()) { 78 1446 : change_list_.erase(change_list_.iterator_to(*entry)); 79 : } 80 298265 : Remove(entry); 81 : } 82 1149265 : } 83 : 84 2075400 : DBTablePartition::DBTablePartition(DBTable *table, int index) 85 2075400 : : DBTablePartBase(table, index) { 86 2075334 : } 87 : 88 648220 : void DBTablePartition::Process(DBClient *client, DBRequest *req) { 89 648220 : DBTable *table = static_cast<DBTable *>(parent()); 90 648199 : table->incr_input_count(); 91 648173 : table->Input(this, client, req); 92 648075 : } 93 : 94 1129208 : void DBTablePartition::Add(DBEntry *entry) { 95 1129208 : std::scoped_lock lock(mutex_); 96 1129344 : std::pair<Tree::iterator, bool> ret = tree_.insert(*entry); 97 1129182 : assert(ret.second); 98 1129182 : entry->set_table_partition(static_cast<DBTablePartBase *>(this)); 99 1129177 : Notify(entry); 100 1129264 : parent()->AddRemoveCallback(entry, true); 101 1129259 : } 102 : 103 224160 : void DBTablePartition::Change(DBEntry *entry) { 104 224160 : std::scoped_lock lock(mutex_); 105 224160 : Notify(entry); 106 224160 : } 107 : 108 1128950 : void DBTablePartition::Remove(DBEntryBase *db_entry) { 109 1128950 : std::scoped_lock lock(mutex_); 110 1129120 : DBEntry *entry = static_cast<DBEntry *>(db_entry); 111 1129120 : parent()->AddRemoveCallback(entry, false); 112 : 113 1129051 : bool success = tree_.erase(*entry); 114 1128754 : if (!success) { 115 0 : LOG(FATAL, "ABORT: DB node erase failed for table " + parent()->name()); 116 0 : LOG(FATAL, "Invalid node " + db_entry->ToString()); 117 0 : abort(); 118 : } 119 1128754 : delete entry; 120 : 121 : // If a table is marked for deletion, then we may trigger the deletion 122 : // process when the last prefix is deleted 123 1129230 : if (tree_.empty()) 124 134477 : table()->RetryDelete(); 125 1129169 : } 126 : 127 0 : void DBTablePartition::AddWithoutAlloc(DBEntry *entry) { 128 0 : std::scoped_lock lock(mutex_); 129 0 : tree_.insert(*entry); 130 0 : entry->set_table_partition(static_cast<DBTablePartBase *>(this)); 131 0 : Notify(entry); 132 0 : parent()->AddRemoveCallback(entry, true); 133 0 : } 134 : 135 0 : void DBTablePartition::RemoveWithoutDelete(DBEntry *entry) { 136 0 : std::scoped_lock lock(mutex_); 137 0 : bool success = tree_.erase(*entry); 138 0 : if (!success) { 139 0 : LOG(FATAL, "ABORT: DB node erase failed for table " + parent()->name()); 140 0 : abort(); 141 : } 142 0 : } 143 : 144 2921706 : DBEntry *DBTablePartition::FindInternal(const DBEntry *entry) { 145 2921706 : Tree::iterator loc = tree_.find(*entry); 146 5842877 : if (loc != tree_.end()) { 147 1365150 : return loc.operator->(); 148 : } 149 1556287 : return NULL; 150 : } 151 : 152 24086 : const DBEntry *DBTablePartition::FindInternal(const DBEntry *entry) const { 153 24086 : Tree::const_iterator loc = tree_.find(*entry); 154 48172 : if (loc != tree_.end()) { 155 16723 : return loc.operator->(); 156 : } 157 7363 : return NULL; 158 : } 159 : 160 98 : DBEntry *DBTablePartition::FindNoLock(const DBEntry *entry) { 161 98 : CHECK_CONCURRENCY("db::DBTable", "db::IFMapTable", 162 : "Agent::FlowEvent", "Agent::FlowUpdate"); 163 98 : return FindInternal(entry); 164 : } 165 : 166 2198053 : DBEntry *DBTablePartition::Find(const DBEntry *entry) { 167 2198053 : std::scoped_lock lock(mutex_); 168 4397194 : return FindInternal(entry); 169 2198279 : } 170 : 171 24086 : const DBEntry *DBTablePartition::Find(const DBEntry *entry) const { 172 24086 : std::scoped_lock lock(mutex_); 173 48172 : return FindInternal(entry); 174 24086 : } 175 : 176 17 : DBEntry *DBTablePartition::FindNoLock(const DBRequestKey *key) { 177 17 : CHECK_CONCURRENCY("db::DBTable", "db::IFMapTable", 178 : "Agent::FlowEvent", "Agent::FlowUpdate"); 179 17 : DBTable *table = static_cast<DBTable *>(parent()); 180 17 : std::unique_ptr<DBEntry> entry_ptr = table->AllocEntry(key); 181 34 : return FindInternal(entry_ptr.get()); 182 17 : } 183 : 184 723059 : DBEntry *DBTablePartition::Find(const DBRequestKey *key) { 185 723059 : DBTable *table = static_cast<DBTable *>(parent()); 186 723059 : std::unique_ptr<DBEntry> entry_ptr = table->AllocEntry(key); 187 723058 : std::scoped_lock lock(mutex_); 188 1446118 : return FindInternal(entry_ptr.get()); 189 723059 : } 190 : 191 0 : DBEntry *DBTablePartition::FindNext(const DBRequestKey *key) { 192 0 : std::scoped_lock lock(mutex_); 193 0 : DBTable *table = static_cast<DBTable *>(parent()); 194 0 : std::unique_ptr<DBEntry> entry_ptr = table->AllocEntry(key); 195 : 196 0 : Tree::iterator loc = tree_.upper_bound(*(entry_ptr.get())); 197 0 : if (loc != tree_.end()) { 198 0 : return loc.operator->(); 199 : } 200 0 : return NULL; 201 0 : } 202 : 203 : // Returns the matching entry or next in lex order 204 662548 : DBEntry *DBTablePartition::lower_bound(const DBEntryBase *key) { 205 662548 : const DBEntry *entry = static_cast<const DBEntry *>(key); 206 662548 : std::scoped_lock lock(mutex_); 207 : 208 662844 : Tree::iterator it = tree_.lower_bound(*entry); 209 1325328 : if (it != tree_.end()) { 210 662578 : return (it.operator->()); 211 : } 212 84 : return NULL; 213 662662 : } 214 : 215 1375393 : DBEntry *DBTablePartition::GetFirst() { 216 1375393 : std::scoped_lock lock(mutex_); 217 1375483 : Tree::iterator it = tree_.begin(); 218 2750890 : if (it == tree_.end()) { 219 1206619 : return NULL; 220 : } 221 168816 : return it.operator->(); 222 1375435 : } 223 : 224 : // Returns the next entry (Doesn't search). Threaded walk 225 1740679 : DBEntry *DBTablePartition::GetNext(const DBEntryBase *key) { 226 1740679 : const DBEntry *entry = static_cast<const DBEntry *>(key); 227 1740679 : std::scoped_lock lock(mutex_); 228 : 229 1741023 : Tree::const_iterator it = tree_.iterator_to(*entry); 230 1740809 : it++; 231 3480550 : if (it != tree_.end()) { 232 1513831 : return const_cast<DBEntry *>(it.operator->()); 233 : } 234 226321 : return NULL; 235 1740152 : } 236 : 237 141899 : DBTable *DBTablePartition::table() { 238 141899 : return static_cast<DBTable *>(parent()); 239 : }