go.etcd.io/etcd@v3.3.27+incompatible/functional/tester/checker_kv_hash.go (about)

     1  // Copyright 2018 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 tester
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"github.com/coreos/etcd/functional/rpcpb"
    22  
    23  	"go.uber.org/zap"
    24  )
    25  
    26  const retries = 7
    27  
    28  type kvHashChecker struct {
    29  	ctype rpcpb.Checker
    30  	clus  *Cluster
    31  }
    32  
    33  func newKVHashChecker(clus *Cluster) Checker {
    34  	return &kvHashChecker{
    35  		ctype: rpcpb.Checker_KV_HASH,
    36  		clus:  clus,
    37  	}
    38  }
    39  
    40  func (hc *kvHashChecker) checkRevAndHashes() (err error) {
    41  	var (
    42  		revs   map[string]int64
    43  		hashes map[string]int64
    44  	)
    45  	// retries in case of transient failure or etcd cluster has not stablized yet.
    46  	for i := 0; i < retries; i++ {
    47  		revs, hashes, err = hc.clus.getRevisionHash()
    48  		if err != nil {
    49  			hc.clus.lg.Warn(
    50  				"failed to get revision and hash",
    51  				zap.Int("retries", i),
    52  				zap.Error(err),
    53  			)
    54  		} else {
    55  			sameRev := getSameValue(revs)
    56  			sameHashes := getSameValue(hashes)
    57  			if sameRev && sameHashes {
    58  				return nil
    59  			}
    60  			hc.clus.lg.Warn(
    61  				"retrying; etcd cluster is not stable",
    62  				zap.Int("retries", i),
    63  				zap.Bool("same-revisions", sameRev),
    64  				zap.Bool("same-hashes", sameHashes),
    65  				zap.String("revisions", fmt.Sprintf("%+v", revs)),
    66  				zap.String("hashes", fmt.Sprintf("%+v", hashes)),
    67  			)
    68  		}
    69  		time.Sleep(time.Second)
    70  	}
    71  
    72  	if err != nil {
    73  		return fmt.Errorf("failed revision and hash check (%v)", err)
    74  	}
    75  
    76  	return fmt.Errorf("etcd cluster is not stable: [revisions: %v] and [hashes: %v]", revs, hashes)
    77  }
    78  
    79  func (hc *kvHashChecker) Type() rpcpb.Checker {
    80  	return hc.ctype
    81  }
    82  
    83  func (hc *kvHashChecker) EtcdClientEndpoints() []string {
    84  	return hc.clus.EtcdClientEndpoints()
    85  }
    86  
    87  func (hc *kvHashChecker) Check() error {
    88  	return hc.checkRevAndHashes()
    89  }