distributed/server.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #if __GNUC__
6 # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
7 #endif
8 
9 #include <gtest/gtest.h>
10 #include <memory>
13 #include "../event_tracer.hpp"
14 #include "../../helpers.hpp"
15 #include "../memory_storage_backend.hpp"
16 
18 
19 
21 {
22  const std::string id_;
23 
24  virtual void handleLogCommitOnLeader(const uavcan::protocol::dynamic_node_id::server::Entry& entry)
25  {
26  std::cout << "ENTRY COMMITTED [" << id_ << "]\n" << entry << std::endl;
27  }
28 
29  virtual void handleLocalLeadershipChange(bool local_node_is_leader)
30  {
31  std::cout << "I AM LEADER [" << id_ << "]: " << (local_node_is_leader ? "YES" : "NOT ANYMORE") << std::endl;
32  }
33 
34 public:
35  CommitHandler(const std::string& id) : id_(id) { }
36 };
37 
38 
39 TEST(dynamic_node_id_server_RaftCore, Basic)
40 {
42  using namespace uavcan::protocol::dynamic_node_id::server;
43 
48 
49  EventTracer tracer_a("a");
50  EventTracer tracer_b("b");
51  MemoryStorageBackend storage_a;
52  MemoryStorageBackend storage_b;
53  CommitHandler commit_handler_a("a");
54  CommitHandler commit_handler_b("b");
55 
57 
58  std::unique_ptr<RaftCore> raft_a(new RaftCore(nodes.a, storage_a, tracer_a, commit_handler_a));
59  std::unique_ptr<RaftCore> raft_b(new RaftCore(nodes.b, storage_b, tracer_b, commit_handler_b));
60 
61  /*
62  * Initialization
63  */
64  ASSERT_LE(0, raft_a->init(2, uavcan::TransferPriority::OneHigherThanLowest));
65  ASSERT_LE(0, raft_b->init(2, uavcan::TransferPriority::OneHigherThanLowest));
66 
67  /*
68  * Running and trying not to fall
69  */
71 
72  // Either must become a leader
73  ASSERT_TRUE(raft_a->isLeader() || raft_b->isLeader());
74 
75  ASSERT_EQ(0, raft_a->getCommitIndex());
76  ASSERT_EQ(0, raft_b->getCommitIndex());
77 
78  /*
79  * Adding some stuff
80  */
81  Entry::FieldTypes::unique_id unique_id;
82  uavcan::fill_n(unique_id.begin(), 16, uint8_t(0xAA));
83 
84  (raft_a->isLeader() ? raft_a : raft_b)->appendLog(unique_id, uavcan::NodeID(1));
85 
87 
88  ASSERT_EQ(1, raft_a->getCommitIndex());
89  ASSERT_EQ(1, raft_b->getCommitIndex());
90 
91  /*
92  * Terminating the leader
93  */
94  raft_a.reset();
95 
97 
98  /*
99  * Reinitializing the leader - current Follower will become the new Leader
100  */
101  storage_a.reset();
102 
103  raft_a.reset(new RaftCore(nodes.a, storage_a, tracer_a, commit_handler_a));
104  ASSERT_LE(0, raft_a->init(2, uavcan::TransferPriority::OneHigherThanLowest));
105  ASSERT_EQ(0, raft_a->getCommitIndex());
106 
108 
109  ASSERT_FALSE(raft_a->isLeader());
110  ASSERT_TRUE(raft_b->isLeader());
111 
112  ASSERT_EQ(1, raft_a->getCommitIndex());
113  ASSERT_EQ(1, raft_b->getCommitIndex());
114 }
115 
116 
117 TEST(dynamic_node_id_server_Server, Basic)
118 {
119  using namespace uavcan::dynamic_node_id_server;
120  using namespace uavcan::protocol::dynamic_node_id;
121  using namespace uavcan::protocol::dynamic_node_id::server;
122 
130 
131  EventTracer tracer;
132  MemoryStorageBackend storage;
133 
134  // Node A is Allocator, Node B is Allocatee
136 
137  UniqueID own_unique_id;
138  own_unique_id[0] = 0xAA;
139  own_unique_id[3] = 0xCC;
140  own_unique_id[7] = 0xEE;
141  own_unique_id[9] = 0xBB;
142 
143  /*
144  * Server
145  */
146  DistributedServer server(nodes.a, storage, tracer);
147 
148  ASSERT_LE(0, server.init(own_unique_id, 1));
149 
150  ASSERT_EQ(0, server.getNumAllocations());
151 
152  /*
153  * Client
154  */
155  uavcan::DynamicNodeIDClient client(nodes.b);
156  uavcan::protocol::HardwareVersion::FieldTypes::unique_id unique_id;
157  for (uavcan::uint8_t i = 0; i < unique_id.size(); i++)
158  {
159  unique_id[i] = i;
160  }
161  const uavcan::NodeID PreferredNodeID = 42;
162  ASSERT_LE(0, client.start(unique_id, PreferredNodeID));
163 
164  /*
165  * Fire
166  */
168 
169  ASSERT_TRUE(client.isAllocationComplete());
170  ASSERT_EQ(PreferredNodeID, client.getAllocatedNodeID());
171 
172  ASSERT_EQ(2, server.getNumAllocations()); // Server's own node ID + client
173 }
174 
175 
176 TEST(dynamic_node_id_server, ObjectSizes)
177 {
178  using namespace uavcan;
179  using namespace uavcan::protocol::dynamic_node_id::server;
180  using namespace uavcan::dynamic_node_id_server;
181 
182  std::cout << "distributed::Log: " << sizeof(distributed::Log) << std::endl;
183  std::cout << "distributed::PersistentState: " << sizeof(distributed::PersistentState) << std::endl;
184  std::cout << "distributed::ClusterManager: " << sizeof(distributed::ClusterManager) << std::endl;
185  std::cout << "distributed::RaftCore: " << sizeof(distributed::RaftCore) << std::endl;
186  std::cout << "distributed::Server: " << sizeof(distributed::Server) << std::endl;
187  std::cout << "AllocationRequestManager: " << sizeof(AllocationRequestManager) << std::endl;
188 
189  std::cout << "ServiceServer<AppendEntries>: " << sizeof(ServiceServer<AppendEntries>) << std::endl;
190  std::cout << "ServiceClient<AppendEntries>: " << sizeof(ServiceClient<AppendEntries>) << std::endl;
191  std::cout << "ServiceServer<RequestVote>: " << sizeof(ServiceServer<RequestVote>) << std::endl;
192  std::cout << "ServiceClient<RequestVote>: " << sizeof(ServiceClient<RequestVote>) << std::endl;
193 }
CommitHandler::CommitHandler
CommitHandler(const std::string &id)
Definition: distributed/server.cpp:35
uavcan::DefaultDataTypeRegistrator
Definition: global_data_type_registry.hpp:186
uavcan::DynamicNodeIDClient::start
int start(const UniqueID &unique_id, const NodeID preferred_node_id=NodeID::Broadcast, const TransferPriority transfer_priority=TransferPriority::OneHigherThanLowest)
Definition: uc_dynamic_node_id_client.cpp:147
CommitHandler::id_
const std::string id_
Definition: distributed/server.cpp:22
uavcan::NodeID
Definition: transfer.hpp:112
uavcan::DurationBase< MonotonicDuration >::fromMSec
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
uavcan::DynamicNodeIDClient::isAllocationComplete
bool isAllocationComplete() const
Definition: dynamic_node_id_client.hpp:94
MemoryStorageBackend::reset
void reset()
Definition: memory_storage_backend.hpp:42
uavcan::fill_n
UAVCAN_EXPORT void fill_n(OutputIt first, std::size_t n, const T &value)
Definition: templates.hpp:268
CommitHandler
Definition: distributed/server.cpp:20
uavcan::dynamic_node_id_server::distributed::RaftCore
Definition: raft_core.hpp:64
uavcan::ServiceServer
Definition: service_server.hpp:90
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
uavcan::TransferPriority::OneHigherThanLowest
static const TransferPriority OneHigherThanLowest
Definition: transfer.hpp:40
TEST
TEST(dynamic_node_id_server_RaftCore, Basic)
Definition: distributed/server.cpp:39
uavcan::DynamicNodeIDClient
Definition: dynamic_node_id_client.hpp:31
uavcan::dynamic_node_id_server::distributed::PersistentState
Definition: persistent_state.hpp:25
InterlinkedTestNodes
Definition: test_node.hpp:149
uavcan::NodeID::Broadcast
static const NodeID Broadcast
Definition: transfer.hpp:122
uavcan::ServiceClient
Definition: service_client.hpp:217
uavcan::DynamicNodeIDClient::getAllocatedNodeID
NodeID getAllocatedNodeID() const
Definition: dynamic_node_id_client.hpp:102
uavcan::dynamic_node_id_server::distributed::IRaftLeaderMonitor
Definition: raft_core.hpp:32
dynamic_node_id_client.hpp
uavcan::dynamic_node_id_server::distributed::Server::getNumAllocations
Log::Index getNumAllocations() const
Definition: distributed/server.hpp:274
uavcan::dynamic_node_id_server::distributed::Server::init
int init(const UniqueID &own_unique_id, const uint8_t cluster_size=ClusterManager::ClusterSizeUnknown, const TransferPriority priority=TransferPriority::OneHigherThanLowest)
Definition: distributed/server.hpp:235
InterlinkedTestNodes::a
TestNode a
Definition: test_node.hpp:155
InterlinkedTestNodes::spinBoth
int spinBoth(uavcan::MonotonicDuration duration)
Definition: test_node.hpp:176
CommitHandler::handleLogCommitOnLeader
virtual void handleLogCommitOnLeader(const uavcan::protocol::dynamic_node_id::server::Entry &entry)
Definition: distributed/server.cpp:24
uavcan::dynamic_node_id_server::distributed
Definition: cluster_manager.hpp:25
MemoryStorageBackend
Definition: memory_storage_backend.hpp:10
uavcan::dynamic_node_id_server
Definition: abstract_server.hpp:16
uavcan::dynamic_node_id_server::distributed::Log
Definition: log.hpp:25
uavcan::GlobalDataTypeRegistry::instance
static GlobalDataTypeRegistry & instance()
Definition: uc_global_data_type_registry.cpp:128
uavcan
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:204
uavcan::dynamic_node_id_server::distributed::Server
Definition: distributed/server.hpp:25
uavcan::dynamic_node_id_server::AllocationRequestManager
Definition: allocation_request_manager.hpp:45
InterlinkedTestNodes::b
TestNode b
Definition: test_node.hpp:156
distributed.hpp
uavcan::dynamic_node_id_server::UniqueID
protocol::dynamic_node_id::server::Entry::FieldTypes::unique_id UniqueID
Definition: protocol/dynamic_node_id_server/types.hpp:22
CommitHandler::handleLocalLeadershipChange
virtual void handleLocalLeadershipChange(bool local_node_is_leader)
Definition: distributed/server.cpp:29
uavcan::dynamic_node_id_server::distributed::ClusterManager
Definition: cluster_manager.hpp:30


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03