Skip to content
New issue

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

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

Already on GitHub? # to your account

Stop add follower retries when the follower is already present #571

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions common/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
CodeInvalidSessionTimeout codes.Code = 109
CodeNamespaceNotFound codes.Code = 110
CodeNotificationsNotEnabled codes.Code = 111
CodeFollowerAlreadyPresent codes.Code = 112
)

var (
Expand All @@ -47,4 +48,5 @@ var (
ErrorInvalidSessionTimeout = status.Error(CodeInvalidSessionTimeout, "oxia: invalid session timeout")
ErrorNamespaceNotFound = status.Error(CodeNamespaceNotFound, "oxia: namespace not found")
ErrorNotificationsNotEnabled = status.Error(CodeNotificationsNotEnabled, "oxia: notifications not enabled on namespace")
ErrorFollowerAlreadyPresent = status.Error(CodeFollowerAlreadyPresent, "oxia: follower is already present")
)
2 changes: 1 addition & 1 deletion coordinator/impl/shard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ func (s *shardController) internalNewTermAndAddFollower(ctx context.Context, nod
if err = s.addFollower(*s.shardMetadata.Leader, node.Internal, &proto.EntryId{
Term: fr.Term,
Offset: fr.Offset,
}); err != nil {
}); err != nil && status.Code(err) != common.CodeFollowerAlreadyPresent {
res <- err
return
}
Expand Down
2 changes: 1 addition & 1 deletion server/leader_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (lc *leaderController) AddFollower(req *proto.AddFollowerRequest) (*proto.A
}

if _, followerAlreadyPresent := lc.followers[req.FollowerName]; followerAlreadyPresent {
return nil, errors.Errorf("follower %s is already present", req.FollowerName)
return nil, errors.Wrapf(common.ErrorFollowerAlreadyPresent, "follower: %s", req.FollowerName)
}

if len(lc.followers) == int(lc.replicationFactor)-1 {
Expand Down
53 changes: 53 additions & 0 deletions server/leader_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,59 @@ func TestLeaderController_AddFollower(t *testing.T) {
assert.NoError(t, walFactory.Close())
}

func TestLeaderController_AddFollowerRepeated(t *testing.T) {
var shard int64 = 1

kvFactory, err := kv.NewPebbleKVFactory(testKVOptions)
assert.NoError(t, err)
walFactory := newTestWalFactory(t)

lc, err := NewLeaderController(Config{}, common.DefaultNamespace, shard, newMockRpcClient(), walFactory, kvFactory)
assert.NoError(t, err)

_, err = lc.NewTerm(&proto.NewTermRequest{
Term: 5,
Shard: shard,
})
assert.NoError(t, err)

assert.EqualValues(t, 5, lc.Term())
assert.Equal(t, proto.ServingStatus_FENCED, lc.Status())

_, err = lc.BecomeLeader(context.Background(), &proto.BecomeLeaderRequest{
Shard: shard,
Term: 5,
ReplicationFactor: 3,
FollowerMaps: map[string]*proto.EntryId{
"f1": InvalidEntryId,
},
})
assert.NoError(t, err)

// f1 is already connected
afRes, err := lc.AddFollower(&proto.AddFollowerRequest{
Shard: shard,
Term: 5,
FollowerName: "f1",
FollowerHeadEntryId: InvalidEntryId,
})
assert.Nil(t, afRes)
assert.Error(t, err)

_, err = lc.AddFollower(&proto.AddFollowerRequest{
Shard: shard,
Term: 5,
FollowerName: "f1",
FollowerHeadEntryId: InvalidEntryId,
})
assert.Error(t, err)
assert.Equal(t, common.CodeFollowerAlreadyPresent, status.Code(err))

assert.NoError(t, lc.Close())
assert.NoError(t, kvFactory.Close())
assert.NoError(t, walFactory.Close())
}

// When a follower is added after the initial leader election,
// the leader should use the head-entry at the time of the
// election instead of the current head-entry.
Expand Down
Loading