forked from dinstein/fabric
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
283 lines (222 loc) · 5.08 KB
/
client.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
package litekfk
import (
"fmt"
)
type client int
type reader struct {
cli client
target *topicUnit
current *readerPos
end *readerPos
autoReset bool
}
var ErrDropOut = fmt.Errorf("Client have been dropped out")
var ErrEOF = fmt.Errorf("EOF")
const (
ReadPos_Empty = iota
ReadPos_Default
ReadPos_Latest
)
const (
ReadPos_Resume = 256 + iota
ReadPos_ResumeOrDefault
ReadPos_ResumeOrLatest
)
func (c client) UnReg(t *topicUnit) {
t.clients.Lock()
defer t.clients.Unlock()
pos, ok := t.clients.readers[c]
if !ok {
//has been unreg, over
return
}
delete(pos.batch().readers, c)
delete(t.clients.readers, c)
if len(t.clients.readers) == 0 {
t.setDryrun(true)
} else {
var oldest *readerPos
//fix passed position
for _, p := range t.clients.readers {
if oldest == nil || oldest.batch().series > p.batch().series {
oldest = p
}
}
if oldest.batch().series > t.clients.passedSeries {
t.clients.passedSeries = oldest.batch().series
purteTo := t.clients.passedSeries
t.clients.Unlock()
t.DoPurge(purteTo)
t.clients.Lock()
}
}
}
func (c client) Read(t *topicUnit, beginPos int) (*reader, error) {
t.clients.Lock()
defer t.clients.Unlock()
pos, ok := t.clients.readers[c]
if !ok {
switch beginPos & (ReadPos_Resume - 1) {
case ReadPos_Latest:
pos = t.getTail()
case ReadPos_Default:
pos = t.getStart()
default:
return nil, ErrDropOut
}
if len(t.clients.readers) == 0 {
t.clients.passedSeries = pos.batch().series
t.setDryrun(false)
} else if pos.batch().series < t.clients.passedSeries {
t.clients.passedSeries = pos.batch().series
}
t.clients.readers[c] = pos
pos.Value.(*batch).readers[c] = true
} else {
if (beginPos & ReadPos_Resume) == 0 {
return nil, fmt.Errorf("Read options not allow resume")
}
}
return &reader{
current: pos.clone(),
end: t.getTail(),
cli: c,
target: t,
autoReset: true,
}, nil
}
func (r *reader) endBatch() bool {
return r.current.batch().series == r.end.batch().series
}
func (r *reader) _eof() bool {
return r.current.batch().series == r.end.batch().series &&
r.current.logpos == r.end.logpos
}
func (r *reader) eof() bool {
if !r._eof() {
return false
}
if r.autoReset {
r.end = r.target.getTail()
if !r._eof() {
return false
}
}
return true
}
func (r *reader) commit() error {
r.target.clients.Lock()
defer r.target.clients.Unlock()
pos, ok := r.target.clients.readers[r.cli]
if !ok {
return ErrDropOut
}
if commitTo := r.current.batch().series; pos.batch().series < commitTo {
r.current.batch().readers[r.cli] = true
delete(pos.batch().readers, r.cli)
//update passed status
for ; pos.batch().series < commitTo; pos = pos.next() {
cur := pos.batch()
if r.target.clients.passedSeries == cur.series && len(cur.readers) == 0 {
r.target.clients.passedSeries++
} else {
break
}
}
r.target.clients.readers[r.cli] = r.current.clone()
purteTo := r.target.clients.passedSeries
r.target.clients.Unlock()
r.target.DoPurge(purteTo)
r.target.clients.Lock()
} else {
//simple update logpos
pos.logpos = r.current.logpos
}
return nil
}
func (r *reader) readOne() (interface{}, error) {
if r.eof() {
return nil, ErrEOF
}
v := r.current.batch().logs[r.current.logpos]
r.current.logpos++
if r.current.logpos == r.target.conf.batchsize {
r.current = r.current.next()
}
return v, nil
}
func (r *reader) readBatch() (ret []interface{}, e error) {
if r.eof() {
e = ErrEOF
return
}
if r.endBatch() {
ret = r.current.batch().logs[r.current.logpos:r.end.logpos]
r.current.logpos = r.end.logpos
} else {
ret = r.current.batch().logs[r.current.logpos:]
r.current = r.current.next()
}
return
}
func (r *reader) CurrentEnd() *readerPos {
return r.end
}
func (r *reader) AutoReset(br bool) {
r.autoReset = br
}
func (r *reader) Position() uint64 {
return r.target._position(r.current)
}
func (r *reader) Reset() {
r.end = r.target.getTail()
}
func (r *reader) ReadOne() (interface{}, error) {
v, err := r.readOne()
if err == nil {
return v, r.commit()
} else {
return v, err
}
}
func (r *reader) ReadBatch() ([]interface{}, error) {
v, err := r.readBatch()
if err == nil {
return v, r.commit()
} else {
return v, err
}
}
func (r *reader) TransactionReadOne() interface{} {
v, err := r.readOne()
if err == nil {
return v
} else {
return nil
}
}
func (r *reader) TransactionReadBatch() []interface{} {
v, err := r.readBatch()
if err == nil {
return v
} else {
return nil
}
}
func (r *reader) Commit() error { return r.commit() }
func (r *reader) Rollback() error {
r.target.clients.Lock()
defer r.target.clients.Unlock()
pos, ok := r.target.clients.readers[r.cli]
if !ok {
return ErrDropOut
}
r.current = pos.clone()
return nil
}
//try to read full batch (at least one more item has been written after current batch)
//this method will locked and the only way to quit is calling the ReleaseWaiting in topic
func (r *reader) ReadFullBatch() ([]interface{}, error) {
r.end = r.target.getTailAndWait(r.current.batch().series)
return r.ReadBatch()
}