-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeneric_runner.go
253 lines (225 loc) · 5.8 KB
/
generic_runner.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
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
package run
import (
"context"
"log"
"os"
"reflect"
"runtime"
"runtime/pprof"
"sync"
"time"
)
type start string
type data string
// Start is the key for the start time of the run.
const Start start = "start"
// Data is the key for additional data of the run.
const Data data = "data"
// GenericRunner creates a new runner from the given components.
func GenericRunner[RunnerConfig, Input, Option, Solution any](
ioHandler IOProducer[RunnerConfig],
inputDecoder Decoder[Input],
inputValidator Validator[Input],
optionDecoder Decoder[Option],
handler Algorithm[Input, Option, Solution],
encoder Encoder[Solution, Option],
) Runner[RunnerConfig, Input, Option, Solution] {
runnerConfig, option, err := FlagParser[
Option, RunnerConfig,
]()
if err != nil {
log.Fatal(err)
}
return &genericRunner[RunnerConfig, Input, Option, Solution]{
IOProducer: ioHandler,
InputDecoder: inputDecoder,
InputValidator: inputValidator,
OptionDecoder: optionDecoder,
Algorithm: handler,
Encoder: encoder,
runnerConfig: runnerConfig,
flagParsedOption: option,
}
}
type genericRunner[RunnerConfig, Input, Option, Solution any] struct {
IOProducer IOProducer[RunnerConfig]
InputDecoder Decoder[Input]
InputValidator Validator[Input]
OptionDecoder Decoder[Option]
Algorithm Algorithm[Input, Option, Solution]
Encoder Encoder[Solution, Option]
runnerConfig RunnerConfig
flagParsedOption Option
}
func (r *genericRunner[RunnerConfig, Input, Option, Solution]) handleCPUProfile(
runnerConfig any,
) (deferFunc func() error, err error) {
deferFunc = func() error {
return nil
}
if cpuProfiler, ok := runnerConfig.(CPUProfiler); ok &&
cpuProfiler.CPUProfilePath() != "" {
// CPU profiler.
f, err := os.Create(cpuProfiler.CPUProfilePath())
if err != nil {
return deferFunc, err
}
deferFunc = func() error {
return f.Close()
}
if err := pprof.StartCPUProfile(f); err != nil {
return deferFunc, err
}
deferFunc = func() error {
pprof.StopCPUProfile()
return f.Close()
}
}
return deferFunc, nil
}
func (r *genericRunner[RunnerConfig, Input, Option, Solution],
) handleMemoryProfile(runnerConfig any,
) (deferFunc func() error, err error) {
deferFunc = func() error {
return nil
}
// Memory profile.
if memoryProfiler, ok := runnerConfig.(MemoryProfiler); ok &&
memoryProfiler.MemoryProfilePath() != "" {
f, err := os.Create(memoryProfiler.MemoryProfilePath())
if err != nil {
return deferFunc, err
}
deferFunc = func() error {
return f.Close()
}
// Clean up unused objects from the heap before profiling. But do not
// garbage collect the runner, so we can see in-use memory.
runtime.GC()
runtime.KeepAlive(r)
if err := pprof.WriteHeapProfile(f); err != nil {
return deferFunc, err
}
}
return deferFunc, nil
}
func (r *genericRunner[RunnerConfig, Input, Option, Solution]) Run(
ctx context.Context,
) (retErr error) {
start := time.Now()
ctx = context.WithValue(ctx, Start, start)
ctx = context.WithValue(ctx, Data, &sync.Map{})
// handle CPU profile
deferFuncCPU, retErr := r.handleCPUProfile(r.runnerConfig)
if retErr != nil {
return retErr
}
defer func() {
err := deferFuncCPU()
// the first error is more important
if retErr == nil {
retErr = err
}
}()
// get IO
ioData, retErr := r.IOProducer(ctx, r.runnerConfig)
if retErr != nil {
return retErr
}
if r.InputValidator != nil {
retErr = r.InputValidator(ctx, ioData.Input())
if retErr != nil {
return retErr
}
}
// decode input
decodedInput, retErr := r.InputDecoder(ctx, ioData.Input())
if retErr != nil {
return retErr
}
// use options configured in runner via flags and environment variables
decodedOption := r.flagParsedOption
// decode option if provided
tempOption, err := r.OptionDecoder(ctx, ioData.Option())
if err != nil {
return err
}
var defaultOption Option
// if option is not default, use it
if !reflect.DeepEqual(tempOption, defaultOption) {
decodedOption = tempOption
}
// run algorithm
solutions := make(chan Solution)
errs := make(chan error, 1)
go func() {
defer close(solutions)
defer close(errs)
retErr = r.Algorithm(ctx, decodedInput, decodedOption, solutions)
if retErr != nil {
errs <- retErr
return
}
}()
// encode solutions
retErr = r.Encoder.Encode(
ctx, solutions, ioData.Writer(), r.runnerConfig, decodedOption,
)
if retErr != nil {
return retErr
}
// handle memory profile
deferFuncMemory, retErr := r.handleMemoryProfile(r.runnerConfig)
if retErr != nil {
return retErr
}
defer func() {
err := deferFuncMemory()
// the first error is more important
if retErr == nil {
retErr = err
}
}()
// return potential errors
return <-errs
}
func (r *genericRunner[RunnerConfig, Input, Option, Solution]) SetIOProducer(
ioProducer IOProducer[RunnerConfig],
) {
r.IOProducer = ioProducer
}
func (r *genericRunner[RunnerConfig, Input, Option, Solution]) SetInputDecoder(
decoder Decoder[Input],
) {
r.InputDecoder = decoder
}
func (r *genericRunner[RunnerConfig, Input, Option, Solution]) SetInputValidator(
validator Validator[Input],
) {
r.InputValidator = validator
}
func (r *genericRunner[RunnerConfig, Input, Option, Solution]) SetOptionDecoder(
decoder Decoder[Option],
) {
r.OptionDecoder = decoder
}
func (r *genericRunner[RunnerConfig, Input, Option, Solution]) SetAlgorithm(
algorithm Algorithm[Input, Option, Solution],
) {
r.Algorithm = algorithm
}
func (r *genericRunner[RunnerConfig, Input, Option, Solution]) SetEncoder(
encoder Encoder[Solution, Option],
) {
r.Encoder = encoder
}
func (r *genericRunner[
RunnerConfig, Input, Option, Solution,
]) GetEncoder() Encoder[Solution, Option] {
return r.Encoder
}
func (r *genericRunner[
RunnerConfig, Input, Option, Solution],
) RunnerConfig() RunnerConfig {
return r.runnerConfig
}