-
Notifications
You must be signed in to change notification settings - Fork 11
/
swimring.go
187 lines (149 loc) · 4.57 KB
/
swimring.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
package main
import (
"fmt"
"net"
"net/rpc"
"sync"
"time"
"github.com/dgryski/go-farm"
"github.com/hungys/swimring/hashring"
"github.com/hungys/swimring/membership"
"github.com/hungys/swimring/storage"
)
type configuration struct {
Host string
ExternalPort int `yaml:"ExternalPort"`
InternalPort int `yaml:"InternalPort"`
JoinTimeout int `yaml:"JoinTimeout"`
SuspectTimeout int `yaml:"SuspectTimeout"`
PingTimeout int `yaml:"PingTimeout"`
PingRequestTimeout int `yaml:"PingRequestTimeout"`
MinProtocolPeriod int `yaml:"MinProtocolPeriod"`
PingRequestSize int `yaml:"PingRequestSize"`
VirtualNodeSize int `yaml:"VirtualNodeSize"`
KVSReplicaPoints int `yaml:"KVSReplicaPoints"`
BootstrapNodes []string `yaml:"BootstrapNodes"`
}
// SwimRing is a local key-value store replica consisting of a SWIM node,
// a consistent hash ring and a storage engine.
type SwimRing struct {
config *configuration
status status
statusMutex sync.RWMutex
node *membership.Node
ring *hashring.HashRing
kvs *storage.KVStore
rc *RequestCoordinator
}
type status uint
const (
created status = iota
initialized
ready
destroyed
)
// NewSwimRing returns a new SwimRing instance.
func NewSwimRing(config *configuration) *SwimRing {
sr := &SwimRing{
config: config,
}
sr.setStatus(created)
return sr
}
func (sr *SwimRing) init() error {
address := fmt.Sprintf("%s:%d", sr.config.Host, sr.config.InternalPort)
sr.node = membership.NewNode(sr, address, &membership.Options{
JoinTimeout: time.Duration(sr.config.JoinTimeout) * time.Millisecond,
SuspectTimeout: time.Duration(sr.config.SuspectTimeout) * time.Millisecond,
PingTimeout: time.Duration(sr.config.PingTimeout) * time.Millisecond,
PingRequestTimeout: time.Duration(sr.config.PingRequestTimeout) * time.Millisecond,
MinProtocolPeriod: time.Duration(sr.config.MinProtocolPeriod) * time.Millisecond,
PingRequestSize: sr.config.PingRequestSize,
BootstrapNodes: sr.config.BootstrapNodes,
})
sr.ring = hashring.NewHashRing(farm.Fingerprint32, sr.config.VirtualNodeSize)
sr.kvs = storage.NewKVStore(address)
sr.rc = NewRequestCoordinator(sr)
sr.setStatus(initialized)
return nil
}
// Status returns the status of the current SwimRing instance.
func (sr *SwimRing) Status() status {
sr.statusMutex.RLock()
r := sr.status
sr.statusMutex.RUnlock()
return r
}
func (sr *SwimRing) setStatus(s status) {
sr.statusMutex.Lock()
sr.status = s
sr.statusMutex.Unlock()
}
// Bootstrap starts communication for this SwimRing instance.
//
// It first checks if the instance is initialized, then registers RPC handlers,
// and calls Bootstap method of Node instance.
//
// After all the operations, the SwimRing instance enters ready state.
func (sr *SwimRing) Bootstrap() ([]string, error) {
if sr.Status() < initialized {
err := sr.init()
if err != nil {
return nil, err
}
}
sr.registerInternalRPCHandlers()
sr.registerExternalRPCHandlers()
joined, err := sr.node.Bootstrap()
if err != nil {
sr.setStatus(initialized)
}
sr.setStatus(ready)
return joined, nil
}
// HandleChanges reveives the change events emitted from memberlist,
// then add/remove servers to/from hashring correspondingly.
func (sr *SwimRing) HandleChanges(changes []membership.Change) {
var serversToAdd, serversToRemove []string
for _, change := range changes {
switch change.Status {
case membership.Alive, membership.Suspect:
serversToAdd = append(serversToAdd, change.Address)
case membership.Faulty:
// serversToRemove = append(serversToRemove, change.Address)
}
}
sr.ring.AddRemoveServers(serversToAdd, serversToRemove)
}
func (sr *SwimRing) registerInternalRPCHandlers() error {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", sr.config.InternalPort))
if err != nil {
return err
}
conn, err := net.ListenTCP("tcp", addr)
if err != nil {
return err
}
server := rpc.NewServer()
sr.node.RegisterRPCHandlers(server)
sr.kvs.RegisterRPCHandlers(server)
go server.Accept(conn)
logger.Noticef("Internal RPC server listening at port %d...", sr.config.InternalPort)
return nil
}
func (sr *SwimRing) registerExternalRPCHandlers() error {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", sr.config.ExternalPort))
if err != nil {
return err
}
conn, err := net.ListenTCP("tcp", addr)
if err != nil {
return err
}
server := rpc.NewServer()
server.RegisterName("SwimRing", sr.rc)
logger.Info("External KVS request RPC handlers registered")
go server.Accept(conn)
logger.Noticef("External RPC server listening at port %d...", sr.config.ExternalPort)
return nil
}