OpenSDN source code
ifmap_update_sender.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 
7 #include "base/task.h"
8 #include "ifmap/ifmap_client.h"
9 #include "ifmap/ifmap_server.h"
10 #include "ifmap/ifmap_exporter.h"
11 #include "ifmap/ifmap_log.h"
12 #include "ifmap/ifmap_log_types.h"
13 #include "ifmap/ifmap_update.h"
15 
16 using namespace std;
17 
19  IFMapUpdateQueue *queue)
20  : server_(server), queue_(queue), message_(new IFMapMessage()),
21  task_scheduled_(false), queue_active_(false) {
22 }
23 
25  delete(message_);
26 }
27 
29 public:
30  explicit SendTask(IFMapUpdateSender *sender)
31  : Task(TaskScheduler::GetInstance()->GetTaskId("db::IFMapTable"), 0),
32  sender_(sender) {
33  }
34  virtual bool Run() {
35  BitSet send_scheduled;
36  sender_->GetSendScheduled(&send_scheduled);
37  sender_->send_blocked_.Reset(send_scheduled);
38  for (size_t i = send_scheduled.find_first(); i != BitSet::npos;
39  i = send_scheduled.find_next(i)) {
40  // Dequeue from client marker (i).
41  IFMAP_UPD_SENDER_TRACE(IFMapUSSendScheduled, "Send scheduled for",
42  send_scheduled.ToNumberedString(), "client", i,
45  }
46  if (sender_->queue_active_) {
47  // Dequeue from tail marker.
48  // Reset queue_active_
49  IFMAP_UPD_SENDER_TRACE(IFMapUSQueueActive, "Queue active for",
52  sender_->queue_active_ = false;
53  }
54  return true;
55  }
56 
57  std::string Description() const { return "IFMapUpdateSender::SendTask"; }
58 private:
60 };
61 
63  if (!task_scheduled_) {
64  // create new task
65  SendTask *send_task = new SendTask(this);
67  scheduler->Enqueue(send_task);
68  task_scheduled_ = true;
69  }
70 }
71 
73  if (queue_active_) {
74  return;
75  }
76  queue_active_ = true;
77  std::scoped_lock lock(mutex_);
78  StartTask();
79 }
80 
82  std::scoped_lock lock(mutex_);
83  send_scheduled_.set(index);
84  StartTask();
85 }
86 
88  std::scoped_lock lock(mutex_);
89  *current = send_scheduled_;
91  task_scheduled_ = false;
92 }
93 
95  std::scoped_lock lock(mutex_);
96  send_scheduled_.reset(index);
97  send_blocked_.reset(index);
98 }
99 
100 // We return only under 2 conditions:
101 // 1. All the clients in the marker are blocked.
102 // 2. We have finished traversing the Q.
103 // Invariant: while we are traversing the Q, the marker that we are working
104 // with only has ready clients. As soon as a client blocks, we split it out and
105 // continue with the ready set.
107  IFMapMarker *marker = imarker;
108 
109  // Get the clients in this marker that are blocked. If all of the clients in
110  // this marker are blocked, we are done.
111  BitSet blocked_clients;
112  blocked_clients = (marker->mask & send_blocked_);
113  if (blocked_clients == marker->mask) {
114  return;
115  }
116 
117  // If any of the clients are blocked, create a new marker for the set of
118  // blocked clients, insert it before marker and continue with the ready
119  // set.
120  if (!blocked_clients.empty()) {
121  IFMAP_UPD_SENDER_TRACE(IFMapUSSplitBlocked, "Splitting blocked clients",
122  blocked_clients.ToNumberedString(), "from", marker->ToString());
123  queue_->MarkerSplitBefore(marker, marker, blocked_clients);
124  }
125 
126  IFMapListEntry *next = queue_->Next(marker);
127  BitSet base_send_set;
128 
129  // Start with the node after the 'marker'
130  for (IFMapListEntry *curr = next; curr != NULL; curr = next) {
131  next = queue_->Next(curr);
132 
133  if (curr->IsMarker()) {
134  IFMapMarker *next_marker = static_cast<IFMapMarker *>(curr);
135  // Processing the next_marker can change the send_set and all
136  // clients in the next_marker should have already seen the updates
137  // currently sitting in the buffer. So, flush the buffer to the
138  // existing client-set before processing the marker so that we dont
139  // send duplicates.
140  if (!message_->IsEmpty()) {
141  BitSet blocked_set;
142  SendUpdate(base_send_set, &blocked_set);
143  }
144  bool done;
145  marker = ProcessMarker(marker, next_marker, &done);
146  if (done) {
147  // All the clients in this marker are blocked. We are done.
148  return;
149  }
150  // marker has the ready clients. Continue as if we are starting
151  // fresh.
152  base_send_set.clear();
153  continue;
154  }
155 
156  // ...else its an update or delete
157 
158  IFMapUpdate *update = static_cast<IFMapUpdate *>(curr);
159  BitSet send_set = update->advertise() & marker->mask;
160  if (send_set.empty()) {
161  continue;
162  }
163 
164  if (base_send_set.empty()) {
165  base_send_set = send_set;
166  }
167 
168  // Flush the message to all possible clients if:
169  // 1. The buffer is full OR
170  // 2. The send_set is changing and buffer is filled.
171  if (message_->IsFull() ||
172  ((base_send_set != send_set) && !message_->IsEmpty())) {
173 
174  BitSet blocked_set;
175  SendUpdate(base_send_set, &blocked_set);
176  if (!blocked_set.empty()) {
177  // All the clients in this marker are blocked. We are done.
178  if (blocked_set == marker->mask) {
179  IFMAP_UPD_SENDER_TRACE(IFMapUSAllBlocked, marker->ToString(),
180  "blocked before", curr->ToString());
181  queue_->MoveMarkerBefore(marker, curr);
182  return;
183  }
184  // Only a subset of clients in this marker are blocked. Insert
185  // a marker for them 'before' curr since they have seen
186  // everything before curr. Let the ready clients continue the
187  // traversal.
188  IFMAP_UPD_SENDER_TRACE(IFMapUSSubsetBlocked, "Clients",
189  blocked_set.ToNumberedString(), "blocked before",
190  curr->ToString(), "and split from", marker->ToString());
191  queue_->MarkerSplitBefore(marker, curr, blocked_set);
192  send_set.Reset(blocked_set);
193  }
194 
195  // The send_set for this marker is changing. Pick up the new one.
196  base_send_set = send_set;
197  }
198 
199  // base_send_set is same as send_set at this point.
200  ProcessUpdate(update, base_send_set);
201  }
202 
203  // The buffer will be filled in the common case of updates being added
204  // after the tail_marker.
205  BitSet blk_set;
206  if (!message_->IsEmpty()) {
207  SendUpdate(base_send_set, &blk_set);
208  }
209  // If the last node in the Q was the tail_marker, we would have already
210  // flushed the buffer and merged with it and we would be the last node in
211  // the Q.
212  IFMapListEntry *last = queue_->GetLast();
213  if (marker != last) {
214  // Since we have reached the end of the Q, we better be the tail_marker
215  assert(marker == queue_->tail_marker());
216  // If we have any blocked clients, splitting markers for them is not
217  // useful at this point. Just move the marker to the end of the Q,
218  // immediately after last, even if it has blocked clients. Being lazy
219  // is advantageous since by the time we get the next trigger, a blocked
220  // client could have become ready and splitting the marker now would be
221  // useless.
222  IFMAP_UPD_SENDER_TRACE(IFMapUSMoveAfterLast, "Moving", marker->ToString(),
223  "before", last->ToString(), "with blocked_set",
224  blk_set.ToNumberedString());
225  queue_->MoveMarkerAfter(marker, last);
226  }
227  return;
228 }
229 
231  const BitSet &base_send_set) {
232  LogAndCountSentUpdate(update, base_send_set);
233 
234  // Append the contents of the update-node to the message.
235  message_->EncodeUpdate(update);
236 
237  // Clean up the node if everybody has seen it.
238  update->AdvertiseReset(base_send_set);
239  if (update->advertise().empty()) {
240  queue_->Dequeue(update);
241  }
242  // Update may be freed.
243  server_->exporter()->StateUpdateOnDequeue(update, base_send_set,
244  update->IsDelete());
245 }
246 
247 // blocked_set is a subset of send_set
248 void IFMapUpdateSender::SendUpdate(BitSet send_set, BitSet *blocked_set) {
249  IFMapClient *client;
250  bool send_result;
251 
252  assert(!message_->IsEmpty());
253 
254  for (size_t i = send_set.find_first(); i != BitSet::npos;
255  i = send_set.find_next(i)) {
256  assert(!send_blocked_.test(i));
257  client = server_->GetClient(i);
258  assert(client);
259 
261  // Close the message to save the document as string
262  message_->Close();
263 
264  // Send the string version of the message to the client.
265  send_result = client->SendUpdate(message_->get_string());
266 
267  // Keep track of all the clients whose buffers are full.
268  if (!send_result) {
269  blocked_set->set(i);
270  send_blocked_.set(i);
271  }
272  }
273  // Reset the message to init things for the next message
274  message_->Reset();
275 }
276 
277 // marker is before next_marker in the Q. next_marker could be the tail_marker.
278 // 'done' is set to true only if all the clients in the union of the
279 // client-sets of the 2 markers are blocked.
281  IFMapMarker *next_marker,
282  bool *done) {
283  // There should never be a marker beyond the tail_marker
284  assert(marker != queue_->tail_marker());
285 
286  // Get the union (total_set) of the client-sets in the 2 markers. Then, get
287  // the subset of clients in the union that are blocked (blocked_set). The
288  // remaining subset of clients are ready (ready_set).
289  BitSet total_set = (marker->mask | next_marker->mask);
290  BitSet blocked_set = (total_set & send_blocked_);
291  BitSet ready_set;
292  ready_set.BuildComplement(total_set, blocked_set); // *this = lhs & ~rhs
293 
294  // If all the clients are ready or all are blocked, merge marker into
295  // next_marker. marker will be deleted.
296  if (blocked_set.empty() || ready_set.empty()) {
297  IFMAP_UPD_SENDER_TRACE(IFMapUSMarkerMerge, "Merging", marker->ToString(),
298  "into", next_marker->ToString());
299  queue_->MarkerMerge(next_marker, marker, marker->mask);
300  assert(next_marker->mask == total_set);
301  } else {
302  // We have both, ready and blocked, clients. First, merge both the
303  // markers into next_marker so that next_marker has the total_set. Then
304  // split next_marker into 2 markers: first with the blocked_set and the
305  // second with the ready_set, with first(blocked) preceding the
306  // second(ready).
307  IFMAP_UPD_SENDER_TRACE(IFMapUSMarkerMerge, "Merging", marker->ToString(),
308  "into", next_marker->ToString());
309  queue_->MarkerMerge(next_marker, marker, marker->mask);
310  assert(next_marker->mask == total_set);
311  IFMAP_UPD_SENDER_TRACE(IFMapUSMarkerSplit, "Splitting blocked clients",
312  blocked_set.ToNumberedString(), "from", next_marker->ToString());
313  queue_->MarkerSplitBefore(next_marker, next_marker, blocked_set);
314  }
315  if (ready_set.empty()) {
316  // If all the clients are blocked, we are done.
317  *done = true;
318  } else {
319  // Atleast some clients are ready to continue.
320  *done = false;
321  }
322 
323  // next_marker has the ready_set if done is false
324  return next_marker;
325 }
326 
328  const BitSet &base_send_set) {
329  size_t total = base_send_set.count();
330  // Avoid dealing with return value of BitSet::npos
331  if (total) {
332  string name = update->ConfigName();
333  string operation = update->TypeToString();
334  size_t client_id = base_send_set.find_first();
335  while (total--) {
336  IFMapClient *client = server_->GetClient(client_id);
337  if (client) {
338  IFMAP_DEBUG_ONLY(IFMapClientSendInfo, operation, name,
339  client->identifier(), client->name());
340  if (update->IsNode()) {
341  if (update->IsUpdate()) {
342  client->incr_update_nodes_sent();
343  } else if (update->IsDelete()) {
344  client->incr_delete_nodes_sent();
345  } else {
346  assert(0);
347  }
348  } else if (update->IsLink()) {
349  if (update->IsUpdate()) {
350  client->incr_update_links_sent();
351  } else if (update->IsDelete()) {
352  client->incr_delete_links_sent();
353  } else {
354  assert(0);
355  }
356  }
357  }
358  client_id = base_send_set.find_next(client_id);
359  }
360  }
361 }
362 
Definition: bitset.h:18
std::string ToNumberedString() const
Definition: bitset.cc:594
BitSet & reset(size_t pos)
Definition: bitset.cc:137
static const size_t npos
Definition: bitset.h:20
size_t find_first() const
Definition: bitset.cc:243
void BuildComplement(const BitSet &lhs, const BitSet &rhs)
Definition: bitset.cc:487
void Reset(const BitSet &rhs)
Definition: bitset.cc:471
void clear()
Definition: bitset.cc:159
size_t find_next(size_t pos) const
Definition: bitset.cc:256
bool empty() const
Definition: bitset.cc:166
BitSet & set(size_t pos)
Definition: bitset.cc:126
bool test(size_t pos) const
Definition: bitset.cc:147
size_t count() const
Definition: bitset.cc:195
void incr_delete_links_sent()
Definition: ifmap_client.h:49
virtual const std::string & name() const
Definition: ifmap_client.h:29
void incr_delete_nodes_sent()
Definition: ifmap_client.h:47
virtual const std::string & identifier() const =0
void incr_update_nodes_sent()
Definition: ifmap_client.h:46
virtual bool SendUpdate(const std::string &msg)=0
void incr_update_links_sent()
Definition: ifmap_client.h:48
void StateUpdateOnDequeue(IFMapUpdate *update, const BitSet &dequeue_set, bool is_delete)
void SetReceiverInMsg(const std::string &cli_identifier)
const std::string & get_string() const
Definition: ifmap_encoder.h:29
void EncodeUpdate(const IFMapUpdate *update)
IFMapClient * GetClient(int index)
IFMapExporter * exporter()
Definition: ifmap_server.h:87
void MoveMarkerBefore(IFMapMarker *marker, IFMapListEntry *current)
IFMapListEntry * Next(IFMapListEntry *current)
void Dequeue(IFMapUpdate *update)
IFMapMarker * MarkerSplitBefore(IFMapMarker *marker, IFMapListEntry *current, const BitSet &msplit)
void MarkerMerge(IFMapMarker *dst, IFMapMarker *src, const BitSet &mmove)
IFMapMarker * GetMarker(int bit)
void MoveMarkerAfter(IFMapMarker *marker, IFMapListEntry *current)
IFMapMarker * tail_marker()
IFMapListEntry * GetLast()
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.
SendTask(IFMapUpdateSender *sender)
IFMapMarker * ProcessMarker(IFMapMarker *marker, IFMapMarker *next_marker, bool *done)
virtual void QueueActive()
IFMapUpdateSender(IFMapServer *server, IFMapUpdateQueue *queue)
virtual void SendActive(int index)
void GetSendScheduled(BitSet *current)
void SendUpdate(BitSet send_set, BitSet *blocked_set)
void Send(IFMapMarker *imarker)
void LogAndCountSentUpdate(IFMapUpdate *update, const BitSet &base_send_set)
IFMapMessage * message_
void ProcessUpdate(IFMapUpdate *update, const BitSet &base_send_set)
IFMapUpdateQueue * queue_
void CleanupClient(int index)
const BitSet & advertise() const
Definition: ifmap_update.h:95
void AdvertiseReset(const BitSet &set)
Definition: ifmap_update.cc:60
bool IsNode() const
Definition: ifmap_update.h:100
std::string ConfigName()
Definition: ifmap_update.cc:44
bool IsLink() const
Definition: ifmap_update.h:101
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
#define IFMAP_DEBUG_ONLY(obj,...)
Definition: ifmap_log.h:40
#define IFMAP_UPD_SENDER_TRACE(obj,...)
Definition: ifmap_log.h:58
bool IsUpdate() const
Definition: ifmap_update.h:66
bool IsDelete() const
Definition: ifmap_update.h:67
virtual std::string ToString()
Definition: ifmap_update.h:62
std::string TypeToString()
Definition: ifmap_update.h:68
virtual std::string ToString()
Definition: ifmap_update.cc:76