/
ring.go
212 lines (185 loc) · 5.92 KB
/
ring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) 2022 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package hashring32
import (
"math/rand"
"strconv"
"sync"
"time"
"go.uber.org/yarpc/api/peer"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/peer/abstractlist"
"go.uber.org/yarpc/peer/hashring32/internal/farmhashring"
"go.uber.org/yarpc/peer/hashring32/internal/hashring32"
"go.uber.org/zap"
)
// NewImplementation creates a new hashring32 abstractlist.Implementation.
//
// Use this constructor instead of NewList, when wanting to do custom peer
// connection management.
func NewImplementation(opts ...Option) abstractlist.Implementation {
options := options{
logger: zap.NewNop(),
}
for _, o := range opts {
o.apply(&options)
}
return newPeerRing(
farmhashring.Fingerprint32,
options.offsetHeader,
options.peerOverrideHeader,
options.alternateShardKeyHeader,
options.offsetGeneratorValue,
options.logger,
options.peerRingOptions...,
)
}
// newPeerRing creates a new peerRing with an initial capacity
func newPeerRing(hashFunc hashring32.HashFunc32, offsetHeader, peerOverrideHeader, alternateShardKeyHeader string, offsetGeneratorValue int, logger *zap.Logger, option ...hashring32.Option) *peerRing {
return &peerRing{
ring: hashring32.New(hashFunc, option...),
subscribers: make(map[string]*subscriber),
offsetHeader: offsetHeader,
offsetGeneratorValue: offsetGeneratorValue,
peerOverrideHeader: peerOverrideHeader,
logger: logger,
alternateShardKeyHeader: alternateShardKeyHeader,
random: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
type subscriber struct {
peer peer.StatusPeer
}
func (s *subscriber) UpdatePendingRequestCount(int) {}
// peerRing provides a safe way to interact (Add/Remove/Get) with a potentially
// changing list of peer objects
type peerRing struct {
ring *hashring32.Hashring32
subscribers map[string]*subscriber
offsetHeader string
offsetGeneratorValue int
peerOverrideHeader string
alternateShardKeyHeader string
logger *zap.Logger
random *rand.Rand
m sync.RWMutex
}
var _ abstractlist.Implementation = (*peerRing)(nil)
// shardIdentifier is the interface for an identifier that have a shard property
type shardIdentifier interface {
Identifier() string
Shard() string
}
// Add a string to the end of the peerRing, if the ring is empty
// it initializes the ring marker
func (pr *peerRing) Add(p peer.StatusPeer, pid peer.Identifier) abstractlist.Subscriber {
pr.m.Lock()
defer pr.m.Unlock()
sub := &subscriber{peer: p}
shardID := getShardID(pid)
pr.ring.Add(shardID)
pr.subscribers[shardID] = sub
return sub
}
// Remove the peer from the ring. Use the subscriber to address the node of the
// ring directly.
func (pr *peerRing) Remove(p peer.StatusPeer, pid peer.Identifier, s abstractlist.Subscriber) {
pr.m.Lock()
defer pr.m.Unlock()
sub, ok := s.(*subscriber)
if !ok {
// Don't panic.
return
}
// Peerlist's responsibility to make sure this never happens.
if sub.peer.Identifier() != p.Identifier() {
return
}
shardID := getShardID(pid)
pr.ring.Add(shardID)
pr.subscribers[shardID] = sub
// validate that given peer is already in the subscriber
pr.ring.Remove(shardID)
// Peerlist's responsibility to make sure this is thread-safe.
delete(pr.subscribers, shardID)
}
func (pr *peerRing) getPeerOverride(req *transport.Request) peer.StatusPeer {
dest, ok := req.Headers.Get(pr.peerOverrideHeader)
if !ok {
return nil
}
sub, ok := pr.subscribers[dest]
if !ok {
return nil
}
return sub.peer
}
// Choose returns the assigned peer in the ring
func (pr *peerRing) Choose(req *transport.Request) peer.StatusPeer {
pr.m.RLock()
defer pr.m.RUnlock()
// Client may want this request to go to a specific destination.
overridePeer := pr.getPeerOverride(req)
if overridePeer != nil {
return overridePeer
}
// Hashring choose can return an error eg if there are no peer available.
var n int
var err error
key, ok := req.Headers.Get(pr.offsetHeader)
if ok {
n, err = strconv.Atoi(key)
if err != nil {
pr.logger.Error("yarpc/hashring32: offset header is not a valid integer", zap.String("offsetHeader", key), zap.Error(err))
}
} else if pr.offsetGeneratorValue != 0 {
n = pr.random.Intn(pr.offsetGeneratorValue)
}
shardKey := req.ShardKey
if pr.alternateShardKeyHeader != "" {
shardKey, _ = req.Headers.Get(pr.alternateShardKeyHeader)
}
ids, err := pr.ring.Choose(hashring32.Shard{
Key: shardKey,
N: n,
})
if err != nil {
return nil
}
if len(ids) <= n {
return nil
}
sub, ok := pr.subscribers[ids[n]]
if !ok {
return nil
}
return sub.peer
}
// getShardID returns the shardID from a StatusPeer.
func getShardID(p peer.Identifier) string {
sp, ok := p.(shardIdentifier)
var id string
if !ok {
id = p.Identifier()
} else {
id = sp.Shard()
}
return id
}