-
Notifications
You must be signed in to change notification settings - Fork 6
/
subscribe.go
250 lines (222 loc) · 6.49 KB
/
subscribe.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
package logstream
import (
"context"
"fmt"
"sync"
"github.com/kakao/varlog/internal/storage"
"github.com/kakao/varlog/pkg/types"
"github.com/kakao/varlog/pkg/verrors"
"github.com/kakao/varlog/proto/varlogpb"
)
type SubscribeResult struct {
c chan varlogpb.LogEntry
decider *decidableCondition
cancel context.CancelFunc
wg sync.WaitGroup
err error
}
func (lse *Executor) newSubscribeResult() (*SubscribeResult, context.Context) {
ctx, cancel := context.WithCancel(context.Background())
sr := &SubscribeResult{
c: make(chan varlogpb.LogEntry),
decider: lse.decider,
cancel: cancel,
}
return sr, ctx
}
// Result returns the channel of the result.
func (sr *SubscribeResult) Result() <-chan varlogpb.LogEntry {
return sr.c
}
// Stop stops the subscription.
// This should be called to release resources after using SubscribeResult.
func (sr *SubscribeResult) Stop() {
sr.stop()
sr.wg.Wait()
}
// Err returns the error of the subscription.
// This should be called after Stop is called.
func (sr *SubscribeResult) Err() error {
err := sr.err
return err
}
func (sr *SubscribeResult) stop() {
sr.decider.change(func() {
sr.cancel()
})
}
func (lse *Executor) newEmptySubscribeResult() (*SubscribeResult, context.Context) {
sr, ctx := lse.newSubscribeResult()
close(sr.c)
sr.err = nil
return sr, ctx
}
// SubscribeWithGLSN subscribes to the log stream with the given range of GLSNs.
// TODO: The first argument ctx may not be necessary, since the subscription can be stopped by the `internal/varlogsn/logstream.(*SubscribeResult).Stop()`.
func (lse *Executor) SubscribeWithGLSN(begin, end types.GLSN) (*SubscribeResult, error) {
lse.inflight.Add(1)
defer lse.inflight.Add(-1)
if lse.esm.load() == executorStateClosed {
return nil, verrors.ErrClosed
}
if begin >= end {
return nil, fmt.Errorf("log stream: invalid range: %w", verrors.ErrInvalid)
}
// Because the log stream executor cannot know the global low watermark after a restart, it returns an io.EOF error instead of ErrTrimmed. Note that the log stream executor keeps the global low watermark in memory.
// NOTE: The client who calls Subscribe API should handle the above issue.
lse.globalLowWatermark.mu.Lock()
if begin < lse.globalLowWatermark.glsn {
lse.globalLowWatermark.mu.Unlock()
return nil, fmt.Errorf("log stream: %w", verrors.ErrTrimmed)
}
lse.globalLowWatermark.mu.Unlock()
localLWM, _, _ := lse.lsc.localWatermarks()
if end <= localLWM.GLSN {
sr, _ := lse.newEmptySubscribeResult()
return sr, nil
}
_, globalHWM, uncommittedBegin, invalid := lse.lsc.reportCommitBase()
if localLWM.GLSN.Invalid() && end <= uncommittedBegin.GLSN {
// NOTE: There are no log entries in the log stream replica. Since the subscribe range is less than uncommittedBegin, the range could have been trimmed. However, there is the possibility that the log entries could not have been written into that range. Due to this ambiguity, it returns an empty result.
sr, _ := lse.newEmptySubscribeResult()
return sr, nil
}
if !invalid && uncommittedBegin.GLSN <= begin && end <= globalHWM+1 {
sr, _ := lse.newEmptySubscribeResult()
return sr, nil
}
sr, ctx := lse.newSubscribeResult()
sr.wg.Add(1)
go func() {
defer sr.wg.Done()
sr.err = lse.scanWithGLSN(ctx, begin, end, sr)
}()
return sr, nil
}
// SubscribeWithLLSN subscribes to the log stream with the given range of LLSNs.
// TODO: The first argument ctx may not be necessary, since the subscription can be stopped by the `internal/varlogsn/logstream.(*SubscribeResult).Stop()`.
func (lse *Executor) SubscribeWithLLSN(begin, end types.LLSN) (*SubscribeResult, error) {
lse.inflight.Add(1)
defer lse.inflight.Add(-1)
if lse.esm.load() == executorStateClosed {
return nil, verrors.ErrClosed
}
if begin >= end {
return nil, fmt.Errorf("log stream: invalid range: %w", verrors.ErrInvalid)
}
localLowWatermark, _, _ := lse.lsc.localWatermarks()
if begin < localLowWatermark.LLSN {
return nil, fmt.Errorf("log stream: %w", verrors.ErrTrimmed)
}
_, _, uncommittedBegin, _ := lse.lsc.reportCommitBase()
if localLowWatermark.LLSN.Invalid() && end <= uncommittedBegin.LLSN {
return nil, fmt.Errorf("log stream: %w", verrors.ErrTrimmed)
//sr, _ := lse.newEmptySubscribeResult()
//return sr, nil
}
sr, ctx := lse.newSubscribeResult()
sr.wg.Add(1)
go func() {
defer sr.wg.Done()
sr.err = lse.scanWithLLSN(ctx, begin, end, sr)
}()
return sr, nil
}
func (lse *Executor) scanWithGLSN(ctx context.Context, begin, end types.GLSN, sr *SubscribeResult) error {
defer close(sr.c)
scanBegin := begin
scanEnd := end
for {
select {
case <-ctx.Done():
return nil
default:
}
_, globalHWM, _, _ := lse.lsc.reportCommitBase()
_, localHWM, _ := lse.lsc.localWatermarks()
if localHWM.GLSN < scanEnd && scanEnd <= globalHWM+1 {
scanEnd = localHWM.GLSN + 1
}
lastGLSN := types.InvalidGLSN
scanner, err := lse.stg.NewScanner(storage.WithGLSN(scanBegin, scanEnd))
if err != nil {
return err
}
for scanner.Valid() {
le, err := scanner.Value()
if err != nil {
_ = scanner.Close()
return err
}
le.TopicID = lse.tpid
le.LogStreamID = lse.lsid
lastGLSN = le.GLSN
select {
case sr.c <- le:
case <-ctx.Done():
_ = scanner.Close()
return nil
}
_ = scanner.Next()
}
_ = scanner.Close()
if lastGLSN == scanEnd-1 {
return nil
}
if !lastGLSN.Invalid() {
scanBegin = lastGLSN + 1
}
if err := lse.decider.waitC(ctx, globalHWM+1); err != nil {
return err
}
}
}
func (lse *Executor) scanWithLLSN(ctx context.Context, begin, end types.LLSN, sr *SubscribeResult) error {
defer close(sr.c)
scanBegin := begin
for {
select {
case <-ctx.Done():
return nil
default:
}
_, globalHWM, _, _ := lse.lsc.reportCommitBase()
_, localHWM, _ := lse.lsc.localWatermarks()
scanEnd := end
if localHWM.LLSN+1 < scanEnd {
scanEnd = localHWM.LLSN + 1
}
lastLLSN := types.InvalidLLSN
scanner, err := lse.stg.NewScanner(storage.WithLLSN(scanBegin, scanEnd))
if err != nil {
return err
}
for scanner.Valid() {
le, err := scanner.Value()
if err != nil {
_ = scanner.Close()
return err
}
le.TopicID = lse.tpid
le.LogStreamID = lse.lsid
lastLLSN = le.LLSN
select {
case sr.c <- le:
case <-ctx.Done():
_ = scanner.Close()
return nil
}
_ = scanner.Next()
}
_ = scanner.Close()
if lastLLSN == end-1 {
return nil
}
if !lastLLSN.Invalid() {
scanBegin = lastLLSN + 1
}
if err := lse.decider.waitC(ctx, globalHWM+1); err != nil {
return err
}
}
}