...

Source file src/go.etcd.io/etcd/raft/v3/rafttest/interaction_env_handler_raft_log.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  	"math"
    20  	"testing"
    21  
    22  	"github.com/cockroachdb/datadriven"
    23  	"go.etcd.io/etcd/raft/v3"
    24  )
    25  
    26  func (env *InteractionEnv) handleRaftLog(t *testing.T, d datadriven.TestData) error {
    27  	idx := firstAsNodeIdx(t, d)
    28  	return env.RaftLog(idx)
    29  }
    30  
    31  // RaftLog pretty prints the raft log to the output buffer.
    32  func (env *InteractionEnv) RaftLog(idx int) error {
    33  	s := env.Nodes[idx].Storage
    34  	fi, err := s.FirstIndex()
    35  	if err != nil {
    36  		return err
    37  	}
    38  	li, err := s.LastIndex()
    39  	if err != nil {
    40  		return err
    41  	}
    42  	if li < fi {
    43  		// TODO(tbg): this is what MemoryStorage returns, but unclear if it's
    44  		// the "correct" thing to do.
    45  		fmt.Fprintf(env.Output, "log is empty: first index=%d, last index=%d", fi, li)
    46  		return nil
    47  	}
    48  	ents, err := s.Entries(fi, li+1, math.MaxUint64)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	env.Output.WriteString(raft.DescribeEntries(ents, defaultEntryFormatter))
    53  	return err
    54  }
    55  

View as plain text