-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.go
More file actions
217 lines (170 loc) · 4.02 KB
/
Copy pathapi.go
File metadata and controls
217 lines (170 loc) · 4.02 KB
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
// concord is a chord core implementation in golang.
// It is fully resilient up to N (configurable) node failures.
//
// It includes callback for range changes so a DHT can be built on
// top of it.
package concord
import (
"context"
"crypto/tls"
"fmt"
"log/slog"
"net"
"sync"
"time"
"google.golang.org/grpc"
)
type TLSConfig struct {
ServerTLS tls.Config
ClientTLS tls.Config
}
// The main configuration for the Concord service.
type Config struct {
Name string
BindAddr string
AdvAddr string
OnRangeChange func(Range)
HashFunc func([]byte) uint64
HashBits uint
SuccessorCount uint
LogHandler slog.Handler
StabilizeInterval time.Duration
TLS *TLSConfig
}
type Range struct {
Start uint64
End uint64
}
type Server struct {
Name string
Id uint64
Address string
}
type ring struct {
Successors []Server
Predecessor *Server
}
type fingerEntry struct {
Start uint64
Node *Server
}
// A handle to an instance of the Concord service.
type Concord struct {
self Server
interval Range
successors []Server
predecessor *Server
finger []fingerEntry
successorCount uint
stabilizeInterval time.Duration
bindAddr string
advAddr string
lock sync.RWMutex
ln net.Listener
srv *grpc.Server
rpc *rpcHandler
started bool
setup bool
clients connectionCache
stabilizeCtx context.Context
stabilizeCancel context.CancelFunc
hashFunc func([]byte) uint64
hashBits uint
rangeChangeCallback func(Range)
logger *slog.Logger
clientTLS *tls.Config
}
// Creates a new instance of the Concord service.
func New(config Config) *Concord {
return newConcord(config)
}
// Returns the name of the Concord service.
func (c *Concord) Name() string {
c.lock.RLock()
defer c.lock.RUnlock()
return c.self.Name
}
// Returns the ID of the Concord service.
func (c *Concord) Id() uint64 {
c.lock.RLock()
defer c.lock.RUnlock()
return c.self.Id
}
// Returns the address of the Concord service.
func (c *Concord) Address() string {
c.lock.RLock()
defer c.lock.RUnlock()
return c.self.Address
}
// Starts the Concord service; listens for incoming connections.
func (c *Concord) Start() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.started {
return fmt.Errorf("service already started")
}
c.started = true
c.logger.Info("starting server", "bind", c.bindAddr, "address", c.self.Address)
c.rpc.RegisterService(c.srv)
ln, err := net.Listen("tcp", c.bindAddr)
if err != nil {
return err
}
c.ln = ln
go func() {
if err := c.srv.Serve(ln); err != nil {
c.logger.Error("failed to serve", "error", err)
}
}()
return nil
}
// Stops the Concord service.
func (c *Concord) Stop() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.setup {
c.setup = false
c.stabilizeCancel()
}
if c.started {
c.srv.Stop()
c.started = false
}
return nil
}
// Creates a new cluster. The Concord instance must be started before calling this method.
func (c *Concord) Create() error {
return c.create()
}
// Joins an existing cluster. The Concord instance must be started before calling this method.
func (c *Concord) Join(ctx context.Context, bootstrapAddress string) error {
return c.join(ctx, bootstrapAddress)
}
// Looks up the server responsible for the given key.
func (c *Concord) Lookup(key []byte) (Server, error) {
return c.findSuccessor(context.Background(), c.hashFunc(key))
}
// Returns the list of successor servers.
func (c *Concord) Successors() []Server {
c.lock.RLock()
defer c.lock.RUnlock()
// do a deep copy to ensure that the values are not modified.
copiedSuccessors := make([]Server, len(c.successors))
copy(copiedSuccessors, c.successors)
return copiedSuccessors
}
// Returns the predecessor server.
func (c *Concord) Predecessor() (Server, bool) {
c.lock.RLock()
defer c.lock.RUnlock()
if c.predecessor == nil {
return Server{}, false
}
return *c.predecessor, true
}
// Returns the range of keys managed by this server.
func (c *Concord) Range() Range {
c.lock.RLock()
defer c.lock.RUnlock()
return c.interval
}