-
Notifications
You must be signed in to change notification settings - Fork 0
/
partitions.go
299 lines (252 loc) · 6.16 KB
/
partitions.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
package events
import (
"fmt"
"github.com/pkg/errors"
"github.com/jeffrom/logd/config"
"github.com/jeffrom/logd/logger"
"github.com/jeffrom/logd/protocol"
)
type partitions struct {
conf *config.Config
logp logger.PartitionManager
head *partition
parts []*partition
nparts int
}
func newPartitions(conf *config.Config, logp logger.PartitionManager) *partitions {
p := &partitions{
conf: conf,
parts: make([]*partition, conf.MaxPartitions),
logp: logp,
}
for i := 0; i < len(p.parts); i++ {
p.parts[i] = newPartition(conf)
}
p.head = p.parts[0]
return p
}
func (p *partitions) String() string {
return fmt.Sprint(p.parts)
}
func (p *partitions) reset() {
p.nparts = 0
p.head = p.parts[0]
}
// add is used when loading the log from disk
func (p *partitions) add(offset uint64, size int) (bool, error) {
var rotated bool
last := p.parts[p.nparts]
if p.nparts == p.conf.MaxPartitions-1 && last.startOffset != 0 {
if err := p.logp.Remove(p.parts[0].startOffset); err != nil {
return rotated, err
}
p.rotate()
rotated = true
}
part := p.parts[p.nparts]
part.reset()
part.startOffset = offset
part.size = size
p.head = part
if p.nparts < p.conf.MaxPartitions-1 {
p.nparts++
}
return rotated, nil
}
func (p *partitions) rotate() {
parts := p.parts
if len(parts) <= 1 {
return
}
// fmt.Println("before rotate", parts)
for i := 0; i < len(parts)-1; i++ {
parts[i], parts[i+1] = parts[i+1], parts[i]
}
// fmt.Println("after rotate", parts)
}
func (p *partitions) available() int {
return p.conf.PartitionSize - p.head.size
}
func (p *partitions) shouldRotate(size int) bool {
return size >= p.conf.PartitionSize-p.head.size
}
func (p *partitions) nextOffset() uint64 {
next := p.head.startOffset + uint64(p.head.size)
return next
}
func (p *partitions) addBatch(b *protocol.Batch, size int) (bool, error) {
var rotated bool
if p.shouldRotate(size) {
wasRotated, err := p.add(p.nextOffset(), 0)
if err != nil {
return rotated, err
}
rotated = wasRotated
}
p.head.addBatch(b, size)
return rotated, nil
}
func (p *partitions) headOffset() uint64 {
return p.head.startOffset + uint64(p.head.size)
}
// getStartOffset gets the start offset from a global offset
func (p *partitions) getStartOffset(off uint64) (uint64, error) {
for i := 0; i < p.nparts; i++ {
part := p.parts[i]
if off >= part.startOffset {
return part.startOffset, nil
}
}
return 0, protocol.ErrNotFound
}
func (p *partitions) lookup(off uint64) (uint64, int, error) {
if p.nparts <= 0 {
return 0, 0, errors.New("no partitions loaded")
}
for i := p.nparts; i >= 0; i-- {
// skip if the newest partition hasn't been set yet
if i == p.nparts && p.nparts > 0 && p.parts[i].startOffset == 0 {
continue
}
part := p.parts[i]
if off >= part.startOffset {
return part.startOffset, int(off - part.startOffset), nil
}
}
return 0, 0, protocol.ErrNotFound
}
type partition struct {
conf *config.Config
startOffset uint64
nbatches int
size int
}
func newPartition(conf *config.Config) *partition {
p := &partition{
conf: conf,
}
return p
}
func (p *partition) String() string {
// return fmt.Sprintf("partition<startOffset: %d, size: %d>", p.startOffset, p.size)
return fmt.Sprintf("%d", p.startOffset)
}
func (p *partition) reset() {
p.startOffset = 0
p.nbatches = 0
p.size = 0
}
func (p *partition) addBatch(b *protocol.Batch, size int) {
p.nbatches++
p.size += size
}
// func (p *partition) addBatch(offset uint64, firstOffset uint64, size int, fullSize int) {
// // allocate more memory for batches if needed
// if p.nbatches > len(p.batches)-1 {
// p.batches = append(p.batches, newBatch())
// for i := p.nbatches; i < cap(p.batches)-1; i++ {
// p.batches[i] = newBatch()
// }
// }
// b := p.batches[p.nbatches]
// b.reset()
// b.offset = offset
// b.firstOffset = firstOffset
// b.size = size
// b.fullSize = fullSize
// p.nbatches++
// p.size += b.fullSize
// }
// func (p *partition) writeOffset() uint64 {
// return p.startOffset + uint64(p.size)
// }
type batch struct {
offset uint64
firstOffset uint64
size int // size of the body
fullSize int // size of the batch including its envelope
nmessages int
}
func newBatch() *batch {
return &batch{}
}
func (b *batch) reset() {
b.offset = 0
b.firstOffset = 0
b.size = 0
b.fullSize = 0
b.nmessages = 0
}
type partitionArgList struct {
max int
parts []*partitionArgs
nparts int
nbatches int
}
func (pl *partitionArgList) String() string {
return fmt.Sprintf("(n: %d) %s", pl.nbatches, pl.parts[:pl.nparts])
}
type partitionArgs struct {
// offset represents the partition offset. It should point to a partition
// file that can be loaded by the logger.
offset uint64
// delta is the offset position from the start of the partition file.
delta int
// limit is the amount of bytes to read from the partition.
limit int
}
func (pa *partitionArgs) String() string {
return fmt.Sprintf("partitionArgs<offset: %d, delta: %d, limit: %d>", pa.offset, pa.delta, pa.limit)
}
func newPartitionArgList(max int) *partitionArgList {
pl := &partitionArgList{
max: max,
parts: make([]*partitionArgs, max),
}
for i := 0; i < max; i++ {
pl.parts[i] = &partitionArgs{}
}
return pl
}
func (pl *partitionArgList) initialize(max int) *partitionArgList {
pl.reset()
pl.max = max
if max > len(pl.parts) {
pl.parts = make([]*partitionArgs, max)
}
for i := 0; i < max; i++ {
if pl.parts[i] == nil {
pl.parts[i] = &partitionArgs{}
}
}
return pl
}
func (pl *partitionArgList) reset() {
pl.nparts = 0
pl.nbatches = 0
}
func (pl *partitionArgList) add(soff uint64, delta int, limit int) {
if pl.nparts >= pl.max {
panic("appended too many partitions")
}
pl.parts[pl.nparts].offset = soff
pl.parts[pl.nparts].delta = delta
pl.parts[pl.nparts].limit = limit
pl.nparts++
}
func (pl *partitionArgList) equals(other *partitionArgList) bool {
if pl.nparts != other.nparts {
return false
}
for i := 0; i < pl.nparts; i++ {
a := pl.parts[i]
b := other.parts[i]
if a == nil || b == nil {
return false
}
if a.offset != b.offset || a.delta != b.delta || a.limit != b.limit {
return false
}
}
return true
}