-
Notifications
You must be signed in to change notification settings - Fork 2
/
topology_builder.go
227 lines (182 loc) · 5.69 KB
/
topology_builder.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
218
219
220
221
222
223
224
225
226
227
package kstreams
import (
"errors"
"github.com/twmb/franz-go/pkg/kgo"
)
type Nexter[K, V any] interface {
AddNext(InputProcessor[K, V])
}
type TopologyBuilder struct {
processors map[string]*TopologyProcessor
stores map[string]*TopologyStore
// Key = TopicNode
sources map[string]*TopologySource
sinks map[string]*TopologySink
}
func (tb *TopologyBuilder) Build() (*Topology, error) {
// TODO Validate for cycles, build partition group once,...
return &Topology{
sources: tb.sources,
stores: tb.stores,
processors: tb.processors,
sinks: tb.sinks,
}, nil
}
func (tb *TopologyBuilder) MustBuild() *Topology {
topology, err := tb.Build()
if err != nil {
panic(err)
}
return topology
}
// Contains reports whether v is present in s.
func ContainsAny[E comparable](s []E, v []E) bool {
for _, item := range s {
for _, check := range v {
if item == check {
return true
}
}
}
return false
}
func NewTopologyBuilder() *TopologyBuilder {
return &TopologyBuilder{
processors: map[string]*TopologyProcessor{},
stores: map[string]*TopologyStore{},
sources: map[string]*TopologySource{},
sinks: map[string]*TopologySink{},
}
}
type TopologyStore struct {
Name string
Build StoreBuilder
}
type TopologySink struct {
Name string
Builder func(*kgo.Client) Flusher
}
type TopologyProcessor struct {
Name string
Build func(stores map[string]Store) Node
ChildNodeNames []string
AddChildFunc func(parent any, child any, childName string) // TODO - possible to do w/o parent ?
StoreNames []string
}
type TopologySource struct {
Name string
Build func() RecordProcessor
ChildNodeNames []string
AddChildFunc func(parent any, child any, childName string) // TODO - possible to do w/o parent ?
}
func MustRegisterSource[K, V any](t *TopologyBuilder, name string, topic string, keyDeserializer Deserializer[K], valueDeserializer Deserializer[V]) {
must(RegisterSource(t, name, topic, keyDeserializer, valueDeserializer))
}
func RegisterSource[K, V any](t *TopologyBuilder, name string, topic string, keyDeserializer Deserializer[K], valueDeserializer Deserializer[V]) error {
topoSource := &TopologySource{
Name: name,
Build: func() RecordProcessor {
return &SourceNode[K, V]{KeyDeserializer: keyDeserializer, ValueDeserializer: valueDeserializer}
},
AddChildFunc: func(parent, child any, childName string) {
parentNode, ok := parent.(*SourceNode[K, V])
if !ok {
panic("type error")
}
childNode, ok := child.(InputProcessor[K, V])
if !ok {
panic("type error")
}
parentNode.AddNext(childNode)
},
ChildNodeNames: []string{},
}
if _, found := t.processors[name]; found {
return ErrNodeAlreadyExists
}
t.sources[name] = topoSource
return nil
}
func must(err error) {
if err != nil {
panic(err)
}
}
func MustRegisterSink[K, V any](t *TopologyBuilder, name, topic string, keySerializer Serializer[K], valueSerializer Serializer[V], parent string) {
must(RegisterSink(t, name, topic, keySerializer, valueSerializer, parent))
}
func RegisterSink[K, V any](t *TopologyBuilder, name, topic string, keySerializer Serializer[K], valueSerializer Serializer[V], parent string) error {
topoSink := &TopologySink{
Name: name,
Builder: func(client *kgo.Client) Flusher {
return NewSinkNode(client, topic, keySerializer, valueSerializer)
},
}
// t.processors[name] = topoProcessor
t.sinks[name] = topoSink
return SetParent(t, parent, name)
}
func MustRegisterProcessor[Kin, Vin, Kout, Vout any](t *TopologyBuilder, p ProcessorBuilder[Kin, Vin, Kout, Vout], name string, parent string, stores ...string) { // TODO: change to functional option for stores
must(RegisterProcessor(t, p, name, parent, stores...))
}
func RegisterProcessor[Kin, Vin, Kout, Vout any](t *TopologyBuilder, p ProcessorBuilder[Kin, Vin, Kout, Vout], name string, parent string, stores ...string) error {
topoProcessor := &TopologyProcessor{
Name: name,
Build: func(stores map[string]Store) Node {
node := &ProcessorNode[Kin, Vin, Kout, Vout]{
userProcessor: p(),
processorContext: &InternalProcessorContext[Kout, Vout]{
outputs: map[string]InputProcessor[Kout, Vout]{},
stores: stores,
},
}
return node
},
ChildNodeNames: []string{},
StoreNames: stores,
}
// TODO validate store names, existence ?
topoProcessor.AddChildFunc = func(parent any, child any, childName string) {
// TODO: try to detect these already when building the topology.
parentNode, ok := parent.(*ProcessorNode[Kin, Vin, Kout, Vout])
if !ok {
panic("type error")
}
// TODO: try to detect these already when building the topology.
childNode, ok := child.(InputProcessor[Kout, Vout])
if !ok {
panic("type error")
}
parentNode.processorContext.outputs[childName] = childNode
}
if _, found := t.processors[name]; found {
return ErrNodeAlreadyExists
}
// how to add to processor context the stores?
t.processors[name] = topoProcessor
for _, store := range stores {
if _, ok := t.stores[store]; !ok {
return errors.New("store not found")
}
}
return SetParent(t, parent, name)
}
func MustSetParent(t *TopologyBuilder, parent, child string) {
must(SetParent(t, parent, child))
}
func SetParent(t *TopologyBuilder, parent, child string) error {
parentNode, ok := t.processors[parent]
if ok {
parentNode.ChildNodeNames = append(parentNode.ChildNodeNames, child)
return nil
}
source, ok := t.sources[parent]
if ok {
source.ChildNodeNames = append(source.ChildNodeNames, child)
return nil
}
return ErrNodeNotFound
}
var ErrNodeAlreadyExists = errors.New("node exists already")
var ErrNodeNotFound = errors.New("node not found")
var ErrInternal = errors.New("internal")