-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
155 lines (134 loc) · 3.8 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
package gravita
import (
"context"
"fmt"
"golang.org/x/sync/errgroup"
)
//LambdaUDFHandler is an interface for handling the actual state of LambdaUDF
type LambdaUDFHandler interface {
ExecuteUDF(context.Context, [][]interface{}) ([]interface{}, error)
}
// LambdaUDFHandlerFunc is a type of function that satisfies LambdaUDFHandler
type LambdaUDFHandlerFunc func(context.Context, [][]interface{}) ([]interface{}, error)
func (f LambdaUDFHandlerFunc) ExecuteUDF(ctx context.Context, args [][]interface{}) ([]interface{}, error) {
return f(ctx, args)
}
//LambdaUDFRowHandler is an interface for handling the actual state of LambdaUDF Row
type LambdaUDFRowHandler interface {
ExecuteUDFRow(context.Context, []interface{}) (interface{}, error)
}
// LambdaUDFRowHandlerFunc is a type of function that satisfies LambdaUDFRowHandler
type LambdaUDFRowHandlerFunc func(context.Context, []interface{}) (interface{}, error)
func (f LambdaUDFRowHandlerFunc) ExecuteUDFRow(ctx context.Context, args []interface{}) (interface{}, error) {
return f(ctx, args)
}
// ParallelRowProcessHandler is a LambdaUDFHandler that can be used when each row is independent and processes rows in parallel
type ParallelRowProcessHandler struct {
RowHandler LambdaUDFRowHandler
}
func (h ParallelRowProcessHandler) ExecuteUDF(ctx context.Context, args [][]interface{}) ([]interface{}, error) {
n := len(args)
results := make([]interface{}, len(args))
if h.RowHandler == nil {
return results, nil
}
var g errgroup.Group
ctx, cancel := context.WithCancel(ctx)
defer cancel()
for i := 0; i < n; i++ {
index := i
rowArgs := args[i]
g.Go(func() error {
result, err := h.RowHandler.ExecuteUDFRow(ctx, rowArgs)
if err != nil {
return err
}
results[index] = result
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return results, nil
}
type BatchProcessHandler struct {
handler LambdaUDFHandler
distinct bool
batchSize int
maxBatchCount *int
}
func NewBatchProcessHandler(batchSize int, handler LambdaUDFHandler) *BatchProcessHandler {
return &BatchProcessHandler{
handler: handler,
distinct: false,
batchSize: batchSize,
}
}
func (h *BatchProcessHandler) Distinct(enable bool) {
h.distinct = enable
}
func (h *BatchProcessHandler) BatchSize(s int) {
h.batchSize = s
}
func (h *BatchProcessHandler) MaxBatchCount(m int) {
h.maxBatchCount = &m
}
func (h *BatchProcessHandler) ExecuteUDF(ctx context.Context, args [][]interface{}) ([]interface{}, error) {
results := make([]interface{}, len(args))
batchArgs := make([][]interface{}, 0, h.batchSize)
batchKeys := make([]string, 0, h.batchSize)
batchIndexes := make(map[string][]int, h.batchSize)
for i, rowArgs := range args {
var key string
if h.distinct {
key = fmt.Sprint(rowArgs)
} else {
key = fmt.Sprintf("%d", i)
}
indexes, ok := batchIndexes[key]
if !ok {
batchArgs = append(batchArgs, rowArgs)
batchKeys = append(batchKeys, key)
}
batchIndexes[key] = append(indexes, i)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var g errgroup.Group
batchCount := 0
for i := h.batchSize; len(batchArgs) > 0; {
if len(batchArgs) < h.batchSize {
i = len(batchArgs)
}
targetArgs := batchArgs[:i]
batchArgs = batchArgs[i:]
targetKeys := batchKeys[:i]
batchKeys = batchKeys[i:]
g.Go(func() error {
batchResults, err := h.handler.ExecuteUDF(ctx, targetArgs)
if err != nil {
return err
}
for j, result := range batchResults {
key := targetKeys[j]
indexes, ok := batchIndexes[key]
if !ok {
continue
}
for _, index := range indexes {
results[index] = result
}
}
return nil
})
batchCount++
if h.maxBatchCount != nil && batchCount >= *h.maxBatchCount {
break
}
}
if err := g.Wait(); err != nil {
return nil, err
}
return results, nil
}