|
| 1 | +// Copyright 2025 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 client |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "time" |
| 21 | + |
| 22 | + "go.uber.org/zap" |
| 23 | + |
| 24 | + clientv3 "go.etcd.io/etcd/client/v3" |
| 25 | + "go.etcd.io/etcd/tests/v3/framework/e2e" |
| 26 | +) |
| 27 | + |
| 28 | +func CheckHashKV(ctx context.Context, clus *e2e.EtcdProcessCluster) error { |
| 29 | + c, err := clientv3.New(clientv3.Config{ |
| 30 | + Endpoints: clus.EndpointsGRPC(), |
| 31 | + Logger: zap.NewNop(), |
| 32 | + DialKeepAliveTime: 10 * time.Second, |
| 33 | + DialKeepAliveTimeout: 100 * time.Millisecond, |
| 34 | + }) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + defer c.Close() |
| 39 | + |
| 40 | + hashKV, err := c.HashKV(ctx, clus.EndpointsGRPC()[0], 0) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + rev := hashKV.Header.Revision |
| 45 | + |
| 46 | + hashKVs := make([]*clientv3.HashKVResponse, 0) |
| 47 | + hashKVs = append(hashKVs, hashKV) |
| 48 | + |
| 49 | + for _, member := range clus.Procs { |
| 50 | + hashKV, err := c.HashKV(ctx, member.EndpointsGRPC()[0], rev) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + if hashKV.Header.Revision != rev { |
| 55 | + return fmt.Errorf("max revision between nodes should be the same. Want %v, get %v", rev, hashKV.Header.Revision) |
| 56 | + } |
| 57 | + hashKVs = append(hashKVs, hashKV) |
| 58 | + } |
| 59 | + |
| 60 | + for i := 1; i < len(hashKVs); i++ { |
| 61 | + if hashKVs[i-1].HashRevision != hashKVs[i].HashRevision { |
| 62 | + return fmt.Errorf("hashRevision mismatch, node %v has %+v, node %v has %+v", hashKVs[i-1].Header.MemberId, hashKVs[i-1], hashKVs[i].Header.MemberId, hashKVs[i]) |
| 63 | + } |
| 64 | + if hashKVs[i-1].CompactRevision != hashKVs[i].CompactRevision { |
| 65 | + return fmt.Errorf("compactRevision mismatch, node %v has %+v, node %v has %+v", hashKVs[i-1].Header.MemberId, hashKVs[i-1], hashKVs[i].Header.MemberId, hashKVs[i]) |
| 66 | + } |
| 67 | + if hashKVs[i-1].Hash != hashKVs[i].Hash { |
| 68 | + return fmt.Errorf("hash mismatch, node %v has %+v, node %v has %+v", hashKVs[i-1].Header.MemberId, hashKVs[i-1], hashKVs[i].Header.MemberId, hashKVs[i]) |
| 69 | + } |
| 70 | + } |
| 71 | + return nil |
| 72 | +} |
0 commit comments