Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: ignore raft messages if member id mismatch #17078

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions server/etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,14 @@ func (s *EtcdServer) Process(ctx context.Context, m raftpb.Message) error {
)
return httptypes.NewHTTPError(http.StatusForbidden, "cannot process message from removed member")
}
if s.MemberId() != types.ID(m.To) {
lg.Warn(
"rejected Raft message to mismatch member",
zap.String("local-member-id", s.MemberId().String()),
zap.String("mismatch-member-id", types.ID(m.To).String()),
)
return httptypes.NewHTTPError(http.StatusForbidden, "cannot process message to mismatch member")
}
if m.Type == raftpb.MsgApp {
s.stats.RecvAppendReq(types.ID(m.From).String(), m.Size())
}
Expand Down
60 changes: 58 additions & 2 deletions server/etcdserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func TestApplyConfigChangeUpdatesConsistIndex(t *testing.T) {
lgMu: new(sync.RWMutex),
lg: lg,
memberId: 1,
r: *realisticRaftNode(lg),
r: *realisticRaftNode(lg, nil),
cluster: cl,
w: wait.New(),
consistIndex: ci,
Expand Down Expand Up @@ -514,9 +514,15 @@ func TestApplyConfigChangeUpdatesConsistIndex(t *testing.T) {
assert.Equal(t, consistIndex, rindex)
}

func realisticRaftNode(lg *zap.Logger) *raftNode {
func realisticRaftNode(lg *zap.Logger, snap *raftpb.Snapshot) *raftNode {
storage := raft.NewMemoryStorage()
storage.SetHardState(raftpb.HardState{Commit: 0, Term: 0})
if snap != nil {
err := storage.ApplySnapshot(*snap)
if err != nil {
panic(err)
}
}
c := &raft.Config{
ID: 1,
ElectionTick: 10,
Expand Down Expand Up @@ -889,6 +895,56 @@ func TestAddMember(t *testing.T) {
}
}

// TestProcessIgnoreMismatchMessage tests Process must ignore messages to
// mismatch member.
func TestProcessIgnoreMismatchMessage(t *testing.T) {
lg := zaptest.NewLogger(t)
cl := newTestCluster(t)
st := v2store.New()
cl.SetStore(st)
be, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, be)
cl.SetBackend(schema.NewMembershipBackend(lg, be))

// Bootstrap a 3-node cluster
cl.AddMember(&membership.Member{ID: types.ID(1)}, true)
cl.AddMember(&membership.Member{ID: types.ID(2)}, true)
cl.AddMember(&membership.Member{ID: types.ID(3)}, true)
r := realisticRaftNode(lg, &raftpb.Snapshot{
Metadata: raftpb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: raftpb.ConfState{Voters: []uint64{1, 2, 3}},
},
})
s := &EtcdServer{
lgMu: new(sync.RWMutex),
lg: lg,
r: *r,
v2store: st,
cluster: cl,
reqIDGen: idutil.NewGenerator(0, time.Time{}),
SyncTicker: &time.Ticker{},
consistIndex: cindex.NewFakeConsistentIndex(0),
beHooks: serverstorage.NewBackendHooks(lg, nil),
}
// Mock a mad switch dispatching messages to wrong node.
m := raftpb.Message{
Type: raftpb.MsgHeartbeat,
To: 2, // Wrong ID.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me some time to get what's current member's ID. You do not explicitly specify a member ID for current member, so it's 0. Please let's explicitly set a member ID for current member to make it clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9f82390, actually I want the server member ID to be 1.

From: 3,
Term: 11,
Commit: 42, // Commit is larger than the last index.
}
if types.ID(m.To) == s.MemberId() {
t.Fatalf("To must mismatch")
ahrtr marked this conversation as resolved.
Show resolved Hide resolved
}
err := s.Process(context.Background(), m)
if err == nil {
t.Fatalf("Must ignore the message and return an error")
}
}

// TestRemoveMember tests RemoveMember can propose and perform node removal.
func TestRemoveMember(t *testing.T) {
lg := zaptest.NewLogger(t)
Expand Down
Loading