...

Source file src/go.etcd.io/etcd/raft/v3/rafttest/interaction_env_handler_propose_conf_change.go

Documentation: go.etcd.io/etcd/raft/v3/rafttest

     1  // Copyright 2019 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rafttest
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"testing"
    21  
    22  	"github.com/cockroachdb/datadriven"
    23  	"go.etcd.io/etcd/raft/v3/raftpb"
    24  )
    25  
    26  func (env *InteractionEnv) handleProposeConfChange(t *testing.T, d datadriven.TestData) error {
    27  	idx := firstAsNodeIdx(t, d)
    28  	var v1 bool
    29  	transition := raftpb.ConfChangeTransitionAuto
    30  	for _, arg := range d.CmdArgs[1:] {
    31  		for _, val := range arg.Vals {
    32  			switch arg.Key {
    33  			case "v1":
    34  				var err error
    35  				v1, err = strconv.ParseBool(val)
    36  				if err != nil {
    37  					return err
    38  				}
    39  			case "transition":
    40  				switch val {
    41  				case "auto":
    42  					transition = raftpb.ConfChangeTransitionAuto
    43  				case "implicit":
    44  					transition = raftpb.ConfChangeTransitionJointImplicit
    45  				case "explicit":
    46  					transition = raftpb.ConfChangeTransitionJointExplicit
    47  				default:
    48  					return fmt.Errorf("unknown transition %s", val)
    49  				}
    50  			default:
    51  				return fmt.Errorf("unknown command %s", arg.Key)
    52  			}
    53  		}
    54  	}
    55  
    56  	ccs, err := raftpb.ConfChangesFromString(d.Input)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	var c raftpb.ConfChangeI
    62  	if v1 {
    63  		if len(ccs) > 1 || transition != raftpb.ConfChangeTransitionAuto {
    64  			return fmt.Errorf("v1 conf change can only have one operation and no transition")
    65  		}
    66  		c = raftpb.ConfChange{
    67  			Type:   ccs[0].Type,
    68  			NodeID: ccs[0].NodeID,
    69  		}
    70  	} else {
    71  		c = raftpb.ConfChangeV2{
    72  			Transition: transition,
    73  			Changes:    ccs,
    74  		}
    75  	}
    76  	return env.ProposeConfChange(idx, c)
    77  }
    78  
    79  // ProposeConfChange proposes a configuration change on the node with the given index.
    80  func (env *InteractionEnv) ProposeConfChange(idx int, c raftpb.ConfChangeI) error {
    81  	return env.Nodes[idx].ProposeConfChange(c)
    82  }
    83  

View as plain text