checker_kv_hash.go 2.1 KB

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