OpenSDN source code
db_table_walker.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "db/db_table_walker.h"
7 
8 #include <atomic>
9 
10 #include "base/logging.h"
11 #include "base/task.h"
12 #include "db/db.h"
13 #include "db/db_partition.h"
14 #include "db/db_table.h"
15 #include "db/db_table_partition.h"
16 
17 int DBTableWalker::max_iteration_to_yield_ = kIterationToYield;
18 
20 public:
21  Walker(WalkId id, DBTableWalker *wkmgr, DBTable *table,
22  const DBRequestKey *key, WalkFn walker,
23  WalkCompleteFn walk_done, bool postpone_walk);
24 
25  void StopWalk() {
26  assert(workers_.empty());
27  should_stop_.exchange(true);
28  }
29 
30  // Test only - resume walk that was postponed at creation.
31  void ResumeWalk() {
32  assert(!workers_.empty());
34  for (std::vector<Task *>::iterator it = workers_.begin();
35  it != workers_.end(); ++it) {
36  scheduler->Enqueue(*it);
37  }
38  workers_.clear();
39  }
40 
42 
43  // Parent walker manager
45 
46  // Table on which walk is done
48 
49  // Take the ownership of key passed
50  std::unique_ptr<DBRequestKey> key_start_;
51 
54 
55  // Will be true if Table walk is cancelled
56  std::atomic<bool> should_stop_;
57 
58  // check whether iteration is completed on all Table Partition
59  std::atomic<long> status_;
60 
61  std::vector<Task *> workers_;
62  int task_id() const { return wkmgr_->task_id(); }
63 };
64 
65 class DBTableWalker::Worker : public Task {
66 public:
67  Worker(Walker *walker, int db_partition_id, const DBRequestKey *key)
68  : Task(walker->task_id(), db_partition_id), walker_(walker),
69  key_start_(key) {
70  tbl_partition_ = static_cast<DBTablePartition *>(
71  walker_->table_->GetTablePartition(db_partition_id));
72  }
73 
74  virtual bool Run();
75  std::string Description() const { return "DBTableWalker::Worker"; }
76 
77 private:
79 
80  // Store the last visited node to continue walk
81  std::unique_ptr<DBRequestKey> walk_ctx_;
82 
83  // This is where the walk started
85 
86  // Table partition for which this worker was created
88 };
89 
90 static void db_walker_wait() {
91  static unsigned int walk_sleep_usecs_;
92  static bool once;
93 
94  if (!once) {
95  once = true;
96 
97  char *wait = getenv("DB_WALKER_WAIT_USECS");
98  if (wait) {
99  walk_sleep_usecs_ = (unsigned int) strtoul(wait, NULL, 0);
100  if (walk_sleep_usecs_ > 1000000)
101  walk_sleep_usecs_ = 1000000;
102  }
103  }
104 
105  if (walk_sleep_usecs_) {
106  usleep(walk_sleep_usecs_);
107  }
108 }
109 
111  int count = 0;
112  DBRequestKey *key_resume;
113 
114  // Check whether Walker was requested to be cancelled
115  if (walker_->should_stop_) {
116  goto walk_done;
117  }
118 
119  // Check where we left in last iteration
120  if ((key_resume = walk_ctx_.get()) == NULL) {
121  // First time invoke of worker thread, start from key_start_
122  key_resume = const_cast <DBRequestKey *>(key_start_);
123  }
124 
125  DBEntry *entry;
126  if (key_resume != NULL) {
127  DBTable *table = walker_->table_;
128  std::unique_ptr<const DBEntryBase> start;
129  start = table->AllocEntry(key_resume);
130  // Find matching or next in sort order
131  entry = tbl_partition_->lower_bound(start.get());
132  } else {
133  entry = tbl_partition_->GetFirst();
134  }
135  if (entry == NULL) {
136  goto walk_done;
137  }
138 
139  for (DBEntry *next = NULL; entry; entry = next) {
140  next = tbl_partition_->GetNext(entry);
141  // Check whether Walker was requested to be cancelled
142  if (walker_->should_stop_) {
143  break;
144  }
145  if (count == GetIterationToYield()) {
146  // store the context
147  walk_ctx_ = entry->GetDBRequestKey();
148  return false;
149  }
150 
151  // Invoke walker function
152  bool more = walker_->walker_fn_(tbl_partition_, entry);
153  if (!more) {
154  break;
155  }
156 
157  db_walker_wait();
158  count++;
159  }
160 
161 walk_done:
162  // Check whether all other walks on the table is completed
163  long num_walkers_on_tpart = walker_->status_.fetch_sub(1);
164  if (num_walkers_on_tpart == 1) {
165  if (walker_->should_stop_) {
167  } else {
169  // Invoke Walker_Complete callback
170  if (walker_->done_fn_ != NULL) {
172  }
173  }
174 
175  // Release the memory for walker and bitmap
177  }
178  return true;
179 }
180 
182  DBTable *table, const DBRequestKey *key,
183  WalkFn walker, WalkCompleteFn walk_done,
184  bool postpone_walk)
185  : id_(id), wkmgr_(wkmgr), table_(table),
186  key_start_(const_cast<DBRequestKey *>(key)),
187  walker_fn_(walker), done_fn_(walk_done) {
188  int num_worker = table->PartitionCount();
189  should_stop_ = false;
190  status_ = num_worker;
191  for (int i = 0; i < num_worker; i++) {
192  Worker *task = new Worker(this, i, key);
193  if (postpone_walk) {
194  workers_.push_back(task);
195  } else {
197  scheduler->Enqueue(task);
198  }
199  }
200 }
201 
203  if (task_id == -1) {
205  task_id_ = scheduler->GetTaskId("db::DBTable");
206  }
207 }
208 
210  const DBRequestKey *key_start,
211  WalkFn walkerfn ,
212  WalkCompleteFn walk_complete,
213  bool postpone_walk) {
214  table->incr_walk_request_count();
215  std::scoped_lock lock(walkers_mutex_);
216  size_t i = walker_map_.find_first();
217  if (i == walker_map_.npos) {
218  i = walkers_.size();
219  Walker *walker = new Walker(i, this, table, key_start,
220  walkerfn, walk_complete, postpone_walk);
221  walkers_.push_back(walker);
222  } else {
223  walker_map_.reset(i);
224  if (walker_map_.none()) {
225  walker_map_.clear();
226  }
227  Walker *walker = new Walker(i, this, table, key_start,
228  walkerfn, walk_complete, postpone_walk);
229  walkers_[i] = walker;
230  }
231  table->incr_walker_count();
232  return i;
233 }
234 
236  std::scoped_lock lock(walkers_mutex_);
237  walkers_[id]->StopWalk();
238  // Purge to be called after task has stopped
239 }
240 
242  std::scoped_lock lock(walkers_mutex_);
243  walkers_[id]->ResumeWalk();
244 }
245 
247  std::scoped_lock lock(walkers_mutex_);
248  Walker *walker = walkers_[id];
249  DBTable *table = walker->table_;
250  delete walker;
251  walkers_[id] = NULL;
252  if ((size_t) id == walkers_.size() - 1) {
253  while (!walkers_.empty() && walkers_.back() == NULL) {
254  walkers_.pop_back();
255  }
256  if (walker_map_.size() > walkers_.size()) {
257  walker_map_.resize(walkers_.size());
258  }
259  } else {
260  if ((size_t) id >= walker_map_.size()) {
261  walker_map_.resize(id + 1);
262  }
263  walker_map_.set(id);
264  }
265 
266  // Retry table deletion when the last walker is purged.
267  if (table->decr_walker_count() == 0) {
268  table->RetryDelete();
269  }
270 }
virtual KeyPtr GetDBRequestKey() const =0
void incr_walker_count()
Definition: db_table.h:131
void incr_walk_request_count()
Definition: db_table.h:139
void incr_walk_cancel_count()
Definition: db_table.h:141
virtual void RetryDelete()
Definition: db_table.h:105
uint64_t decr_walker_count()
Definition: db_table.h:132
void incr_walk_complete_count()
Definition: db_table.h:140
virtual DBEntry * GetNext(const DBEntryBase *entry)
virtual DBEntry * lower_bound(const DBEntryBase *entry)
virtual DBEntry * GetFirst()
std::atomic< long > status_
Walker(WalkId id, DBTableWalker *wkmgr, DBTable *table, const DBRequestKey *key, WalkFn walker, WalkCompleteFn walk_done, bool postpone_walk)
std::atomic< bool > should_stop_
WalkCompleteFn done_fn_
std::vector< Task * > workers_
std::unique_ptr< DBRequestKey > key_start_
DBTableWalker * wkmgr_
DBTableWalker::Walker * walker_
Worker(Walker *walker, int db_partition_id, const DBRequestKey *key)
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
std::unique_ptr< DBRequestKey > walk_ctx_
std::string Description() const
Gives a description of the task.
DBTablePartition * tbl_partition_
const DBRequestKey * key_start_
static int GetIterationToYield()
boost::function< bool(DBTablePartBase *, DBEntryBase *)> WalkFn
static int max_iteration_to_yield_
DBTableWalker(int task_id=-1)
WalkId WalkTable(DBTable *table, const DBRequestKey *key_start, WalkFn walker, WalkCompleteFn walk_complete, bool postpone_walk=false)
void PurgeWalker(WalkId id)
WalkerList walkers_
WalkerMap walker_map_
std::mutex walkers_mutex_
void WalkCancel(WalkId id)
void WalkResume(WalkId id)
boost::function< void(DBTableBase *)> WalkCompleteFn
int task_id() const
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *key) const =0
virtual int PartitionCount() const
Definition: db_table.cc:447
virtual DBTablePartBase * GetTablePartition(const DBRequestKey *key)
Definition: db_table.cc:463
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
int GetTaskId(const std::string &name)
Definition: task.cc:884
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
Definition: task.cc:662
static TaskScheduler * GetInstance()
Definition: task.cc:574
Task is a class to describe a computational task within OpenSDN control plane applications....
Definition: task.h:82
static void db_walker_wait()
Definition: task_int.h:11