-
Notifications
You must be signed in to change notification settings - Fork 3
/
topology.go
193 lines (153 loc) · 5.49 KB
/
topology.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
package vshard_router //nolint:revive
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/pool"
)
var (
ErrReplicasetExists = fmt.Errorf("replicaset exists")
ErrReplicasetNotExists = fmt.Errorf("replicaset not exists")
)
// TopologyController is an entity that allows you to interact with the topology.
// TopologyController is not concurrent safe.
// This decision is made intentionally because there is no point in providing concurrence safety for this case.
// In any case, a caller can use his own external synchronization primitive to handle concurrent access.
type TopologyController interface {
AddInstance(ctx context.Context, rsID uuid.UUID, info InstanceInfo) error
RemoveReplicaset(ctx context.Context, rsID uuid.UUID) []error
RemoveInstance(ctx context.Context, rsID, instanceID uuid.UUID) error
AddReplicaset(ctx context.Context, rsInfo ReplicasetInfo, instances []InstanceInfo) error
AddReplicasets(ctx context.Context, replicasets map[ReplicasetInfo][]InstanceInfo) error
}
func (r *Router) getIDToReplicaset() map[uuid.UUID]*Replicaset {
r.idToReplicasetMutex.RLock()
idToReplicasetRef := r.idToReplicaset
r.idToReplicasetMutex.RUnlock()
return idToReplicasetRef
}
func (r *Router) setIDToReplicaset(idToReplicasetNew map[uuid.UUID]*Replicaset) {
r.idToReplicasetMutex.Lock()
r.idToReplicaset = idToReplicasetNew
r.idToReplicasetMutex.Unlock()
}
func (r *Router) Topology() TopologyController {
return r
}
func (r *Router) AddInstance(ctx context.Context, rsID uuid.UUID, info InstanceInfo) error {
r.log().Debugf(ctx, "trying to add instance %s to router topology in rs %s", info, rsID)
err := info.Validate()
if err != nil {
return err
}
instance := pool.Instance{
Name: info.UUID.String(),
Dialer: tarantool.NetDialer{
Address: info.Addr,
User: r.cfg.User,
Password: r.cfg.Password,
},
}
idToReplicasetRef := r.getIDToReplicaset()
rs := idToReplicasetRef[rsID]
if rs == nil {
return ErrReplicasetNotExists
}
return rs.conn.Add(ctx, instance)
}
func (r *Router) RemoveInstance(ctx context.Context, rsID, instanceID uuid.UUID) error {
r.log().Debugf(ctx, "trying to remove instance %s from router topology in rs %s", instanceID, rsID)
idToReplicasetRef := r.getIDToReplicaset()
rs := idToReplicasetRef[rsID]
if rs == nil {
return ErrReplicasetNotExists
}
return rs.conn.Remove(instanceID.String())
}
func (r *Router) AddReplicaset(ctx context.Context, rsInfo ReplicasetInfo, instances []InstanceInfo) error {
r.log().Debugf(ctx, "trying to add replicaset %s to router topology", rsInfo)
idToReplicasetOld := r.getIDToReplicaset()
if _, ok := idToReplicasetOld[rsInfo.UUID]; ok {
return ErrReplicasetExists
}
replicaset := &Replicaset{
info: ReplicasetInfo{
Name: rsInfo.Name,
UUID: rsInfo.UUID,
},
}
rsInstances := make([]pool.Instance, 0, len(instances))
for _, instance := range instances {
rsInstances = append(rsInstances, pool.Instance{
Name: instance.UUID.String(),
Dialer: tarantool.NetDialer{
Address: instance.Addr,
User: r.cfg.User,
Password: r.cfg.Password,
},
Opts: r.cfg.PoolOpts,
})
}
conn, err := pool.Connect(ctx, rsInstances)
if err != nil {
return err
}
poolInfo := conn.GetInfo()
for instName, instConnInfo := range poolInfo {
connectStatus := "connected now"
if !instConnInfo.ConnectedNow {
connectStatus = "not connected"
}
r.log().Infof(ctx, "[replicaset %s ] instance %s %s in role %s", rsInfo, instName, connectStatus, instConnInfo.ConnRole)
}
isConnected, err := conn.ConnectedNow(pool.RW)
if err != nil {
return fmt.Errorf("cant check rs pool conntected rw now with error: %s", err)
}
if !isConnected {
return fmt.Errorf("got connected now as false, storage must be configured first")
}
replicaset.conn = conn
// Create an entirely new map object
idToReplicasetNew := make(map[uuid.UUID]*Replicaset)
for k, v := range idToReplicasetOld {
idToReplicasetNew[k] = v
}
idToReplicasetNew[rsInfo.UUID] = replicaset // add when conn is ready
// We could detect concurrent access to the TopologyController interface
// by comparing references to r.idToReplicaset and idToReplicasetOld.
// But it requires reflection which I prefer to avoid.
// See: https://stackoverflow.com/questions/58636694/how-to-know-if-2-go-maps-reference-the-same-data.
r.setIDToReplicaset(idToReplicasetNew)
return nil
}
func (r *Router) AddReplicasets(ctx context.Context, replicasets map[ReplicasetInfo][]InstanceInfo) error {
for rsInfo, rsInstances := range replicasets {
// We assume that AddReplicasets is called only once during initialization.
// We also expect that cluster configuration changes very rarely,
// so we prefer more simple code rather than the efficiency of this part of logic.
// Even if there are 1000 replicasets, it is still cheap.
err := r.AddReplicaset(ctx, rsInfo, rsInstances)
if err != nil {
return err
}
}
return nil
}
func (r *Router) RemoveReplicaset(ctx context.Context, rsID uuid.UUID) []error {
r.log().Debugf(ctx, "trying to remove replicaset %s from router topology", rsID)
idToReplicasetOld := r.getIDToReplicaset()
rs := idToReplicasetOld[rsID]
if rs == nil {
return []error{ErrReplicasetNotExists}
}
// Create an entirely new map object
idToReplicasetNew := make(map[uuid.UUID]*Replicaset)
for k, v := range idToReplicasetOld {
idToReplicasetNew[k] = v
}
delete(idToReplicasetNew, rsID)
r.setIDToReplicaset(idToReplicasetNew)
return rs.conn.CloseGraceful()
}