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 18268 : void DBTablePartBase::Notify(DBEntryBase *entry) { 18 18268 : if (entry->is_onlist()) { 19 7445 : return; 20 : } 21 10823 : entry->set_onlist(); 22 10823 : bool was_empty = change_list_.empty(); 23 10823 : change_list_.push_back(*entry); 24 10823 : if (was_empty) { 25 4770 : DB *db = parent()->database(); 26 4770 : DBPartition *partition = db->GetPartition(index_); 27 4770 : 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 4772 : bool DBTablePartBase::RunNotify() { 41 15565 : for (int i = 0; ((i < kMaxIterations) && !change_list_.empty()); ++i) { 42 10793 : DBEntryBase *entry = &change_list_.front(); 43 10793 : change_list_.pop_front(); 44 : 45 10793 : parent()->RunNotify(this, entry); 46 10793 : 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 11244 : if (entry->IsDeleted() && entry->is_state_empty(this) && 57 451 : !entry->IsOnRemoveQ()) { 58 451 : Remove(entry); 59 : } 60 : } 61 : 62 4772 : if (!change_list_.empty()) { 63 2 : DB *db = parent()->database(); 64 2 : DBPartition *partition = db->GetPartition(index_); 65 2 : partition->OnTableChange(this); 66 2 : return false; 67 : } 68 4770 : return true; 69 : } 70 : 71 8498 : void DBTablePartBase::Delete(DBEntryBase *entry) { 72 8498 : if (parent_->HasListeners()) { 73 597 : entry->MarkDelete(); 74 597 : Notify(entry); 75 : } else { 76 : // Remove from change_list 77 7901 : if (entry->is_onlist()) { 78 60 : change_list_.erase(change_list_.iterator_to(*entry)); 79 : } 80 7901 : Remove(entry); 81 : } 82 8498 : } 83 : 84 75417 : DBTablePartition::DBTablePartition(DBTable *table, int index) 85 75417 : : DBTablePartBase(table, index) { 86 75417 : } 87 : 88 8987 : void DBTablePartition::Process(DBClient *client, DBRequest *req) { 89 8987 : DBTable *table = static_cast<DBTable *>(parent()); 90 8987 : table->incr_input_count(); 91 8987 : table->Input(this, client, req); 92 8987 : } 93 : 94 8489 : void DBTablePartition::Add(DBEntry *entry) { 95 8489 : std::scoped_lock lock(mutex_); 96 8489 : std::pair<Tree::iterator, bool> ret = tree_.insert(*entry); 97 8489 : assert(ret.second); 98 8489 : entry->set_table_partition(static_cast<DBTablePartBase *>(this)); 99 8489 : Notify(entry); 100 8489 : parent()->AddRemoveCallback(entry, true); 101 8489 : } 102 : 103 7574 : void DBTablePartition::Change(DBEntry *entry) { 104 7574 : std::scoped_lock lock(mutex_); 105 7574 : Notify(entry); 106 7574 : } 107 : 108 8489 : void DBTablePartition::Remove(DBEntryBase *db_entry) { 109 8489 : std::scoped_lock lock(mutex_); 110 8489 : DBEntry *entry = static_cast<DBEntry *>(db_entry); 111 8489 : parent()->AddRemoveCallback(entry, false); 112 : 113 8489 : bool success = tree_.erase(*entry); 114 8488 : 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 8488 : 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 8489 : if (tree_.empty()) 124 2777 : table()->RetryDelete(); 125 8489 : } 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 27910 : DBEntry *DBTablePartition::FindInternal(const DBEntry *entry) { 145 27910 : Tree::iterator loc = tree_.find(*entry); 146 55820 : if (loc != tree_.end()) { 147 14365 : return loc.operator->(); 148 : } 149 13545 : return NULL; 150 : } 151 : 152 0 : const DBEntry *DBTablePartition::FindInternal(const DBEntry *entry) const { 153 0 : Tree::const_iterator loc = tree_.find(*entry); 154 0 : if (loc != tree_.end()) { 155 0 : return loc.operator->(); 156 : } 157 0 : return NULL; 158 : } 159 : 160 0 : DBEntry *DBTablePartition::FindNoLock(const DBEntry *entry) { 161 0 : CHECK_CONCURRENCY("db::DBTable", "db::IFMapTable", 162 : "Agent::FlowEvent", "Agent::FlowUpdate"); 163 0 : return FindInternal(entry); 164 : } 165 : 166 15286 : DBEntry *DBTablePartition::Find(const DBEntry *entry) { 167 15286 : std::scoped_lock lock(mutex_); 168 30572 : return FindInternal(entry); 169 15286 : } 170 : 171 0 : const DBEntry *DBTablePartition::Find(const DBEntry *entry) const { 172 0 : std::scoped_lock lock(mutex_); 173 0 : return FindInternal(entry); 174 0 : } 175 : 176 0 : DBEntry *DBTablePartition::FindNoLock(const DBRequestKey *key) { 177 0 : CHECK_CONCURRENCY("db::DBTable", "db::IFMapTable", 178 : "Agent::FlowEvent", "Agent::FlowUpdate"); 179 0 : DBTable *table = static_cast<DBTable *>(parent()); 180 0 : std::unique_ptr<DBEntry> entry_ptr = table->AllocEntry(key); 181 0 : return FindInternal(entry_ptr.get()); 182 0 : } 183 : 184 12624 : DBEntry *DBTablePartition::Find(const DBRequestKey *key) { 185 12624 : DBTable *table = static_cast<DBTable *>(parent()); 186 12624 : std::unique_ptr<DBEntry> entry_ptr = table->AllocEntry(key); 187 12624 : std::scoped_lock lock(mutex_); 188 25248 : return FindInternal(entry_ptr.get()); 189 12624 : } 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 0 : DBEntry *DBTablePartition::lower_bound(const DBEntryBase *key) { 205 0 : const DBEntry *entry = static_cast<const DBEntry *>(key); 206 0 : std::scoped_lock lock(mutex_); 207 : 208 0 : Tree::iterator it = tree_.lower_bound(*entry); 209 0 : if (it != tree_.end()) { 210 0 : return (it.operator->()); 211 : } 212 0 : return NULL; 213 0 : } 214 : 215 70170 : DBEntry *DBTablePartition::GetFirst() { 216 70170 : std::scoped_lock lock(mutex_); 217 70170 : Tree::iterator it = tree_.begin(); 218 140340 : if (it == tree_.end()) { 219 67210 : return NULL; 220 : } 221 2960 : return it.operator->(); 222 70170 : } 223 : 224 : // Returns the next entry (Doesn't search). Threaded walk 225 11793 : DBEntry *DBTablePartition::GetNext(const DBEntryBase *key) { 226 11793 : const DBEntry *entry = static_cast<const DBEntry *>(key); 227 11793 : std::scoped_lock lock(mutex_); 228 : 229 11793 : Tree::const_iterator it = tree_.iterator_to(*entry); 230 11793 : it++; 231 23586 : if (it != tree_.end()) { 232 8834 : return const_cast<DBEntry *>(it.operator->()); 233 : } 234 2959 : return NULL; 235 11793 : } 236 : 237 8678 : DBTable *DBTablePartition::table() { 238 8678 : return static_cast<DBTable *>(parent()); 239 : }