manager/state/raft/raft.go
File raft.go
has 1512 lines of code (exceeds 500 allowed). Consider refactoring. Open
Open
package raft
import (
"context"
"fmt"
Method Node.Run
has a Cognitive Complexity of 115 (exceeds 20 allowed). Consider refactoring. Open
Open
func (n *Node) Run(ctx context.Context) error {
ctx = log.WithLogger(ctx, logrus.WithField("raft_id", fmt.Sprintf("%x", n.Config.ID)))
ctx, cancel := context.WithCancel(ctx)
for _, node := range n.bootstrapMembers {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Node
has 65 methods (exceeds 20 allowed). Consider refactoring. Open
Open
type Node struct {
raftNode raft.Node
cluster *membership.Cluster
transport *transport.Transport
Method Node.Run
has 133 lines of code (exceeds 50 allowed). Consider refactoring. Open
Open
func (n *Node) Run(ctx context.Context) error {
ctx = log.WithLogger(ctx, logrus.WithField("raft_id", fmt.Sprintf("%x", n.Config.ID)))
ctx, cancel := context.WithCancel(ctx)
for _, node := range n.bootstrapMembers {
Method Node.Join
has 75 lines of code (exceeds 50 allowed). Consider refactoring. Open
Open
func (n *Node) Join(ctx context.Context, req *api.JoinRequest) (*api.JoinResponse, error) {
nodeInfo, err := ca.RemoteNode(ctx)
if err != nil {
return nil, err
}
Method Node.Join
has a Cognitive Complexity of 27 (exceeds 20 allowed). Consider refactoring. Open
Open
func (n *Node) Join(ctx context.Context, req *api.JoinRequest) (*api.JoinResponse, error) {
nodeInfo, err := ca.RemoteNode(ctx)
if err != nil {
return nil, err
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method Node.processInternalRaftRequest
has 59 lines of code (exceeds 50 allowed). Consider refactoring. Open
Open
func (n *Node) processInternalRaftRequest(ctx context.Context, r *api.InternalRaftRequest, cb func()) (proto.Message, error) {
n.stopMu.RLock()
if !n.IsMember() {
n.stopMu.RUnlock()
return nil, ErrStopped
Method Node.JoinAndStart
has 58 lines of code (exceeds 50 allowed). Consider refactoring. Open
Open
func (n *Node) JoinAndStart(ctx context.Context) (err error) {
ctx, cancel := n.WithContext(ctx)
defer func() {
cancel()
if err != nil {
Method Node.Join
has 12 return statements (exceeds 4 allowed). Open
Open
func (n *Node) Join(ctx context.Context, req *api.JoinRequest) (*api.JoinResponse, error) {
nodeInfo, err := ca.RemoteNode(ctx)
if err != nil {
return nil, err
}
Method Node.processInternalRaftRequest
has 11 return statements (exceeds 4 allowed). Open
Open
func (n *Node) processInternalRaftRequest(ctx context.Context, r *api.InternalRaftRequest, cb func()) (proto.Message, error) {
n.stopMu.RLock()
if !n.IsMember() {
n.stopMu.RUnlock()
return nil, ErrStopped
Method Node.JoinAndStart
has 8 return statements (exceeds 4 allowed). Open
Open
func (n *Node) JoinAndStart(ctx context.Context) (err error) {
ctx, cancel := n.WithContext(ctx)
defer func() {
cancel()
if err != nil {
Avoid deeply nested control flow statements. Open
Open
if atomic.LoadUint32(&n.signalledLeadership) == 1 {
atomic.StoreUint32(&n.signalledLeadership, 0)
n.leadershipBroadcast.Publish(IsFollower)
}
Method Node.reportNewAddress
has 7 return statements (exceeds 4 allowed). Open
Open
func (n *Node) reportNewAddress(ctx context.Context, id uint64) error {
// too early
if !n.IsMember() {
return nil
}
Method Node.registerNode
has 7 return statements (exceeds 4 allowed). Open
Open
func (n *Node) registerNode(node *api.RaftMember) error {
if n.cluster.IsIDRemoved(node.RaftID) {
return nil
}
Method Node.ProcessRaftMessage
has 7 return statements (exceeds 4 allowed). Open
Open
func (n *Node) ProcessRaftMessage(ctx context.Context, msg *api.ProcessRaftMessageRequest) (*api.ProcessRaftMessageResponse, error) {
if msg == nil || msg.Message == nil {
n.processRaftMessageLogger(ctx, msg).Debug("received empty message")
return &api.ProcessRaftMessageResponse{}, nil
}
Avoid deeply nested control flow statements. Open
Open
} else if !wasLeader && rd.SoftState.RaftState == raft.StateLeader {
// Node just became a leader.
wasLeader = true
}
Method Node.LeaderConn
has 6 return statements (exceeds 4 allowed). Open
Open
func (n *Node) LeaderConn(ctx context.Context) (*grpc.ClientConn, error) {
cc, err := n.getLeaderConn()
if err == nil {
return cc, nil
}
Method Node.ChangesBetween
has 6 return statements (exceeds 4 allowed). Open
Open
func (n *Node) ChangesBetween(from, to api.Version) ([]state.Change, error) {
n.stopMu.RLock()
defer n.stopMu.RUnlock()
if from.Index > to.Index {
Method Node.saveToStorage
has 5 arguments (exceeds 4 allowed). Consider refactoring. Open
Open
ctx context.Context,
raftConfig *api.RaftConfig,
hardState raftpb.HardState,
entries []raftpb.Entry,
snapshot raftpb.Snapshot,
Method Node.StreamRaftMessage
has 5 return statements (exceeds 4 allowed). Open
Open
func (n *Node) StreamRaftMessage(stream api.Raft_StreamRaftMessageServer) error {
// recvdMsg is the current messasge received from the stream.
// assembledMessage is where the data from recvdMsg is appended to.
var recvdMsg, assembledMessage *api.StreamRaftMessageRequest
var err error
Method Node.saveToStorage
has 5 return statements (exceeds 4 allowed). Open
Open
func (n *Node) saveToStorage(
ctx context.Context,
raftConfig *api.RaftConfig,
hardState raftpb.HardState,
entries []raftpb.Entry,
Method Node.TransferLeadership
has 5 return statements (exceeds 4 allowed). Open
Open
func (n *Node) TransferLeadership(ctx context.Context) error {
ctx, cancelTransfer := context.WithTimeout(ctx, n.reqTimeout())
defer cancelTransfer()
n.stopMu.RLock()