forked from Conflux-Chain/confura
-
Notifications
You must be signed in to change notification settings - Fork 14
/
router.go
319 lines (252 loc) · 7.25 KB
/
router.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package node
import (
"context"
"strings"
"time"
"github.com/buraksezer/consistent"
"github.com/cespare/xxhash"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"github.com/go-redis/redis/v8"
rpcutil "github.com/scroll-tech/rpc-gateway/util/rpc"
"github.com/sirupsen/logrus"
)
// Group allows to manage full nodes in multiple groups.
type Group string
const (
// Note, group name must begin with cfx or eth as space name for metrics
// core space fullnode groups
GroupCfxHttp = "cfxhttp"
GroupCfxWs = "cfxws"
GroupCfxLogs = "cfxlog"
GroupCfxArchives = "cfxarchives"
// evm space fullnode groups
GroupEthHttp = "ethhttp"
GroupEthWs = "ethws"
GroupEthLogs = "ethlogs"
// debug space fullnode groups
GroupDebugHttp = "debughttp"
)
// Space parses space from group name
func (g Group) Space() string {
if strings.HasPrefix(string(g), "eth") {
return "eth"
}
return "cfx"
}
func (g Group) String() string {
return string(g)
}
// Router is used to route RPC requests to multiple full nodes.
type Router interface {
// Route returns the full node URL for specified group and key.
Route(group Group, key []byte) string
}
// MustNewRouter creates an instance of Router.
func MustNewRouter(redisURL string, nodeRPCURL string, groupConf map[Group]UrlConfig) Router {
var routers []Router
// Add redis router if configured
if len(redisURL) > 0 {
// redis://<user>:<password>@<host>:<port>/<db_number>
opt, err := redis.ParseURL(redisURL)
if err != nil {
logrus.WithError(err).Fatal("Failed to parse redis URL")
}
client := redis.NewClient(opt)
routers = append(routers, NewRedisRouter(client))
}
// Add node rpc router if configured
if len(nodeRPCURL) > 0 {
// http://127.0.0.1:22530
client, err := rpc.DialHTTP(nodeRPCURL)
if err != nil {
logrus.WithError(err).Fatal("Failed to create rpc client")
}
routers = append(routers, NewNodeRpcRouter(client))
// Also add local router in case node rpc temporary unavailable
localRouter, err := NewLocalRouterFromNodeRPC(client, groupConf)
if err != nil {
logrus.WithError(err).Fatal("Failed to new local router with node rpc")
}
routers = append(routers, localRouter)
}
// If redis and node rpc not configured, add local router for failover.
if len(routers) == 0 && len(groupConf) > 0 {
group2Urls := make(map[Group][]string)
for k, v := range groupConf {
group2Urls[k] = v.Nodes
}
routers = append(routers, NewLocalRouter(group2Urls))
}
return NewChainedRouter(groupConf, routers...)
}
// chainedRouter routes RPC requests in chained responsibility pattern.
type chainedRouter struct {
groupConf map[Group]UrlConfig
routers []Router
}
func NewChainedRouter(groupConf map[Group]UrlConfig, routers ...Router) Router {
return &chainedRouter{
groupConf: groupConf,
routers: routers,
}
}
func (r *chainedRouter) Route(group Group, key []byte) string {
for _, r := range r.routers {
if val := r.Route(group, key); len(val) > 0 {
return val
}
}
// Failover if configured
config, ok := r.groupConf[group]
if !ok {
return ""
}
logrus.WithFields(logrus.Fields{
"failover": config.Failover,
"key": string(key),
}).Warn("No router handled the route key, failover to chained default")
return config.Failover
}
// RedisRouter routes RPC requests via redis.
// It should be used together with RedisRepartitionResolver.
type RedisRouter struct {
client *redis.Client
}
func NewRedisRouter(client *redis.Client) *RedisRouter {
return &RedisRouter{
client: client,
}
}
func (r *RedisRouter) Route(group Group, key []byte) string {
uintKey := xxhash.Sum64(key)
redisKey := redisRepartitionKey(uintKey, string(group))
node, err := r.client.Get(context.Background(), redisKey).Result()
if err == redis.Nil {
return ""
}
if err != nil {
logrus.WithError(err).WithField("key", redisKey).Error("Failed to route key from redis")
return ""
}
return node
}
// NodeRpcRouter routes RPC requests via node management RPC service.
type NodeRpcRouter struct {
client *rpc.Client
}
func NewNodeRpcRouter(client *rpc.Client) *NodeRpcRouter {
return &NodeRpcRouter{
client: client,
}
}
func (r *NodeRpcRouter) Route(group Group, key []byte) string {
var result string
if err := r.client.Call(&result, "node_route", group, hexutil.Bytes(key)); err != nil {
logrus.WithError(err).Error("Failed to route key from node RPC")
return ""
}
return result
}
type localNode string
func (n localNode) String() string { return string(n) }
type hasher struct{}
func (h hasher) Sum64(data []byte) uint64 {
return xxhash.Sum64(data)
}
type localNodeGroup struct {
nodes map[string]localNode // name -> node URL
hashRing *consistent.Consistent
}
func newLocalNodeGroup(urls []string) *localNodeGroup {
item := localNodeGroup{
nodes: make(map[string]localNode),
}
var members []consistent.Member
for _, v := range urls {
nodeName := rpcutil.Url2NodeName(v)
if _, ok := item.nodes[nodeName]; !ok {
item.nodes[nodeName] = localNode(v)
members = append(members, localNode(v))
}
}
item.hashRing = consistent.New(members, cfg.HashRingRaw())
return &item
}
// LocalRouter routes RPC requests based on local hash ring.
type LocalRouter struct {
groups map[Group]*localNodeGroup
}
func NewLocalRouter(group2Urls map[Group][]string) *LocalRouter {
groups := make(map[Group]*localNodeGroup)
for k, v := range group2Urls {
groups[k] = newLocalNodeGroup(v)
}
return &LocalRouter{groups}
}
func (r *LocalRouter) Route(group Group, key []byte) string {
item, ok := r.groups[group]
if !ok {
return ""
}
if member := item.hashRing.LocateKey(key); member != nil {
return member.String()
}
return ""
}
func NewLocalRouterFromNodeRPC(client *rpc.Client, groupConf map[Group]UrlConfig) (*LocalRouter, error) {
group2Urls := make(map[Group][]string)
for key := range groupConf {
var urls []string
if err := client.Call(&urls, "node_list", key); err != nil {
logrus.WithError(err).WithField("group", key).Error("Failed to get nodes from node manager RPC")
return nil, err
}
group2Urls[key] = urls
}
router := NewLocalRouter(group2Urls)
go router.update(client)
return router, nil
}
func (r *LocalRouter) update(client *rpc.Client) {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
// could update nodes periodically all the time
for range ticker.C {
for grp := range r.groups {
var urls []string
if err := client.Call(&urls, "node_list", grp); err != nil {
logrus.WithError(err).WithField("group", grp).Debug("Failed to get nodes from node manager RPC periodically")
continue
}
r.updateOnce(urls, grp)
}
}
}
func (r *LocalRouter) updateOnce(urls []string, group Group) {
fnNodes, hashRing := r.groups[group].nodes, r.groups[group].hashRing
// detect new added
for _, v := range urls {
nodeName := rpcutil.Url2NodeName(v)
if _, ok := fnNodes[nodeName]; !ok {
fnNodes[nodeName] = localNode(v)
hashRing.Add(localNode(v))
}
}
// detect removed
nodes := make(map[string]bool)
for _, v := range urls {
nodes[rpcutil.Url2NodeName(v)] = true
}
var removed []string
for name := range fnNodes {
if !nodes[name] {
removed = append(removed, name)
}
}
for _, v := range removed {
node := fnNodes[v]
delete(fnNodes, v)
hashRing.Remove(node.String())
}
}