-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_engine.go
More file actions
312 lines (281 loc) · 6.14 KB
/
Copy pathruntime_engine.go
File metadata and controls
312 lines (281 loc) · 6.14 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
package batchflow
import (
"context"
"errors"
"fmt"
"hash/fnv"
"sync"
"sync/atomic"
"time"
)
// Flow is the stable public runtime surface for the v2 module.
type Flow = RuntimeEngine
// RuntimeEngine owns sharding, routing, backpressure and memory protection.
type RuntimeEngine struct {
cfg RuntimeConfig
shards []*BatchFlow
rr atomic.Uint64
closed atomic.Bool
done chan struct{}
once sync.Once
runErrMu sync.RWMutex
runErr error
}
// New creates the converged runtime flow.
func New(ctx context.Context, cfg Config) (*Flow, error) {
return NewRuntimeEngine(ctx, cfg)
}
func NewRuntimeEngine(ctx context.Context, cfg Config) (*RuntimeEngine, error) {
if cfg.Executor == nil {
return nil, &ConfigError{Field: "Executor", Cause: errors.New("must not be nil")}
}
if err := cfg.Pipeline.Validate(); err != nil {
return nil, err
}
pipelineCfg := cfg.Pipeline.withDefaults()
runtimeCfg := cfg.Runtime.withDefaults()
engine := &RuntimeEngine{
cfg: runtimeCfg,
shards: make([]*BatchFlow, 0, int(runtimeCfg.ShardCount)),
done: make(chan struct{}),
}
for i := uint32(0); i < runtimeCfg.ShardCount; i++ {
engine.shards = append(engine.shards, newBatchFlow(ctx, pipelineCfg, cfg.Executor))
}
go func() {
var wg sync.WaitGroup
wg.Add(len(engine.shards))
for _, shard := range engine.shards {
go func(s *BatchFlow) {
defer wg.Done()
<-s.Done()
}(shard)
}
wg.Wait()
close(engine.done)
}()
go func() {
<-ctx.Done()
engine.closed.Store(true)
}()
return engine, nil
}
func (e *RuntimeEngine) Submit(ctx context.Context, req *Request) error {
if err := ctx.Err(); err != nil {
return err
}
if e == nil || len(e.shards) == 0 {
return ErrInvalidSchema
}
if e.closed.Load() {
return context.Canceled
}
if req == nil {
return ErrEmptyRequest
}
if err := validateRequestForSubmit(req); err != nil {
return err
}
if err := e.waitMemoryLimit(ctx); err != nil {
return err
}
idx := e.pickShard(req)
if err := e.waitBackpressure(ctx, idx); err != nil {
return err
}
return e.shards[idx].Submit(ctx, req)
}
func (e *RuntimeEngine) Close() error {
if e == nil {
return nil
}
e.once.Do(func() {
e.closed.Store(true)
var wg sync.WaitGroup
errCh := make(chan error, len(e.shards))
for _, shard := range e.shards {
wg.Add(1)
go func(s *BatchFlow) {
defer wg.Done()
if err := s.Close(); err != nil {
errCh <- err
}
}(shard)
}
wg.Wait()
close(errCh)
var errs []error
for err := range errCh {
errs = append(errs, err)
}
e.setRunErr(errors.Join(errs...))
})
return e.Wait()
}
func (e *RuntimeEngine) Wait() error {
if e == nil {
return nil
}
<-e.done
return e.getRunErr()
}
func (e *RuntimeEngine) Done() <-chan struct{} {
if e == nil {
ch := make(chan struct{})
close(ch)
return ch
}
return e.done
}
func (e *RuntimeEngine) ErrorChan(size int) <-chan error {
out := make(chan error, size)
if e == nil || len(e.shards) == 0 {
close(out)
return out
}
go func() {
var wg sync.WaitGroup
for _, shard := range e.shards {
wg.Add(1)
go func(ch <-chan error) {
defer wg.Done()
for {
select {
case err, ok := <-ch:
if !ok {
return
}
if err != nil {
select {
case out <- err:
case <-e.Done():
return
}
}
case <-e.Done():
return
}
}
}(shard.ErrorChan(size))
}
wg.Wait()
close(out)
}()
return out
}
func (e *RuntimeEngine) pickShard(req *Request) int {
n := len(e.shards)
if n <= 1 {
return 0
}
switch e.cfg.Routing {
case ShardRoutingRoundRobin:
return int(e.rr.Add(1)-1) % n
case ShardRoutingLeastLoaded:
return e.leastLoadedShard()
case ShardRoutingHash:
fallthrough
default:
key := e.requestKey(req)
return int(key % uint64(n))
}
}
func (e *RuntimeEngine) leastLoadedShard() int {
best := 0
bestDepth := e.queueDepth(0)
for i := 1; i < len(e.shards); i++ {
depth := e.queueDepth(i)
if depth < bestDepth {
best = i
bestDepth = depth
}
}
return best
}
func (e *RuntimeEngine) requestKey(req *Request) uint64 {
if e.cfg.ShardKeyFunc != nil {
return e.cfg.ShardKeyFunc(req)
}
h := fnv.New64a()
schema := req.Schema()
_, _ = h.Write([]byte(schema.Name()))
if sqlSchema, ok := schema.(*SQLSchema); ok {
for _, col := range sqlSchema.operationConfig.ConflictColumns {
_, _ = h.Write([]byte("|"))
_, _ = h.Write([]byte(col))
_, _ = h.Write([]byte("="))
_, _ = h.Write([]byte(fmt.Sprintf("%#v", req.columns[col])))
}
}
return h.Sum64()
}
func (e *RuntimeEngine) waitBackpressure(ctx context.Context, shard int) error {
bp := e.cfg.Backpressure.withDefaults()
if !bp.Enabled || shard < 0 || shard >= len(e.shards) {
return nil
}
high := bp.HighWatermark
if high <= 0 {
high = cap(e.shards[shard].pipeline.DataChan())
}
if high <= 0 || e.queueDepth(shard) < high {
return nil
}
switch bp.Mode {
case BackpressureReject:
return ErrBackpressure
case BackpressureTimeout:
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, bp.Timeout)
defer cancel()
}
ticker := time.NewTicker(bp.CheckInterval)
defer ticker.Stop()
for {
if e.queueDepth(shard) < high {
return nil
}
select {
case <-ctx.Done():
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return ErrBackpressure
}
return ctx.Err()
case <-e.Done():
return context.Canceled
case <-ticker.C:
}
}
}
func (e *RuntimeEngine) queueDepth(shard int) int {
if shard < 0 || shard >= len(e.shards) || e.shards[shard] == nil || e.shards[shard].pipeline == nil {
return 0
}
return len(e.shards[shard].pipeline.DataChan())
}
func (e *RuntimeEngine) setRunErr(err error) {
e.runErrMu.Lock()
defer e.runErrMu.Unlock()
e.runErr = err
}
func (e *RuntimeEngine) getRunErr() error {
e.runErrMu.RLock()
defer e.runErrMu.RUnlock()
return e.runErr
}
func validateRequestForSubmit(request *Request) error {
if request == nil {
return ErrEmptyRequest
}
schema := request.Schema()
if schema == nil {
return ErrInvalidSchema
}
if schema.Columns() == nil || len(schema.Columns()) == 0 {
return ErrMissingColumn
}
if len(schema.Name()) == 0 {
return ErrEmptySchemaName
}
return nil
}