/
log.go
235 lines (195 loc) · 4.41 KB
/
log.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
package log
import (
"sort"
"sync"
)
type Config struct {
MaxSegmentSize int64
MaxSyncLag int
}
type Log struct {
config Config
store Store
segments []Segment
appender SegmentAppender
nextOffset uint64
syncOffset uint64
writeMutex sync.Mutex
segmentSwitchMutex sync.Mutex
offsetCond *sync.Cond
syncOffsetCond *sync.Cond
}
// Open a log from a store
func Open(config Config, store Store) (*Log, error) {
segments, err := store.Segments()
if err != nil {
return nil, err
}
var nextOffset uint64 = 1
if len(segments) == 0 {
// new store
segment, err := store.AddSegment(1)
if err != nil {
return nil, err
}
segments = append(segments, segment)
}
sort.Sort(ByStartOffset(segments))
segment := segments[len(segments)-1]
reader, err := segment.Reader()
if err != nil {
return nil, err
}
defer reader.Close()
lastOffset, err := reader.SeekToEnd()
if err != nil {
return nil, err
}
nextOffset = lastOffset + 1
appender, err := segment.Appender()
if err != nil {
return nil, err
}
l := &Log{
config: config,
nextOffset: nextOffset,
syncOffset: nextOffset - 1,
store: store,
segments: segments,
appender: appender,
offsetCond: sync.NewCond(&sync.Mutex{}),
syncOffsetCond: sync.NewCond(&sync.Mutex{}),
}
return l, nil
}
func (l *Log) NextOffset() uint64 {
return l.nextOffset
}
// Wait for this log to reach an offset of at least minOffset.
func (l *Log) WaitOffset(minOffset uint64) {
// (1) invariant: nextOffset == lastOffset+1 <=> nextOffset-1 == lastOffset
// (2) condition: lastOffset >= minOffset
// so
// (2) <=> nextOffset-1 >= minOffset
// <=> nextOffset >= minOffset+1
// so
// not (2) <=> nextOffset < minOffset+1
//
// also, since offsets are integers
// (2) <=> nextOffset > minOffset
if l.nextOffset > minOffset {
return
}
l.offsetCond.L.Lock()
for l.nextOffset < minOffset+1 {
l.offsetCond.Wait()
}
l.offsetCond.L.Unlock()
}
// Wait for this log to sync an offset of at least minOffset.
func (l *Log) WaitSyncOffset(minOffset uint64) {
if l.syncOffset >= minOffset {
return
}
l.syncOffsetCond.L.Lock()
defer l.syncOffsetCond.L.Unlock()
for l.syncOffset < minOffset {
l.syncOffsetCond.Wait()
}
}
// Append a message to this log
func (l *Log) Append(message *Message) (uint64, error) {
l.writeMutex.Lock()
defer l.writeMutex.Unlock()
offset := l.nextOffset
sizeAfterAppend, err := l.appender.Append(offset, message)
if err != nil {
return 0, err
}
if sizeAfterAppend > l.config.MaxSegmentSize {
if err := l.switchSegment(); err != nil {
return 0, err
}
}
l.offsetCond.L.Lock()
l.nextOffset++
l.offsetCond.Broadcast()
l.offsetCond.L.Unlock()
//log.Printf("l.nextOffset is now %d", l.nextOffset)
// TODO more async "sync" support?
if l.config.MaxSyncLag >= 0 && (offset-l.syncOffset) > uint64(l.config.MaxSyncLag) {
l.Sync()
}
return offset, nil
}
func (l *Log) switchSegment() error {
l.segmentSwitchMutex.Lock()
defer l.segmentSwitchMutex.Unlock()
if l.appender != nil {
l.appender.Sync()
l.appender.Close()
l.appender = nil
}
segment, err := l.store.AddSegment(l.nextOffset)
if err != nil {
return err
}
l.segments = append(l.segments, segment)
appender, err := segment.Appender()
if err != nil {
return err
}
l.appender = appender
return nil
}
func (l *Log) Sync() {
if l.appender == nil {
return
}
l.syncOffsetCond.L.Lock()
defer l.syncOffsetCond.L.Unlock()
offset := l.nextOffset - 1
l.appender.Sync()
l.syncOffset = offset
l.syncOffsetCond.Broadcast()
}
// Change the configuration
func (l *Log) SetConfig(config Config) {
// not sure sync is needed... but config changes are not frequent
l.writeMutex.Lock()
l.config = config
l.writeMutex.Unlock()
}
func (l *Log) Close() {
if l.appender != nil {
l.appender.Sync()
l.appender.Close()
}
}
// Creates a new consumer starting at startOffset.
// If startOffset == 0, starts at the end of the log.
func (l *Log) Consumer(startOffset uint64) (*Consumer, error) {
l.segmentSwitchMutex.Lock()
defer l.segmentSwitchMutex.Unlock()
if startOffset == 0 {
startOffset = l.nextOffset
}
c := &Consumer{
log: l,
offset: startOffset,
}
if err := c.setReader(); err != nil {
return nil, err
}
return c, nil
}
func (l *Log) segmentForOffset(offset uint64) Segment {
segment := l.segments[0]
for _, s := range l.segments[1:] {
if s.StartOffset() > offset {
break
}
segment = s
}
return segment
}