summaryrefslogtreecommitdiffstats
path: root/net/spdy/spdy_priority_tree.h
blob: bf73c622b3c8400fba0f3825c16ca628a1acdd98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_SPDY_SPDY_PRIORITY_TREE_H_
#define NET_SPDY_SPDY_PRIORITY_TREE_H_

#include <cmath>
#include <list>
#include <map>
#include <queue>
#include <set>

#include "base/basictypes.h"
#include "base/containers/hash_tables.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"

namespace net {

// This data structure implements the HTTP/2 stream priority tree defined in
// section 5.3 of RFC 7540:
// http://tools.ietf.org/html/rfc7540#section-5.3
//
// Nodes can be added and removed, and dependencies between them defined.
// Nodes constitute a tree rooted at node ID 0: each node has a single parent
// node, and 0 or more child nodes.  Individual nodes can be marked as ready to
// read/write, and then the whole structure can be queried to pick the next
// node to read/write out of those that are ready.
//
// The NodeId type must be a POD that supports comparison (most
// likely, it will be a number).

namespace test {
template <typename NodeId>
class SpdyPriorityTreePeer;
}

const int kRootNodeId = 0;
const int kDefaultWeight = 16;
const int kMinWeight = 1;
const int kMaxWeight = 256;

template <typename NodeId>
class SpdyPriorityTree {
  typedef std::vector<std::pair<NodeId, float> > PriorityNodeList;

 public:
  SpdyPriorityTree();
  ~SpdyPriorityTree();

  // Orders in descending order of priority.
  struct NodePriorityComparator {
    bool operator ()(const std::pair<NodeId, float>& lhs,
                     const std::pair<NodeId, float>& rhs);
  };

  friend class test::SpdyPriorityTreePeer<NodeId>;

  // Return the number of nodes currently in the tree.
  int num_nodes() const;

  // Return true if the tree contains a node with the given ID.
  bool NodeExists(NodeId node_id) const;

  // Add a new node with the given weight and parent. Non-exclusive nodes
  // simply get added below the parent node. If exclusive = true, the node
  // becomes the parent's sole child and the parent's previous children
  // become the children of the new node.
  // Returns true on success. Returns false if the node already exists
  // in the tree, or if the parent node does not exist.
  bool AddNode(NodeId node_id, NodeId parent_id, int weight, bool exclusive);

  // Remove an existing node from the tree.  Returns true on success, or
  // false if the node doesn't exist.
  bool RemoveNode(NodeId node_id);

  // Get the weight of the given node.
  int GetWeight(NodeId node_id) const;

  // Get the parent of the given node.  If the node doesn't exist, or is a root
  // node (and thus has no parent), returns NodeId().
  NodeId GetParent(NodeId node_id) const;

  // Get the child list of the given node.  If the node doesn't exist, or has no
  // child, returns NULL.
  std::list<NodeId>* GetChildren(NodeId node_id) const;

  // Set the priority of the given node.
  bool SetWeight(NodeId node_id, int weight);

  // Set the parent of the given node.  Returns true on success.
  // Returns false and has no effect if the node and/or the parent doesn't
  // exist. If the new parent is a descendant of the node (i.e. this would have
  // created a cycle) then we rearrange the topology of the tree as described
  // in section 5.3.3 of RFC 7540:
  // https://tools.ietf.org/html/rfc7540#section-5.3.3
  bool SetParent(NodeId node_id, NodeId parent_id, bool exclusive);

  // Returns true if the node parent_id has child_id in its child_list.
  bool HasChild(NodeId parent_id, NodeId child_id) const;

  // Mark a node as blocked or unblocked. Return true on success, or false
  // if unable to mark the specified node.
  bool SetBlocked(NodeId node_id, bool blocked);

  // Mark whether or not a node is ready to write; i.e. whether there is
  // buffered data for the associated stream. Return true on success, or false
  // if unable to mark the specified node.
  bool SetReady(NodeId node_id, bool ready);

  // Returns an ordered list of writeable nodes and their priorities.
  // Priority is calculated as:
  // parent's priority * (node's weight / sum of sibling weights)
  PriorityNodeList GetPriorityList();

 private:
  typedef std::list<NodeId> List;

  struct Node {
    Node();
    ~Node();

    // ID for this node.
    NodeId id;
    // ID of parent node.
    NodeId parent_id;
    // Weights can range between 1 and 256 (inclusive).
    int weight;
    // The total weight of this node's direct descendants.
    int total_child_weights;
    // The total weight of direct descendants that are writeable
    // (ready to write and not blocked). This value does not necessarily
    // reflect the current state of the tree; instead, we lazily update it
    // on calls to PropagateNodeState(node.id).
    int total_writeable_child_weights;
    // Node IDs of children, if any.
    List* child_list;
    // Is the associated stream write-blocked?
    bool blocked;
    // Does the stream have data ready for writing?
    bool ready;
    // The fraction of resources to dedicate to this node.
    float priority;
  };

  typedef base::hash_map<NodeId, Node> NodeMap;

  // Update the value of total_writeable_child_weights for the given node
  // to reflect the current state of the tree.
  void PropagateNodeState(NodeId node);

  // Get the given node, or return nullptr if it doesn't exist.
  const Node* FindNode(NodeId node_id) const;

  // Return true if all internal invariants hold (useful for unit tests).
  // Unless there are bugs, this should always return true.
  bool ValidateInvariantsForTests() const;

  NodeMap all_nodes_;  // maps from node IDs to Node objects

  DISALLOW_COPY_AND_ASSIGN(SpdyPriorityTree);
};

template <typename NodeId>
SpdyPriorityTree<NodeId>::SpdyPriorityTree() {
  Node* root_node = &all_nodes_[kRootNodeId];
  root_node->id = kRootNodeId;
  root_node->weight = kDefaultWeight;
  root_node->parent_id = static_cast<NodeId>(kRootNodeId);
  root_node->child_list = new std::list<NodeId>;
  root_node->priority = 1.0;
  root_node->ready = true;
}

template <typename NodeId>
SpdyPriorityTree<NodeId>::~SpdyPriorityTree() {}

template <typename NodeId>
SpdyPriorityTree<NodeId>::Node::Node() :
  parent_id(kRootNodeId),
  weight(kDefaultWeight),
  total_child_weights(0),
  total_writeable_child_weights(0),
  child_list(),
  blocked(false),
  ready(false),
  priority(0) {
}

template <typename NodeId>
SpdyPriorityTree<NodeId>::Node::~Node() {
  delete child_list;
}

template <typename NodeId>
bool SpdyPriorityTree<NodeId>::NodePriorityComparator::operator ()(
    const std::pair<NodeId, float>& lhs,
    const std::pair<NodeId, float>& rhs) {
  return lhs.second > rhs.second;
}

template <typename NodeId>
int SpdyPriorityTree<NodeId>::num_nodes() const {
  return all_nodes_.size();
}

template <typename NodeId>
bool SpdyPriorityTree<NodeId>::NodeExists(NodeId node_id) const {
  return all_nodes_.count(node_id) != 0;
}

template <typename NodeId>
bool SpdyPriorityTree<NodeId>::AddNode(NodeId node_id,
                                       NodeId parent_id,
                                       int weight,
                                       bool exclusive) {
  if (NodeExists(node_id) || !NodeExists(parent_id)) {
    return false;
  }
  if (weight < kMinWeight || weight > kMaxWeight) {
    return false;
  }
  Node* parent = &all_nodes_[parent_id];
  Node* new_node = &all_nodes_[node_id];
  new_node->id = node_id;
  new_node->weight = weight;
  new_node->parent_id = parent_id;
  if (exclusive) {
    // Move the parent's current children below the new node.
    new_node->child_list = parent->child_list;
    new_node->total_child_weights = parent->total_child_weights;
    // Update each child's parent_id.
    for (typename List::iterator it = new_node->child_list->begin();
         it != new_node->child_list->end(); ++it) {
      Node* child = &all_nodes_[*it];
      child->parent_id = node_id;
    }
    // Clear parent's old child data.
    parent->child_list = new std::list<NodeId>;
    parent->total_child_weights = 0;
  } else {
    new_node->child_list = new std::list<NodeId>;
  }
  // Add new node to parent.
  parent->child_list->push_back(node_id);
  parent->total_child_weights += weight;
  return true;
}

template <typename NodeId>
bool SpdyPriorityTree<NodeId>::RemoveNode(NodeId node_id) {
  if (node_id == static_cast<NodeId>(kRootNodeId) || !NodeExists(node_id)) {
    return false;
  }
  const Node& node = all_nodes_[node_id];

  DCHECK(NodeExists(node.parent_id));
  Node* parent = &all_nodes_[node.parent_id];
  // Remove the node id from parent's child list.
  parent->child_list->remove(node_id);
  parent->total_child_weights -= node.weight;

  // Move the node's children to the parent's child list.
  if (node.child_list != NULL) {
    // Update each child's parent_id and weight.
    for (typename List::iterator it = node.child_list->begin();
         it != node.child_list->end(); ++it) {
      Node* child = &all_nodes_[*it];
      child->parent_id = node.parent_id;
      // Divide the removed node's weight among its children, rounding to the
      // nearest valid weight.
      float float_weight =  node.weight * static_cast<float>(child->weight) /
                            static_cast<float>(node.total_child_weights);
      int new_weight = std::floor(float_weight + 0.5);
      if (new_weight == 0) {
        new_weight = 1;
      }
      child->weight = new_weight;
      parent->total_child_weights += child->weight;
    }
    parent->child_list->splice(parent->child_list->end(), *node.child_list);
  }

  // Delete the node.
  all_nodes_.erase(node_id);
  return true;
}

template <typename NodeId>
int SpdyPriorityTree<NodeId>::GetWeight(NodeId node_id) const {
  const Node* node = FindNode(node_id);
  if (node != NULL) {
    return node->weight;
  }
  return 0;
}

template <typename NodeId>
NodeId SpdyPriorityTree<NodeId>::GetParent(NodeId node_id) const {
  const Node* node = FindNode(node_id);
  if (node != NULL && node->id != static_cast<NodeId>(kRootNodeId)) {
    return node->parent_id;
  }
  return static_cast<NodeId>(kRootNodeId);
}

template <typename NodeId>
std::list<NodeId>* SpdyPriorityTree<NodeId>::GetChildren(NodeId node_id) const {
  const Node* node = FindNode(node_id);
  if (node != NULL) {
    return node->child_list;
  }
  return NULL;
}

template <typename NodeId>
bool SpdyPriorityTree<NodeId>::SetWeight(
    NodeId node_id, int weight) {
  if (!NodeExists(node_id)) {
    return false;
  }
  if (weight < kMinWeight || weight > kMaxWeight) {
    return false;
  }

  Node* node = &all_nodes_[node_id];
  Node* parent = &all_nodes_[node->parent_id];

  parent->total_child_weights += (weight - node->weight);
  node->weight = weight;

  return true;
}


template <typename NodeId>
bool SpdyPriorityTree<NodeId>::SetParent(
    NodeId node_id, NodeId parent_id, bool exclusive) {
  if (!NodeExists(node_id) || !NodeExists(parent_id)) {
    return false;
  }
  if (node_id == parent_id) return false;

  Node* node = &all_nodes_[node_id];
  Node* new_parent = &all_nodes_[parent_id];
  // If the new parent is already the node's parent, we're done.
  if (node->parent_id == parent_id) {
    return true;
  }

  // Next, check to see if the new parent is currently a descendant
  // of the node.
  Node* last = new_parent;
  NodeId last_id = parent_id;
  bool cycle_exists = false;
  while (last->parent_id != static_cast<NodeId>(kRootNodeId)) {
    if (last->parent_id == node_id) {
      cycle_exists = true;
      break;
    }
    last_id = last->parent_id;
    DCHECK(NodeExists(last_id));
    last = &all_nodes_[last_id];
  }

  if (cycle_exists) {
    // The new parent moves to the level of the current node.
    SetParent(parent_id, node->parent_id, false);
  }

  // Remove node from old parent's child list.
  const NodeId old_parent_id = node->parent_id;
  DCHECK(NodeExists(old_parent_id));
  Node* old_parent = &all_nodes_[old_parent_id];
  old_parent->child_list->remove(node_id);
  old_parent->total_child_weights -= node->weight;

  if (exclusive) {
    // Move the new parent's current children below the current node.
    node->child_list->insert(node->child_list->end(),
                             new_parent->child_list->begin(),
                             new_parent->child_list->end());
    node->total_child_weights += new_parent->total_child_weights;
    for (NodeId child_id : *new_parent->child_list) {
      Node* child = &all_nodes_[child_id];
      child->parent_id = node_id;
    }
    // Clear new parent's old child data.
    new_parent->child_list->clear();
    new_parent->total_child_weights = 0;
  }

  // Make the change.
  node->parent_id = parent_id;
  new_parent->child_list->push_back(node_id);
  new_parent->total_child_weights += node->weight;
  return true;
}

template <typename NodeId>
bool SpdyPriorityTree<NodeId>::SetBlocked(NodeId node_id, bool blocked) {
  if (!NodeExists(node_id)) {
    return false;
  }

  Node* node = &all_nodes_[node_id];
  node->blocked = blocked;
  return true;
}

template <typename NodeId>
bool SpdyPriorityTree<NodeId>::SetReady(NodeId node_id, bool ready) {
  if (!NodeExists(node_id)) {
    return false;
  }
  Node* node = &all_nodes_[node_id];
  node->ready = ready;
  return true;
}

template <typename NodeId>
void SpdyPriorityTree<NodeId>::PropagateNodeState(NodeId node_id) {
  // Reset total_writeable_child_weights to its maximum value.
  Node* node = &all_nodes_[node_id];
  node->total_writeable_child_weights = node->total_child_weights;
  for (typename List::iterator it = node->child_list->begin();
       it != node->child_list->end(); ++it) {
    PropagateNodeState(*it);
  }
  if (node->total_writeable_child_weights == 0 &&
      (node->blocked || !node->ready)) {
    // Tell the parent that this entire subtree is unwriteable.
    Node* parent = &all_nodes_[node->parent_id];
    parent->total_writeable_child_weights -= node->weight;
  }
}

template <typename NodeId>
const typename SpdyPriorityTree<NodeId>::Node*
SpdyPriorityTree<NodeId>::FindNode(NodeId node_id) const {
  typename NodeMap::const_iterator iter = all_nodes_.find(node_id);
  if (iter == all_nodes_.end()) {
    return NULL;
  }
  return &iter->second;
}

template <typename NodeId>
bool SpdyPriorityTree<NodeId>::HasChild(NodeId parent_id,
                                        NodeId child_id) const {
  const Node* parent = FindNode(parent_id);
  return parent->child_list->end() !=
         std::find(parent->child_list->begin(),
                   parent->child_list->end(),
                   child_id);
}

template <typename NodeId>
std::vector<std::pair<NodeId, float> >
SpdyPriorityTree<NodeId>::GetPriorityList() {
  typedef std::pair<NodeId, float> PriorityNode;
  typedef std::vector<PriorityNode> PriorityList;
  PriorityList priority_list;

  // Update total_writeable_child_weights to reflect the current
  // state of the tree.
  PropagateNodeState(kRootNodeId);

  List queue;
  const Node* root_node = FindNode(kRootNodeId);
  DCHECK(root_node->priority == 1.0);
  // Start by examining our top-level nodes.
  for (typename List::iterator it = root_node->child_list->begin();
       it != root_node->child_list->end(); ++it) {
    queue.push_back(*it);
  }
  while (!queue.empty()) {
    NodeId current_node_id = queue.front();
    Node* current_node = &all_nodes_[current_node_id];
    const Node* parent_node = FindNode(current_node->parent_id);
    if (current_node->blocked || !current_node->ready) {
      if (current_node->total_writeable_child_weights > 0) {
        // This node isn't writeable, but it has writeable children.
        // Calculate the total fraction of resources we can allot
        // to this subtree.
        current_node->priority = parent_node->priority *
            (static_cast<float>(current_node->weight) /
             static_cast<float>(parent_node->total_writeable_child_weights));
        // Examine the children.
        for (typename List::iterator it = current_node->child_list->begin();
             it != current_node->child_list->end(); ++it) {
          queue.push_back(*it);
        }
      } else {
        // There's nothing to see in this subtree.
        current_node->priority = 0;
      }
    } else {
      // This node is writeable; calculate its priority.
      current_node->priority = parent_node->priority *
          (static_cast<float>(current_node->weight) /
           static_cast<float>(parent_node->total_writeable_child_weights));
      // Add this node to the priority list.
      priority_list.push_back(PriorityNode(current_node_id,
                                           current_node->priority));
    }
    // Remove this node from the queue.
    queue.pop_front();
  }

  // Sort the nodes in descending order of priority.
  std::sort(priority_list.begin(), priority_list.end(),
            NodePriorityComparator());

  return priority_list;
}

template <typename NodeId>
bool SpdyPriorityTree<NodeId>::ValidateInvariantsForTests() const {
  int total_nodes = 0;
  int nodes_visited = 0;
  // Iterate through all nodes in the map.
  for (typename NodeMap::const_iterator iter = all_nodes_.begin();
       iter != all_nodes_.end(); ++iter) {
    ++total_nodes;
    ++nodes_visited;
    const Node& node = iter->second;
    // All nodes except the root should have a parent, and should appear in
    // the child_list of that parent.
    if (node.id != static_cast<NodeId>(kRootNodeId) &&
        (!NodeExists(node.parent_id) ||
         !HasChild(node.parent_id, node.id))) {
      DLOG(INFO) << "Parent node " << node.parent_id
                 << " does not exist, or does not list node " << node.id
                 << " as its child.";
      return false;
    }

    if (!node.child_list->empty()) {
      int total_child_weights = 0;
      // Iterate through the node's children.
      for (typename List::iterator it = node.child_list->begin();
           it != node.child_list->end(); ++it) {
        ++nodes_visited;
        // Each node in the list should exist and should have this node
        // set as its parent.
        if (!NodeExists(*it) || node.id != GetParent(*it)) {
          DLOG(INFO) << "Child node " << *it << " does not exist, "
                     << "or does not list " << node.id << " as its parent.";
          return false;
        }
        const Node* child = FindNode(*it);
        total_child_weights += child->weight;
      }
      // Verify that total_child_weights is correct.
      if (total_child_weights != node.total_child_weights) {
        DLOG(INFO) << "Child weight totals do not agree. For node " << node.id
                   << " total_child_weights has value "
                   << node.total_child_weights
                   << ", expected " << total_child_weights;
        return false;
      }
    }
  }

  // Make sure num_nodes reflects the total number of nodes the map contains.
  if (total_nodes != num_nodes()) {
    DLOG(INFO) << "Map contains incorrect number of nodes.";
    return false;
  }
  // Validate the validation function; we should have visited each node twice
  // (except for the root)
  DCHECK(nodes_visited == 2*num_nodes() - 1);
  return true;
}

}  // namespace net

#endif  // NET_SPDY_SPDY_PRIORITY_TREE_H_