This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
/
topic.go
178 lines (147 loc) · 3.62 KB
/
topic.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
package topic
import (
"math/rand"
"path/filepath"
"sync"
"fmt"
"github.com/celrenheit/sandflake"
"github.com/celrenheit/sandglass-grpc/go/sgproto"
"github.com/celrenheit/sandglass/sgutils"
"golang.org/x/sync/errgroup"
)
type Topic struct {
Name string
Kind sgproto.TopicKind
ReplicationFactor int
NumPartitions int
Partitions []*Partition
StorageDriver sgproto.StorageDriver
basepath string
}
func (t *Topic) Validate() error {
if t.Name == "" { // TODO: check non ascii chars
return fmt.Errorf("topic name should not be empty")
}
if t.ReplicationFactor < 1 {
return fmt.Errorf("replication factor should not be > 0")
}
if t.NumPartitions < 1 {
return fmt.Errorf("number of partitions should not be > 0")
}
return nil
}
func (t *Topic) InitStore(basePath string) error {
msgdir := filepath.Join(basePath, t.Name)
t.basepath = msgdir
if err := sgutils.MkdirIfNotExist(t.basepath); err != nil {
return err
}
if t.NumPartitions <= 0 {
return fmt.Errorf("Num partitions should be > 0")
}
for _, p := range t.Partitions {
err := t.initPartition(p)
if err != nil {
return err
}
}
return nil
}
func (t *Topic) initPartition(p *Partition) error {
p.topic = t
err := p.InitStore(t.basepath)
if err != nil {
return err
}
return nil
}
func (t *Topic) ListPartitions() []*Partition { return t.Partitions }
func (t *Topic) GetPartition(id string) *Partition {
for _, p := range t.Partitions {
if p.Id == id {
return p
}
}
return nil
}
func (t *Topic) ChoosePartitionForKey(key []byte) *Partition {
var id []byte
switch t.Kind {
case sgproto.TopicKind_TimerKind:
panic(fmt.Sprintf("not for timer kind %v %v", t.Name, t.Partitions))
case sgproto.TopicKind_KVKind:
id = key
}
idx := sgutils.Hash(id, len(t.Partitions))
return t.Partitions[idx]
}
func (t *Topic) ChoosePartition(msg *sgproto.Message) *Partition {
var id []byte
switch t.Kind {
case sgproto.TopicKind_TimerKind:
id = msg.Offset.Bytes()
case sgproto.TopicKind_KVKind:
id = msg.Key
}
idx := sgutils.Hash(id, len(t.Partitions))
return t.Partitions[idx]
}
func (t *Topic) ChooseRandomPartition() *Partition {
idx := rand.Int() % len(t.Partitions)
return t.Partitions[idx]
}
func (t *Topic) PutMessage(partition string, msg *sgproto.Message) error {
var p *Partition
if partition != "" {
p = t.GetPartition(partition)
} else {
p = t.ChoosePartition(msg)
}
if msg.Offset == sandflake.Nil {
msg.Offset = p.NextID()
}
return p.PutMessage(msg)
}
func (t *Topic) BatchPutMessages(msgs []*sgproto.Message) error {
msgsByPartitions := make([][]*sgproto.Message, len(t.Partitions))
for _, m := range msgs {
pidx := sgutils.HashString(m.Offset.String(), len(t.Partitions))
msgsByPartitions[pidx] = append(msgsByPartitions[pidx], m)
}
var group errgroup.Group
for pidx, msgs := range msgsByPartitions {
p := t.Partitions[pidx]
msgs := msgs
group.Go(func() error {
return p.BatchPutMessages(msgs)
})
}
return group.Wait()
}
func (t *Topic) Close() error {
var group errgroup.Group
for _, p := range t.Partitions {
group.Go(p.Close)
}
return group.Wait()
}
func (t *Topic) ForEach(fn func(msg *sgproto.Message) error) error {
return t.ForRange(sandflake.Nil, sandflake.MaxID, fn)
}
func (t *Topic) ForRange(min, max sandflake.ID, fn func(msg *sgproto.Message) error) error {
// FIXME
var mu sync.Mutex
var group errgroup.Group
for _, p := range t.Partitions {
p := p
group.Go(func() error {
return p.ForRange(min, max, func(msg *sgproto.Message) error {
mu.Lock()
err := fn(msg)
mu.Unlock()
return err
})
})
}
return group.Wait()
}