/
writer.go
170 lines (158 loc) · 3.78 KB
/
writer.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
package ingest
import (
"time"
"github.com/prometheus/client_golang/prometheus"
)
// NewWriter converts a Log to an io.Writer. Active segments are rotated
// once sz bytes are written, or every d if the segment is nonempty.
func NewWriter(
log Log,
d time.Duration,
sz int,
bytes, records, syncs prometheus.Counter,
age, size prometheus.Histogram,
) (*Writer, error) {
curr, err := log.Create()
if err != nil {
return nil, err
}
w := &Writer{
log: log,
curr: curr,
cursz: 0,
maxsz: sz,
action: make(chan func()),
bytes: bytes,
records: records,
syncs: syncs,
age: age,
size: size,
stop: make(chan chan struct{}),
}
go w.loop(d)
return w, nil
}
// Writer implements io.Writer on top of a Log.
type Writer struct {
log Log
curr WriteSegment
curts time.Time // of first write
cursz int
maxsz int
action chan func()
bytes prometheus.Counter
records prometheus.Counter
syncs prometheus.Counter
age prometheus.Histogram
size prometheus.Histogram
stop chan chan struct{}
}
// Write implements io.Writer.
func (w *Writer) Write(p []byte) (int, error) {
type res struct {
n int
err error
}
c := make(chan res)
w.action <- func() {
n, err := w.curr.Write(p)
if err != nil {
c <- res{n, err}
return
}
if w.curts.IsZero() {
w.curts = time.Now()
}
w.bytes.Add(float64(n))
w.records.Inc()
w.cursz += n
if w.cursz >= w.maxsz {
w.closeRotate()
}
c <- res{n, err}
}
r := <-c
return r.n, r.err
}
// Sync the current segment to disk.
func (w *Writer) Sync() error {
c := make(chan error)
w.action <- func() {
c <- w.curr.Sync()
w.syncs.Inc()
}
return <-c
}
// Stop terminates the Writer. No further writes are allowed.
func (w *Writer) Stop() {
c := make(chan struct{})
w.stop <- c
<-c
}
// loop serializes the events that hit the Writer. That includes user requests,
// like Write, Sync, and Stop; and the time.Ticker that controls time-based
// segment rotation.
//
// We need this single point of synchronization only because of the time-based
// segment rotation, which is asynchronous. Without that, we could control
// everything pretty elegantly from the Write method via a simple mutex.
func (w *Writer) loop(d time.Duration) {
rotate := time.NewTicker(d)
defer rotate.Stop()
for {
select {
case f := <-w.action:
f()
case <-rotate.C:
// Note we invoke closeRotate every d, even if it's been only a
// short while since the last flush to disk. This could be optimized
// by only starting the timer once bytes are written and resetting
// it with every segment rotation, at the cost of some garbage
// generation. Profiling data is necessary.
w.closeRotate()
case c := <-w.stop:
w.closeOnly()
w.stop = nil
close(c)
return
}
}
}
func (w *Writer) closeRotate() {
if w.cursz <= 0 {
// closeRotate is called, but the segment is empty!
// We can just keep it open, instead of cycling it.
return
}
if w.curr != nil {
if err := w.curr.Close(); err != nil {
panic(err)
}
w.age.Observe(time.Since(w.curts).Seconds())
w.size.Observe(float64(w.cursz))
}
next, err := w.log.Create()
if err != nil {
panic(err)
}
w.curr, w.curts, w.cursz = next, time.Time{}, 0
}
func (w *Writer) closeOnly() {
// This function exists because we need to rotate the active segment away
// when the user requests a stop. That is, we shouldn't leave an active
// segment lying around.
if w.curr != nil {
if w.cursz <= 0 {
// closeOnly is called, but the segment is empty!
// Delete the active segment instead of syncing it.
w.curr.Delete()
} else {
if err := w.curr.Close(); err != nil {
panic(err)
}
w.age.Observe(time.Since(w.curts).Seconds())
w.size.Observe(float64(w.cursz))
}
w.curr, w.curts, w.cursz = nil, time.Time{}, 0
}
}