-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition.go
533 lines (500 loc) · 14.1 KB
/
partition.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package MqServer
import (
"bytes"
"sync"
"sync/atomic"
)
import (
"github.com/YarBor/BorsMqServer/common"
"github.com/YarBor/BorsMqServer/consumer_group"
err_ "github.com/YarBor/BorsMqServer/error"
"github.com/YarBor/BorsMqServer/message_memory"
"github.com/YarBor/BorsMqServer/raft_server"
Pack "github.com/YarBor/BorsMqServer/raft_server/pack"
)
const (
Partition_Mode_ToDel = int32(1)
Partition_Mode_Normal = int32(0)
)
type Partition struct {
Mode int32
Node *raft_server.RaftNode
T string
P string
ConsumerGroupManager *consumer_group.GroupsManager
MessageEntry *message_memory.MessageEntry
}
const (
PartitionCommand_Write = "w"
PartitionCommand_ToDel = "t"
PartitionCommand_Read = "r"
//PartitionCommand_PartUpdate = "p" // TODO Think:Should Do this in clusters ?
PartitionCommand_GroupUpdate = "g"
)
func (_p *Partition) Handle(i interface{}) (error, interface{}) {
cmd, ok := i.(PartitionCommand)
if !ok {
cmd = PartitionCommand{
Mode: i.(map[string]interface{})["Mode"].(string),
Data: i.(map[string]interface{})["Data"],
}
}
switch cmd.Mode {
case PartitionCommand_ToDel:
err := _p.partToDel()
return err, nil
case PartitionCommand_Write:
data, ok1 := cmd.Data.([][]byte)
if !ok1 {
td := cmd.Data.([]interface{})
data = make([][]byte, 0)
for _, i3 := range td {
data = append(data, i3.([]byte))
}
}
err := _p.write(data)
return err, nil
case PartitionCommand_GroupUpdate:
data, ok1 := cmd.Data.(struct {
Gid string
Cons *consumer_group.Consumer
})
if !ok1 {
_m := cmd.Data.(map[string]interface{})
_m_Cons := _m["Cons"].(map[string]interface{})
data = struct {
Gid string
Cons *consumer_group.Consumer
}{Gid: _m["Gid"].(string), Cons: &consumer_group.Consumer{
Mode: _m_Cons["Mode"].(int32),
SelfId: _m_Cons["SelfId"].(string),
GroupId: _m_Cons["GroupId"].(string),
Time: _m_Cons["Time"].(int64),
TimeoutSessionMsec: _m_Cons["TimeoutSessionMsec"].(int32),
MaxReturnMessageSize: _m_Cons["MaxReturnMessageSize"].(int32),
MaxReturnMessageEntries: _m_Cons["MaxReturnMessageEntries"].(int32),
}}
}
err := _p.updateGroupConsumer(data.Gid, data.Cons)
return err, nil
//case PartitionCommand_ModeChange:
case PartitionCommand_Read:
data, ok1 := cmd.Data.(struct {
ConsId, ConsGid string
CommitIndex int64
ReadEntryNum int32
})
if !ok1 {
_data := cmd.Data.(map[string]interface{})
data = struct {
ConsId, ConsGid string
CommitIndex int64
ReadEntryNum int32
}{
ConsId: _data["ConsId"].(string),
ConsGid: _data["ConsGid"].(string),
CommitIndex: _data["CommitIndex"].(int64),
ReadEntryNum: _data["ReadEntryNum"].(int32),
}
}
Data, ReadBeginOffset, ReadEntriesNum, IsAllow2Del, err := _p.read(data.ConsId, data.ConsGid, data.CommitIndex, data.ReadEntryNum)
if err != nil {
return err, nil
}
return nil, struct {
Data [][]byte
ReadBeginOffset int64
ReadEntriesNum int64
IsAllow2Del bool
err error
}{Data, ReadBeginOffset, ReadEntriesNum, IsAllow2Del, nil}
default:
panic("unknown command")
}
}
func (_p *Partition) partToDel() error {
_p.ChangeModeToDel()
return nil
}
func (_p *Partition) PartToDel() error {
err, _ := _p.commit(PartitionCommand_ToDel, nil)
return err
}
func (_p *Partition) write(data [][]byte) error {
for _, datum := range data {
_p.MessageEntry.Write(datum)
}
return nil
}
func (_p *Partition) Write(data [][]byte) error {
err, _ := _p.commit(PartitionCommand_Write, data)
return err
}
type PartitionCommand struct {
Mode string
Data interface{}
}
func (_p *Partition) updateGroupConsumer(gid string, cons *consumer_group.Consumer) error {
g, err := _p.ConsumerGroupManager.GetConsumerGroup(gid)
if err != nil {
return err
}
return g.SetWaitConsumer(cons)
}
func (_p *Partition) UpdateGroupConsumer(gid string, cons *consumer_group.Consumer) (err error) {
err, _ = _p.commit(PartitionCommand_GroupUpdate, struct {
Gid string
Cons *consumer_group.Consumer
}{gid, cons})
return
}
func (_p *Partition) GetAllConsumerGroup() []*consumer_group.ConsumerGroup {
return _p.ConsumerGroupManager.GetAllGroup()
}
func (_p *Partition) commit(cmd string, data interface{}) (error, interface{}) {
if _p.IsLeader() == false {
return err_.ErrRequestNotLeader, nil
}
return _p.Node.Commit(
PartitionCommand{
Mode: cmd,
Data: data,
})
}
func (_p *Partition) IsLeader() bool {
return _p.Node.IsLeader()
}
func (_p *Partition) MakeSnapshot() []byte {
bf := bytes.NewBuffer(nil)
encode := Pack.NewEncoder(bf)
if err := encode.Encode(atomic.LoadInt32(&_p.Mode)); err != nil {
panic(err)
}
if err := encode.Encode(_p.ConsumerGroupManager.MakeSnapshot()); err != nil {
panic(err)
}
if err := encode.Encode(_p.MessageEntry.MakeSnapshot()); err != nil {
panic(err)
}
return bf.Bytes()
}
func (_p *Partition) LoadSnapshot(data []byte) {
bf := bytes.NewBuffer(data)
decode := Pack.NewDecoder(bf)
var mode int32
if err := decode.Decode(&mode); err != nil {
panic(err)
} else {
atomic.StoreInt32(&_p.Mode, mode)
}
var data1 []byte
if err := decode.Decode(&data1); err != nil {
panic(err)
} else {
_p.ConsumerGroupManager.LoadSnapshot(data1)
}
var data2 []byte
if err := decode.Decode(&data2); err != nil {
panic(err)
} else {
_p.MessageEntry.LoadSnapshot(data2)
}
}
type PartitionsController struct {
rfServer *raft_server.RaftServer
ttMu sync.RWMutex
TopicTerm map[string]*int32
cgtMu sync.RWMutex
ConsumerGroupTerm map[string]*int32
partsMu sync.RWMutex
P map[string]*map[string]*Partition // key: "Topic/Partition"
handleTimeout consumer_group.SessionLogoutNotifier
}
func (c *PartitionsController) GetAllPart() []*Partition {
c.partsMu.RLock()
defer c.partsMu.RUnlock()
data := []*Partition{}
for _, m := range c.P {
for _, partition := range *m {
data = append(data, partition)
}
}
return data
}
func (c *PartitionsController) Stop() {
c.ttMu.Lock()
defer c.ttMu.Unlock()
c.cgtMu.Lock()
defer c.cgtMu.Unlock()
c.partsMu.Lock()
defer c.partsMu.Unlock()
for _, ps := range c.P {
for _, partition := range *ps {
partition.Stop()
}
}
}
func (p *PartitionsController) GetTopicTerm(id string) (int32, error) {
p.ttMu.RLock()
defer p.ttMu.RUnlock()
i, ok := p.TopicTerm[id]
if ok {
return atomic.LoadInt32(i), nil
} else {
return -1, (err_.ErrSourceNotExist)
}
}
func (p *PartitionsController) GetConsumerGroupTerm(id string) (int32, error) {
p.cgtMu.RLock()
defer p.cgtMu.RUnlock()
i, ok := p.ConsumerGroupTerm[id]
if ok {
return atomic.LoadInt32(i), nil
} else {
return -1, (err_.ErrSourceNotExist)
}
}
func (_p *Partition) Stop() {
_p.ConsumerGroupManager.Stop()
}
// ChangeModeToDel TODO : Commit Handle
func (_p *Partition) ChangeModeToDel() {
atomic.StoreInt32(&_p.Mode, Partition_Mode_ToDel)
_p.ConsumerGroupManager.CorrespondPart2Del()
}
func (_p *Partition) CheckToDel() bool {
return atomic.LoadInt32(&_p.Mode) == Partition_Mode_ToDel || _p.ConsumerGroupManager.IsNoGroupExist()
}
func newPartition(t, p string,
MaxEntries, MaxSize uint64,
handleTimeout consumer_group.SessionLogoutNotifier,
server *raft_server.RaftServer,
peers ...struct{ ID, Url string },
) (*Partition, error) {
if len(peers) <= 0 {
return nil, err_.ErrRequestIllegal
}
part := &Partition{
Mode: Partition_Mode_Normal,
Node: nil,
T: t,
P: p,
MessageEntry: message_memory.NewMessageEntry(MaxEntries, MaxSize, common.DefaultEntryMaxSizeOfEachBlock, t, p),
}
part.ConsumerGroupManager = consumer_group.NewGroupsManager(handleTimeout, part)
node, err := server.RegisterRfNode(t, p, part, part, peers...)
if err != nil {
return nil, err
}
part.Node = node
return part, nil
}
func (_p *Partition) Start() error {
if _p.Node == nil {
return err_.ErrRequestIllegal
}
_p.Node.Start()
return nil
}
// TODO : Commit-Handle
func (_p *Partition) registerConsumerGroup(groupId string, consumer *consumer_group.Consumer, consumeOff int64) (*consumer_group.ConsumerGroup, error) {
if atomic.LoadInt32(&_p.Mode) == Partition_Mode_ToDel {
return nil, err_.ErrSourceNotExist
}
return _p.ConsumerGroupManager.RegisterConsumerGroup(consumer_group.NewConsumerGroup(groupId, consumer, consumeOff))
}
func NewPartitionsController(rf *raft_server.RaftServer, handleTimeout consumer_group.SessionLogoutNotifier) *PartitionsController {
return &PartitionsController{
rfServer: rf,
ttMu: sync.RWMutex{},
TopicTerm: make(map[string]*int32),
cgtMu: sync.RWMutex{},
ConsumerGroupTerm: make(map[string]*int32),
partsMu: sync.RWMutex{},
P: make(map[string]*map[string]*Partition),
handleTimeout: handleTimeout,
}
}
func (_p *Partition) Read(ConsId, ConsGid string, CommitIndex int64, ReadEntryNum int32) ([][]byte, int64, int64, bool, error) {
err, data := _p.commit(PartitionCommand_Read, struct {
ConsId, ConsGid string
CommitIndex int64
ReadEntryNum int32
}{ConsId, ConsGid, CommitIndex, ReadEntryNum})
if err != nil {
return nil, 0, 0, false, err
}
res := data.(struct {
Data [][]byte
ReadBeginOffset int64
ReadEntriesNum int64
IsAllow2Del bool
err error
})
return res.Data, res.ReadBeginOffset, res.ReadEntriesNum, res.IsAllow2Del, res.err
}
// return: data , ReadBeginOffset ,readEntries Num, IsAllow to del , err_
// consider FirstTime , commitIndex == -1
// TODO : ADD Field to save Offset Consumer consumed offset
func (_p *Partition) read(ConsId, ConsGid string, CommitIndex int64, ReadEntryNum int32) ([][]byte, int64, int64, bool, error) {
g, err := _p.ConsumerGroupManager.GetConsumerGroup(ConsGid)
if err != nil {
return nil, -1, -1, false, err
}
gMode := g.GetMode()
Begin:
switch gMode {
case
consumer_group.ConsumerGroupStart:
if !g.CheckConsumer(ConsId) {
return nil, -1, -1, false, err_.ErrRequestIllegal
}
g.Consumers.TimeUpdate()
if ReadEntryNum != 0 {
BeginOffset, data, ReadNum := _p.MessageEntry.Read(g.GetConsumeOffset(), ReadEntryNum, g.Consumers.MaxReturnMessageSize)
err := g.SetLastTimeOffset_Data(BeginOffset, &data)
if err != nil {
panic(err)
}
err = g.ChangeState(consumer_group.ConsumerGroupNormal)
if err != nil {
panic(err)
}
if !g.SetConsumeOffset(BeginOffset + ReadNum) {
return nil, -1, -1, false, err_.ErrRequestIllegal
}
return data, BeginOffset, ReadNum, false, nil
}
case
consumer_group.ConsumerGroupNormal:
if !g.CheckConsumer(ConsId) {
return nil, -1, -1, false, err_.ErrRequestIllegal
}
g.Consumers.TimeUpdate()
if ReadEntryNum != 0 {
success, LastData, off := g.Commit(CommitIndex)
if success {
BeginOffset, data, ReadNum := _p.MessageEntry.Read(g.GetConsumeOffset(), ReadEntryNum, g.Consumers.MaxReturnMessageSize)
err := g.SetLastTimeOffset_Data(BeginOffset, &data)
if err != nil {
panic(err)
}
if !g.SetConsumeOffset(BeginOffset + ReadNum) {
return nil, -1, -1, false, err_.ErrRequestIllegal
}
return data, BeginOffset, ReadNum, false, nil
} else {
return LastData, off, int64(len(LastData)), false, nil
}
}
case
consumer_group.ConsumerGroupToDel:
if !g.CheckConsumer(ConsId) {
return nil, -1, -1, false, err_.ErrRequestIllegal
}
g.Consumers.TimeUpdate()
if ReadEntryNum != 0 {
success, Lastdata, off := g.Commit(CommitIndex)
if success {
if _p.MessageEntry.IsClearToDel(CommitIndex) {
_p.ConsumerGroupManager.DelGroup(ConsGid)
// Part-Check-Del In Other Part where Call Read
return nil, -1, -1, true, nil
} else {
BeginOffset, data, ReadNum := _p.MessageEntry.Read(g.GetConsumeOffset(), ReadEntryNum, g.Consumers.MaxReturnMessageSize)
err := g.SetLastTimeOffset_Data(BeginOffset, &data)
if err != nil {
panic(err)
}
if !g.SetConsumeOffset(BeginOffset + ReadNum) {
return nil, -1, -1, false, err_.ErrRequestIllegal
}
return data, BeginOffset, ReadNum, false, nil
}
} else {
return Lastdata, off, int64(len(Lastdata)), false, nil
}
}
case
consumer_group.ConsumerGroupChangeAndWaitCommit:
if g.CheckConsumer(ConsId) {
g.Consumers.TimeUpdate()
if ReadEntryNum == 0 {
Success, data, off := g.Commit(CommitIndex)
if Success {
err := g.ChangeState(consumer_group.ConsumerGroupStart)
if err != nil {
return nil, -1, -1, false, err
} else {
return nil, -1, -1, true, nil
}
} else {
return data, off, int64(len(data)), false, nil
}
} else {
}
} else {
if err := g.ChangeConsumer(ConsId); err == nil {
goto Begin
} else {
return nil, -1, -1, false, err
}
}
}
return nil, CommitIndex, 0, false, nil
}
//一个是Part的回收,一个是心跳监测去call_Part状态改变
func (pc *PartitionsController) CheckPartToDel(t, p string) {
part, err := pc.GetPart(t, p)
if err != nil {
return
}
if part.CheckToDel() {
pc.partsMu.Lock()
//(*pc.P[t])[p].ChangeModeToDel()
delete(*pc.P[t], p)
if len(*pc.P[t]) == 0 {
delete(pc.P, t)
}
pc.partsMu.Unlock()
}
}
func (pc *PartitionsController) GetPart(t, p string) (*Partition, error) {
pc.partsMu.RLock()
parts, ok := pc.P[t]
pc.partsMu.RUnlock()
if ok {
part, ok := (*parts)[p]
if ok {
return part, nil
}
}
return nil, err_.ErrSourceNotExist
}
func (ptc *PartitionsController) RegisterPart(t, p string,
MaxEntries, MaxSize uint64,
peers ...struct{ ID, Url string },
) (part *Partition, err error) {
ptc.partsMu.Lock()
defer ptc.partsMu.Unlock()
if MaxSize == 0 {
MaxSize = uint64(common.PartDefaultMaxSize)
}
if MaxEntries == 0 {
MaxEntries = uint64(common.PartDefaultMaxEntries)
}
ok := false
if data, ok := ptc.P[t]; !ok || data == nil {
data = &map[string]*Partition{}
ptc.P[t] = data
}
part, ok = (*ptc.P[t])[p]
if !ok {
part, err = newPartition(t, p, MaxEntries, MaxSize, ptc.handleTimeout, ptc.rfServer, peers...)
if err != nil {
return nil, err
}
(*ptc.P[t])[p] = part
}
return part, nil
}