-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploader.go
More file actions
352 lines (298 loc) · 8.19 KB
/
uploader.go
File metadata and controls
352 lines (298 loc) · 8.19 KB
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
package main
import (
"context"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/exp/slices"
"golang.org/x/sync/errgroup"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.ntppool.org/common/logger"
"go.ntppool.org/common/tracing"
"go.ntppool.org/common/ulid"
"go.ntppool.org/common/version"
)
const workerCount = 2
type uploadSet struct {
client *httpClientManager
fl *fileLock
uploadTimeHistogram prometheus.Histogram
}
func uploader(ctx context.Context, clientManager *httpClientManager, logpath string, metrics prometheus.Registerer) error {
log := logger.Setup()
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
g, ctx := errgroup.WithContext(ctx)
// todo: detect if the list is full and rescan directory
processCh := make(chan string, 10000)
metrics.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Name: "queue_internal_count",
Help: "Files in the internal queue to be processed",
}, func() float64 {
size := len(processCh)
return float64(size)
}))
metrics.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Name: "queue_disk_count",
Help: "Files in the disk queue to be processed",
}, func() float64 {
dir, err := os.ReadDir(logpath)
if err != nil {
return -1
}
count := 0
for _, fi := range dir {
if isAvroFileName(fi.Name()) {
count++
}
}
return float64(count)
}))
g.Go(func() error {
for {
select {
case <-ctx.Done():
log.Info("fswatcher shutting down")
return nil
case event, ok := <-watcher.Events:
if !ok {
return fmt.Errorf("got not ok event")
}
// log.Info("event info", "event", event)
// if event.Has(fsnotify.Write) {
// log.Info("modified file", "name", event.Name)
// }
if event.Has(fsnotify.Rename) || event.Has(fsnotify.Create) {
if isAvroFileName(event.Name) {
processCh <- event.Name
}
}
case err, ok := <-watcher.Errors:
if !ok {
return nil
}
log.Error("fswatcher error", "err", err)
return err
}
}
})
err = watcher.Add(logpath)
if err != nil {
return err
}
uploadOkCounter := prometheus.NewCounter(prometheus.CounterOpts{
Name: "upload_success_total",
Help: "Files uploaded ok",
})
metrics.MustRegister(uploadOkCounter)
uploadErrorCounter := prometheus.NewCounter(prometheus.CounterOpts{
Name: "upload_failure_total",
Help: "File upload failures",
})
metrics.MustRegister(uploadErrorCounter)
uploadTimeHistogram := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "upload_duration_seconds",
Help: "Upload time for successful requests",
Buckets: []float64{.5, 1, 1.5, 2, 2.5, 3, 3.5, 5, 10},
})
metrics.MustRegister(uploadTimeHistogram)
up := uploadSet{
client: clientManager,
fl: NewFileLock(),
uploadTimeHistogram: uploadTimeHistogram,
}
g.Go(func() error {
first := true
// start with the new files for a few moments
time.Sleep(15 * time.Second)
for {
if !(first || len(processCh) < 2) {
select {
case <-ctx.Done():
return nil
case <-time.After(20 * time.Second):
continue // check queue length again
}
}
dir, err := os.ReadDir(logpath)
if err != nil {
return err
}
for _, fi := range dir {
files := []string{}
if isAvroFileName(fi.Name()) {
files = append(files, fi.Name())
}
slices.Sort(files)
for _, n := range files {
fullName := path.Join(logpath, n)
if !up.fl.IsLocked(fullName) {
processCh <- fullName
}
}
}
first = false
select {
case <-ctx.Done():
return nil
case <-time.After(10 * time.Minute):
}
}
})
for workerN := 1; workerN <= workerCount; workerN++ {
workerN := workerN
log.Info("starting upload worker", "n", workerN)
g.Go(func() error {
log := log.With("n", workerN)
for {
select {
case <-ctx.Done():
return nil
case p := <-processCh:
log.Debug("change to file", "path", p)
id, err := ulid.MakeULID(time.Now())
if err != nil {
log.Error("could not generate ulid", "err", err)
}
log := log.With("id", id.String())
err = up.upload(ctx, log, id.String(), p)
if err != nil {
log.Error("error uploading", "path", p, "err", err)
uploadErrorCounter.Inc()
up.client.Client().CloseIdleConnections()
continue
}
uploadOkCounter.Inc()
}
}
})
}
return g.Wait()
}
func isAvroFileName(name string) bool {
return strings.HasSuffix(name, ".avro") && len(name) > 5
}
func (up *uploadSet) upload(ctx context.Context, log *slog.Logger, id string, filePath string) error {
ctx, span := tracing.Tracer().Start(ctx, "upload", trace.WithSpanKind(trace.SpanKindClient))
defer span.End()
log = log.With("traceid", span.SpanContext().TraceID())
_, baseName := path.Split(filePath)
span.SetAttributes(attribute.String("file", baseName))
log = log.With("file", baseName)
if !up.fl.Lock(filePath) {
err := fmt.Errorf("%s is already being uploaded", baseName)
span.RecordError(err)
return err
}
defer up.fl.Unlock(filePath)
timer := prometheus.NewTimer(up.uploadTimeHistogram)
fh, err := os.Open(filePath)
if err != nil {
span.RecordError(err)
return err
}
defer fh.Close()
stat, err := fh.Stat()
if err != nil {
span.RecordError(err)
return err
}
span.SetAttributes(attribute.Int64("size", stat.Size()))
reqCtx, cancel := context.WithTimeout(ctx, time.Second*120) // todo: get from config
defer cancel()
sha := sha256.New()
_, err = io.Copy(sha, fh)
if err != nil {
return err
}
_, err = fh.Seek(0, io.SeekStart)
if err != nil {
return err
}
// Get target URL from SRV pool (handles priority, weight, and health)
targetURL := up.client.GetTargetURL()
targetAddr := GetTargetAddress(targetURL)
uploadURL, err := url.JoinPath(targetURL, "api/v1/geodns/avro")
if err != nil {
span.RecordError(err)
return err
}
span.SetAttributes(attribute.String("target", targetAddr))
log = log.With("server", targetAddr)
// Record that we're making a request to this target
up.client.TargetPool().RecordRequest(targetAddr)
req, err := http.NewRequestWithContext(
reqCtx, "POST",
uploadURL,
fh,
)
if err != nil {
span.RecordError(err)
return err
}
req.Header.Add("Request-ID", id)
req.Header.Add("User-Agent", "logferry/"+version.Version())
req.Header.Add("file-size", strconv.FormatInt(stat.Size(), 10))
req.Header.Add("content-sha256", base64.StdEncoding.EncodeToString(sha.Sum(nil)))
cl := up.client.Client()
resp, err := cl.Do(req)
if err != nil {
// Mark target as unhealthy on connection error
up.client.TargetPool().MarkUnhealthy(targetAddr, err)
cl.CloseIdleConnections()
span.RecordError(err)
return err
}
if resp.Body != nil {
b, err := io.ReadAll(resp.Body)
if err != nil {
span.RecordError(fmt.Errorf("error reading response body: %w", err))
log.Warn("error reading response body", "err", err)
}
if len(b) > 0 {
log.Warn("unexpected bytes in response body (expected empty)", "count", len(b), "data", string(b))
}
resp.Body.Close()
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
// Mark target as healthy on successful response
up.client.TargetPool().MarkHealthy(targetAddr)
duration := timer.ObserveDuration()
log.Info("uploaded", "code", resp.StatusCode, "duration", duration)
} else if resp.StatusCode >= 500 {
// Mark target as unhealthy on 5xx errors
err := fmt.Errorf("upload error %d (%s)", resp.StatusCode, resp.Status)
up.client.TargetPool().MarkUnhealthy(targetAddr, err)
log.Warn("upload error", "code", resp.StatusCode, "status", resp.Status)
span.RecordError(err)
return err
} else {
// 4xx errors are client errors, don't affect target health
log.Warn("upload error", "code", resp.StatusCode, "status", resp.Status)
err := fmt.Errorf("upload error %d (%s)", resp.StatusCode, resp.Status)
span.RecordError(err)
return err
}
err = os.Remove(filePath)
if err != nil {
err = fmt.Errorf("error removing log: %w", err)
span.RecordError(err)
return err
}
return nil
}