OpenSDN source code
db_table_walk_mgr.cc
Go to the documentation of this file.
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 
22  : walk_request_trigger_(new TaskTrigger(
23  boost::bind(&DBTableWalkMgr::ProcessWalkRequestList, this),
24  TaskScheduler::GetInstance()->GetTaskId("db::Walker"), 0)),
25  walk_done_trigger_(new TaskTrigger(
26  boost::bind(&DBTableWalkMgr::ProcessWalkDone, this),
27  TaskScheduler::GetInstance()->GetTaskId("db::Walker"), 0)) {
28 }
29 
31  CHECK_CONCURRENCY("db::Walker");
32  std::scoped_lock lock(mutex_);
33  if (!current_table_walk_.empty()) return true;
34  while (true) {
35  if (walk_request_list_.empty()) break;
37  walk_request_set_.erase(info.get());
38  walk_request_list_.pop_front();
39  current_table_walk_.swap(info->pending_requests);
40  DBTable *table = info->table;
41  bool walk_table = false;
42  for (auto walker : current_table_walk_) {
43  if (walker->stopped()) continue;
44  walker->set_in_progress();
45  walker->reset_walk_again();
46  walk_table = true;
47  }
48  if (walk_table) {
49  // start the walk
50  table->StartWalk();
51  break;
52  } else {
53  current_table_walk_.clear();
54  }
55  }
56  return true;
57 }
58 
60  CHECK_CONCURRENCY("db::Walker");
61  assert(!current_table_walk_.empty());
62  for (auto walker : current_table_walk_) {
63  if (walker->walk_again())
64  walker->set_walk_requested();
65  else if (!walker->stopped())
66  walker->set_walk_done();
67  if (walker->stopped() || walker->walk_again()) continue;
68  walker->walk_complete()(walker, walker->table());
69  }
70  current_table_walk_.clear();
71  walk_request_trigger_->Set();
72  return true;
73 }
74 
76  DBTable::WalkFn walk_fn, DBTable::WalkCompleteFn walk_complete) {
77  table->incr_walker_count();
78  DBTableWalk *walker = new DBTableWalk(table, walk_fn, walk_complete);
79  return DBTable::DBTableWalkRef(walker);
80 }
81 
83  ref->set_walk_stopped();
84  ref.reset();
85 }
86 
88  WalkTable(ref);
89 }
90 
92  std::scoped_lock lock(mutex_);
93  DBTable *table = walk->table();
94 
95  if (walk->in_progress()) {
96  table->incr_walk_again_count();
97  walk->set_walk_again();
98  } else {
99  table->incr_walk_request_count();
100  walk->set_walk_requested();
101  }
102 
103  WalkRequestInfo tmp_info = WalkRequestInfo(table);
104  WalkRequestInfoSet::iterator it = walk_request_set_.find(&tmp_info);
105  if (it != walk_request_set_.end()) {
106  (*it)->AppendWalkReq(walk);
107  return;
108  }
109 
110  WalkRequestInfo *new_info = new WalkRequestInfo(table);
111  new_info->AppendWalkReq(walk);
112  walk_request_list_.push_back(WalkRequestInfoPtr(new_info));
113  walk_request_set_.insert(new_info);
114  walk_request_trigger_->Set();
115 }
116 
118  walk_done_trigger_->Set();
119 }
120 
122  uint32_t skip_walk_count = 0;
123  for (auto walker : current_table_walk_) {
124  if (walker->done() || walker->stopped() || walker->walk_again()) {
125  skip_walk_count++;
126  continue;
127  }
128  bool more = walker->walk_fn()(part, entry);
129  if (!more) {
130  skip_walk_count++;
131  if (!walker->stopped()) walker->set_walk_done();
132  }
133  }
134  return (skip_walk_count < current_table_walk_.size());
135 }
void incr_walker_count()
Definition: db_table.h:131
void incr_walk_request_count()
Definition: db_table.h:139
void incr_walk_again_count()
Definition: db_table.h:142
WalkRequestInfoSet walk_request_set_
void ReleaseWalker(DBTable::DBTableWalkRef &walk)
DBTable::DBTableWalkRef AllocWalker(DBTable *table, DBTable::WalkFn walk_fn, DBTable::WalkCompleteFn walk_complete)
boost::scoped_ptr< TaskTrigger > walk_request_trigger_
void WalkTable(DBTable::DBTableWalkRef walk)
boost::scoped_ptr< TaskTrigger > walk_done_trigger_
WalkReqList current_table_walk_
bool InvokeWalkCb(DBTablePartBase *part, DBEntryBase *entry)
bool ProcessWalkRequestList()
void WalkAgain(DBTable::DBTableWalkRef walk)
WalkRequestInfoList walk_request_list_
boost::shared_ptr< WalkRequestInfo > WalkRequestInfoPtr
void StartWalk()
Definition: db_table.cc:424
boost::function< void(DBTableWalkRef, DBTableBase *)> WalkCompleteFn
Definition: db_table.h:180
boost::intrusive_ptr< DBTableWalk > DBTableWalkRef
Definition: db_table.h:170
boost::function< bool(DBTablePartBase *, DBEntryBase *)> WalkFn
Definition: db_table.h:177
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
void AppendWalkReq(DBTable::DBTableWalkRef ref)
#define CHECK_CONCURRENCY(...)