-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
217 lines (170 loc) · 4.92 KB
/
handler.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
package bqloader
import (
"context"
"regexp"
"sync"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
"golang.org/x/text/encoding"
"golang.org/x/text/transform"
"golang.org/x/xerrors"
)
const defaultBatchSize = 10000
// Handler defines how to handle events which match specified pattern.
type Handler struct {
// Name is the handler's name.
Name string
Pattern *regexp.Regexp
Encoding encoding.Encoding
Parser Parser
Notifier Notifier
Projector Projector
SkipLeadingRows uint
Preprocessor Preprocessor
// BatchSize specifies how much records are processed in a groutine.
// Default is 10000.
BatchSize int
// Project specifies GCP project name of destination BigQuery table.
Project string
// Dataset specifies BigQuery dataset ID of destination table
Dataset string
// Table specifies BigQuery table ID as destination.
Table string
Extractor Extractor
Loader Loader
semaphore chan struct{}
}
// Projector transforms source records into records for destination.
type Projector func(context.Context, []string) ([]string, error)
// Preprocessor preprocesses event and store data into a map.
type Preprocessor func(context.Context, Event) (context.Context, error)
func (h *Handler) match(name string) bool {
return h.Pattern != nil && h.Pattern.MatchString(name)
}
// SetConcurrency sets handler's concurrency directly.
// Normally set concurrency to BQLoader with WithConcurrency option.
func (h *Handler) SetConcurrency(n int) {
h.semaphore = make(chan struct{}, n)
}
// Handle handles events.
func (h *Handler) Handle(ctx context.Context, e Event) error {
ctx = withHandlerStartedTime(ctx)
l := log.Ctx(ctx)
l = h.logger(ctx, l)
ctx = l.WithContext(ctx)
l.Info().Msgf("handler %s started to handle an event", h.Name)
defer func() {
now := time.Now()
e := l.Info().Time("handlerFinished", now)
if t, ok := handlerStartedTimeFrom(ctx); ok {
e.TimeDiff("handlerElapsed", now, t)
}
e.Msgf("handler %s finished to handle an event", h.Name)
}()
err := h.process(ctx, e)
if err != nil {
err = xerrors.Errorf("failed to handle: %w", err)
l.Err(err).Msg(err.Error())
}
if h.Notifier != nil {
res := &Result{Event: e, Handler: h, Error: err}
if nerr := h.Notifier.Notify(ctx, res); nerr != nil {
nerr = xerrors.Errorf("failed to notify: %w", nerr)
l.Err(nerr).Msg(nerr.Error())
}
}
return err
}
func (h *Handler) process(ctx context.Context, e Event) error {
ctx, err := h.preprocess(ctx, e)
if err != nil {
return xerrors.Errorf("failed to preprocess: %w", err)
}
r, closer, err := h.Extractor.Extract(ctx, e)
if err != nil {
return xerrors.Errorf("failed to extract: %w", err)
}
defer closer()
if h.Encoding != nil {
r = transform.NewReader(r, h.Encoding.NewDecoder())
}
source, err := h.Parser(ctx, r)
if err != nil {
return xerrors.Errorf("failed to parse: %w", err)
}
records, err := h.project(ctx, source[h.SkipLeadingRows:])
if err != nil {
return xerrors.Errorf("failed to project: %w", err)
}
if err := h.Loader.Load(ctx, records); err != nil {
return xerrors.Errorf("failed to load: %w", err)
}
return nil
}
func (h *Handler) preprocess(ctx context.Context, e Event) (context.Context, error) {
if h.Preprocessor == nil {
return ctx, nil
}
return h.Preprocessor(ctx, e)
}
func (h *Handler) project(ctx context.Context, source [][]string) ([][]string, error) {
records := [][]string{}
mu := sync.Mutex{}
eg := errgroup.Group{}
numBatches := h.calcBatches(len(source))
for i := 0; i < numBatches; i++ {
startLine := h.BatchSize * i
endLine := h.BatchSize * (i + 1)
if endLine > len(source) {
endLine = len(source)
}
h.semaphore <- struct{}{}
eg.Go(func() error {
defer func() { <-h.semaphore }()
batchRecords := [][]string{}
for j := startLine; j < endLine; j++ {
record, err := h.Projector(ctx, source[j])
if err != nil {
return xerrors.Errorf("failed to project row %d (line %d): %w", j, uint(j)+h.SkipLeadingRows, err)
}
if record != nil {
batchRecords = append(batchRecords, record)
}
}
mu.Lock()
defer mu.Unlock()
records = append(records, batchRecords...)
return nil
})
}
if err := eg.Wait(); err != nil {
return nil, xerrors.Errorf("failed to wait errgroup: %w", err)
}
return records, nil
}
func (h *Handler) logger(ctx context.Context, l *zerolog.Logger) *zerolog.Logger {
lctx := l.With()
if t, ok := handlerStartedTimeFrom(ctx); ok {
lctx = lctx.Time("handlerStarted", t)
}
d := zerolog.Dict().
Str("name", h.Name).
Uint("skipLeadingRows", h.SkipLeadingRows).
Str("project", h.Project).
Str("dataset", h.Dataset).
Str("table", h.Table)
if h.Pattern != nil {
d = d.Str("pattern", h.Pattern.String())
}
logger := lctx.Dict("handler", d).Logger()
return &logger
}
func (h *Handler) calcBatches(length int) int {
r := length / h.BatchSize
if length%h.BatchSize != 0 {
r++
}
return r
}