...

Source file src/go.etcd.io/etcd/raft/v3/rafttest/interaction_env.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  	"bufio"
    19  	"fmt"
    20  	"math"
    21  	"strings"
    22  
    23  	"go.etcd.io/etcd/raft/v3"
    24  	pb "go.etcd.io/etcd/raft/v3/raftpb"
    25  )
    26  
    27  // InteractionOpts groups the options for an InteractionEnv.
    28  type InteractionOpts struct {
    29  	OnConfig func(*raft.Config)
    30  }
    31  
    32  // Node is a member of a raft group tested via an InteractionEnv.
    33  type Node struct {
    34  	*raft.RawNode
    35  	Storage
    36  
    37  	Config  *raft.Config
    38  	History []pb.Snapshot
    39  }
    40  
    41  // InteractionEnv facilitates testing of complex interactions between the
    42  // members of a raft group.
    43  type InteractionEnv struct {
    44  	Options  *InteractionOpts
    45  	Nodes    []Node
    46  	Messages []pb.Message // in-flight messages
    47  
    48  	Output *RedirectLogger
    49  }
    50  
    51  // NewInteractionEnv initializes an InteractionEnv. opts may be nil.
    52  func NewInteractionEnv(opts *InteractionOpts) *InteractionEnv {
    53  	if opts == nil {
    54  		opts = &InteractionOpts{}
    55  	}
    56  	return &InteractionEnv{
    57  		Options: opts,
    58  		Output: &RedirectLogger{
    59  			Builder: &strings.Builder{},
    60  		},
    61  	}
    62  }
    63  
    64  func (env *InteractionEnv) withIndent(f func()) {
    65  	orig := env.Output.Builder
    66  	env.Output.Builder = &strings.Builder{}
    67  	f()
    68  
    69  	scanner := bufio.NewScanner(strings.NewReader(env.Output.Builder.String()))
    70  	for scanner.Scan() {
    71  		orig.WriteString("  " + scanner.Text() + "\n")
    72  	}
    73  	env.Output.Builder = orig
    74  }
    75  
    76  // Storage is the interface used by InteractionEnv. It is comprised of raft's
    77  // Storage interface plus access to operations that maintain the log and drive
    78  // the Ready handling loop.
    79  type Storage interface {
    80  	raft.Storage
    81  	SetHardState(state pb.HardState) error
    82  	ApplySnapshot(pb.Snapshot) error
    83  	Compact(newFirstIndex uint64) error
    84  	Append([]pb.Entry) error
    85  }
    86  
    87  // defaultRaftConfig sets up a *raft.Config with reasonable testing defaults.
    88  // In particular, no limits are set.
    89  func defaultRaftConfig(id uint64, applied uint64, s raft.Storage) *raft.Config {
    90  	return &raft.Config{
    91  		ID:              id,
    92  		Applied:         applied,
    93  		ElectionTick:    3,
    94  		HeartbeatTick:   1,
    95  		Storage:         s,
    96  		MaxSizePerMsg:   math.MaxUint64,
    97  		MaxInflightMsgs: math.MaxInt32,
    98  	}
    99  }
   100  
   101  func defaultEntryFormatter(b []byte) string {
   102  	return fmt.Sprintf("%q", b)
   103  }
   104  

View as plain text