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 32153 : void DBTablePartBase::Notify(DBEntryBase *entry) { 19 32153 : if (entry->is_onlist()) { 20 10128 : return; 21 : } 22 22024 : entry->set_onlist(); 23 22026 : bool was_empty = change_list_.empty(); 24 22025 : change_list_.push_back(*entry); 25 22025 : if (was_empty) { 26 14898 : DB *db = parent()->database(); 27 14899 : DBPartition *partition = db->GetPartition(index_); 28 14899 : 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 14902 : bool DBTablePartBase::RunNotify() { 42 36906 : for (int i = 0; ((i < kMaxIterations) && !change_list_.empty()); ++i) { 43 22004 : DBEntryBase *entry = &change_list_.front(); 44 22003 : change_list_.pop_front(); 45 : 46 22003 : parent()->RunNotify(this, entry); 47 22003 : 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 24022 : if (entry->IsDeleted() && entry->is_state_empty(this) && 58 2018 : !entry->IsOnRemoveQ()) { 59 2019 : Remove(entry); 60 : } 61 : } 62 : 63 14901 : if (!change_list_.empty()) { 64 2 : DB *db = parent()->database(); 65 2 : DBPartition *partition = db->GetPartition(index_); 66 2 : partition->OnTableChange(this); 67 2 : return false; 68 : } 69 14901 : return true; 70 : } 71 : 72 10159 : void DBTablePartBase::Delete(DBEntryBase *entry) { 73 10159 : if (parent_->HasListeners()) { 74 3577 : entry->MarkDelete(); 75 3577 : Notify(entry); 76 : } else { 77 : // Remove from change_list 78 6581 : if (entry->is_onlist()) { 79 50 : change_list_.erase(change_list_.iterator_to(*entry)); 80 : } 81 6581 : Remove(entry); 82 : } 83 10159 : } 84 : 85 41573 : DBTablePartition::DBTablePartition(DBTable *table, int index) 86 41573 : : DBTablePartBase(table, index) { 87 41573 : } 88 : 89 15305 : void DBTablePartition::Process(DBClient *client, DBRequest *req) { 90 15305 : DBTable *table = static_cast<DBTable *>(parent()); 91 15305 : table->incr_input_count(); 92 15305 : table->Input(this, client, req); 93 15305 : } 94 : 95 9883 : void DBTablePartition::Add(DBEntry *entry) { 96 9883 : std::scoped_lock lock(mutex_); 97 9883 : std::pair<Tree::iterator, bool> ret = tree_.insert(*entry); 98 9883 : assert(ret.second); 99 9883 : entry->set_table_partition(static_cast<DBTablePartBase *>(this)); 100 9883 : Notify(entry); 101 9883 : parent()->AddRemoveCallback(entry, true); 102 9883 : } 103 : 104 6376 : void DBTablePartition::Change(DBEntry *entry) { 105 6376 : std::scoped_lock lock(mutex_); 106 6376 : Notify(entry); 107 6376 : } 108 : 109 9883 : void DBTablePartition::Remove(DBEntryBase *db_entry) { 110 9883 : std::scoped_lock lock(mutex_); 111 9883 : DBEntry *entry = static_cast<DBEntry *>(db_entry); 112 9883 : parent()->AddRemoveCallback(entry, false); 113 : 114 9883 : bool success = tree_.erase(*entry); 115 9883 : 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 9883 : 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 9883 : if (tree_.empty()) 125 2747 : table()->RetryDelete(); 126 9883 : } 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 36794 : DBEntry *DBTablePartition::FindInternal(const DBEntry *entry) { 146 36794 : Tree::iterator loc = tree_.find(*entry); 147 73584 : if (loc != tree_.end()) { 148 21789 : return loc.operator->(); 149 : } 150 15003 : return NULL; 151 : } 152 : 153 0 : const DBEntry *DBTablePartition::FindInternal(const DBEntry *entry) const { 154 0 : Tree::const_iterator loc = tree_.find(*entry); 155 0 : if (loc != tree_.end()) { 156 0 : return loc.operator->(); 157 : } 158 0 : return NULL; 159 : } 160 : 161 0 : DBEntry *DBTablePartition::FindNoLock(const DBEntry *entry) { 162 0 : CHECK_CONCURRENCY("db::DBTable", "db::IFMapTable", 163 : "Agent::FlowEvent", "Agent::FlowUpdate"); 164 0 : return FindInternal(entry); 165 : } 166 : 167 22876 : DBEntry *DBTablePartition::Find(const DBEntry *entry) { 168 22876 : std::scoped_lock lock(mutex_); 169 45756 : return FindInternal(entry); 170 22876 : } 171 : 172 0 : const DBEntry *DBTablePartition::Find(const DBEntry *entry) const { 173 0 : std::scoped_lock lock(mutex_); 174 0 : return FindInternal(entry); 175 0 : } 176 : 177 0 : DBEntry *DBTablePartition::FindNoLock(const DBRequestKey *key) { 178 0 : CHECK_CONCURRENCY("db::DBTable", "db::IFMapTable", 179 : "Agent::FlowEvent", "Agent::FlowUpdate"); 180 0 : DBTable *table = static_cast<DBTable *>(parent()); 181 0 : std::unique_ptr<DBEntry> entry_ptr = table->AllocEntry(key); 182 0 : return FindInternal(entry_ptr.get()); 183 0 : } 184 : 185 13916 : DBEntry *DBTablePartition::Find(const DBRequestKey *key) { 186 13916 : DBTable *table = static_cast<DBTable *>(parent()); 187 13916 : std::unique_ptr<DBEntry> entry_ptr = table->AllocEntry(key); 188 13916 : std::scoped_lock lock(mutex_); 189 27832 : return FindInternal(entry_ptr.get()); 190 13916 : } 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 5322 : DBEntry *DBTablePartition::lower_bound(const DBEntryBase *key) { 206 5322 : const DBEntry *entry = static_cast<const DBEntry *>(key); 207 5322 : std::scoped_lock lock(mutex_); 208 : 209 5321 : Tree::iterator it = tree_.lower_bound(*entry); 210 10633 : if (it != tree_.end()) { 211 5317 : return (it.operator->()); 212 : } 213 0 : return NULL; 214 5317 : } 215 : 216 49759 : DBEntry *DBTablePartition::GetFirst() { 217 49759 : std::scoped_lock lock(mutex_); 218 49793 : Tree::iterator it = tree_.begin(); 219 99562 : if (it == tree_.end()) { 220 42485 : return NULL; 221 : } 222 7292 : return it.operator->(); 223 49777 : } 224 : 225 : // Returns the next entry (Doesn't search). Threaded walk 226 26518 : DBEntry *DBTablePartition::GetNext(const DBEntryBase *key) { 227 26518 : const DBEntry *entry = static_cast<const DBEntry *>(key); 228 26518 : std::scoped_lock lock(mutex_); 229 : 230 26516 : Tree::const_iterator it = tree_.iterator_to(*entry); 231 26499 : it++; 232 52982 : if (it != tree_.end()) { 233 16241 : return const_cast<DBEntry *>(it.operator->()); 234 : } 235 10236 : return NULL; 236 26477 : } 237 : 238 8634 : DBTable *DBTablePartition::table() { 239 8634 : return static_cast<DBTable *>(parent()); 240 : }