(entry raftpb.Entry)
| 469 | } |
| 470 | |
| 471 | func (n *Node) applyEntry(entry raftpb.Entry) error { |
| 472 | switch entry.Type { |
| 473 | case raftpb.EntryNormal: |
| 474 | return n.dataStore.applyDataEntry(entry) |
| 475 | case raftpb.EntryConfChangeV2, raftpb.EntryConfChange: |
| 476 | // apply config change to the state machine |
| 477 | var cc raftpb.ConfChange |
| 478 | if err := cc.Unmarshal(entry.Data); err != nil { |
| 479 | return err |
| 480 | } |
| 481 | |
| 482 | n.confState = *n.raftNode.ApplyConfChange(cc) |
| 483 | switch cc.Type { |
| 484 | case raftpb.ConfChangeAddNode: |
| 485 | if cc.NodeID != n.config.ID && len(cc.Context) > 0 { |
| 486 | n.logger.Info("Add the new peer", zap.String("context", string(cc.Context))) |
| 487 | n.transport.AddPeer(types.ID(cc.NodeID), []string{string(cc.Context)}) |
| 488 | n.peers.Store(cc.NodeID, string(cc.Context)) |
| 489 | } |
| 490 | case raftpb.ConfChangeRemoveNode: |
| 491 | if cc.NodeID == n.config.ID { |
| 492 | n.logger.Info("I have been removed from the cluster, will shutdown") |
| 493 | n.Close() |
| 494 | return nil |
| 495 | } |
| 496 | n.transport.RemovePeer(types.ID(cc.NodeID)) |
| 497 | n.peers.Delete(cc.NodeID) |
| 498 | n.logger.Info("Remove the peer", zap.Uint64("node_id", cc.NodeID)) |
| 499 | case raftpb.ConfChangeUpdateNode: |
| 500 | n.transport.UpdatePeer(types.ID(cc.NodeID), []string{string(cc.Context)}) |
| 501 | if _, ok := n.peers.Load(cc.NodeID); ok { |
| 502 | n.peers.Store(cc.NodeID, string(cc.Context)) |
| 503 | } |
| 504 | case raftpb.ConfChangeAddLearnerNode: |
| 505 | // TODO: add the learner node |
| 506 | } |
| 507 | } |
| 508 | return nil |
| 509 | } |
| 510 | |
| 511 | func (n *Node) Process(ctx context.Context, m raftpb.Message) error { |
| 512 | return n.raftNode.Step(ctx, m) |
no test coverage detected