Skip to content

Commit

Permalink
Merge pull request #13377 from hexfusion/add-learner-limit-flag
Browse files Browse the repository at this point in the history
Add experimental-max-learners flag
  • Loading branch information
hexfusion authored Nov 15, 2021
2 parents db6cd70 + 63a1cc3 commit 29c3b0f
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 48 deletions.
3 changes: 3 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ type ServerConfig struct {
// consider running defrag during bootstrap. Needs to be set to non-zero value to take effect.
ExperimentalBootstrapDefragThresholdMegabytes uint `json:"experimental-bootstrap-defrag-threshold-megabytes"`

// ExperimentalMaxLearners sets a limit to the number of learner members that can exist in the cluster membership.
ExperimentalMaxLearners int `json:"experimental-max-learners"`

// V2Deprecation defines a phase of v2store deprecation process.
V2Deprecation V2DeprecationEnum `json:"v2-deprecation"`
}
Expand Down
4 changes: 4 additions & 0 deletions server/embed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"go.etcd.io/etcd/pkg/v3/netutil"
"go.etcd.io/etcd/server/v3/config"
"go.etcd.io/etcd/server/v3/etcdserver"
"go.etcd.io/etcd/server/v3/etcdserver/api/membership"
"go.etcd.io/etcd/server/v3/etcdserver/api/v3compactor"

bolt "go.etcd.io/bbolt"
Expand Down Expand Up @@ -329,6 +330,8 @@ type Config struct {
// ExperimentalWarningUnaryRequestDuration is the time duration after which a warning is generated if applying
// unary request takes more time than this value.
ExperimentalWarningUnaryRequestDuration time.Duration `json:"experimental-warning-unary-request-duration"`
// ExperimentalMaxLearners sets a limit to the number of learner members that can exist in the cluster membership.
ExperimentalMaxLearners int `json:"experimental-max-learners"`

// ForceNewCluster starts a new cluster even if previously started; unsafe.
ForceNewCluster bool `json:"force-new-cluster"`
Expand Down Expand Up @@ -503,6 +506,7 @@ func NewConfig() *Config {
ExperimentalDowngradeCheckTime: DefaultDowngradeCheckTime,
ExperimentalMemoryMlock: false,
ExperimentalTxnModeWriteWithSharedBuffer: true,
ExperimentalMaxLearners: membership.DefaultMaxLearners,

V2Deprecation: config.V2_DEPR_DEFAULT,
}
Expand Down
4 changes: 3 additions & 1 deletion server/embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {
ExperimentalMemoryMlock: cfg.ExperimentalMemoryMlock,
ExperimentalTxnModeWriteWithSharedBuffer: cfg.ExperimentalTxnModeWriteWithSharedBuffer,
ExperimentalBootstrapDefragThresholdMegabytes: cfg.ExperimentalBootstrapDefragThresholdMegabytes,
V2Deprecation: cfg.V2DeprecationEffective(),
ExperimentalMaxLearners: cfg.ExperimentalMaxLearners,
V2Deprecation: cfg.V2DeprecationEffective(),
}

if srvcfg.ExperimentalEnableDistributedTracing {
Expand Down Expand Up @@ -345,6 +346,7 @@ func print(lg *zap.Logger, ec Config, sc config.ServerConfig, memberInitialized
zap.String("discovery-url", sc.DiscoveryURL),
zap.String("discovery-proxy", sc.DiscoveryProxy),
zap.String("downgrade-check-interval", sc.DowngradeCheckTime.String()),
zap.Int("max-learners", sc.ExperimentalMaxLearners),
)
}

Expand Down
2 changes: 2 additions & 0 deletions server/etcdmain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"go.etcd.io/etcd/pkg/v3/flags"
cconfig "go.etcd.io/etcd/server/v3/config"
"go.etcd.io/etcd/server/v3/embed"
"go.etcd.io/etcd/server/v3/etcdserver/api/membership"
"go.etcd.io/etcd/server/v3/etcdserver/api/rafthttp"

"go.uber.org/zap"
Expand Down Expand Up @@ -291,6 +292,7 @@ func newConfig() *config {
fs.BoolVar(&cfg.ec.ExperimentalMemoryMlock, "experimental-memory-mlock", cfg.ec.ExperimentalMemoryMlock, "Enable to enforce etcd pages (in particular bbolt) to stay in RAM.")
fs.BoolVar(&cfg.ec.ExperimentalTxnModeWriteWithSharedBuffer, "experimental-txn-mode-write-with-shared-buffer", true, "Enable the write transaction to use a shared buffer in its readonly check operations.")
fs.UintVar(&cfg.ec.ExperimentalBootstrapDefragThresholdMegabytes, "experimental-bootstrap-defrag-threshold-megabytes", 0, "Enable the defrag during etcd server bootstrap on condition that it will free at least the provided threshold of disk space. Needs to be set to non-zero value to take effect.")
fs.IntVar(&cfg.ec.ExperimentalMaxLearners, "experimental-max-learners", membership.DefaultMaxLearners, "Sets the maximum number of learners that can be available in the cluster membership.")

// unsafe
fs.BoolVar(&cfg.ec.UnsafeNoFsync, "unsafe-no-fsync", false, "Disables fsync, unsafe, will cause data loss.")
Expand Down
4 changes: 3 additions & 1 deletion server/etcdmain/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,15 @@ Experimental feature:
--experimental-watch-progress-notify-interval '10m'
Duration of periodical watch progress notification.
--experimental-warning-apply-duration '100ms'
Warning is generated if requests take more than this duration.
Warning is generated if requests take more than this duration.
--experimental-txn-mode-write-with-shared-buffer 'true'
Enable the write transaction to use a shared buffer in its readonly check operations.
--experimental-bootstrap-defrag-threshold-megabytes
Enable the defrag during etcd server bootstrap on condition that it will free at least the provided threshold of disk space. Needs to be set to non-zero value to take effect.
--experimental-warning-unary-request-duration '300ms'
Set time duration after which a warning is generated if a unary request takes more than this duration.
--experimental-max-learners '1'
Set the max number of learner members allowed in the cluster membership.
Unsafe feature:
--force-new-cluster 'false'
Expand Down
74 changes: 49 additions & 25 deletions server/etcdserver/api/membership/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import (
"go.uber.org/zap"
)

const maxLearners = 1

// RaftCluster is a list of Members that belong to the same raft cluster
type RaftCluster struct {
lg *zap.Logger
Expand All @@ -60,6 +58,7 @@ type RaftCluster struct {
removed map[types.ID]bool

downgradeInfo *serverversion.DowngradeInfo
maxLearners int
versionChanged *notify.Notifier
}

Expand All @@ -81,8 +80,8 @@ const (

// NewClusterFromURLsMap creates a new raft cluster using provided urls map. Currently, it does not support creating
// cluster with raft learner member.
func NewClusterFromURLsMap(lg *zap.Logger, token string, urlsmap types.URLsMap) (*RaftCluster, error) {
c := NewCluster(lg)
func NewClusterFromURLsMap(lg *zap.Logger, token string, urlsmap types.URLsMap, opts ...ClusterOption) (*RaftCluster, error) {
c := NewCluster(lg, opts...)
for name, urls := range urlsmap {
m := NewMember(name, urls, token, nil)
if _, ok := c.members[m.ID]; ok {
Expand All @@ -97,24 +96,27 @@ func NewClusterFromURLsMap(lg *zap.Logger, token string, urlsmap types.URLsMap)
return c, nil
}

func NewClusterFromMembers(lg *zap.Logger, id types.ID, membs []*Member) *RaftCluster {
c := NewCluster(lg)
func NewClusterFromMembers(lg *zap.Logger, id types.ID, membs []*Member, opts ...ClusterOption) *RaftCluster {
c := NewCluster(lg, opts...)
c.cid = id
for _, m := range membs {
c.members[m.ID] = m
}
return c
}

func NewCluster(lg *zap.Logger) *RaftCluster {
func NewCluster(lg *zap.Logger, opts ...ClusterOption) *RaftCluster {
if lg == nil {
lg = zap.NewNop()
}
clOpts := newClusterOpts(opts...)

return &RaftCluster{
lg: lg,
members: make(map[types.ID]*Member),
removed: make(map[types.ID]bool),
downgradeInfo: &serverversion.DowngradeInfo{Enabled: false},
maxLearners: clOpts.maxLearners,
}
}

Expand Down Expand Up @@ -289,6 +291,7 @@ func (c *RaftCluster) Recover(onSet func(*zap.Logger, *semver.Version)) {
zap.String("local-member-id", c.localID.String()),
zap.String("recovered-remote-peer-id", m.ID.String()),
zap.Strings("recovered-remote-peer-urls", m.PeerURLs),
zap.Bool("recovered-remote-peer-is-learner", m.IsLearner),
)
}
if c.version != nil {
Expand All @@ -303,9 +306,9 @@ func (c *RaftCluster) Recover(onSet func(*zap.Logger, *semver.Version)) {
// ensures that it is still valid.
func (c *RaftCluster) ValidateConfigurationChange(cc raftpb.ConfChange) error {
// TODO: this must be switched to backend as well.
members, removed := membersFromStore(c.lg, c.v2store)
membersMap, removedMap := membersFromStore(c.lg, c.v2store)
id := types.ID(cc.NodeID)
if removed[id] {
if removedMap[id] {
return ErrIDRemoved
}
switch cc.Type {
Expand All @@ -316,19 +319,21 @@ func (c *RaftCluster) ValidateConfigurationChange(cc raftpb.ConfChange) error {
}

if confChangeContext.IsPromote { // promoting a learner member to voting member
if members[id] == nil {
if membersMap[id] == nil {
return ErrIDNotFound
}
if !members[id].IsLearner {
if !membersMap[id].IsLearner {
return ErrMemberNotLearner
}
} else { // adding a new member
if members[id] != nil {
if membersMap[id] != nil {
return ErrIDExists
}

var members []*Member
urls := make(map[string]bool)
for _, m := range members {
for _, m := range membersMap {
members = append(members, m)
for _, u := range m.PeerURLs {
urls[u] = true
}
Expand All @@ -339,29 +344,24 @@ func (c *RaftCluster) ValidateConfigurationChange(cc raftpb.ConfChange) error {
}
}

if confChangeContext.Member.IsLearner { // the new member is a learner
numLearners := 0
for _, m := range members {
if m.IsLearner {
numLearners++
}
}
if numLearners+1 > maxLearners {
return ErrTooManyLearners
if confChangeContext.Member.RaftAttributes.IsLearner && cc.Type == raftpb.ConfChangeAddLearnerNode { // the new member is a learner
scaleUpLearners := true
if err := ValidateMaxLearnerConfig(c.maxLearners, members, scaleUpLearners); err != nil {
return err
}
}
}
case raftpb.ConfChangeRemoveNode:
if members[id] == nil {
if membersMap[id] == nil {
return ErrIDNotFound
}

case raftpb.ConfChangeUpdateNode:
if members[id] == nil {
if membersMap[id] == nil {
return ErrIDNotFound
}
urls := make(map[string]bool)
for _, m := range members {
for _, m := range membersMap {
if m.ID == id {
continue
}
Expand Down Expand Up @@ -407,6 +407,7 @@ func (c *RaftCluster) AddMember(m *Member, shouldApplyV3 ShouldApplyV3) {
zap.String("local-member-id", c.localID.String()),
zap.String("added-peer-id", m.ID.String()),
zap.Strings("added-peer-peer-urls", m.PeerURLs),
zap.Bool("added-peer-is-learner", m.IsLearner),
)
}

Expand Down Expand Up @@ -434,6 +435,7 @@ func (c *RaftCluster) RemoveMember(id types.ID, shouldApplyV3 ShouldApplyV3) {
zap.String("local-member-id", c.localID.String()),
zap.String("removed-remote-peer-id", id.String()),
zap.Strings("removed-remote-peer-urls", m.PeerURLs),
zap.Bool("removed-remote-peer-is-learner", m.IsLearner),
)
} else {
c.lg.Warn(
Expand Down Expand Up @@ -517,6 +519,7 @@ func (c *RaftCluster) UpdateRaftAttributes(id types.ID, raftAttr RaftAttributes,
zap.String("local-member-id", c.localID.String()),
zap.String("updated-remote-peer-id", id.String()),
zap.Strings("updated-remote-peer-urls", raftAttr.PeerURLs),
zap.Bool("updated-remote-peer-is-learner", raftAttr.IsLearner),
)
}

Expand Down Expand Up @@ -831,3 +834,24 @@ func (c *RaftCluster) updateMembershipMetric(peer types.ID, known bool) {
}
knownPeers.WithLabelValues(c.localID.String(), peer.String()).Set(v)
}

// ValidateMaxLearnerConfig verifies the existing learner members in the cluster membership and an optional N+1 learner
// scale up are not more than maxLearners.
func ValidateMaxLearnerConfig(maxLearners int, members []*Member, scaleUpLearners bool) error {
numLearners := 0
for _, m := range members {
if m.IsLearner {
numLearners++
}
}
// Validate config can accommodate scale up.
if scaleUpLearners {
numLearners++
}

if numLearners > maxLearners {
return ErrTooManyLearners
}

return nil
}
43 changes: 43 additions & 0 deletions server/etcdserver/api/membership/cluster_opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2021 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package membership

const DefaultMaxLearners = 1

type ClusterOptions struct {
maxLearners int
}

// ClusterOption are options which can be applied to the raft cluster.
type ClusterOption func(*ClusterOptions)

func newClusterOpts(opts ...ClusterOption) *ClusterOptions {
clOpts := &ClusterOptions{}
clOpts.applyOpts(opts)
return clOpts
}

func (co *ClusterOptions) applyOpts(opts []ClusterOption) {
for _, opt := range opts {
opt(co)
}
}

// WithMaxLearners sets the maximum number of learners that can exist in the cluster membership.
func WithMaxLearners(max int) ClusterOption {
return func(co *ClusterOptions) {
co.maxLearners = max
}
}
35 changes: 33 additions & 2 deletions server/etcdserver/api/membership/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,14 @@ func TestClusterValidateAndAssignIDs(t *testing.T) {
}

func TestClusterValidateConfigurationChange(t *testing.T) {
cl := NewCluster(zaptest.NewLogger(t))
cl := NewCluster(zaptest.NewLogger(t), WithMaxLearners(1))
cl.SetStore(v2store.New())
for i := 1; i <= 4; i++ {
attr := RaftAttributes{PeerURLs: []string{fmt.Sprintf("http://127.0.0.1:%d", i)}}
var isLearner bool
if i == 1 {
isLearner = true
}
attr := RaftAttributes{PeerURLs: []string{fmt.Sprintf("http://127.0.0.1:%d", i)}, IsLearner: isLearner}
cl.AddMember(&Member{ID: types.ID(i), RaftAttributes: attr}, true)
}
cl.RemoveMember(4, true)
Expand Down Expand Up @@ -326,6 +330,17 @@ func TestClusterValidateConfigurationChange(t *testing.T) {
t.Fatal(err)
}

attr = RaftAttributes{PeerURLs: []string{fmt.Sprintf("http://127.0.0.1:%d", 7)}, IsLearner: true}
ctx7, err := json.Marshal(&ConfigChangeContext{Member: Member{ID: types.ID(7), RaftAttributes: attr}})
if err != nil {
t.Fatal(err)
}

attr = RaftAttributes{PeerURLs: []string{fmt.Sprintf("http://127.0.0.1:%d", 1)}, IsLearner: true}
ctx8, err := json.Marshal(&ConfigChangeContext{Member: Member{ID: types.ID(1), RaftAttributes: attr}, IsPromote: true})
if err != nil {
t.Fatal(err)
}
tests := []struct {
cc raftpb.ConfChange
werr error
Expand Down Expand Up @@ -423,6 +438,22 @@ func TestClusterValidateConfigurationChange(t *testing.T) {
},
ErrIDNotFound,
},
{
raftpb.ConfChange{
Type: raftpb.ConfChangeAddLearnerNode,
NodeID: 7,
Context: ctx7,
},
ErrTooManyLearners,
},
{
raftpb.ConfChange{
Type: raftpb.ConfChangeAddNode,
NodeID: 1,
Context: ctx8,
},
nil,
},
}
for i, tt := range tests {
err := cl.ValidateConfigurationChange(tt.cc)
Expand Down
4 changes: 3 additions & 1 deletion server/etcdserver/api/membership/membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ func TestAddRemoveMember(t *testing.T) {
c := newTestCluster(t, nil)
be := &backendMock{}
c.SetBackend(be)
c.AddMember(newTestMember(17, nil, "node17", nil), true)
c.AddMember(newTestMemberAsLearner(17, nil, "node17", nil), true)
c.RemoveMember(17, true)
c.AddMember(newTestMember(18, nil, "node18", nil), true)
c.RemoveMember(18, true)

// Skipping removal of already removed member
c.RemoveMember(17, true)
c.RemoveMember(18, true)

if false {
// TODO: Enable this code when Recover is reading membership from the backend.
Expand Down
Loading

0 comments on commit 29c3b0f

Please sign in to comment.