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 "db/db_partition.h" 7 : 8 : #include <list> 9 : #include <mutex> 10 : #include <atomic> 11 : 12 : #include <tbb/concurrent_queue.h> 13 : 14 : #include "base/task.h" 15 : #include "db/db.h" 16 : #include "db/db_client.h" 17 : #include "db/db_entry.h" 18 : 19 : using tbb::concurrent_queue; 20 : 21 : struct RequestQueueEntry { 22 : // Constructor takes ownership of DBRequest key, data. 23 1165163 : RequestQueueEntry(DBTablePartBase *tpart, DBClient *client, DBRequest *req) 24 1165163 : : tpart(tpart), client(client) { 25 1165162 : request.Swap(req); 26 1165141 : } 27 : DBTablePartBase *tpart; 28 : DBClient *client; 29 : DBRequest request; 30 : }; 31 : 32 : struct RemoveQueueEntry { 33 523774 : RemoveQueueEntry(DBTablePartBase *tpart, DBEntryBase *db_entry) 34 523774 : : tpart(tpart), db_entry(db_entry) { 35 523774 : } 36 : DBTablePartBase *tpart; 37 : DBEntryBase *db_entry; 38 : }; 39 : 40 : class DBPartition::WorkQueue { 41 : public: 42 : static const int kThreshold = 1024; 43 : typedef concurrent_queue<RequestQueueEntry *> RequestQueue; 44 : typedef concurrent_queue<RemoveQueueEntry *> RemoveQueue; 45 : typedef std::list<DBTablePartBase *> TablePartList; 46 : 47 109374 : explicit WorkQueue(DBPartition *partition, int partition_id) 48 109374 : : db_partition_(partition), 49 109374 : db_partition_id_(partition_id), 50 109374 : disable_(false), 51 109374 : running_(false) { 52 109374 : request_count_ = 0; 53 109374 : max_request_queue_len_ = 0; 54 109374 : total_request_count_ = 0; 55 109374 : } 56 109310 : ~WorkQueue() { 57 109310 : for (RequestQueue::iterator iter = request_queue_.unsafe_begin(); 58 109310 : iter != request_queue_.unsafe_end();) { 59 0 : RequestQueueEntry *req_entry = *iter; 60 0 : ++iter; 61 0 : delete req_entry; 62 : } 63 109310 : request_queue_.clear(); 64 109310 : } 65 : 66 1165139 : bool EnqueueRequest(RequestQueueEntry *req_entry) { 67 1165139 : request_queue_.push(req_entry); 68 1165192 : MaybeStartRunner(); 69 1165209 : uint32_t max = request_count_.fetch_add(1); 70 1165209 : if (max > max_request_queue_len_) 71 157877 : max_request_queue_len_ = max; 72 1165209 : total_request_count_++; 73 1165209 : return max < (kThreshold - 1); 74 : 75 : } 76 : 77 2016795 : bool DequeueRequest(RequestQueueEntry **req_entry) { 78 2016795 : bool success = request_queue_.try_pop(*req_entry); 79 2016817 : if (success) { 80 1165200 : request_count_.fetch_sub(1); 81 : } 82 2016817 : return success; 83 : } 84 : 85 523924 : void EnqueueRemove(RemoveQueueEntry *rm_entry) { 86 523924 : remove_queue_.push(rm_entry); 87 523907 : MaybeStartRunner(); 88 523966 : } 89 : 90 1380135 : bool DequeueRemove(RemoveQueueEntry **rm_entry) { 91 1380135 : bool success = remove_queue_.try_pop(*rm_entry); 92 1380159 : return success; 93 : } 94 : 95 : void MaybeStartRunnerUnlocked(); 96 : void MaybeStartRunner(); 97 : bool RunnerDone(); 98 : 99 : // Normally called from single task that either runs in DB context or is 100 : // exclusive with DB task, but can be called concurrently from multiple 101 : // bgp::ConfigHelper tasks. 102 1699927 : void SetActive(DBTablePartBase *tpart) { 103 1699927 : std::scoped_lock lock(mutex_); 104 1700208 : change_list_.push_back(tpart); 105 1699817 : MaybeStartRunnerUnlocked(); 106 1699952 : } 107 : 108 2551653 : DBTablePartBase *GetActiveTable() { 109 2551653 : DBTablePartBase *tpart = NULL; 110 2551653 : if (!change_list_.empty()) { 111 1700136 : tpart = change_list_.front(); 112 1700084 : change_list_.pop_front(); 113 : } 114 2551648 : return tpart; 115 : } 116 : 117 803785 : int db_partition_id() { 118 803785 : return db_partition_id_; 119 : } 120 : 121 803775 : int db_task_id() const { return db_partition_->task_id(); } 122 : 123 6252026 : bool IsDBQueueEmpty() const { 124 6252026 : return (request_queue_.empty() && change_list_.empty()); 125 : } 126 : 127 859453 : bool disable() { return disable_; } 128 90 : void set_disable(bool disable) { disable_ = disable; } 129 : 130 0 : long request_queue_len() const { 131 0 : return request_count_; 132 : } 133 : 134 0 : uint64_t total_request_count() const { 135 0 : return total_request_count_; 136 : } 137 : 138 0 : uint64_t max_request_queue_len() const { 139 0 : return max_request_queue_len_; 140 : } 141 : 142 : private: 143 : DBPartition *db_partition_; 144 : RequestQueue request_queue_; 145 : TablePartList change_list_; 146 : std::atomic<long> request_count_; 147 : uint64_t total_request_count_; 148 : uint64_t max_request_queue_len_; 149 : RemoveQueue remove_queue_; 150 : std::mutex mutex_; 151 : int db_partition_id_; 152 : bool disable_; 153 : bool running_; 154 : 155 : DISALLOW_COPY_AND_ASSIGN(WorkQueue); 156 : }; 157 : 158 6252026 : bool DBPartition::IsDBQueueEmpty() const { 159 6252026 : return work_queue_->IsDBQueueEmpty(); 160 : } 161 : 162 90 : void DBPartition::SetQueueDisable(bool disable) { 163 90 : if (disable) { 164 45 : work_queue_->set_disable(true); 165 : } else { 166 45 : work_queue_->set_disable(false); 167 45 : work_queue_->MaybeStartRunner(); 168 : } 169 90 : } 170 : 171 : class DBPartition::QueueRunner : public Task { 172 : public: 173 : static const int kMaxIterations = 32; 174 803788 : QueueRunner(WorkQueue *queue) 175 803788 : : Task(queue->db_task_id(), queue->db_partition_id()), 176 803788 : queue_(queue) { 177 803749 : } 178 : 179 859450 : virtual bool Run() { 180 859450 : int count = 0; 181 : 182 : // Skip if the queue is disabled. 183 859450 : if (queue_->disable()) 184 1110 : return queue_->RunnerDone(); 185 : 186 858341 : RemoveQueueEntry *rm_entry = NULL; 187 1380174 : while (queue_->DequeueRemove(&rm_entry)) { 188 523965 : DBEntryBase *db_entry = rm_entry->db_entry; 189 : { 190 : tbb::spin_rw_mutex::scoped_lock 191 523965 : lock(rm_entry->tpart->dbstate_mutex(), false); 192 1047782 : if (!db_entry->IsDeleted() || db_entry->is_onlist() || 193 523867 : !db_entry->is_state_empty_unlocked(rm_entry->tpart)) { 194 43 : db_entry->ClearOnRemoveQ(); 195 43 : db_entry = NULL; 196 : } 197 523899 : } 198 523978 : if (db_entry) { 199 523935 : rm_entry->tpart->Remove(db_entry); 200 : } 201 523974 : delete rm_entry; 202 523990 : if (++count == kMaxIterations) { 203 2157 : return false; 204 : } 205 : } 206 : 207 856188 : RequestQueueEntry *req_entry = NULL; 208 2016800 : while (queue_->DequeueRequest(&req_entry)) { 209 1165189 : req_entry->tpart->Process(req_entry->client, &req_entry->request); 210 1165155 : delete req_entry; 211 1165181 : if (++count == kMaxIterations) { 212 4569 : return false; 213 : } 214 : } 215 : 216 : while (true) { 217 2551654 : DBTablePartBase *tpart = queue_->GetActiveTable(); 218 2551596 : if (tpart == NULL) { 219 851574 : break; 220 : } 221 1700022 : bool done = tpart->RunNotify(); 222 1700083 : if (!done) { 223 59 : return false; 224 : } 225 1700024 : } 226 : 227 : // Running is done only if queue_ is empty. It's possible that more 228 : // entries are added into in the input or remove queues during the 229 : // time we were processing those queues. 230 851574 : return queue_->RunnerDone(); 231 : } 232 : 233 0 : std::string Description() const { 234 0 : return "DBPartition QueueRunner"; 235 : } 236 : private: 237 : WorkQueue *queue_; 238 : }; 239 : 240 3388910 : void DBPartition::WorkQueue::MaybeStartRunnerUnlocked() { 241 3388910 : if (running_) { 242 2585293 : return; 243 : } 244 803617 : running_ = true; 245 803617 : QueueRunner *runner = new QueueRunner(this); 246 803748 : TaskScheduler *scheduler = TaskScheduler::GetInstance(); 247 803733 : scheduler->Enqueue(runner); 248 : } 249 : 250 1689119 : void DBPartition::WorkQueue::MaybeStartRunner() { 251 1689119 : std::scoped_lock lock(mutex_); 252 1689187 : MaybeStartRunnerUnlocked(); 253 1689210 : } 254 : 255 852678 : bool DBPartition::WorkQueue::RunnerDone() { 256 852678 : std::scoped_lock lock(mutex_); 257 852704 : if (disable_ || (request_queue_.empty() && remove_queue_.empty())) { 258 803785 : running_ = false; 259 803785 : return true; 260 : } 261 : 262 48855 : running_ = true; 263 48855 : return false; 264 852640 : } 265 : 266 109374 : DBPartition::DBPartition(DB *db, int partition_id) 267 109374 : : db_(db), work_queue_(new WorkQueue(this, partition_id)) { 268 109374 : } 269 : 270 : // The DBPartition destructor needs to be defined after WorkQueue has 271 : // been declared. 272 109310 : DBPartition::~DBPartition() { 273 109310 : } 274 : 275 1165165 : bool DBPartition::EnqueueRequest(DBTablePartBase *tpart, DBClient *client, 276 : DBRequest *req) { 277 1165165 : RequestQueueEntry *entry = new RequestQueueEntry(tpart, client, req); 278 1165141 : return work_queue_->EnqueueRequest(entry); 279 : } 280 : 281 523765 : void DBPartition::EnqueueRemove(DBTablePartBase *tpart, DBEntryBase *db_entry) { 282 523765 : RemoveQueueEntry *entry = new RemoveQueueEntry(tpart, db_entry); 283 523779 : db_entry->SetOnRemoveQ(); 284 524002 : work_queue_->EnqueueRemove(entry); 285 523962 : } 286 : 287 : // concurrency: called from DBPartition task. 288 1700010 : void DBPartition::OnTableChange(DBTablePartBase *tablepart) { 289 1700010 : work_queue_->SetActive(tablepart); 290 1700164 : } 291 : 292 0 : long DBPartition::request_queue_len() const { 293 0 : return work_queue_->request_queue_len(); 294 : } 295 : 296 0 : uint64_t DBPartition::total_request_count() const { 297 0 : return work_queue_->total_request_count(); 298 : } 299 : 300 0 : uint64_t DBPartition::max_request_queue_len() const { 301 0 : return work_queue_->max_request_queue_len(); 302 : } 303 : 304 803772 : int DBPartition::task_id() const { 305 803772 : return db_->task_id(); 306 : }