/
pickle.go
197 lines (174 loc) · 5.57 KB
/
pickle.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
package input
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"math/big"
"net"
"github.com/graphite-ng/carbon-relay-ng/badmetrics"
"github.com/graphite-ng/carbon-relay-ng/cfg"
"github.com/graphite-ng/carbon-relay-ng/table"
"github.com/graphite-ng/carbon-relay-ng/validate"
ogorek "github.com/kisielk/og-rek"
m20 "github.com/metrics20/go-metrics20/carbon20"
)
type Pickle struct {
config cfg.Config
bad *badmetrics.BadMetrics
table *table.Table
}
func NewPickle(config cfg.Config, addr string, tbl *table.Table, bad *badmetrics.BadMetrics) (net.Listener, error) {
l, err := listen(addr, &Pickle{config, bad, tbl})
if err != nil {
return nil, err
}
return l, nil
}
func (p *Pickle) Handle(c net.Conn) {
defer c.Close()
// TODO c.SetTimeout(60e9)
r := bufio.NewReaderSize(c, 4096)
log.Debug("pickle.go: entering ReadLoop...")
ReadLoop:
for {
// Note that everything in this loop should proceed as fast as it can
// so we're not blocked and can keep processing
// so the validation, the pipeline initiated via table.Dispatch(), etc
// must never block.
log.Debug("pickle.go: detecting payload length with binary.Read...")
var length uint32
err := binary.Read(r, binary.BigEndian, &length)
if err != nil {
if io.EOF != err {
log.Error("couldn't read payload length: " + err.Error())
}
log.Debug("pickle.go: detected EOF while detecting payload length with binary.Read, nothing more to read, breaking")
break
}
log.Debug(fmt.Sprintf("pickle.go: done detecting payload length with binary.Read, length is %d", int(length)))
lengthTotal := int(length)
lengthRead := 0
payload := make([]byte, lengthTotal, lengthTotal)
for {
log.Debug("pickle.go: reading payload...")
tmpLengthRead, err := r.Read(payload[lengthRead:])
if err != nil {
log.Error("couldn't read payload: " + err.Error())
break ReadLoop
}
lengthRead += tmpLengthRead
if lengthRead == lengthTotal {
log.Debug("pickle.go: done reading payload")
break
}
if lengthRead > lengthTotal {
log.Error(fmt.Sprintf("expected to read %d bytes, but read %d", length, lengthRead))
break ReadLoop
}
}
decoder := ogorek.NewDecoder(bytes.NewBuffer(payload))
log.Debug("pickle.go: decoding pickled data...")
rawDecoded, err := decoder.Decode()
if err != nil {
if io.ErrUnexpectedEOF != err {
log.Error("error reading pickled data " + err.Error())
}
log.Debug("pickle.go: detected ErrUnexpectedEOF while decoding pickled data, nothing more to decode, breaking")
break
}
log.Debug("pickle.go: done decoding pickled data")
log.Debug("pickle.go: checking the type of pickled data...")
decoded, ok := rawDecoded.([]interface{})
if !ok {
log.Error(fmt.Sprintf("Unrecognized type %T for pickled data", rawDecoded))
break
}
log.Debug("pickle.go: done checking the type of pickled data")
log.Debug("pickle.go: entering ItemLoop...")
ItemLoop:
for _, rawItem := range decoded {
numIn.Inc(1)
log.Debug("pickle.go: doing high-level validation of unpickled item and data...")
item, ok := rawItem.(ogorek.Tuple)
if !ok {
log.Error(fmt.Sprintf("Unrecognized type %T for item", rawItem))
numInvalid.Inc(1)
continue
}
if len(item) != 2 {
log.Error(fmt.Sprintf("item length must be 2, got %d", len(item)))
numInvalid.Inc(1)
continue
}
metric, ok := item[0].(string)
if !ok {
log.Error(fmt.Sprintf("item metric must be a string, got %T", item[0]))
numInvalid.Inc(1)
continue
}
data, ok := item[1].(ogorek.Tuple)
if !ok {
log.Error(fmt.Sprintf("item data must be an array, got %T", item[1]))
numInvalid.Inc(1)
continue
}
if len(data) != 2 {
log.Error(fmt.Sprintf("item data length must be 2, got %d", len(data)))
numInvalid.Inc(1)
continue
}
log.Debug("pickle.go: done doing high-level validation of unpickled item and data")
var value string
switch data[1].(type) {
case string:
value = data[1].(string)
case uint8, uint16, uint32, uint64, int8, int16, int32, int64:
value = fmt.Sprintf("%d", data[1])
case float32, float64:
value = fmt.Sprintf("%f", data[1])
default:
log.Error(fmt.Sprintf("Unrecognized type %T for value", data[1]))
numInvalid.Inc(1)
continue ItemLoop
}
var timestamp string
switch data[0].(type) {
case string:
timestamp = data[0].(string)
case uint8, uint16, uint32, uint64, int8, int16, int32, int64, (*big.Int):
timestamp = fmt.Sprintf("%d", data[0])
case float32, float64:
timestamp = fmt.Sprintf("%.0f", data[0])
default:
log.Error(fmt.Sprintf("Unrecognized type %T for timestamp", data[0]))
numInvalid.Inc(1)
continue ItemLoop
}
buf := []byte(metric + " " + value + " " + timestamp)
log.Debug("pickle.go: passing unpickled metric to m20 Packet validator...")
key, val, ts, err := m20.ValidatePacket(buf, p.config.Validation_level_legacy.Level, p.config.Validation_level_m20.Level)
if err != nil {
log.Debug("pickle.go: metric failed to pass m20 Packet validation!")
p.bad.Add(key, buf, err)
numInvalid.Inc(1)
continue
}
if p.config.Validate_order {
log.Debug("pickle.go: order validation enabled, performing order validation...")
err = validate.Ordered(key, ts)
if err != nil {
log.Debug("pickle.go: order validation failed!")
p.bad.Add(key, buf, err)
numOutOfOrder.Inc(1)
continue
}
}
log.Debug("pickle.go: all good, dispatching metrics buffer")
p.table.Dispatch(buf, val, ts)
log.Debug("pickle.go: exiting ItemLoop")
}
log.Debug("pickle.go: exiting ReadLoop")
}
}