-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributed.go
376 lines (319 loc) · 8.26 KB
/
distributed.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package goutube
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"net"
"sync"
"time"
streaming_api "github.com/Brijeshlakkad/goutube/api/streaming/v1"
"github.com/Brijeshlakkad/goutube/pointcron"
"github.com/Brijeshlakkad/ring"
"github.com/hashicorp/go-hclog"
"google.golang.org/protobuf/proto"
)
var (
ErrMaxChunkSizeInvalid = errors.New("configuration error: max chunk size cannot be zero")
)
type DistributedLoci struct {
config Config
locus *Locus
store Store
logger hclog.Logger
ring *ring.Ring
arc *Arc
mu sync.RWMutex
bundler *RequestBundler
}
func NewDistributedLoci(dataDir string, config Config) (
*DistributedLoci,
error,
) {
if config.Distributed.MaxChunkSize <= 0 {
return nil, ErrMaxChunkSizeInvalid
}
if config.Distributed.Logger == nil {
config.Distributed.Logger = hclog.New(&hclog.LoggerOptions{
Name: "distributed-loci",
Output: hclog.DefaultOutput,
Level: hclog.DefaultLevel,
})
}
d := &DistributedLoci{
config: config,
bundler: &RequestBundler{},
ring: config.Distributed.Ring,
}
var err error
if err = d.setupLocus(dataDir); err != nil {
return nil, err
}
if err = d.setupStore(dataDir); err != nil {
return nil, err
}
arcConfig := ArcConfig{
StreamLayer: config.Distributed.StreamLayer,
fsm: &fsm{
locus: d.locus,
},
store: d.store,
Bundler: d.bundler,
}
d.arc, err = NewArc(arcConfig)
if err != nil {
return nil, err
}
return d, nil
}
func (d *DistributedLoci) setupLocus(dataDir string) error {
lociDir, err := createDirectory(dataDir, "locus")
if err != nil {
return err
}
d.config.Point.pointScheduler = pointcron.NewPointScheduler(pointcron.Config{
CloseTimeout: d.config.Point.CloseTimeout,
TickTime: d.config.Point.TickTime,
})
d.config.Point.pointScheduler.StartAsync()
d.locus, err = NewLocus(lociDir, d.config)
return err
}
func (d *DistributedLoci) setupStore(dataDir string) (err error) {
logDir, err := createDirectory(dataDir, "log")
if err != nil {
return err
}
d.store, err = NewInMomoryPointStore(logDir)
return err
}
func (d *DistributedLoci) GetPoints() []string {
return d.locus.GetPoints()
}
func (d *DistributedLoci) Append(record *streaming_api.ProduceRequest) (uint64, error) {
d.mu.Lock()
defer d.mu.Unlock()
apply, err := d.apply(AppendRequestType, record.Point, record.Frame)
if err != nil {
return 0, err
}
records := apply.(*streaming_api.ProduceResponse).Records
if len(records) > 0 {
return records[0].Offset, nil
}
return 0, nil
}
func (d *DistributedLoci) apply(reqType RequestType, key interface{}, value interface{}) (
interface{},
error,
) {
b, err := d.bundler.Build(reqType, key, value)
if err != nil {
return nil, err
}
timeout := 10 * time.Second
commandPromise := d.arc.Apply(b, timeout)
if err := commandPromise.Error(); err != nil {
return nil, err
}
res := commandPromise.Response().(*RecordResponse)
return res.Response, nil
}
func (d *DistributedLoci) ReadWithLimit(pointId string, pos uint64, chunkSize uint64, limit uint64) (uint64, []byte, error) {
return d.locus.Read(pointId, pos, chunkSize, limit)
}
func (d *DistributedLoci) Read(pointId string, pos uint64) (uint64, []byte, error) {
return d.locus.Read(pointId, pos, 0, 0)
}
func (d *DistributedLoci) ReadAt(pointId string, b []byte, off uint64) (int, error) {
return d.locus.ReadAt(pointId, b, off)
}
func (d *DistributedLoci) GetMetadata(pointId string) (PointMetadata, error) {
return d.locus.GetMetadata(pointId)
}
func (d *DistributedLoci) ClosePoint(pointId string) error {
d.mu.Lock()
defer d.mu.Unlock()
return d.locus.Close(pointId)
}
// OnChange allows to get notified when new server joins the ring at the position next on the ring to this server.
// Hence, this server needs to send off points that should not be handled by this server anymore.
func (d *DistributedLoci) OnChange(batch []ring.ShardResponsibility) {
d.arc.onResponsibilityChange(batch, d.GetPoints())
}
func (d *DistributedLoci) Shutdown() error {
d.mu.Lock()
defer d.mu.Unlock()
promise := d.arc.Shutdown()
if err := promise.Error(); err != nil {
return err
}
res := promise.Response()
if err, ok := res.(error); ok {
return err
}
return d.locus.CloseAll()
}
func (d *DistributedLoci) Remove() error {
d.mu.Lock()
defer d.mu.Unlock()
return d.locus.RemoveAll()
}
func (d *DistributedLoci) Join(rpcAddr string, rule ParticipationRule) error {
d.mu.Lock()
defer d.mu.Unlock()
if d.canArcJoin(rule) {
return d.arc.join(rpcAddr)
}
return nil
}
func (d *DistributedLoci) Leave(rpcAddr string) error {
d.mu.Lock()
defer d.mu.Unlock()
return d.arc.leave(rpcAddr)
}
func (d *DistributedLoci) canArcJoin(rule ParticipationRule) bool {
return d.config.Distributed.Rule == LeaderRule && rule == FollowerRule
}
func (d *DistributedLoci) GetServers(req *streaming_api.GetServersRequest) ([]*streaming_api.Server, error) {
d.mu.RLock()
defer d.mu.RUnlock()
var servers []*streaming_api.Server
serverList := d.ring.GetLoadBalancers()
for _, serverAddr := range serverList {
servers = append(servers, &streaming_api.Server{
RpcAddr: serverAddr,
})
}
return servers, nil
}
func (d *DistributedLoci) GetFollowers(req *streaming_api.GetFollowersRequest) ([]*streaming_api.Server, error) {
d.mu.RLock()
defer d.mu.RUnlock()
var servers []*streaming_api.Server
serverList := d.arc.GetFollowers()
// Include the leader as well.
for _, server := range serverList {
servers = append(servers, &streaming_api.Server{
RpcAddr: string(server.Address),
})
}
return servers, nil
}
type LocusStreamLayer struct {
ln net.Listener
serverTLSConfig *tls.Config
peerTLSConfig *tls.Config
}
func NewStreamLayer(
ln net.Listener,
serverTLSConfig,
peerTLSConfig *tls.Config,
) *LocusStreamLayer {
return &LocusStreamLayer{
ln: ln,
serverTLSConfig: serverTLSConfig,
peerTLSConfig: peerTLSConfig,
}
}
const RingRPC = 1
func (s *LocusStreamLayer) Dial(address ServerAddress, timeout time.Duration) (net.Conn, error) {
dialer := &net.Dialer{Timeout: timeout}
var conn, err = dialer.Dial("tcp", string(address))
if err != nil {
return nil, err
}
// identify to mux this is a raft rpc
_, err = conn.Write([]byte{byte(RingRPC)})
if err != nil {
return nil, err
}
if s.peerTLSConfig != nil {
conn = tls.Client(conn, s.peerTLSConfig)
}
return conn, err
}
func (s *LocusStreamLayer) Accept() (net.Conn, error) {
conn, err := s.ln.Accept()
if err != nil {
return nil, err
}
b := make([]byte, 1)
_, err = conn.Read(b)
if err != nil {
return nil, err
}
if bytes.Compare([]byte{byte(RingRPC)}, b) != 0 {
return nil, fmt.Errorf("not a raft rpc")
}
if s.serverTLSConfig != nil {
return tls.Server(conn, s.serverTLSConfig), nil
}
return conn, nil
}
func (s *LocusStreamLayer) Close() error {
return s.ln.Close()
}
func (s *LocusStreamLayer) Addr() net.Addr {
return s.ln.Addr()
}
var _ FSM = (*fsm)(nil)
type fsm struct {
locus *Locus
}
// Apply Invokes this method after committing a log entry.
func (f *fsm) Apply(record *RecordRequest) *FSMRecordResponse {
buf := record.Data
reqType := RequestType(buf[0])
switch reqType {
case AppendRequestType:
return f.applyAppend(buf[1:])
}
return nil
}
func (f *fsm) applyAppend(b []byte) *FSMRecordResponse {
var req streaming_api.ProduceRequest
err := proto.Unmarshal(b, &req)
if err != nil {
return &FSMRecordResponse{Response: err}
}
nextOffset, offset, err := f.locus.Append(req.GetPoint(), req.GetFrame())
if err != nil {
return &FSMRecordResponse{Response: err}
}
return &FSMRecordResponse{
StoreKey: req.Point,
StoreValue: nextOffset,
Response: &streaming_api.ProduceResponse{Records: []*streaming_api.Record{
{
Point: req.Point,
Offset: offset,
},
}},
}
}
func (f *fsm) Read(key string, offset uint64) (uint64, []byte, error) {
return f.locus.Read(key, offset, 0, 0)
}
type RequestBundler struct {
}
func (rb *RequestBundler) Build(header interface{}, key interface{}, value interface{}) (
[]byte,
error,
) {
req := &streaming_api.ProduceRequest{Point: key.(string), Frame: value.([]byte)}
var buf bytes.Buffer
_, err := buf.Write([]byte{byte(header.(RequestType))})
if err != nil {
return nil, err
}
b, err := proto.Marshal(req)
if err != nil {
return nil, err
}
_, err = buf.Write(b)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}