-
Notifications
You must be signed in to change notification settings - Fork 3
/
registry.go
225 lines (195 loc) · 6.72 KB
/
registry.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
package reg
import (
"fmt"
"sort"
"github.com/bitflow-stream/go-bitflow/bitflow"
"github.com/bitflow-stream/go-bitflow/bitflow/fork"
)
const (
MultiplexForkName = "multiplex"
)
type AnalysisFunc func(pipeline *bitflow.SamplePipeline, params map[string]string) error
type ForkFunc func(subpiplines []Subpipeline, params map[string]string) (fork.Distributor, error)
type RegisteredAnalysis struct {
Name string
Func AnalysisFunc
Description string
Params registeredParameters
SupportsBatchProcessing bool
SupportsStreamProcessing bool
}
type RegisteredFork struct {
Name string
Func ForkFunc
Description string
Params registeredParameters
}
type registeredParameters struct {
required []string
optional []string
}
type Subpipeline interface {
Build() (*bitflow.SamplePipeline, error)
Keys() []string
}
// Avoid having to specify a pointer to a ProcessorRegistry at many locations.
type ProcessorRegistry struct {
*ProcessorRegistryImpl
}
type ProcessorRegistryImpl struct {
Endpoints bitflow.EndpointFactory
analysisRegistry map[string]RegisteredAnalysis
forkRegistry map[string]RegisteredFork
}
func NewProcessorRegistry() ProcessorRegistry {
reg := ProcessorRegistry{
ProcessorRegistryImpl: &ProcessorRegistryImpl{
Endpoints: *bitflow.NewEndpointFactory(),
analysisRegistry: make(map[string]RegisteredAnalysis),
forkRegistry: make(map[string]RegisteredFork),
},
}
RegisterMultiplexFork(reg)
return reg
}
func (r *ProcessorRegistryImpl) GetAnalysis(name string) (analysisProcessor RegisteredAnalysis, ok bool) {
analysisProcessor, ok = r.analysisRegistry[name]
return
}
func (r *ProcessorRegistryImpl) GetFork(name string) (fork RegisteredFork, ok bool) {
fork, ok = r.forkRegistry[name]
return
}
func (r *ProcessorRegistryImpl) RegisterAnalysisParamsErr(name string, setupPipeline AnalysisFunc, description string, options ...Option) {
if _, ok := r.analysisRegistry[name]; ok {
panic("Analysis already registered: " + name)
}
opts := GetOpts(options)
params := registeredParameters{opts.RequiredParams, opts.OptionalParams}
r.analysisRegistry[name] = RegisteredAnalysis{
Name: name,
Func: setupPipeline,
Description: params.makeDescription(description),
Params: params,
SupportsBatchProcessing: opts.SupportBatchProcessing,
SupportsStreamProcessing: !opts.EnforceBatchProcessing,
}
}
func (r *ProcessorRegistryImpl) RegisterAnalysisParams(name string, setupPipeline func(pipeline *bitflow.SamplePipeline, params map[string]string), description string, options ...Option) {
r.RegisterAnalysisParamsErr(name, func(pipeline *bitflow.SamplePipeline, params map[string]string) error {
setupPipeline(pipeline, params)
return nil
}, description, options...)
}
func (r *ProcessorRegistryImpl) RegisterAnalysis(name string, setupPipeline func(pipeline *bitflow.SamplePipeline), description string, options ...Option) {
r.RegisterAnalysisParams(name, func(pipeline *bitflow.SamplePipeline, _ map[string]string) {
setupPipeline(pipeline)
}, description, options...)
}
func (r *ProcessorRegistryImpl) RegisterAnalysisErr(name string, setupPipeline func(pipeline *bitflow.SamplePipeline) error, description string, options ...Option) {
r.RegisterAnalysisParamsErr(name, func(pipeline *bitflow.SamplePipeline, _ map[string]string) error {
return setupPipeline(pipeline)
}, description, options...)
}
func (r *ProcessorRegistryImpl) RegisterFork(name string, createFork ForkFunc, description string, options ...Option) {
if _, ok := r.forkRegistry[name]; ok {
panic("Fork already registered: " + name)
}
opts := GetOpts(options)
params := registeredParameters{opts.RequiredParams, opts.OptionalParams}
r.forkRegistry[name] = RegisteredFork{name, createFork, params.makeDescription(description), params}
}
func (params registeredParameters) Verify(input map[string]string) error {
checked := map[string]bool{}
for _, opt := range params.optional {
checked[opt] = true
}
for _, req := range params.required {
if _, ok := input[req]; !ok {
return fmt.Errorf("Missing required parameter '%v'", req)
}
checked[req] = true
}
if params.required != nil {
for key := range input {
if _, ok := checked[key]; !ok {
return fmt.Errorf("Unexpected parameter '%v'", key)
}
}
}
return nil
}
func (params registeredParameters) makeDescription(description string) string {
if len(params.required) > 0 {
description += fmt.Sprintf(". Required parameters: %v", params.required)
} else if params.required == nil {
description += ". Variable parameters"
}
if len(params.optional) > 0 {
description += fmt.Sprintf(". Optional parameters: %v", params.optional)
}
return description
}
type AvailableProcessor struct {
Name string
IsFork bool
Description string
RequiredParams []string
OptionalParams []string
}
type AvailableProcessorSlice []AvailableProcessor
func (slice AvailableProcessorSlice) Len() int {
return len(slice)
}
func (slice AvailableProcessorSlice) Less(i, j int) bool {
// Sort the forks after the regular processing steps
return (!slice[i].IsFork && slice[j].IsFork) || slice[i].Name < slice[j].Name
}
func (slice AvailableProcessorSlice) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
func (r ProcessorRegistryImpl) GetAvailableProcessors() AvailableProcessorSlice {
all := make(AvailableProcessorSlice, 0, len(r.analysisRegistry))
for _, step := range r.analysisRegistry {
if step.Func == nil {
continue
}
all = append(all, AvailableProcessor{
Name: step.Name,
IsFork: false,
Description: step.Description,
RequiredParams: step.Params.required,
OptionalParams: step.Params.optional,
})
}
for _, registeredFork := range r.forkRegistry {
if registeredFork.Func == nil {
continue
}
all = append(all, AvailableProcessor{
Name: registeredFork.Name,
IsFork: true,
Description: registeredFork.Description,
RequiredParams: registeredFork.Params.required,
OptionalParams: registeredFork.Params.optional,
})
}
sort.Sort(all)
return all
}
// default Fork
func RegisterMultiplexFork(builder ProcessorRegistry) {
builder.RegisterFork(MultiplexForkName, createMultiplexFork, "Basic fork forwarding samples to all subpipelines. Subpipeline keys are ignored.")
}
func createMultiplexFork(subpipelines []Subpipeline, _ map[string]string) (fork.Distributor, error) {
var res fork.MultiplexDistributor
res.Subpipelines = make([]*bitflow.SamplePipeline, len(subpipelines))
var err error
for i, pipe := range subpipelines {
res.Subpipelines[i], err = pipe.Build()
if err != nil {
return nil, err
}
}
return &res, nil
}