/
script.go
117 lines (103 loc) · 3.33 KB
/
script.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
// Copyright 2024 The Cockroach Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// Package script provides an API shim that integrates the userscript
// into the [sequencer.Sequencer] API.
package script
import (
"context"
"github.com/cockroachdb/cdc-sink/internal/script"
"github.com/cockroachdb/cdc-sink/internal/sequencer"
"github.com/cockroachdb/cdc-sink/internal/types"
"github.com/cockroachdb/cdc-sink/internal/util/ident"
"github.com/cockroachdb/cdc-sink/internal/util/notify"
"github.com/cockroachdb/cdc-sink/internal/util/stopper"
)
// Sequencer injects the userscript shim into a [sequencer.Sequencer]
// stack.
type Sequencer struct {
loader *script.Loader
targetPool *types.TargetPool
watchers types.Watchers
}
var _ sequencer.Shim = (*Sequencer)(nil)
// Wrap implements [sequencer.Shim].
func (s *Sequencer) Wrap(
_ *stopper.Context, delegate sequencer.Sequencer,
) (sequencer.Sequencer, error) {
return &wrapper{s, delegate}, nil
}
type wrapper struct {
*Sequencer
delegate sequencer.Sequencer
}
var _ sequencer.Sequencer = (*wrapper)(nil)
// Start injects a userscript shim into the Sequencer stack.
func (w *wrapper) Start(
ctx *stopper.Context, opts *sequencer.StartOptions,
) (types.MultiAcceptor, *notify.Var[sequencer.Stat], error) {
// Loader is nil if no userscript has been configured.
if w.loader == nil {
return w.delegate.Start(ctx, opts)
}
schema, err := opts.Group.Schema()
if err != nil {
return nil, nil, err
}
scr, err := w.loader.Bind(ctx, schema, opts.Delegate, w.watchers)
if err != nil {
return nil, nil, err
}
// Only inject if the source or any tables have a configuration.
_, inject := scr.Sources.Get(opts.Group.Name)
if !inject {
for _, tbl := range opts.Group.Tables {
_, inject = scr.Targets.Get(tbl)
if inject {
break
}
}
}
if inject {
// If the userscript has defined any apply functions, we will
// need to ensure that a database transaction will be available
// to support the api.getTX() function. This is mainly relevant
// to immediate mode, in which the sequencer caller won't
// necessarily have created a transaction.
ensureTX := false
// No interesting error returned from Range.
_ = scr.Targets.Range(func(_ ident.Table, target *script.Target) error {
if target.UserAcceptor != nil {
ensureTX = true
return context.Canceled // Arbitrary error to stop early.
}
return nil
})
opts = opts.Copy()
opts.Delegate = types.OrderedAcceptorFrom(&acceptor{
delegate: opts.Delegate,
ensureTX: ensureTX,
group: opts.Group,
targetPool: w.targetPool,
userScript: scr,
watchers: w.watchers,
}, w.watchers)
}
return w.delegate.Start(ctx, opts)
}
// Unwrap is an informal protocol to return the delegate.
func (w *wrapper) Unwrap() sequencer.Sequencer {
return w.delegate
}