NodeI.h
Go to the documentation of this file.
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
9 
10 #ifndef ELECTION_I_H
11 #define ELECTION_I_H
12 
13 #include <IceUtil/IceUtil.h>
14 #include <Ice/Ice.h>
15 #include <IceStorm/Election.h>
16 #include <IceStorm/Replica.h>
17 #include <IceStorm/Instance.h>
18 #include <IceUtil/Timer.h>
19 #include <set>
20 
21 namespace IceStormElection
22 {
23 
24  class Observers;
26 
27  class NodeI : public Node, public IceUtil::Monitor<IceUtil::RecMutex>
28  {
29  public:
30 
31  NodeI(const IceStorm::InstancePtr&, const ReplicaPtr&, const Ice::ObjectPrx&,
32  int, const std::map<int, NodePrx>&);
33 
34  void start();
35 
36  void check();
37  void timeout();
38  void merge(const std::set<int>&);
39  void mergeContinue();
40  virtual void invitation(int, const std::string&, const Ice::Current&);
41  virtual void ready(int, const std::string&, const Ice::ObjectPrx&, int, Ice::Long, const Ice::Current&);
42  virtual void accept(int, const std::string&, const Ice::IntSeq&, const Ice::ObjectPrx&, const LogUpdate&, int,
43  const Ice::Current&);
44  virtual bool areYouCoordinator(const Ice::Current&) const;
45  virtual bool areYouThere(const std::string&, int, const Ice::Current&) const;
46  virtual Ice::ObjectPrx sync(const Ice::Current&) const;
47  virtual NodeInfoSeq nodes(const Ice::Current&) const;
48  virtual QueryInfo query(const Ice::Current&) const;
49  void recovery(Ice::Long = -1);
50 
51  void destroy();
52 
53  // Notify the node that we're about to start an update.
55  Ice::ObjectPrx startUpdate(Ice::Long&, const char*, int);
56  Ice::ObjectPrx startCachedRead(Ice::Long&, const char*, int);
57  void startObserverUpdate(Ice::Long, const char*, int);
58  bool updateMaster(const char*, int);
59 
60  // The node has completed the update.
61  void finishUpdate();
62 
63  private:
64 
65  void setState(NodeState);
66 
67  const IceUtil::TimerPtr _timer;
68  const IceStorm::TraceLevelsPtr _traceLevels;
69  const IceStormElection::ObserversPtr _observers;
70  const ReplicaPtr _replica; // The replica.
71  const Ice::ObjectPrx _replicaProxy; // A proxy to the individual replica.
72 
73  const int _id; // My node id.
74  const std::map<int, NodePrx> _nodes; // The nodes indexed by their id.
75  const std::map<int, NodePrx> _nodesOneway; // The nodes indexed by their id (as oneway proxies).
76 
77  const IceUtil::Time _masterTimeout;
78  const IceUtil::Time _electionTimeout;
79  const IceUtil::Time _mergeTimeout;
80 
81  NodeState _state;
82  int _updateCounter;
83 
84  int _coord; // Id of the coordinator.
85  std::string _group; // My group id.
86 
87  std::set<GroupNodeInfo> _up; // Set of nodes in my group.
88  std::set<int> _invitesIssued; // The issued invitations.
89  std::set<int> _invitesAccepted; // The accepted invitations.
90 
91  unsigned int _max; // The highest group count I've seen.
92  Ice::Long _generation; // The current generation (or -1 if not set).
93 
94  Ice::ObjectPrx _coordinatorProxy;
95  bool _destroy;
96 
97  // Various timers.
98  IceUtil::TimerTaskPtr _mergeTask;
99  IceUtil::TimerTaskPtr _timeoutTask;
100  IceUtil::TimerTaskPtr _checkTask;
101  IceUtil::TimerTaskPtr _mergeContinueTask;
102  };
104 
106  {
107  public:
108 
110  _node(node)
111  {
112  }
113 
115  {
116  if (_node)
117  {
118  _node->finishUpdate();
119  }
120  }
121 
122  private:
123 
124  const NodeIPtr _node;
125  };
126 
128  {
129  public:
130 
131  CachedReadHelper(const NodeIPtr& node, const char* file, int line) :
132  _node(node)
133  {
134  if (_node)
135  {
136  _master = _node->startCachedRead(_generation, file, line);
137  }
138  }
139 
141  {
142  if (_node)
143  {
144  _node->finishUpdate();
145  }
146  }
147 
148  Ice::ObjectPrx
149  getMaster() const
150  {
151  return _master;
152  }
153 
154  Ice::Long
155  generation() const
156  {
157  return _generation;
158  }
159 
160  bool
162  {
163  return generation == _generation && _master;
164  }
165 
166  private:
167 
168  const NodeIPtr _node;
169  Ice::ObjectPrx _master;
170  Ice::Long _generation;
171  };
172 
174  {
175  public:
176 
177  ObserverUpdateHelper(const NodeIPtr& node, Ice::Long generation, const char* file, int line) :
178  _node(node)
179  {
180  if (_node)
181  {
182  _node->startObserverUpdate(generation, file, line);
183  }
184  }
185 
187  {
188  if (_node)
189  {
190  _node->finishUpdate();
191  }
192  }
193 
194  private:
195 
196  const NodeIPtr _node;
197  };
198 
199 }
200 
201 #endif // ELECTION_I_H
IceStormElection::FinishUpdateHelper::~FinishUpdateHelper
~FinishUpdateHelper()
Definition: NodeI.h:114
IceStormElection::NodeI::merge
void merge(const std::set< int > &)
Definition: NodeI.cpp:404
IceStormElection::ObserversPtr
IceUtil::Handle< Observers > ObserversPtr
Definition: Instance.h:32
IceStormElection::NodeIPtr
IceUtil::Handle< NodeI > NodeIPtr
Definition: Instance.h:35
IceStormElection::ObserverUpdateHelper::ObserverUpdateHelper
ObserverUpdateHelper(const NodeIPtr &node, Ice::Long generation, const char *file, int line)
Definition: NodeI.h:177
IceStormElection::Node::nodes
idempotent NodeInfoSeq nodes()
Get the replication group information.
IceStormElection::QueryInfo
Definition: Election.ice:177
Replica.h
IceStormElection::LogUpdate
A struct used for marking the last log update.
Definition: LLURecord.h:100
IceStormElection::CachedReadHelper::getMaster
Ice::ObjectPrx getMaster() const
Definition: NodeI.h:149
IceStormElection::Node::areYouCoordinator
idempotent bool areYouCoordinator()
Determine if this node is a coordinator.
IceStormElection::NodeI::recovery
void recovery(Ice::Long=-1)
Definition: NodeI.cpp:1001
IceStormElection::NodeI::destroy
void destroy()
Definition: NodeI.cpp:1055
IceStormElection::NodeI::updateMaster
bool updateMaster(const char *, int)
Definition: NodeI.cpp:1134
IceStormElection::NodeI::start
void start()
Definition: NodeI.cpp:189
IceStormElection::Node::sync
idempotent Object * sync()
Get the sync object for the replica hosted by this node.
IceStormElection::NodeI::startCachedRead
Ice::ObjectPrx startCachedRead(Ice::Long &, const char *, int)
Definition: NodeI.cpp:1165
IceStormElection::Node
A replica node.
Definition: Election.ice:202
IceStormElection::NodeState
NodeState
The node state.
Definition: Election.ice:139
IceStormElection
Definition: DBTypes.ice:17
IceStormElection::NodeI::timeout
void timeout()
Definition: NodeI.cpp:356
IceStormElection::NodeI::check
void check()
Definition: NodeI.cpp:220
IceStormElection::NodeI::ready
virtual void ready(int, const std::string &, const Ice::ObjectPrx &, int, Ice::Long, const Ice::Current &)
Definition: NodeI.cpp:835
IceStormElection::NodeI
Definition: NodeI.h:27
IceStormElection::ObserverUpdateHelper::~ObserverUpdateHelper
~ObserverUpdateHelper()
Definition: NodeI.h:186
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
IceStormElection::NodeI::finishUpdate
void finishUpdate()
Definition: NodeI.cpp:1205
IceStormElection::CachedReadHelper::generation
Ice::Long generation() const
Definition: NodeI.h:155
IceStormElection::NodeI::accept
virtual void accept(int, const std::string &, const Ice::IntSeq &, const Ice::ObjectPrx &, const LogUpdate &, int, const Ice::Current &)
Definition: NodeI.cpp:878
IceStormElection::NodeI::mergeContinue
void mergeContinue()
Definition: NodeI.cpp:514
Election.h
IceStormElection::FinishUpdateHelper::FinishUpdateHelper
FinishUpdateHelper(const NodeIPtr &node)
Definition: NodeI.h:109
IceStormElection::CachedReadHelper
Definition: NodeI.h:127
IceStormElection::CachedReadHelper::observerPrecondition
bool observerPrecondition(Ice::Long generation) const
Definition: NodeI.h:161
IceUtil::Handle< Observers >
IceStormElection::NodeI::invitation
virtual void invitation(int, const std::string &, const Ice::Current &)
Definition: NodeI.cpp:707
IceStormElection::NodeInfoSeq
::std::vector< ::IceStormElection::NodeInfo > NodeInfoSeq
A sequence of node info.
Definition: Election.h:857
IceStormElection::NodeI::checkObserverInit
void checkObserverInit(Ice::Long)
Definition: NodeI.cpp:1090
IceStormElection::Node::query
idempotent QueryInfo query()
Get the query information for the given node.
IceStormElection::ObserverUpdateHelper
Definition: NodeI.h:173
IceStormElection::NodeI::startUpdate
Ice::ObjectPrx startUpdate(Ice::Long &, const char *, int)
Definition: NodeI.cpp:1105
IceStormElection::CachedReadHelper::~CachedReadHelper
~CachedReadHelper()
Definition: NodeI.h:140
IceStormElection::NodeI::areYouThere
virtual bool areYouThere(const std::string &, int, const Ice::Current &) const
Definition: NodeI.cpp:950
Instance.h
IceStormElection::NodeI::NodeI
NodeI(const IceStorm::InstancePtr &, const ReplicaPtr &, const Ice::ObjectPrx &, int, const std::map< int, NodePrx > &)
Definition: NodeI.cpp:155
IceStormElection::CachedReadHelper::CachedReadHelper
CachedReadHelper(const NodeIPtr &node, const char *file, int line)
Definition: NodeI.h:131
IceStormElection::NodeI::startObserverUpdate
void startObserverUpdate(Ice::Long, const char *, int)
Definition: NodeI.cpp:1182
IceStormElection::FinishUpdateHelper
Definition: NodeI.h:105