GComm  0.2.3
evs_input_map2.hpp
1 /*
2  * Copyright (C) 2009 Codership Oy <info@codership.com>
3  *
4  * $Id$
5  */
6 
19 #ifndef EVS_INPUT_MAP2_HPP
20 #define EVS_INPUT_MAP2_HPP
21 
22 #include "evs_message2.hpp"
23 #include "gcomm/map.hpp"
24 #include "gcomm/datagram.hpp"
25 
26 #include <vector>
27 
28 
29 namespace gcomm
30 {
31  /* Forward declarations */
32  class InputMapMsgKey;
33  std::ostream& operator<<(std::ostream&, const InputMapMsgKey&);
34  namespace evs
35  {
36  class InputMapMsg;
37  std::ostream& operator<<(std::ostream&, const InputMapMsg&);
38  class InputMapMsgIndex;
39  class InputMapNode;
40  std::ostream& operator<<(std::ostream&, const InputMapNode&);
41  typedef std::vector<InputMapNode> InputMapNodeIndex;
42  std::ostream& operator<<(std::ostream&, const InputMapNodeIndex&);
43  class InputMap;
44  }
45 }
46 
47 
48 /* Internal msg representation */
50 {
51 public:
52  InputMapMsgKey(const size_t index, const evs::seqno_t seq) :
53  index_ (index),
54  seq_ (seq)
55  { }
56 
57  size_t index() const { return index_; }
58  evs::seqno_t seq () const { return seq_; }
59 
60  bool operator<(const InputMapMsgKey& cmp) const
61  {
62  return (seq_ < cmp.seq_ || (seq_ == cmp.seq_ && index_ < cmp.index_));
63  }
64 
65 private:
66  size_t const index_;
67  evs::seqno_t const seq_;
68 };
69 
70 
71 /* Internal message representation */
73 {
74 public:
75  InputMapMsg(const UserMessage& msg,
76  const Datagram& rb)
77  :
78  msg_(msg),
79  rb_ (rb)
80  { }
81  InputMapMsg(const InputMapMsg& m) : msg_(m.msg_), rb_ (m.rb_) { }
82  ~InputMapMsg() { }
83 
84  const UserMessage& msg () const { return msg_; }
85  const Datagram& rb () const { return rb_; }
86 private:
87  void operator=(const InputMapMsg&);
88 
89  UserMessage const msg_;
90  Datagram rb_;
91 };
92 
93 
94 #if defined(GALERA_USE_BOOST_POOL_ALLOC)
95 
96 #include <boost/pool/pool_alloc.hpp>
97 
99  public Map<InputMapMsgKey, InputMapMsg,
100  std::map<InputMapMsgKey,
101  InputMapMsg,
102  std::less<InputMapMsgKey>,
103  boost::fast_pool_allocator<
104  std::pair<const InputMapMsgKey, InputMapMsg>,
105  boost::default_user_allocator_new_delete,
106  boost::details::pool::null_mutex
107  >
108  >
109  >
110 {};
111 
112 #else /* GALERA_USE_BOOST_POOL_ALLOC */
113 
115  public Map<InputMapMsgKey, InputMapMsg>
116 {};
117 
118 #endif /* GALERA_USE_BOOST_POOL_ALLOC */
119 
120 /* Internal node representation */
122 {
123 public:
124  InputMapNode() : idx_(), range_(0, -1), safe_seq_(-1) { }
125 
126  void set_range (const Range r) { range_ = r; }
127  void set_safe_seq (const seqno_t s) { safe_seq_ = s; }
128  void set_index (const size_t i) { idx_ = i; }
129 
130  Range range () const { return range_; }
131  seqno_t safe_seq () const { return safe_seq_; }
132  size_t index () const { return idx_; }
133 
134 private:
135  size_t idx_;
136  Range range_;
137  seqno_t safe_seq_;
138 };
139 
140 
141 
142 
143 
149 {
150 public:
151 
152  /* Iterators exposed to user */
153  typedef InputMapMsgIndex::iterator iterator;
154  typedef InputMapMsgIndex::const_iterator const_iterator;
155 
159  InputMap();
160 
164  ~InputMap();
165 
171  seqno_t aru_seq () const { return aru_seq_; }
172 
178  seqno_t safe_seq() const { return safe_seq_; }
179 
189  void set_safe_seq(const size_t uuid, const seqno_t seq);
190 
200  seqno_t safe_seq(const size_t uuid) const
201  {
202  return node_index_->at(uuid).safe_seq();
203  }
204 
214  Range range (const size_t uuid) const
215  {
216  return node_index_->at(uuid).range();
217  }
218 
219  seqno_t min_hs() const;
220 
221  seqno_t max_hs() const;
222 
228  iterator begin() const { return msg_index_->begin(); }
229 
235  iterator end () const { return msg_index_->end(); }
236 
242  bool is_safe (iterator i) const
243  {
244  const seqno_t seq(InputMapMsgIndex::key(i).seq());
245  return (seq <= safe_seq_);
246  }
247 
253  bool is_agreed(iterator i) const
254  {
255  const seqno_t seq(InputMapMsgIndex::key(i).seq());
256  return (seq <= aru_seq_);
257  }
258 
264  bool is_fifo (iterator i) const
265  {
266  const seqno_t seq(InputMapMsgIndex::key(i).seq());
267  const InputMapNode& node((*node_index_)[
268  InputMapMsgIndex::key(i).index()]);
269  return (node.range().lu() > seq);
270  }
271 
272  bool has_deliverables() const
273  {
274  if (msg_index_->empty() == false)
275  {
276  if (n_msgs_[O_FIFO] > 0 && is_fifo(msg_index_->begin()))
277  return true;
278  else if (n_msgs_[O_AGREED] > 0 && is_agreed(msg_index_->begin()))
279  return true;
280  else if (n_msgs_[O_SAFE] > 0 && is_safe(msg_index_->begin()))
281  return true;
282  else if (n_msgs_[O_DROP] > max_droppable_)
283  return true;
284  return false;
285  }
286  else
287  {
288  return false;
289  }
290  }
291 
305  Range insert(const size_t uuid, const UserMessage& msg,
306  const Datagram& dg = Datagram());
307 
317  void erase(iterator i);
318 
329  iterator find(const size_t uuid, const seqno_t seq) const;
330 
341  iterator recover(const size_t uuid, const seqno_t seq) const;
342 
346  void reset(const size_t, const seqno_t = 256);
347 
351  void clear();
352 
353 private:
354 
355  friend std::ostream& operator<<(std::ostream&, const InputMap&);
356  /* Non-copyable */
357  InputMap(const InputMap&);
358  void operator=(const InputMap&);
359 
363  void update_aru();
364 
368  void cleanup_recovery_index();
369 
370  seqno_t window_;
371  seqno_t safe_seq_;
372  seqno_t aru_seq_;
373  InputMapNodeIndex* node_index_;
374  InputMapMsgIndex* msg_index_;
375  InputMapMsgIndex* recovery_index_;
377  std::vector<size_t> n_msgs_;
378  size_t max_droppable_;
379 };
380 
381 #endif // EVS_INPUT_MAP2_HPP
Definition: evs_seqno.hpp:31
seqno_t aru_seq() const
Definition: evs_input_map2.hpp:171
Definition: map.hpp:210
iterator recover(const size_t uuid, const seqno_t seq) const
Definition: evs_input_map2.hpp:114
Definition: evs_input_map2.hpp:72
bool is_fifo(iterator i) const
Definition: evs_input_map2.hpp:264
bool is_agreed(iterator i) const
Definition: evs_input_map2.hpp:253
void set_safe_seq(const size_t uuid, const seqno_t seq)
Range insert(const size_t uuid, const UserMessage &msg, const Datagram &dg=Datagram())
Definition: evs_input_map2.hpp:49
iterator end() const
Definition: evs_input_map2.hpp:235
seqno_t safe_seq() const
Definition: evs_input_map2.hpp:178
void erase(iterator i)
bool is_safe(iterator i) const
Definition: evs_input_map2.hpp:242
Definition: evs_message2.hpp:390
Range range(const size_t uuid) const
Definition: evs_input_map2.hpp:214
iterator find(const size_t uuid, const seqno_t seq) const
iterator begin() const
Definition: evs_input_map2.hpp:228
Definition: evs_input_map2.hpp:121
seqno_t safe_seq(const size_t uuid) const
Definition: evs_input_map2.hpp:200
Definition: evs_input_map2.hpp:148
Datagram container.
Definition: datagram.hpp:151