forked from fluent/fluentd-forwarder
/
output.go
285 lines (269 loc) · 7.35 KB
/
output.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
//
// Fluentd Forwarder
//
// Copyright (C) 2014 Treasure Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package fluentd_forwarder
import (
"bytes"
logging "github.com/op/go-logging"
"github.com/ugorji/go/codec"
"io"
"math/rand"
"net"
"os"
"reflect"
"sync"
"sync/atomic"
"time"
)
var randSource = rand.NewSource(time.Now().UnixNano())
type ForwardOutput struct {
logger *logging.Logger
codec *codec.MsgpackHandle
bind string
retryInterval time.Duration
connectionTimeout time.Duration
writeTimeout time.Duration
enc *codec.Encoder
conn net.Conn
flushInterval time.Duration
wg sync.WaitGroup
journalGroup JournalGroup
journal Journal
emitterChan chan FluentRecordSet
spoolerShutdownChan chan struct{}
isShuttingDown uintptr
completion sync.Cond
hasShutdownCompleted bool
metadata string
}
func encodeRecordSet(encoder *codec.Encoder, recordSet FluentRecordSet) error {
v := []interface{}{recordSet.Tag, recordSet.Records}
err := encoder.Encode(v)
if err != nil {
return err
}
return err
}
func (output *ForwardOutput) ensureConnected() error {
if output.conn == nil {
output.logger.Notice("Connecting to %s...", output.bind)
conn, err := net.DialTimeout("tcp", output.bind, output.connectionTimeout)
if err != nil {
output.logger.Error("Failed to connect to %s (reason: %s)", output.bind, err.Error())
return err
} else {
output.conn = conn
}
}
return nil
}
func (output *ForwardOutput) sendBuffer(buf []byte) error {
for len(buf) > 0 {
if atomic.LoadUintptr(&output.isShuttingDown) != 0 {
break
}
err := output.ensureConnected()
if err != nil {
output.logger.Info("Will be retried in %s", output.retryInterval.String())
time.Sleep(output.retryInterval)
continue
}
startTime := time.Now()
if output.writeTimeout == 0 {
output.conn.SetWriteDeadline(time.Time{})
} else {
output.conn.SetWriteDeadline(startTime.Add(output.writeTimeout))
}
n, err := output.conn.Write(buf)
buf = buf[n:]
if err != nil {
output.logger.Error("Failed to flush buffer (reason: %s, left: %d bytes)", err.Error(), len(buf))
err_, ok := err.(net.Error)
if !ok || (!err_.Timeout() && !err_.Temporary()) {
output.conn.Close()
output.conn = nil
continue
}
}
if n > 0 {
elapsed := time.Now().Sub(startTime)
output.logger.Info("Forwarded %d bytes in %f seconds (%d bytes left)\n", n, elapsed.Seconds(), len(buf))
}
}
return nil
}
func (output *ForwardOutput) spawnSpooler() {
output.logger.Notice("Spawning spooler")
output.wg.Add(1)
go func() {
ticker := time.NewTicker(output.flushInterval)
defer func() {
ticker.Stop()
output.journal.Dispose()
if output.conn != nil {
output.conn.Close()
}
output.conn = nil
output.wg.Done()
}()
output.logger.Notice("Spooler started")
outer:
for {
select {
case <-ticker.C:
buf := make([]byte, 16777216)
output.logger.Notice("Flushing...")
err := output.journal.Flush(func(chunk JournalChunk) interface{} {
defer chunk.Dispose()
output.logger.Info("Flushing chunk %s", chunk.String())
reader, err := chunk.Reader()
defer reader.Close()
if err != nil {
return err
}
for {
n, err := reader.Read(buf)
if n > 0 {
err_ := output.sendBuffer(buf[:n])
if err_ != nil {
return err
}
}
if err != nil {
if err == io.EOF {
break
} else {
return err
}
}
}
return nil
})
if err != nil {
output.logger.Error("Error during reading from the journal: %s", err.Error())
}
case <-output.spoolerShutdownChan:
break outer
}
}
output.logger.Notice("Spooler ended")
}()
}
func (output *ForwardOutput) spawnEmitter() {
output.logger.Notice("Spawning emitter")
output.wg.Add(1)
go func() {
defer func() {
output.spoolerShutdownChan <- struct{}{}
output.wg.Done()
}()
output.logger.Notice("Emitter started")
buffer := bytes.Buffer{}
for recordSet := range output.emitterChan {
buffer.Reset()
encoder := codec.NewEncoder(&buffer, output.codec)
addMetadata(&recordSet, output.metadata)
err := encodeRecordSet(encoder, recordSet)
if err != nil {
output.logger.Error("%s", err.Error())
continue
}
output.logger.Debug("Emitter processed %d entries", len(recordSet.Records))
output.journal.Write(buffer.Bytes())
}
output.logger.Notice("Emitter ended")
}()
}
func (output *ForwardOutput) Emit(recordSets []FluentRecordSet) error {
defer func() {
recover()
}()
for _, recordSet := range recordSets {
output.emitterChan <- recordSet
}
return nil
}
func (output *ForwardOutput) String() string {
return "output"
}
func (output *ForwardOutput) Stop() {
if atomic.CompareAndSwapUintptr(&output.isShuttingDown, 0, 1) {
close(output.emitterChan)
}
}
func (output *ForwardOutput) WaitForShutdown() {
output.completion.L.Lock()
if !output.hasShutdownCompleted {
output.completion.Wait()
}
output.completion.L.Unlock()
}
func (output *ForwardOutput) Start() {
syncCh := make(chan struct{})
go func() {
<-syncCh
output.wg.Wait()
err := output.journalGroup.Dispose()
if err != nil {
output.logger.Error("%s", err.Error())
}
output.completion.L.Lock()
output.hasShutdownCompleted = true
output.completion.Broadcast()
output.completion.L.Unlock()
}()
output.spawnSpooler()
output.spawnEmitter()
syncCh <- struct{}{}
}
func NewForwardOutput(logger *logging.Logger, bind string, retryInterval time.Duration, connectionTimeout time.Duration, writeTimeout time.Duration, flushInterval time.Duration, journalGroupPath string, maxJournalChunkSize int64, metadata string) (*ForwardOutput, error) {
_codec := codec.MsgpackHandle{}
_codec.MapType = reflect.TypeOf(map[string]interface{}(nil))
_codec.RawToString = false
_codec.StructToArray = true
journalFactory := NewFileJournalGroupFactory(
logger,
randSource,
time.Now,
".log",
os.FileMode(0600),
maxJournalChunkSize,
)
output := &ForwardOutput{
logger: logger,
codec: &_codec,
bind: bind,
retryInterval: retryInterval,
connectionTimeout: connectionTimeout,
writeTimeout: writeTimeout,
wg: sync.WaitGroup{},
flushInterval: flushInterval,
emitterChan: make(chan FluentRecordSet),
spoolerShutdownChan: make(chan struct{}),
isShuttingDown: 0,
completion: sync.Cond{L: &sync.Mutex{}},
hasShutdownCompleted: false,
metadata: metadata,
}
journalGroup, err := journalFactory.GetJournalGroup(journalGroupPath, output)
if err != nil {
return nil, err
}
output.journalGroup = journalGroup
output.journal = journalGroup.GetJournal("output")
return output, nil
}