This repository was archived by the owner on Nov 8, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathprocessor.go
More file actions
219 lines (201 loc) · 6.53 KB
/
Copy pathprocessor.go
File metadata and controls
219 lines (201 loc) · 6.53 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
// Package worker applies queued mutations through the synchronous Sink path.
package worker
import (
"context"
"errors"
"fmt"
sink "github.com/batchstream/sink-protocol/sink/v1"
"github.com/batchstream/sink/internal/queue"
)
// Applier executes one collected mutation group. Errors keep the source records
// retryable; the processor never splits or replays a request after a top-level error.
type Applier interface {
Write(ctx context.Context, req *sink.WriteRequest) (*sink.WriteResponse, error)
Delete(ctx context.Context, req *sink.DeleteRequest) (*sink.DeleteResponse, error)
}
type Processor struct {
applier Applier
}
func NewProcessor(applier Applier) (*Processor, error) {
if applier == nil {
return nil, errors.New("create mutation processor: applier is required")
}
processor := &Processor{applier: applier}
return processor, nil
}
func (p *Processor) Handle(ctx context.Context, mutation queue.Mutation) error {
mutations := []queue.Mutation{mutation}
results := p.HandleBatch(ctx, mutations)
return results[0]
}
type mutationWork struct {
index int
routing string
mutation queue.Mutation
}
func (p *Processor) HandleBatch(ctx context.Context, mutations []queue.Mutation) []error {
results := make([]error, len(mutations))
prepared := make([]mutationWork, 0, len(mutations))
for index, mutation := range mutations {
key, err := queue.MutationKey(mutation)
if err != nil {
results[index] = NewApplyError("apply queued mutation", false, err)
continue
}
work := mutationWork{index: index, routing: string(key), mutation: mutation}
prepared = append(prepared, work)
}
waves := buildMutationWaves(prepared)
blocked := make(map[string]error)
for _, wave := range waves {
writes := make([]mutationWork, 0, len(wave))
deletes := make([]mutationWork, 0, len(wave))
for _, work := range wave {
if blockedErr := blocked[work.routing]; blockedErr != nil {
results[work.index] = blockedErr
continue
}
if work.mutation.Write != nil {
writes = append(writes, work)
continue
}
deletes = append(deletes, work)
}
p.applyWrites(ctx, writes, results)
p.applyDeletes(ctx, deletes, results)
for _, work := range wave {
var failure *ApplyError
if errors.As(results[work.index], &failure) && failure.Retryable() {
blocked[work.routing] = results[work.index]
}
}
}
return results
}
func buildMutationWaves(operations []mutationWork) [][]mutationWork {
waves := make([][]mutationWork, 0)
occurrences := make(map[string]int)
for _, operation := range operations {
waveIndex := occurrences[operation.routing]
occurrences[operation.routing]++
for len(waves) <= waveIndex {
waves = append(waves, nil)
}
waves[waveIndex] = append(waves[waveIndex], operation)
}
return waves
}
func (p *Processor) applyWrites(ctx context.Context, operations []mutationWork, results []error) {
if len(operations) == 0 {
return
}
writeOperations := make([]*sink.WriteOperation, 0, len(operations))
for _, operation := range operations {
writeOperations = append(writeOperations, operation.mutation.Write)
}
request := &sink.WriteRequest{
CompletionMode: sink.CompletionMode_COMPLETION_MODE_WAIT_UNTIL_APPLIED,
Operations: writeOperations,
}
response, err := p.applier.Write(ctx, request)
if err != nil {
for _, operation := range operations {
results[operation.index] = NewApplyError("apply queued writes", true, err)
}
return
}
if len(response.GetResults()) != len(operations) {
err := errors.New("sink returned an invalid queued write result count")
for _, operation := range operations {
results[operation.index] = NewApplyError("apply queued writes", true, err)
}
return
}
for index, result := range response.GetResults() {
if result.GetStatus() == sink.WriteStatus_WRITE_STATUS_APPLIED {
continue
}
operation := operations[index]
if err := ctx.Err(); err != nil {
results[operation.index] = NewApplyError("apply queued write", true, err)
continue
}
results[operation.index] = resultApplyError("apply queued write", result.GetFailure())
}
}
func (p *Processor) applyDeletes(ctx context.Context, operations []mutationWork, results []error) {
if len(operations) == 0 {
return
}
deleteOperations := make([]*sink.DeleteOperation, 0, len(operations))
for _, operation := range operations {
deleteOperations = append(deleteOperations, operation.mutation.Delete)
}
request := &sink.DeleteRequest{
CompletionMode: sink.CompletionMode_COMPLETION_MODE_WAIT_UNTIL_APPLIED,
Operations: deleteOperations,
}
response, err := p.applier.Delete(ctx, request)
if err != nil {
for _, operation := range operations {
results[operation.index] = NewApplyError("apply queued deletes", true, err)
}
return
}
if len(response.GetResults()) != len(operations) {
err := errors.New("sink returned an invalid queued delete result count")
for _, operation := range operations {
results[operation.index] = NewApplyError("apply queued deletes", true, err)
}
return
}
for index, result := range response.GetResults() {
if result.GetStatus() == sink.DeleteStatus_DELETE_STATUS_APPLIED {
continue
}
operation := operations[index]
if err := ctx.Err(); err != nil {
results[operation.index] = NewApplyError("apply queued delete", true, err)
continue
}
results[operation.index] = resultApplyError("apply queued delete", result.GetFailure())
}
}
type ApplyError struct {
operation string
retryable bool
cause error
}
func NewApplyError(operation string, retryable bool, cause error) *ApplyError {
applyError := &ApplyError{
operation: operation,
retryable: retryable,
cause: cause,
}
return applyError
}
func (e *ApplyError) Error() string {
return fmt.Sprintf("%s: %v", e.operation, e.cause)
}
func (e *ApplyError) Unwrap() error {
return e.cause
}
func (e *ApplyError) Retryable() bool {
return e.retryable
}
func resultApplyError(operation string, failure *sink.Failure) error {
if failure == nil {
err := errors.New("sink returned a mutation failure without details")
return NewApplyError(operation, true, err)
}
cause := errors.New(failure.GetMessage())
// An absent/unknown/internal failure is not evidence of a bad record. Keep
// its source offset and same-record barrier until the dependency is repaired.
retryable := true
switch failure.GetCode() {
case sink.FailureCode_FAILURE_CODE_INVALID_ARGUMENT, sink.FailureCode_FAILURE_CODE_PRECONDITION_FAILED,
sink.FailureCode_FAILURE_CODE_NOT_FOUND, sink.FailureCode_FAILURE_CODE_RESOURCE_EXHAUSTED:
retryable = failure.GetRetryable()
}
return NewApplyError(operation, retryable, cause)
}