OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
db_table_walker.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef ctrlplane_db_table_walker_h
6 #define ctrlplane_db_table_walker_h
7 
8 #include <boost/function.hpp>
9 #include <boost/dynamic_bitset.hpp>
10 #include <tbb/task.h>
11 
12 #include "base/logging.h"
13 #include "db/db_table.h"
14 #include "db/db_table_partition.h"
15 
16 // A DB contains a TableWalker that is able to iterate though all the
17 // entries in a certain routing table.
19 public:
20 
21  // Walker function:
22  // Called for each DBEntry under a task that corresponds to the
23  // specific partition.
24  // arguments: DBTable partition and DBEntry.
25  // returns: true (continue); false (stop).
26  typedef boost::function<bool(DBTablePartBase *, DBEntryBase *)> WalkFn;
27 
28  // Called when all partitions are done iterating.
29  typedef boost::function<void(DBTableBase *)> WalkCompleteFn;
30 
31  typedef int WalkId;
32 
33  static const int kIterationToYield = 1024;
34  static const WalkId kInvalidWalkerId = -1;
35 
36  DBTableWalker(int task_id = -1);
37 
38  // Start a walk request on the specified table. If non null, 'key_start'
39  // specifies the starting point for the walk. The walk is performed in
40  // all table shards in parallel.
41  WalkId WalkTable(DBTable *table, const DBRequestKey *key_start,
42  WalkFn walker, WalkCompleteFn walk_complete,
43  bool postpone_walk = false);
44 
45  // cancel a walk that may be in progress. This cannot be called from
46  // the walker function itself.
47  void WalkCancel(WalkId id);
48  void WalkResume(WalkId id);
49 
50  int task_id() const { return task_id_; }
51 
52  static void SetIterationToYield(int count) {
54  }
55 
56 private:
58 
59  static int GetIterationToYield() {
60  static bool init_ = false;
61 
62  if (!init_) {
63 
64  // XXX To be used for testing purposes only.
65  char *count_str = getenv("DB_ITERATION_TO_YIELD");
66  if (count_str) {
67  max_iteration_to_yield_ = (int) strtol(count_str, NULL, 0);
68  if (max_iteration_to_yield_ <= 0)
70  }
71  init_ = true;
72  }
73 
75  }
76 
77  // A Walker allocated to iterator through a DBTable
78  class Walker;
79 
80  // A Job for walking through the DBTablePartition
81  class Worker;
82 
83  typedef std::vector<Walker *> WalkerList;
84  typedef boost::dynamic_bitset<> WalkerMap;
85 
86  // Purge the walker after the walk is completed/cancelled
87  void PurgeWalker(WalkId id);
88 
89  // List of walkers allocated
90  int task_id_;
91  tbb::mutex walkers_mutex_;
94 };
95 
96 #endif
std::vector< Walker * > WalkerList
static int GetIterationToYield()
DBTableWalker(int task_id=-1)
void PurgeWalker(WalkId id)
int task_id() const
tbb::mutex walkers_mutex_
boost::function< bool(DBTablePartBase *, DBEntryBase *)> WalkFn
WalkerList walkers_
static int max_iteration_to_yield_
static void SetIterationToYield(int count)
void WalkCancel(WalkId id)
static const int kIterationToYield
WalkerMap walker_map_
void WalkResume(WalkId id)
WalkId WalkTable(DBTable *table, const DBRequestKey *key_start, WalkFn walker, WalkCompleteFn walk_complete, bool postpone_walk=false)
boost::function< void(DBTableBase *)> WalkCompleteFn
static const WalkId kInvalidWalkerId
boost::dynamic_bitset WalkerMap