Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
3b232e74
Unverified
Commit
3b232e74
authored
Oct 31, 2023
by
Danyal Prout
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
proxyd: add weighting feature to node selection
parent
a6d211ab
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
109 additions
and
5 deletions
+109
-5
backend.go
proxyd/backend.go
+52
-3
backend_test.go
proxyd/backend_test.go
+49
-0
config.go
proxyd/config.go
+4
-0
proxyd.go
proxyd/proxyd.go
+4
-2
No files found.
proxyd/backend.go
View file @
3b232e74
...
...
@@ -159,6 +159,8 @@ type Backend struct {
latencySlidingWindow
*
sw
.
AvgSlidingWindow
networkRequestsSlidingWindow
*
sw
.
AvgSlidingWindow
networkErrorsSlidingWindow
*
sw
.
AvgSlidingWindow
weight
int
}
type
BackendOpt
func
(
b
*
Backend
)
...
...
@@ -239,6 +241,12 @@ func WithConsensusForcedCandidate(forcedCandidate bool) BackendOpt {
}
}
func
WithWeight
(
weight
int
)
BackendOpt
{
return
func
(
b
*
Backend
)
{
b
.
weight
=
weight
}
}
func
WithMaxDegradedLatencyThreshold
(
maxDegradedLatencyThreshold
time
.
Duration
)
BackendOpt
{
return
func
(
b
*
Backend
)
{
b
.
maxDegradedLatencyThreshold
=
maxDegradedLatencyThreshold
...
...
@@ -683,9 +691,10 @@ func sortBatchRPCResponse(req []*RPCReq, res []*RPCRes) {
}
type
BackendGroup
struct
{
Name
string
Backends
[]
*
Backend
Consensus
*
ConsensusPoller
Name
string
Backends
[]
*
Backend
UseWeightedRouting
bool
Consensus
*
ConsensusPoller
}
func
(
bg
*
BackendGroup
)
Forward
(
ctx
context
.
Context
,
rpcReqs
[]
*
RPCReq
,
isBatch
bool
)
([]
*
RPCRes
,
string
,
error
)
{
...
...
@@ -741,6 +750,8 @@ func (bg *BackendGroup) Forward(ctx context.Context, rpcReqs []*RPCReq, isBatch
}
}
rpcReqs
=
rewrittenReqs
}
else
if
bg
.
UseWeightedRouting
{
backends
=
randomizeFirstBackendByWeight
(
backends
)
}
rpcRequestsTotal
.
Inc
()
...
...
@@ -808,6 +819,39 @@ func (bg *BackendGroup) Forward(ctx context.Context, rpcReqs []*RPCReq, isBatch
return
nil
,
""
,
ErrNoBackends
}
func
randomizeFirstBackendByWeight
(
backends
[]
*
Backend
)
[]
*
Backend
{
if
len
(
backends
)
==
0
{
return
backends
}
totalWeight
:=
0
for
_
,
backend
:=
range
backends
{
totalWeight
+=
backend
.
weight
}
r
:=
rand
.
New
(
rand
.
NewSource
(
time
.
Now
()
.
UnixNano
()))
random
:=
r
.
Intn
(
totalWeight
)
currentSum
:=
0
for
idx
,
backend
:=
range
backends
{
currentSum
+=
backend
.
weight
if
currentSum
>
random
{
return
moveIndexToStart
(
backends
,
idx
)
}
}
log
.
Warn
(
"unable to select weighted backend, using ordered input"
)
return
backends
}
func
moveIndexToStart
(
backends
[]
*
Backend
,
index
int
)
[]
*
Backend
{
result
:=
make
([]
*
Backend
,
0
,
len
(
backends
))
result
=
append
(
result
,
backends
[
index
])
result
=
append
(
result
,
backends
[
:
index
]
...
)
result
=
append
(
result
,
backends
[
index
+
1
:
]
...
)
return
result
}
func
(
bg
*
BackendGroup
)
ProxyWS
(
ctx
context
.
Context
,
clientConn
*
websocket
.
Conn
,
methodWhitelist
*
StringSet
)
(
*
WSProxier
,
error
)
{
for
_
,
back
:=
range
bg
.
Backends
{
proxier
,
err
:=
back
.
ProxyWS
(
clientConn
,
methodWhitelist
)
...
...
@@ -872,6 +916,11 @@ func (bg *BackendGroup) loadBalancedConsensusGroup() []*Backend {
backendsDegraded
[
i
],
backendsDegraded
[
j
]
=
backendsDegraded
[
j
],
backendsDegraded
[
i
]
})
if
bg
.
UseWeightedRouting
{
backendsHealthy
=
randomizeFirstBackendByWeight
(
backendsHealthy
)
backendsDegraded
=
randomizeFirstBackendByWeight
(
backendsDegraded
)
}
// healthy are put into a priority position
// degraded backends are used as fallback
backendsHealthy
=
append
(
backendsHealthy
,
backendsDegraded
...
)
...
...
proxyd/backend_test.go
View file @
3b232e74
...
...
@@ -19,3 +19,52 @@ func TestStripXFF(t *testing.T) {
assert
.
Equal
(
t
,
test
.
out
,
actual
)
}
}
func
TestMoveIndexToStart
(
t
*
testing
.
T
)
{
backends
:=
[]
*
Backend
{
{
Name
:
"node1"
,
},
{
Name
:
"node1"
,
},
{
Name
:
"node1"
,
},
}
tests
:=
[]
struct
{
index
int
out
[]
*
Backend
}{
{
index
:
0
,
out
:
[]
*
Backend
{
backends
[
0
],
backends
[
1
],
backends
[
2
],
},
},
{
index
:
1
,
out
:
[]
*
Backend
{
backends
[
1
],
backends
[
0
],
backends
[
2
],
},
},
{
index
:
2
,
out
:
[]
*
Backend
{
backends
[
2
],
backends
[
0
],
backends
[
1
],
},
},
}
for
_
,
test
:=
range
tests
{
result
:=
moveIndexToStart
(
backends
,
test
.
index
)
assert
.
Equal
(
t
,
test
.
out
,
result
)
}
}
proxyd/config.go
View file @
3b232e74
...
...
@@ -98,6 +98,8 @@ type BackendConfig struct {
ConsensusSkipPeerCountCheck
bool
`toml:"consensus_skip_peer_count"`
ConsensusForcedCandidate
bool
`toml:"consensus_forced_candidate"`
ConsensusReceiptsTarget
string
`toml:"consensus_receipts_target"`
Weight
int
`toml:"weight"`
}
type
BackendsConfig
map
[
string
]
*
BackendConfig
...
...
@@ -105,6 +107,8 @@ type BackendsConfig map[string]*BackendConfig
type
BackendGroupConfig
struct
{
Backends
[]
string
`toml:"backends"`
UseWeightedRouting
bool
`toml:"weighted_routing"`
ConsensusAware
bool
`toml:"consensus_aware"`
ConsensusAsyncHandler
string
`toml:"consensus_handler"`
...
...
proxyd/proxyd.go
View file @
3b232e74
...
...
@@ -144,6 +144,7 @@ func Start(config *Config) (*Server, func(), error) {
opts
=
append
(
opts
,
WithProxydIP
(
os
.
Getenv
(
"PROXYD_IP"
)))
opts
=
append
(
opts
,
WithConsensusSkipPeerCountCheck
(
cfg
.
ConsensusSkipPeerCountCheck
))
opts
=
append
(
opts
,
WithConsensusForcedCandidate
(
cfg
.
ConsensusForcedCandidate
))
opts
=
append
(
opts
,
WithWeight
(
cfg
.
Weight
))
receiptsTarget
,
err
:=
ReadFromEnvOrConfig
(
cfg
.
ConsensusReceiptsTarget
)
if
err
!=
nil
{
...
...
@@ -175,8 +176,9 @@ func Start(config *Config) (*Server, func(), error) {
backends
=
append
(
backends
,
backendsByName
[
bName
])
}
group
:=
&
BackendGroup
{
Name
:
bgName
,
Backends
:
backends
,
Name
:
bgName
,
UseWeightedRouting
:
bg
.
UseWeightedRouting
,
Backends
:
backends
,
}
backendGroups
[
bgName
]
=
group
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment