GComm  0.2.3
evs_node.hpp
1 /*
2  * Copyright (C) 2009 Codership Oy <info@codership.com>
3  */
4 
5 #ifndef EVS_NODE_HPP
6 #define EVS_NODE_HPP
7 
8 #include "evs_message2.hpp"
9 
10 #include "gcomm/map.hpp"
11 #include "gcomm/uuid.hpp"
12 #include "gu_datetime.hpp"
13 #include "gu_logger.hpp"
14 
15 #include <limits>
16 
17 #include <stdint.h>
18 
19 
20 
21 namespace gcomm
22 {
23  namespace evs
24  {
25  class Node;
26  class NodeMap;
27  std::ostream& operator<<(std::ostream&, const Node&);
28  class InspectNode;
29  class OperationalSelect;
30  }
31 }
32 
33 
35 {
36 public:
37  Node(const gu::datetime::Period& inactive_timeout,
38  const gu::datetime::Period& suspect_timeout)
39  :
40  index_ (std::numeric_limits<size_t>::max()),
41  operational_ (true),
42  suspected_ (false),
43  inactive_ (false),
44  committed_ (false),
45  installed_ (false),
46  join_message_ (0),
47  leave_message_ (0),
48  suspect_timeout_ (suspect_timeout),
49  inactive_timeout_(inactive_timeout),
50  tstamp_ (gu::datetime::Date::now()),
51  fifo_seq_ (-1),
52  segment_ (0)
53  {}
54 
55  Node(const Node& n);
56 
57  ~Node();
58 
59  void set_index(const size_t idx) { index_ = idx; }
60  size_t index() const { return index_; }
61 
62  void set_operational(const bool op)
63  {
64  gcomm_assert(op == false);
65  operational_ = op;
66  }
67  bool operational() const { return operational_; }
68 
69  void set_suspected(const bool s)
70  {
71  suspected_ = s;
72  }
73  bool suspected() const { return suspected_; }
74 
75  void set_committed(const bool comm) { committed_ = comm; }
76  bool committed() const { return committed_; }
77  void set_installed(const bool inst) { installed_ = inst; }
78  bool installed() const { return installed_; }
79 
80  void set_join_message(const JoinMessage* msg);
81 
82  const JoinMessage* join_message() const { return join_message_; }
83 
84  void set_leave_message(const LeaveMessage* msg);
85 
86  const LeaveMessage* leave_message() const { return leave_message_; }
87 
88  void set_tstamp(const gu::datetime::Date& t) { tstamp_ = t; }
89  const gu::datetime::Date& tstamp() const { return tstamp_; }
90 
91  void set_fifo_seq(const int64_t seq) { fifo_seq_ = seq; }
92  int64_t fifo_seq() const { return fifo_seq_; }
93  SegmentId segment() const { return segment_; }
94 
95  bool is_inactive() const;
96  bool is_suspected() const;
97 
98  void set_suspect_timeout(const gu::datetime::Period& p)
99  {
100  suspect_timeout_ = p;
101  }
102  void set_inactive_timeout(const gu::datetime::Period& p)
103  {
104  inactive_timeout_ = p;
105  }
106 
107 private:
108 
109  void operator=(const Node&);
110 
111  friend class InspectNode;
112 
113  // Index for input map
114  size_t index_;
115  // True if instance is considered to be operational (has produced messages)
116  bool operational_;
117  bool suspected_;
118  bool inactive_;
119  // True if it is known that the instance has committed to install message
120  bool committed_;
121  // True if it is known that the instance has installed current view
122  bool installed_;
123  // Last received JOIN message
124  JoinMessage* join_message_;
125  // Last activity timestamp
126  LeaveMessage* leave_message_;
127  gu::datetime::Period suspect_timeout_;
128  //
129  gu::datetime::Period inactive_timeout_;
130  //
131  gu::datetime::Date tstamp_;
132  //
133  int64_t fifo_seq_;
134  SegmentId segment_;
135 };
136 
137 class gcomm::evs::NodeMap : public Map<UUID, Node> { };
138 
139 
141 {
142 public:
143  OperationalSelect(NodeMap& nm_) : nm(nm_) { }
144 
145  void operator()(const NodeMap::value_type& vt) const
146  {
147  if (NodeMap::value(vt).operational() == true)
148  {
149  nm.insert_unique(vt);
150  }
151  }
152 private:
153  NodeMap& nm;
154 };
155 
156 
158 {
159 public:
160  void operator()(std::pair<const gcomm::UUID, Node>& p) const
161  {
162  Node& node(p.second);
163  gu::datetime::Date now(gu::datetime::Date::now());
164  if (node.tstamp() + node.suspect_timeout_ < now)
165  {
166  if (node.suspected_ == false)
167  {
168  log_debug << "declaring node with index "
169  << node.index_
170  << " suspected, timeout " << node.suspect_timeout_;
171  }
172  node.suspected_ = true;
173  }
174  else
175  {
176  node.suspected_ = false;
177  }
178  if (node.tstamp() + node.inactive_timeout_ < now)
179  {
180  if (node.inactive_ == false)
181  {
182  log_debug << "declaring node with index "
183  << node.index_ << " inactive ";
184  }
185  node.inactive_ = true;
186  }
187  else
188  {
189  node.inactive_ = false;
190  }
191  }
192 };
193 
194 
195 #endif // EVS_NODE_HPP
Definition: evs_node.hpp:34
Definition: evs_message2.hpp:520
Definition: map.hpp:210
Definition: evs_node.hpp:157
Definition: evs_node.hpp:140
Definition: evs_message2.hpp:585
Definition: evs_node.hpp:137
#define gcomm_assert(cond_)
Definition: exception.hpp:21