/
ingest.go
436 lines (358 loc) · 11.1 KB
/
ingest.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package anelace
import (
"encoding/binary"
"fmt"
"github.com/anjor/anelace/internal/block"
"github.com/anjor/anelace/internal/chunker"
"github.com/anjor/anelace/internal/constants"
"github.com/anjor/anelace/internal/util/encoding"
"github.com/anjor/anelace/internal/util/text"
"github.com/anjor/anelace/internal/util/zcpstring"
"io"
"log"
"sync/atomic"
"time"
"github.com/ipfs/go-qringbuf"
)
// SANCHECK: not sure if any of these make sense, nor have I measured the cost
const (
carQueueSize = 2048
)
const (
ErrorString = IngestionEventType(iota)
NewRootJsonl
)
type IngestionEvent struct {
_ constants.Incomparabe
Type IngestionEventType
Body string
}
type IngestionEventType int
// SANCHECK - we probably want some sort of timeout or somesuch here...
func (anl *Anelace) maybeSendEvent(t IngestionEventType, s string) {
if anl.externalEventBus != nil {
anl.externalEventBus <- IngestionEvent{Type: t, Body: s}
}
}
var preProcessTasks, postProcessTasks func(anl *Anelace)
func (anl *Anelace) ProcessReader(inputReader io.Reader, optionalEventChan chan<- IngestionEvent) (err error) {
var t0 time.Time
defer func() {
// a little helper to deal with error stack craziness
deferErrors := make(chan error, 1)
// if we are already in error - just put it on the channel
// we already sent the event earlier
if err != nil {
deferErrors <- err
}
// keep sending out events but keep at most 1 error to return synchronously
addErr := func(e error) {
if e != nil {
anl.maybeSendEvent(ErrorString, e.Error())
select {
case deferErrors <- e:
default:
}
}
}
// we need to wait all crunching to complete, then shutdown emitter, then measure/return
anl.asyncWG.Wait()
// we are writing data: need to wait/close things
if anl.carDataQueue != nil {
close(anl.carDataQueue) // signal data-write stop
addErr(<-anl.carWriteError) // wait for data-write stop
}
if err == nil && len(deferErrors) > 0 {
err = <-deferErrors
}
if postProcessTasks != nil {
postProcessTasks(anl)
}
anl.qrb = nil
if anl.externalEventBus != nil {
close(anl.externalEventBus)
}
anl.statSummary.SysStats.ElapsedNsecs = time.Since(t0).Nanoseconds()
}()
anl.externalEventBus = optionalEventChan
defer func() {
if err != nil {
var buffered int
if anl.qrb != nil {
anl.qrb.Lock()
buffered = anl.qrb.Buffered()
anl.qrb.Unlock()
}
err = fmt.Errorf(
"failure at byte offset %s of sub-stream #%d with %s bytes buffered/unprocessed: %s",
text.Commify64(anl.curStreamOffset),
anl.statSummary.Streams,
text.Commify(buffered),
err,
)
anl.maybeSendEvent(ErrorString, err.Error())
}
}()
if preProcessTasks != nil {
preProcessTasks(anl)
}
t0 = time.Now()
anl.qrb, err = qringbuf.NewFromReader(inputReader, qringbuf.Config{
// MinRegion must be twice the maxchunk, otherwise chunking chains won't work (hi, Claude Shannon)
MinRegion: 2 * constants.MaxLeafPayloadSize,
MinRead: anl.cfg.RingBufferMinRead,
MaxCopy: 2 * constants.MaxLeafPayloadSize, // SANCHECK having it equal to the MinRegion may be daft...
BufferSize: anl.cfg.RingBufferSize,
SectorSize: anl.cfg.RingBufferSectSize,
Stats: &anl.statSummary.SysStats.Stats,
TrackTiming: ((anl.cfg.StatsActive & statsRingbuf) == statsRingbuf),
})
if err != nil {
return
}
// We got that far - got to write out the data portion prequel
// .oO( The machine of a dream, such a clean machine
// With the pistons a pumpin', and the hubcaps all gleam )
if anl.carDataWriter != nil {
if _, err = io.WriteString(anl.carDataWriter, anlblock.NulRootCarHeader); err != nil {
return
}
// start the async writer here, once we know nothing errorred
anl.carDataQueue = make(chan carUnit, carQueueSize)
anl.carWriteError = make(chan error, 1)
go anl.backgroundCarDataWriter()
}
if (anl.cfg.StatsActive & statsBlocks) == statsBlocks {
anl.seenBlocks = make(seenBlocks, 1024) // SANCHECK: somewhat arbitrary, but eh...
anl.seenRoots = make(seenRoots, 32)
}
// use 64bits everywhere
var substreamSize int64
// outer stream loop: read() syscalls happen only here and in the qrb.collector()
for {
if anl.cfg.MultipartStream {
err := binary.Read(
inputReader,
binary.BigEndian,
&substreamSize,
)
anl.statSummary.SysStats.ReadCalls++
if err == io.EOF {
// no new multipart coming - bail
break
} else if err != nil {
return fmt.Errorf(
"error reading next 8-byte multipart substream size: %s",
err,
)
}
if substreamSize == 0 && anl.cfg.SkipNulInputs {
continue
}
anl.statSummary.Streams++
anl.curStreamOffset = 0
}
if anl.cfg.MultipartStream && substreamSize == 0 {
// If we got here: cfg.ProcessNulInputs is true
// Special case for a one-time zero-CID emission
anl.streamAppend(nil)
} else if err := anl.processStream(substreamSize); err != nil {
if err == io.ErrUnexpectedEOF {
return fmt.Errorf(
"unexpected end of substream #%s after %s bytes (stream expected to be %s bytes long)",
text.Commify64(anl.statSummary.Streams),
text.Commify64(anl.curStreamOffset+int64(anl.qrb.Buffered())),
text.Commify64(substreamSize),
)
} else if err != io.EOF {
return err
} else if anl.curStreamOffset == 0 && !anl.cfg.SkipNulInputs {
// we did try to process a stream and ended up with an EOF + 0
// emit a zero-CID like above
anl.streamAppend(nil)
}
}
if anl.generateRoots || anl.seenRoots != nil || anl.externalEventBus != nil {
rootBlock := anl.collector.FlushState()
var rootPayloadSize, rootDagSize uint64
if rootBlock != nil {
rootPayloadSize = rootBlock.SizeCumulativePayload()
rootDagSize = rootBlock.SizeCumulativeDag()
if anl.seenRoots != nil {
anl.mu.Lock()
var rootSeen bool
if sk := seenKey(rootBlock); sk != nil {
if _, rootSeen = anl.seenRoots[*sk]; !rootSeen {
anl.seenRoots[*sk] = seenRoot{
order: len(anl.seenRoots),
cid: rootBlock.Cid(),
}
}
}
anl.statSummary.Roots = append(anl.statSummary.Roots, rootStats{
Cid: anl.formattedCid(rootBlock),
SizePayload: rootBlock.SizeCumulativePayload(),
SizeDag: rootBlock.SizeCumulativeDag(),
Dup: rootSeen,
})
anl.mu.Unlock()
}
}
jsonl := fmt.Sprintf(
"{\"event\": \"root\", \"payload\":%12d, \"stream\":%7d, %-67s, \"wiresize\":%12d }\n",
rootPayloadSize,
anl.statSummary.Streams,
fmt.Sprintf(`"cid":"%s"`, anl.formattedCid(rootBlock)),
rootDagSize,
)
anl.maybeSendEvent(NewRootJsonl, jsonl)
if rootBlock != nil && anl.cfg.emitters[emRootsJsonl] != nil {
if _, err := io.WriteString(anl.cfg.emitters[emRootsJsonl], jsonl); err != nil {
return fmt.Errorf("emitting '%s' failed: %s", emRootsJsonl, err)
}
}
}
// we are in EOF-state: if we are not expecting multiparts - we are done
if !anl.cfg.MultipartStream {
break
}
}
return
}
func (anl *Anelace) backgroundCarDataWriter() {
defer close(anl.carWriteError)
var err error
var cid, sizeVI []byte
for {
carUnit, chanOpen := <-anl.carDataQueue
if !chanOpen {
return
}
cid = carUnit.hdr.Cid()
sizeVI = encoding.AppendVarint(
sizeVI[:0],
uint64(len(cid)+carUnit.hdr.SizeBlock()),
)
if _, err = anl.carDataWriter.Write(sizeVI); err == nil {
if _, err = anl.carDataWriter.Write(cid); err == nil {
_, err = carUnit.hdr.Content().WriteTo(anl.carDataWriter)
}
}
carUnit.hdr.EvictContent()
if carUnit.region != nil {
carUnit.region.Release()
}
if err != nil {
anl.maybeSendEvent(ErrorString, err.Error())
anl.carWriteError <- err
return
}
}
}
type splitResult struct {
_ constants.Incomparabe
chunkBufRegion *qringbuf.Region
chunk anlchunker.Chunk
}
func (anl *Anelace) processStream(streamLimit int64) error {
// begin reading and filling buffer
if err := anl.qrb.StartFill(streamLimit); err != nil {
return err
}
var availableFromReader, processedFromReader int
var streamOffset int64
for {
// next 2 lines evaluate processedInRound and availableForRound from *LAST* iteration
streamOffset += int64(processedFromReader)
workRegion, readErr := anl.qrb.NextRegion(availableFromReader - processedFromReader)
if workRegion == nil || (readErr != nil && readErr != io.EOF) {
return readErr
}
availableFromReader = workRegion.Size()
processedFromReader = 0
if err := anl.chunker.instance.Split(
workRegion.Bytes(),
(readErr == io.EOF),
func(result anlchunker.Chunk) error {
sr := splitResult{
chunk: result,
chunkBufRegion: workRegion.SubRegion(processedFromReader, result.Size),
}
sr.chunkBufRegion.Reserve()
anl.streamAppend(&sr)
processedFromReader += result.Size
anl.statSummary.Dag.Payload += int64(result.Size)
return nil
},
); err != nil {
return err
}
}
}
func (anl *Anelace) streamAppend(res *splitResult) {
var ds anlblock.DataSource
var dr *qringbuf.Region
if res != nil {
dr = res.chunkBufRegion
ds.Chunk = res.chunk
ds.Content = zcpstring.WrapSlice(dr.Bytes())
}
hdr := anl.collector.AppendData(ds)
anl.curStreamOffset += int64(ds.Size)
// The leaf block processing is entirely decoupled from the collector chain,
// in order to not leak the Region lifetime management outside the framework
// Collectors call that same processor on intermediate link nodes they produce
anl.asyncWG.Add(1)
go anl.postProcessBlock(
hdr,
dr,
)
}
// This function is called as multiple "fire and forget" goroutines
// It may only try to send an error event, and it should(?) probably log.Fatal on its own
func (anl *Anelace) postProcessBlock(
hdr *anlblock.Header,
dataRegion *qringbuf.Region,
) {
defer anl.asyncWG.Done()
if constants.PerformSanityChecks {
if hdr == nil {
log.Panic("block registration of a nil block header reference")
} else if hdr.SizeBlock() != 0 && hdr.SizeCumulativeDag() == 0 {
log.Panic("block header with dag-size of 0 encountered")
}
}
atomic.AddInt64(&anl.statSummary.Dag.Size, int64(hdr.SizeBlock()))
atomic.AddInt64(&anl.statSummary.Dag.Nodes, 1)
if hdr.SizeBlock() > 0 && anl.seenBlocks != nil {
if k := seenKey(hdr); k != nil {
var seen bool
anl.mu.Lock()
if _, seen = anl.seenBlocks[*k]; !seen {
anl.seenBlocks[*k] = uniqueBlockStats{
sizeBlock: hdr.SizeBlock(),
isData: (dataRegion != nil),
}
}
anl.mu.Unlock()
if !seen && anl.carDataQueue != nil {
anl.carDataQueue <- carUnit{hdr: hdr, region: dataRegion}
return // early return to avoid double-free below
}
}
}
// NOTE - these 3 steps will be done by the car emitter ( early return above )
// if that's what the options ask for
{
// Ensure we are finished generating a CID before eviction
// FIXME - there should be a mechanism to signal we will never
// need the CID in the first place, so that no effort is spent
hdr.Cid()
// If we are holding parts of the qringbuf - we can drop them now
if dataRegion != nil {
dataRegion.Release()
}
// Once we processed a block in this function - dump all of its content too
hdr.EvictContent()
}
}