|
| 1 | +package consistenthash |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/zeromicro/go-zero/core/hash" |
| 7 | + "google.golang.org/grpc/balancer" |
| 8 | + "google.golang.org/grpc/balancer/base" |
| 9 | + "google.golang.org/grpc/codes" |
| 10 | + "google.golang.org/grpc/status" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + Name = "consistent_hash" |
| 15 | + |
| 16 | + defaultReplicaCount = 100 |
| 17 | +) |
| 18 | + |
| 19 | +var emptyPickResult balancer.PickResult |
| 20 | + |
| 21 | +func init() { |
| 22 | + balancer.Register(newBuilder()) |
| 23 | +} |
| 24 | + |
| 25 | +type ( |
| 26 | + // hashKey is the key type for consistent hash in context. |
| 27 | + hashKey struct{} |
| 28 | + // pickerBuilder is a builder for picker. |
| 29 | + pickerBuilder struct{} |
| 30 | + // picker is a picker that uses consistent hash to pick a sub connection. |
| 31 | + picker struct { |
| 32 | + hashRing *hash.ConsistentHash |
| 33 | + conns map[string]balancer.SubConn |
| 34 | + } |
| 35 | +) |
| 36 | + |
| 37 | +func (b *pickerBuilder) Build(info base.PickerBuildInfo) balancer.Picker { |
| 38 | + readySCs := info.ReadySCs |
| 39 | + if len(readySCs) == 0 { |
| 40 | + return base.NewErrPicker(balancer.ErrNoSubConnAvailable) |
| 41 | + } |
| 42 | + |
| 43 | + conns := make(map[string]balancer.SubConn, len(readySCs)) |
| 44 | + hashRing := hash.NewCustomConsistentHash(defaultReplicaCount, hash.Hash) |
| 45 | + for conn, connInfo := range readySCs { |
| 46 | + addr := connInfo.Address.Addr |
| 47 | + conns[addr] = conn |
| 48 | + hashRing.Add(addr) |
| 49 | + } |
| 50 | + |
| 51 | + return &picker{ |
| 52 | + hashRing: hashRing, |
| 53 | + conns: conns, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func newBuilder() balancer.Builder { |
| 58 | + return base.NewBalancerBuilder(Name, &pickerBuilder{}, base.Config{HealthCheck: true}) |
| 59 | +} |
| 60 | + |
| 61 | +func (p *picker) Pick(info balancer.PickInfo) (balancer.PickResult, error) { |
| 62 | + hashKey := GetHashKey(info.Ctx) |
| 63 | + if len(hashKey) == 0 { |
| 64 | + return emptyPickResult, status.Error(codes.InvalidArgument, |
| 65 | + "[consistent_hash] missing hash key in context") |
| 66 | + } |
| 67 | + |
| 68 | + if addrAny, ok := p.hashRing.Get(hashKey); ok { |
| 69 | + addr, ok := addrAny.(string) |
| 70 | + if !ok { |
| 71 | + return emptyPickResult, status.Error(codes.Internal, |
| 72 | + "[consistent_hash] invalid addr type in consistent hash") |
| 73 | + } |
| 74 | + |
| 75 | + subConn, ok := p.conns[addr] |
| 76 | + if !ok { |
| 77 | + return emptyPickResult, status.Errorf(codes.Internal, |
| 78 | + "[consistent_hash] no subConn for addr: %s", addr) |
| 79 | + } |
| 80 | + |
| 81 | + return balancer.PickResult{SubConn: subConn}, nil |
| 82 | + } |
| 83 | + |
| 84 | + return emptyPickResult, status.Errorf(codes.Unavailable, |
| 85 | + "[consistent_hash] no matching conn for hashKey: %s", hashKey) |
| 86 | +} |
| 87 | + |
| 88 | +// SetHashKey sets the hash key into context. |
| 89 | +func SetHashKey(ctx context.Context, key string) context.Context { |
| 90 | + return context.WithValue(ctx, hashKey{}, key) |
| 91 | +} |
| 92 | + |
| 93 | +// GetHashKey gets the hash key from context. |
| 94 | +func GetHashKey(ctx context.Context) string { |
| 95 | + v, _ := ctx.Value(hashKey{}).(string) |
| 96 | + return v |
| 97 | +} |
0 commit comments