OpenSDN source code
db_partition.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_partition.h"
7 
8 #include <list>
9 #include <mutex>
10 #include <atomic>
11 
12 #include <tbb/concurrent_queue.h>
13 
14 #include "base/task.h"
15 #include "db/db.h"
16 #include "db/db_client.h"
17 #include "db/db_entry.h"
18 
19 using tbb::concurrent_queue;
20 
22  // Constructor takes ownership of DBRequest key, data.
24  : tpart(tpart), client(client) {
25  request.Swap(req);
26  }
30 };
31 
35  }
38 };
39 
41 public:
42  static const int kThreshold = 1024;
43  typedef concurrent_queue<RequestQueueEntry *> RequestQueue;
44  typedef concurrent_queue<RemoveQueueEntry *> RemoveQueue;
45  typedef std::list<DBTablePartBase *> TablePartList;
46 
47  explicit WorkQueue(DBPartition *partition, int partition_id)
48  : db_partition_(partition),
49  db_partition_id_(partition_id),
50  disable_(false),
51  running_(false) {
52  request_count_ = 0;
55  }
57  for (RequestQueue::iterator iter = request_queue_.unsafe_begin();
58  iter != request_queue_.unsafe_end();) {
59  RequestQueueEntry *req_entry = *iter;
60  ++iter;
61  delete req_entry;
62  }
63  request_queue_.clear();
64  }
65 
66  bool EnqueueRequest(RequestQueueEntry *req_entry) {
67  request_queue_.push(req_entry);
69  uint32_t max = request_count_.fetch_add(1);
70  if (max > max_request_queue_len_)
73  return max < (kThreshold - 1);
74 
75  }
76 
77  bool DequeueRequest(RequestQueueEntry **req_entry) {
78  bool success = request_queue_.try_pop(*req_entry);
79  if (success) {
80  request_count_.fetch_sub(1);
81  }
82  return success;
83  }
84 
85  void EnqueueRemove(RemoveQueueEntry *rm_entry) {
86  remove_queue_.push(rm_entry);
88  }
89 
90  bool DequeueRemove(RemoveQueueEntry **rm_entry) {
91  bool success = remove_queue_.try_pop(*rm_entry);
92  return success;
93  }
94 
96  void MaybeStartRunner();
97  bool RunnerDone();
98 
99  // Normally called from single task that either runs in DB context or is
100  // exclusive with DB task, but can be called concurrently from multiple
101  // bgp::ConfigHelper tasks.
102  void SetActive(DBTablePartBase *tpart) {
103  std::scoped_lock lock(mutex_);
104  change_list_.push_back(tpart);
106  }
107 
109  DBTablePartBase *tpart = NULL;
110  if (!change_list_.empty()) {
111  tpart = change_list_.front();
112  change_list_.pop_front();
113  }
114  return tpart;
115  }
116 
118  return db_partition_id_;
119  }
120 
121  int db_task_id() const { return db_partition_->task_id(); }
122 
123  bool IsDBQueueEmpty() const {
124  return (request_queue_.empty() && change_list_.empty());
125  }
126 
127  bool disable() { return disable_; }
129 
130  long request_queue_len() const {
131  return request_count_;
132  }
133 
134  uint64_t total_request_count() const {
135  return total_request_count_;
136  }
137 
138  uint64_t max_request_queue_len() const {
139  return max_request_queue_len_;
140  }
141 
142 private:
146  std::atomic<long> request_count_;
150  std::mutex mutex_;
152  bool disable_;
153  bool running_;
154 
156 };
157 
159  return work_queue_->IsDBQueueEmpty();
160 }
161 
162 void DBPartition::SetQueueDisable(bool disable) {
163  if (disable) {
164  work_queue_->set_disable(true);
165  } else {
166  work_queue_->set_disable(false);
167  work_queue_->MaybeStartRunner();
168  }
169 }
170 
172 public:
173  static const int kMaxIterations = 32;
175  : Task(queue->db_task_id(), queue->db_partition_id()),
176  queue_(queue) {
177  }
178 
179  virtual bool Run() {
180  int count = 0;
181 
182  // Skip if the queue is disabled.
183  if (queue_->disable())
184  return queue_->RunnerDone();
185 
186  RemoveQueueEntry *rm_entry = NULL;
187  while (queue_->DequeueRemove(&rm_entry)) {
188  DBEntryBase *db_entry = rm_entry->db_entry;
189  {
190  tbb::spin_rw_mutex::scoped_lock
191  lock(rm_entry->tpart->dbstate_mutex(), false);
192  if (!db_entry->IsDeleted() || db_entry->is_onlist() ||
193  !db_entry->is_state_empty_unlocked(rm_entry->tpart)) {
194  db_entry->ClearOnRemoveQ();
195  db_entry = NULL;
196  }
197  }
198  if (db_entry) {
199  rm_entry->tpart->Remove(db_entry);
200  }
201  delete rm_entry;
202  if (++count == kMaxIterations) {
203  return false;
204  }
205  }
206 
207  RequestQueueEntry *req_entry = NULL;
208  while (queue_->DequeueRequest(&req_entry)) {
209  req_entry->tpart->Process(req_entry->client, &req_entry->request);
210  delete req_entry;
211  if (++count == kMaxIterations) {
212  return false;
213  }
214  }
215 
216  while (true) {
218  if (tpart == NULL) {
219  break;
220  }
221  bool done = tpart->RunNotify();
222  if (!done) {
223  return false;
224  }
225  }
226 
227  // Running is done only if queue_ is empty. It's possible that more
228  // entries are added into in the input or remove queues during the
229  // time we were processing those queues.
230  return queue_->RunnerDone();
231  }
232 
233  std::string Description() const {
234  return "DBPartition QueueRunner";
235  }
236 private:
238 };
239 
241  if (running_) {
242  return;
243  }
244  running_ = true;
245  QueueRunner *runner = new QueueRunner(this);
247  scheduler->Enqueue(runner);
248 }
249 
251  std::scoped_lock lock(mutex_);
252  MaybeStartRunnerUnlocked();
253 }
254 
256  std::scoped_lock lock(mutex_);
257  if (disable_ || (request_queue_.empty() && remove_queue_.empty())) {
258  running_ = false;
259  return true;
260  }
261 
262  running_ = true;
263  return false;
264 }
265 
266 DBPartition::DBPartition(DB *db, int partition_id)
267  : db_(db), work_queue_(new WorkQueue(this, partition_id)) {
268 }
269 
270 // The DBPartition destructor needs to be defined after WorkQueue has
271 // been declared.
273 }
274 
276  DBRequest *req) {
277  RequestQueueEntry *entry = new RequestQueueEntry(tpart, client, req);
278  return work_queue_->EnqueueRequest(entry);
279 }
280 
282  RemoveQueueEntry *entry = new RemoveQueueEntry(tpart, db_entry);
283  db_entry->SetOnRemoveQ();
284  work_queue_->EnqueueRemove(entry);
285 }
286 
287 // concurrency: called from DBPartition task.
289  work_queue_->SetActive(tablepart);
290 }
291 
293  return work_queue_->request_queue_len();
294 }
295 
297  return work_queue_->total_request_count();
298 }
299 
301  return work_queue_->max_request_queue_len();
302 }
303 
304 int DBPartition::task_id() const {
305  return db_->task_id();
306 }
bool is_state_empty_unlocked(DBTablePartBase *tpart)
Definition: db_entry.cc:93
void ClearOnRemoveQ()
Definition: db_entry.h:59
void SetOnRemoveQ()
Definition: db_entry.h:55
bool IsDeleted() const
Definition: db_entry.h:49
bool is_onlist()
Definition: db_entry.h:53
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
std::string Description() const
Gives a description of the task.
QueueRunner(WorkQueue *queue)
static const int kMaxIterations
bool IsDBQueueEmpty() const
std::atomic< long > request_count_
bool DequeueRequest(RequestQueueEntry **req_entry)
Definition: db_partition.cc:77
concurrent_queue< RequestQueueEntry * > RequestQueue
Definition: db_partition.cc:43
void EnqueueRemove(RemoveQueueEntry *rm_entry)
Definition: db_partition.cc:85
WorkQueue(DBPartition *partition, int partition_id)
Definition: db_partition.cc:47
DISALLOW_COPY_AND_ASSIGN(WorkQueue)
concurrent_queue< RemoveQueueEntry * > RemoveQueue
Definition: db_partition.cc:44
bool EnqueueRequest(RequestQueueEntry *req_entry)
Definition: db_partition.cc:66
uint64_t total_request_count() const
bool DequeueRemove(RemoveQueueEntry **rm_entry)
Definition: db_partition.cc:90
long request_queue_len() const
void SetActive(DBTablePartBase *tpart)
void set_disable(bool disable)
DBPartition * db_partition_
std::list< DBTablePartBase * > TablePartList
Definition: db_partition.cc:45
static const int kThreshold
Definition: db_partition.cc:42
TablePartList change_list_
RequestQueue request_queue_
DBTablePartBase * GetActiveTable()
uint64_t max_request_queue_len() const
long request_queue_len() const
uint64_t total_request_count() const
void EnqueueRemove(DBTablePartBase *tpart, DBEntryBase *db_entry)
void SetQueueDisable(bool disable)
DBPartition(DB *db, int partition_id)
uint64_t max_request_queue_len() const
bool EnqueueRequest(DBTablePartBase *tpart, DBClient *client, DBRequest *req)
int task_id() const
std::unique_ptr< WorkQueue > work_queue_
Definition: db_partition.h:50
bool IsDBQueueEmpty() const
void OnTableChange(DBTablePartBase *tpart)
tbb::spin_rw_mutex & dbstate_mutex()
virtual void Process(DBClient *client, DBRequest *req)=0
virtual void Remove(DBEntryBase *)=0
Definition: db.h:25
int task_id() const
Definition: db.h:80
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
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
void Swap(DBRequest *rhs)
Definition: db_table.cc:46
RemoveQueueEntry(DBTablePartBase *tpart, DBEntryBase *db_entry)
Definition: db_partition.cc:33
DBTablePartBase * tpart
Definition: db_partition.cc:36
DBEntryBase * db_entry
Definition: db_partition.cc:37
DBTablePartBase * tpart
Definition: db_partition.cc:27
DBClient * client
Definition: db_partition.cc:28
RequestQueueEntry(DBTablePartBase *tpart, DBClient *client, DBRequest *req)
Definition: db_partition.cc:23