/
rp_test_cluster.go
217 lines (195 loc) · 6.9 KB
/
rp_test_cluster.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
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 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 membership
import (
"context"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/pborman/uuid"
"github.com/temporalio/ringpop-go"
"github.com/temporalio/tchannel-go"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/primitives"
)
// TestRingpopCluster is a type that represents a test ringpop cluster
type TestRingpopCluster struct {
hostUUIDs []string
hostAddrs []string
hostInfoList []HostInfo
rings []Monitor
channels []*tchannel.Channel
seedNode string
}
// NewTestRingpopCluster creates a new test cluster with the given name and cluster size
// All the nodes in the test cluster will register themselves in Ringpop
// with the specified name. This is only intended for unit tests.
func NewTestRingpopCluster(
t *testing.T,
ringPopApp string,
size int,
listenIPAddr string,
seed string,
serviceName primitives.ServiceName,
broadcastAddress string,
) *TestRingpopCluster {
logger := log.NewTestLogger()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockMgr := persistence.NewMockClusterMetadataManager(ctrl)
mockMgr.EXPECT().PruneClusterMembership(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
mockMgr.EXPECT().UpsertClusterMembership(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
cluster := &TestRingpopCluster{
hostUUIDs: make([]string, size),
hostAddrs: make([]string, size),
hostInfoList: make([]HostInfo, size),
rings: make([]Monitor, size),
channels: make([]*tchannel.Channel, size),
seedNode: seed,
}
for i := 0; i < size; i++ {
var err error
cluster.channels[i], err = tchannel.NewChannel(ringPopApp, nil)
if err != nil {
logger.Error("Failed to create tchannel", tag.Error(err))
return nil
}
listenAddr := listenIPAddr + ":0"
err = cluster.channels[i].ListenAndServe(listenAddr)
if err != nil {
logger.Error("tchannel listen failed", tag.Error(err))
return nil
}
cluster.hostUUIDs[i] = uuid.New()
cluster.hostAddrs[i], err = BuildBroadcastHostPort(cluster.channels[i].PeerInfo(), broadcastAddress)
if err != nil {
logger.Error("Failed to build broadcast hostport", tag.Error(err))
return nil
}
cluster.hostInfoList[i] = HostInfo{addr: cluster.hostAddrs[i]}
}
// if seed node is already supplied, use it; if not, set it
if cluster.seedNode == "" {
cluster.seedNode = cluster.hostAddrs[0]
}
logger.Info("seedNode", tag.Name(cluster.seedNode))
seedAddress, seedPort, err := SplitHostPortTyped(cluster.seedNode)
if err != nil {
logger.Error("unable to split host port", tag.Error(err))
return nil
}
seedMember := &persistence.ClusterMember{
HostID: uuid.NewUUID(),
RPCAddress: seedAddress,
RPCPort: seedPort,
SessionStart: time.Now().UTC(),
LastHeartbeat: time.Now().UTC(),
}
firstGetClusterMemberCall := true
mockMgr.EXPECT().GetClusterMembers(gomock.Any(), gomock.Any()).DoAndReturn(
func(_ context.Context, _ *persistence.GetClusterMembersRequest) (*persistence.GetClusterMembersResponse, error) {
res := &persistence.GetClusterMembersResponse{ActiveMembers: []*persistence.ClusterMember{seedMember}}
if firstGetClusterMemberCall {
// The first time GetClusterMembers is invoked, we simulate returning a stale/bad heartbeat.
// All subsequent calls only return the single "good" seed member
// This ensures that we exercise the retry path in bootstrap properly.
badSeedMember := &persistence.ClusterMember{
HostID: uuid.NewUUID(),
RPCAddress: seedAddress,
RPCPort: seedPort + 1,
SessionStart: time.Now().UTC(),
LastHeartbeat: time.Now().UTC(),
}
res = &persistence.GetClusterMembersResponse{ActiveMembers: []*persistence.ClusterMember{seedMember, badSeedMember}}
}
firstGetClusterMemberCall = false
return res, nil
}).AnyTimes()
for i := 0; i < size; i++ {
resolver := func() (string, error) {
return BuildBroadcastHostPort(cluster.channels[i].PeerInfo(), broadcastAddress)
}
ringPop, err := ringpop.New(ringPopApp, ringpop.Channel(cluster.channels[i]), ringpop.AddressResolverFunc(resolver))
if err != nil {
logger.Error("failed to create ringpop instance", tag.Error(err))
return nil
}
rpWrapper := NewRingPop(ringPop, time.Second*2, logger)
_, port, _ := SplitHostPortTyped(cluster.hostAddrs[i])
cluster.rings[i] = NewRingpopMonitor(
serviceName,
map[primitives.ServiceName]int{serviceName: int(port)}, // use same port for "grpc" port
rpWrapper,
logger,
mockMgr,
resolver,
)
cluster.rings[i].Start()
}
return cluster
}
// GetSeedNode returns the seedNode for this cluster
func (c *TestRingpopCluster) GetSeedNode() string {
return c.seedNode
}
// KillHost kills the given host within the cluster
func (c *TestRingpopCluster) KillHost(uuid string) {
for i := 0; i < len(c.hostUUIDs); i++ {
if c.hostUUIDs[i] == uuid {
c.rings[i].Stop()
c.channels[i].Close()
c.rings[i] = nil
c.channels[i] = nil
}
}
}
// Stop stops the cluster
func (c *TestRingpopCluster) Stop() {
for i := 0; i < len(c.hostAddrs); i++ {
if c.rings[i] != nil {
c.rings[i].Stop()
c.channels[i].Close()
}
}
}
// GetHostInfoList returns the list of all hosts within the cluster
func (c *TestRingpopCluster) GetHostInfoList() []HostInfo {
return c.hostInfoList
}
// GetHostAddrs returns all host addrs within the cluster
func (c *TestRingpopCluster) GetHostAddrs() []string {
return c.hostAddrs
}
// FindHostByAddr returns the host info corresponding to
// the given addr, if it exists
func (c *TestRingpopCluster) FindHostByAddr(addr string) (HostInfo, bool) {
for _, hi := range c.hostInfoList {
if hi.addr == addr {
return hi, true
}
}
return HostInfo{}, false
}