-
Notifications
You must be signed in to change notification settings - Fork 376
/
provider.go
101 lines (89 loc) · 3.19 KB
/
provider.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
// Copyright (c) 2021 Terminus, Inc.
//
// 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.
package persist
import (
"context"
"fmt"
"time"
"github.com/erda-project/erda-infra/base/logs"
"github.com/erda-project/erda-infra/base/servicehub"
"github.com/erda-project/erda-infra/providers/kafka"
"github.com/erda-project/erda/modules/core/monitor/storekit"
"github.com/erda-project/erda/modules/msp/apm/exception/erda-event/storage"
)
type (
config struct {
Input kafka.BatchReaderConfig `file:"input"`
Parallelism int `file:"parallelism" default:"1"`
BufferSize int `file:"buffer_size" default:"1024"`
ReadTimeout time.Duration `file:"read_timeout" default:"5s"`
IDKeys []string `file:"id_keys"`
PrintInvalidEvent bool `file:"print_invalid_event" default:"false"`
}
provider struct {
Cfg *config
Log logs.Logger
Kafka kafka.Interface `autowired:"kafka"`
StorageWriter storage.Storage `autowired:"error-event-storage-writer"`
storage storage.Storage
stats Statistics
validator Validator
metadata MetadataProcessor
}
)
func (p *provider) Init(ctx servicehub.Context) (err error) {
p.validator = newValidator(p.Cfg)
if runner, ok := p.validator.(servicehub.ProviderRunnerWithContext); ok {
ctx.AddTask(runner.Run, servicehub.WithTaskName("event validator"))
}
p.metadata = newMetadataProcessor(p.Cfg)
if runner, ok := p.metadata.(servicehub.ProviderRunnerWithContext); ok {
ctx.AddTask(runner.Run, servicehub.WithTaskName("event metadata processor"))
}
p.stats = sharedStatistics
// add consumer task
for i := 0; i < p.Cfg.Parallelism; i++ {
//spot
ctx.AddTask(func(ctx context.Context) error {
r, err := p.Kafka.NewBatchReader(&p.Cfg.Input, kafka.WithReaderDecoder(p.decodeEvent))
if err != nil {
return err
}
defer r.Close()
w, err := p.StorageWriter.NewWriter(ctx)
if err != nil {
return err
}
defer w.Close()
return storekit.BatchConsume(ctx, r, w, &storekit.BatchConsumeOptions{
BufferSize: p.Cfg.BufferSize,
ReadTimeout: p.Cfg.ReadTimeout,
ReadErrorHandler: p.handleReadError,
WriteErrorHandler: p.handleWriteError,
ConfirmErrorHandler: p.confirmErrorHandler,
Statistics: p.stats,
})
}, servicehub.WithTaskName(fmt.Sprintf("spot-error-event-consumer(%d)", i)))
}
return nil
}
func init() {
servicehub.Register("error-event-persist", &servicehub.Spec{
Dependencies: []string{"kafka.topic.initializer"},
ConfigFunc: func() interface{} { return &config{} },
Creator: func() servicehub.Provider {
return &provider{}
},
})
}