mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-04-14 10:07:39 +08:00
Changed RaftCore API; giving up Leader status if the leader cannot write its log
This commit is contained in:
parent
a6b0a256fb
commit
731d9f4574
@ -812,10 +812,10 @@ public:
|
||||
|
||||
/**
|
||||
* Inserts one entry into log.
|
||||
* Failures are tolerble because all operations are idempotent.
|
||||
* This method will trigger an assertion failure and return error if the current node is not the leader.
|
||||
* If operation fails, the node may give up its Leader status.
|
||||
*/
|
||||
int appendLog(const Entry::FieldTypes::unique_id& unique_id, NodeID node_id)
|
||||
void appendLog(const Entry::FieldTypes::unique_id& unique_id, NodeID node_id)
|
||||
{
|
||||
if (isLeader())
|
||||
{
|
||||
@ -825,12 +825,15 @@ public:
|
||||
entry.term = persistent_state_.getCurrentTerm();
|
||||
|
||||
trace(TraceRaftNewLogEntry, entry.node_id);
|
||||
return persistent_state_.getLog().append(entry);
|
||||
const int res = persistent_state_.getLog().append(entry);
|
||||
if (res < 0)
|
||||
{
|
||||
handlePersistentStateUpdateError(res);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
UAVCAN_ASSERT(0);
|
||||
return -ErrLogic;
|
||||
}
|
||||
}
|
||||
|
||||
@ -853,8 +856,8 @@ public:
|
||||
* Predicate is a callable of the following prototype:
|
||||
* bool (const LogEntryInfo& entry)
|
||||
* Once the predicate returns true, the loop will be terminated and the method will return an initialized lazy
|
||||
* contructor to the last visited entry; otherwise the constructor will not be initialized. In this case, lazy
|
||||
* constructor is used as boost::optional.
|
||||
* contructor with the last visited entry; otherwise the constructor will not be initialized.
|
||||
* In this case, lazy constructor is used as boost::optional.
|
||||
* The log is always traversed from HIGH to LOW index values, i.e. entry 0 will be traversed last.
|
||||
*/
|
||||
template <typename Predicate>
|
||||
|
||||
@ -169,11 +169,7 @@ class Server : IAllocationRequestHandler
|
||||
|
||||
if (raft_core_.isLeader())
|
||||
{
|
||||
const int res = raft_core_.appendLog(uid, node_id);
|
||||
if (res < 0)
|
||||
{
|
||||
node_.registerInternalFailure("Raft log append discovered node");
|
||||
}
|
||||
raft_core_.appendLog(uid, node_id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -200,11 +196,7 @@ class Server : IAllocationRequestHandler
|
||||
|
||||
if (!result.isConstructed())
|
||||
{
|
||||
const int res = raft_core_.appendLog(own_unique_id_, node_.getNodeID());
|
||||
if (res < 0)
|
||||
{
|
||||
node_.registerInternalFailure("Raft log append with self ID");
|
||||
}
|
||||
raft_core_.appendLog(own_unique_id_, node_.getNodeID());
|
||||
}
|
||||
}
|
||||
|
||||
@ -230,11 +222,7 @@ class Server : IAllocationRequestHandler
|
||||
|
||||
UAVCAN_TRACE("dynamic_node_id_server::distributed::Server", "New node ID allocated: %d",
|
||||
int(allocated_node_id.get()));
|
||||
const int res = raft_core_.appendLog(unique_id, allocated_node_id);
|
||||
if (res < 0)
|
||||
{
|
||||
node_.registerInternalFailure("Raft log append new allocation");
|
||||
}
|
||||
raft_core_.appendLog(unique_id, allocated_node_id);
|
||||
}
|
||||
|
||||
void tryPublishAllocationResult(const protocol::dynamic_node_id::server::Entry& entry)
|
||||
|
||||
@ -83,7 +83,7 @@ TEST(DynamicNodeIDServer, RaftCoreBasic)
|
||||
Entry::FieldTypes::unique_id unique_id;
|
||||
uavcan::fill_n(unique_id.begin(), 16, uint8_t(0xAA));
|
||||
|
||||
ASSERT_LE(0, raft_a->appendLog(unique_id, uavcan::NodeID(1)));
|
||||
raft_a->appendLog(unique_id, uavcan::NodeID(1));
|
||||
|
||||
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(2000));
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user