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 357 : DBTableWalkMgr::DBTableWalkMgr() 22 714 : : walk_request_trigger_(new TaskTrigger( 23 : boost::bind(&DBTableWalkMgr::ProcessWalkRequestList, this), 24 714 : TaskScheduler::GetInstance()->GetTaskId("db::Walker"), 0)), 25 714 : walk_done_trigger_(new TaskTrigger( 26 : boost::bind(&DBTableWalkMgr::ProcessWalkDone, this), 27 714 : TaskScheduler::GetInstance()->GetTaskId("db::Walker"), 0)) { 28 357 : } 29 : 30 5730 : bool DBTableWalkMgr::ProcessWalkRequestList() { 31 5730 : CHECK_CONCURRENCY("db::Walker"); 32 5730 : std::scoped_lock lock(mutex_); 33 5730 : if (!current_table_walk_.empty()) return true; 34 : while (true) { 35 5727 : if (walk_request_list_.empty()) break; 36 3259 : WalkRequestInfoPtr info = walk_request_list_.front(); 37 3259 : walk_request_set_.erase(info.get()); 38 3259 : walk_request_list_.pop_front(); 39 3259 : current_table_walk_.swap(info->pending_requests); 40 3259 : DBTable *table = info->table; 41 3259 : bool walk_table = false; 42 6552 : for (auto walker : current_table_walk_) { 43 3293 : if (walker->stopped()) continue; 44 3293 : walker->set_in_progress(); 45 3293 : walker->reset_walk_again(); 46 3293 : walk_table = true; 47 3293 : } 48 3259 : if (walk_table) { 49 : // start the walk 50 3259 : table->StartWalk(); 51 3259 : break; 52 : } else { 53 0 : current_table_walk_.clear(); 54 : } 55 3259 : } 56 5727 : return true; 57 5730 : } 58 : 59 3259 : bool DBTableWalkMgr::ProcessWalkDone() { 60 3259 : CHECK_CONCURRENCY("db::Walker"); 61 3259 : assert(!current_table_walk_.empty()); 62 6552 : for (auto walker : current_table_walk_) { 63 3293 : if (walker->walk_again()) 64 0 : walker->set_walk_requested(); 65 3293 : else if (!walker->stopped()) 66 3293 : walker->set_walk_done(); 67 3293 : if (walker->stopped() || walker->walk_again()) continue; 68 3293 : walker->walk_complete()(walker, walker->table()); 69 3293 : } 70 3259 : current_table_walk_.clear(); 71 3259 : walk_request_trigger_->Set(); 72 3259 : return true; 73 : } 74 : 75 2738 : DBTable::DBTableWalkRef DBTableWalkMgr::AllocWalker(DBTable *table, 76 : DBTable::WalkFn walk_fn, DBTable::WalkCompleteFn walk_complete) { 77 2738 : table->incr_walker_count(); 78 2738 : DBTableWalk *walker = new DBTableWalk(table, walk_fn, walk_complete); 79 2738 : return DBTable::DBTableWalkRef(walker); 80 : } 81 : 82 2738 : void DBTableWalkMgr::ReleaseWalker(DBTable::DBTableWalkRef &ref) { 83 2738 : ref->set_walk_stopped(); 84 2738 : ref.reset(); 85 2738 : } 86 : 87 0 : void DBTableWalkMgr::WalkAgain(DBTable::DBTableWalkRef ref) { 88 0 : WalkTable(ref); 89 0 : } 90 : 91 3506 : void DBTableWalkMgr::WalkTable(DBTable::DBTableWalkRef walk) { 92 3506 : std::scoped_lock lock(mutex_); 93 3506 : DBTable *table = walk->table(); 94 : 95 3506 : if (walk->in_progress()) { 96 0 : table->incr_walk_again_count(); 97 0 : walk->set_walk_again(); 98 : } else { 99 3506 : table->incr_walk_request_count(); 100 3506 : walk->set_walk_requested(); 101 : } 102 : 103 3506 : WalkRequestInfo tmp_info = WalkRequestInfo(table); 104 3506 : WalkRequestInfoSet::iterator it = walk_request_set_.find(&tmp_info); 105 3506 : if (it != walk_request_set_.end()) { 106 247 : (*it)->AppendWalkReq(walk); 107 247 : return; 108 : } 109 : 110 3259 : WalkRequestInfo *new_info = new WalkRequestInfo(table); 111 3259 : new_info->AppendWalkReq(walk); 112 3259 : walk_request_list_.push_back(WalkRequestInfoPtr(new_info)); 113 3259 : walk_request_set_.insert(new_info); 114 3259 : walk_request_trigger_->Set(); 115 3753 : } 116 : 117 3259 : void DBTableWalkMgr::WalkDone() { 118 3259 : walk_done_trigger_->Set(); 119 3259 : } 120 : 121 10688 : bool DBTableWalkMgr::InvokeWalkCb(DBTablePartBase *part, DBEntryBase *entry) { 122 10688 : uint32_t skip_walk_count = 0; 123 21453 : for (auto walker : current_table_walk_) { 124 10737 : if (walker->done() || walker->stopped() || walker->walk_again()) { 125 0 : skip_walk_count++; 126 0 : continue; 127 : } 128 10566 : bool more = walker->walk_fn()(part, entry); 129 10599 : if (!more) { 130 0 : skip_walk_count++; 131 0 : if (!walker->stopped()) walker->set_walk_done(); 132 : } 133 10600 : } 134 10721 : return (skip_walk_count < current_table_walk_.size()); 135 : }