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