Line data Source code
1 : /* 2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include "db/db_table_walk_mgr.h" 7 : 8 : #include <boost/bind/bind.hpp> 9 : #include <boost/foreach.hpp> 10 : 11 : #include "base/logging.h" 12 : #include "base/task.h" 13 : #include "base/task_annotations.h" 14 : #include "db/db.h" 15 : #include "db/db_partition.h" 16 : #include "db/db_table.h" 17 : #include "db/db_table_partition.h" 18 : 19 : using namespace boost::placeholders; 20 : 21 263 : DBTableWalkMgr::DBTableWalkMgr() 22 526 : : walk_request_trigger_(new TaskTrigger( 23 : boost::bind(&DBTableWalkMgr::ProcessWalkRequestList, this), 24 526 : TaskScheduler::GetInstance()->GetTaskId("db::Walker"), 0)), 25 526 : walk_done_trigger_(new TaskTrigger( 26 : boost::bind(&DBTableWalkMgr::ProcessWalkDone, this), 27 526 : TaskScheduler::GetInstance()->GetTaskId("db::Walker"), 0)) { 28 263 : } 29 : 30 8547 : bool DBTableWalkMgr::ProcessWalkRequestList() { 31 8547 : CHECK_CONCURRENCY("db::Walker"); 32 8547 : std::scoped_lock lock(mutex_); 33 8547 : if (!current_table_walk_.empty()) return true; 34 : while (true) { 35 8370 : if (walk_request_list_.empty()) break; 36 5856 : WalkRequestInfoPtr info = walk_request_list_.front(); 37 5856 : walk_request_set_.erase(info.get()); 38 5856 : walk_request_list_.pop_front(); 39 5856 : current_table_walk_.swap(info->pending_requests); 40 5856 : DBTable *table = info->table; 41 5856 : bool walk_table = false; 42 12952 : for (auto walker : current_table_walk_) { 43 7096 : if (walker->stopped()) continue; 44 7096 : walker->set_in_progress(); 45 7096 : walker->reset_walk_again(); 46 7096 : walk_table = true; 47 7096 : } 48 5856 : if (walk_table) { 49 : // start the walk 50 5856 : table->StartWalk(); 51 5856 : break; 52 : } else { 53 0 : current_table_walk_.clear(); 54 : } 55 5856 : } 56 8370 : return true; 57 8547 : } 58 : 59 5856 : bool DBTableWalkMgr::ProcessWalkDone() { 60 5856 : CHECK_CONCURRENCY("db::Walker"); 61 5856 : assert(!current_table_walk_.empty()); 62 12952 : for (auto walker : current_table_walk_) { 63 7096 : if (walker->walk_again()) 64 6 : walker->set_walk_requested(); 65 7090 : else if (!walker->stopped()) 66 7090 : walker->set_walk_done(); 67 7096 : if (walker->stopped() || walker->walk_again()) continue; 68 7090 : walker->walk_complete()(walker, walker->table()); 69 7096 : } 70 5856 : current_table_walk_.clear(); 71 5856 : walk_request_trigger_->Set(); 72 5856 : return true; 73 : } 74 : 75 7362 : DBTable::DBTableWalkRef DBTableWalkMgr::AllocWalker(DBTable *table, 76 : DBTable::WalkFn walk_fn, DBTable::WalkCompleteFn walk_complete) { 77 7362 : table->incr_walker_count(); 78 7362 : DBTableWalk *walker = new DBTableWalk(table, walk_fn, walk_complete); 79 7362 : return DBTable::DBTableWalkRef(walker); 80 : } 81 : 82 5454 : void DBTableWalkMgr::ReleaseWalker(DBTable::DBTableWalkRef &ref) { 83 5454 : ref->set_walk_stopped(); 84 5454 : ref.reset(); 85 5454 : } 86 : 87 3241 : void DBTableWalkMgr::WalkAgain(DBTable::DBTableWalkRef ref) { 88 3241 : WalkTable(ref); 89 3241 : } 90 : 91 7429 : void DBTableWalkMgr::WalkTable(DBTable::DBTableWalkRef walk) { 92 7429 : std::scoped_lock lock(mutex_); 93 7429 : DBTable *table = walk->table(); 94 : 95 7429 : if (walk->in_progress()) { 96 6 : table->incr_walk_again_count(); 97 6 : walk->set_walk_again(); 98 : } else { 99 7423 : table->incr_walk_request_count(); 100 7423 : walk->set_walk_requested(); 101 : } 102 : 103 7429 : WalkRequestInfo tmp_info = WalkRequestInfo(table); 104 7429 : WalkRequestInfoSet::iterator it = walk_request_set_.find(&tmp_info); 105 7429 : if (it != walk_request_set_.end()) { 106 1573 : (*it)->AppendWalkReq(walk); 107 1573 : return; 108 : } 109 : 110 5856 : WalkRequestInfo *new_info = new WalkRequestInfo(table); 111 5856 : new_info->AppendWalkReq(walk); 112 5856 : walk_request_list_.push_back(WalkRequestInfoPtr(new_info)); 113 5856 : walk_request_set_.insert(new_info); 114 5856 : walk_request_trigger_->Set(); 115 9002 : } 116 : 117 5856 : void DBTableWalkMgr::WalkDone() { 118 5856 : walk_done_trigger_->Set(); 119 5856 : } 120 : 121 57441 : bool DBTableWalkMgr::InvokeWalkCb(DBTablePartBase *part, DBEntryBase *entry) { 122 57441 : uint32_t skip_walk_count = 0; 123 116112 : for (auto walker : current_table_walk_) { 124 58643 : if (walker->done() || walker->stopped() || walker->walk_again()) { 125 0 : skip_walk_count++; 126 0 : continue; 127 : } 128 58489 : bool more = walker->walk_fn()(part, entry); 129 58401 : if (!more) { 130 0 : skip_walk_count++; 131 0 : if (!walker->stopped()) walker->set_walk_done(); 132 : } 133 58402 : } 134 57379 : return (skip_walk_count < current_table_walk_.size()); 135 : }