Skip to content

Commit 61092fb

Browse files
Add NodeOverride option (#224)
Adds NodeOverride option to override the default SWIM-based gossip-style node membership protocol used for membership management.
1 parent d1ad20e commit 61092fb

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

.github/workflows/test.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ jobs:
88
test:
99
name: Run tests
1010
runs-on: ubuntu-latest
11-
1211
env:
1312
GOPATH: ${{ github.workspace }}
1413
GO111MODULE: off
1514
defaults:
1615
run:
1716
working-directory: ${{ env.GOPATH }}/src/github.com/${{ github.repository }}
18-
1917
steps:
2018
- name: Setup Go
2119
uses: actions/setup-go@v2

glide.lock

+5-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import:
99
- package: github.com/dgryski/go-farm
1010
- package: github.com/rcrowley/go-metrics
1111
- package: github.com/stretchr/testify
12+
version: 1.7.0
1213
subpackages:
1314
- assert
1415
- mock

options.go

+9
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,15 @@ func RequiresAppInPing(requiresAppInPing bool) Option {
368368
}
369369
}
370370

371+
// NodeOverride is used to optionally override the default SWIM-based
372+
// gossip-style node membership protocol used for membership management.
373+
func NodeOverride(node swim.NodeInterface) Option {
374+
return func(r *Ringpop) error {
375+
r.node = node
376+
return nil
377+
}
378+
}
379+
371380
// Default options
372381

373382
// defaultClock sets the ringpop clock interface to use the system clock

options_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ import (
2626

2727
"github.com/stretchr/testify/mock"
2828
"github.com/stretchr/testify/suite"
29+
"github.com/uber/ringpop-go/events"
2930
"github.com/uber/ringpop-go/hashring"
3031
"github.com/uber/ringpop-go/logging"
3132
"github.com/uber/ringpop-go/membership"
3233
"github.com/uber/ringpop-go/swim"
3334
"github.com/uber/ringpop-go/test/mocks"
34-
"github.com/uber/ringpop-go/test/mocks/logger"
35+
mocklogger "github.com/uber/ringpop-go/test/mocks/logger"
3536
"github.com/uber/tchannel-go"
3637
)
3738

@@ -345,6 +346,25 @@ func (s *RingpopOptionsTestSuite) TestSelfEvictOptions() {
345346

346347
}
347348

349+
// TestNodeOverride tests that the swim.NodeInterface implementation that is
350+
// provided to NodeOverride is correctly set on the Ringpop instance.
351+
func (s *RingpopOptionsTestSuite) TestNodeOverride() {
352+
type mockNode struct {
353+
events.SyncEventEmitter
354+
mocks.SwimNode
355+
}
356+
357+
mockNode1 := &mockNode{}
358+
mockNode2 := &mockNode{}
359+
360+
rp, err := New("test", Channel(s.channel), NodeOverride(mockNode1))
361+
s.Require().NotNil(rp)
362+
s.Require().NoError(err)
363+
364+
s.Same(mockNode1, rp.node)
365+
s.NotSame(mockNode2, rp.node)
366+
}
367+
348368
func TestRingpopOptionsTestSuite(t *testing.T) {
349369
suite.Run(t, new(RingpopOptionsTestSuite))
350370
}

ringpop.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ type state uint
123123

124124
const (
125125
// created means the Ringpop instance has been created but the swim node,
126-
// stats and hasring haven't been set up. The listen address has not been
126+
// stats and hashring haven't been set up. The listen address has not been
127127
// resolved yet either.
128128
created state = iota
129129
// initialized means the listen address has been resolved and the swim
@@ -190,14 +190,16 @@ func (rp *Ringpop) init() error {
190190
rp.subChannel = rp.channel.GetSubChannel("ringpop", tchannel.Isolated)
191191
rp.registerHandlers()
192192

193-
rp.node = swim.NewNode(rp.config.App, address, rp.subChannel, &swim.Options{
194-
StateTimeouts: rp.config.StateTimeouts,
195-
Clock: rp.clock,
196-
LabelLimits: rp.config.LabelLimits,
197-
InitialLabels: rp.config.InitialLabels,
198-
SelfEvict: rp.config.SelfEvict,
199-
RequiresAppInPing: rp.config.RequiresAppInPing,
200-
})
193+
if rp.node == nil {
194+
rp.node = swim.NewNode(rp.config.App, address, rp.subChannel, &swim.Options{
195+
StateTimeouts: rp.config.StateTimeouts,
196+
Clock: rp.clock,
197+
LabelLimits: rp.config.LabelLimits,
198+
InitialLabels: rp.config.InitialLabels,
199+
SelfEvict: rp.config.SelfEvict,
200+
RequiresAppInPing: rp.config.RequiresAppInPing,
201+
})
202+
}
201203
rp.node.AddListener(rp)
202204

203205
rp.ring = hashring.New(farm.Fingerprint32, rp.configHashRing.ReplicaPoints)

0 commit comments

Comments
 (0)