forked from timescale/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
process.go
103 lines (90 loc) · 2.47 KB
/
process.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
package main
import (
"bytes"
"fmt"
"github.com/benchant/tsbs/pkg/targets"
"time"
"github.com/valyala/fasthttp"
)
const backingOffChanCap = 100
// allows for testing
var printFn = fmt.Printf
type processor struct {
backingOffChan chan bool
backingOffDone chan struct{}
httpWriter *HTTPWriter
}
func (p *processor) Init(numWorker int, _, _ bool) {
daemonURL := daemonURLs[numWorker%len(daemonURLs)]
cfg := HTTPWriterConfig{
DebugInfo: fmt.Sprintf("worker #%d, dest url: %s", numWorker, daemonURL),
Host: daemonURL,
Database: loader.DatabaseName(),
}
w := NewHTTPWriter(cfg, consistency)
p.initWithHTTPWriter(numWorker, w)
}
func (p *processor) initWithHTTPWriter(numWorker int, w *HTTPWriter) {
p.backingOffChan = make(chan bool, backingOffChanCap)
p.backingOffDone = make(chan struct{})
p.httpWriter = w
go p.processBackoffMessages(numWorker)
}
func (p *processor) Close(_ bool) {
close(p.backingOffChan)
<-p.backingOffDone
}
func (p *processor) ProcessBatch(b targets.Batch, doLoad bool) (uint64, uint64) {
batch := b.(*batch)
// Write the batch: try until backoff is not needed.
if doLoad {
var err error
for {
if useGzip {
compressedBatch := bufPool.Get().(*bytes.Buffer)
fasthttp.WriteGzip(compressedBatch, batch.buf.Bytes())
_, err = p.httpWriter.WriteLineProtocol(compressedBatch.Bytes(), true)
// Return the compressed batch buffer to the pool.
compressedBatch.Reset()
bufPool.Put(compressedBatch)
} else {
_, err = p.httpWriter.WriteLineProtocol(batch.buf.Bytes(), false)
}
if err == errBackoff {
p.backingOffChan <- true
time.Sleep(backoff)
} else {
p.backingOffChan <- false
break
}
}
if err != nil {
fatal("Error writing: %s\n", err.Error())
}
}
metricCnt := batch.metrics
rowCnt := batch.rows
// Return the batch buffer to the pool.
batch.buf.Reset()
bufPool.Put(batch.buf)
return metricCnt, uint64(rowCnt)
}
func (p *processor) processBackoffMessages(workerID int) {
var totalBackoffSecs float64
var start time.Time
last := false
for this := range p.backingOffChan {
if this && !last {
start = time.Now()
last = true
} else if !this && last {
took := time.Now().Sub(start)
printFn("[worker %d] backoff took %.02fsec\n", workerID, took.Seconds())
totalBackoffSecs += took.Seconds()
last = false
start = time.Now()
}
}
printFn("[worker %d] backoffs took a total of %fsec of runtime\n", workerID, totalBackoffSecs)
p.backingOffDone <- struct{}{}
}