add: external function for update consensus value

This commit is contained in:
bahadylbekov 2020-08-05 20:23:42 +03:00
parent b6e174b830
commit 81d6ce0c46

View File

@ -24,6 +24,7 @@ type RaftConsensus struct {
Consensus *libp2praft.Consensus Consensus *libp2praft.Consensus
State consensus.State State consensus.State
Transport *raft.NetworkTransport Transport *raft.NetworkTransport
Actor *libp2praft.Actor
} }
type RaftState struct { type RaftState struct {
@ -177,22 +178,21 @@ func (raftConsensus *RaftConsensus) StartConsensus(host host.Host, peers []peer.
raftConsensus.MakeConsensus(host, peers, nil) raftConsensus.MakeConsensus(host, peers, nil)
// Create the actors using the Raft nodes // Create the actors using the Raft nodes
actor := libp2praft.NewActor(raftConsensus.Raft) raftConsensus.Actor = libp2praft.NewActor(raftConsensus.Raft)
// Set the actors so that we can CommitState() and GetCurrentState() // Set the actors so that we can CommitState() and GetCurrentState()
raftConsensus.Consensus.SetActor(actor) raftConsensus.Consensus.SetActor(raftConsensus.Actor)
// Provide some time for leader election // Provide some time for leader election
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
}
// Run the 1000 updates on the leader func (raft *RaftConsensus) UpdateConsensus(value string) {
// Barrier() will wait until updates have been applied if raft.Actor.IsLeader() {
if actor.IsLeader() { if err := raft.UpdateState(value); err != nil {
value := "new value" raft.Logger.Fatal("Failed to update state", err)
if err := raftConsensus.UpdateState(value); err != nil {
raftConsensus.Logger.Fatal("Failed to update state", err)
} }
} else { } else {
raftConsensus.WaitForLeader(raftConsensus.Raft) raft.WaitForLeader(raft.Raft)
} }
} }