/
provider.go
216 lines (195 loc) · 6.69 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
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
// 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 cleaner
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"time"
cfgpkg "github.com/recallsong/go-utils/config"
"github.com/recallsong/go-utils/lang/size"
"github.com/erda-project/erda-infra/base/logs"
"github.com/erda-project/erda-infra/base/servicehub"
election "github.com/erda-project/erda-infra/providers/etcd-election"
"github.com/erda-project/erda-infra/providers/httpserver"
"github.com/erda-project/erda-infra/providers/httpserver/interceptors"
"github.com/erda-project/erda/modules/core/monitor/storekit/elasticsearch/index"
"github.com/erda-project/erda/modules/core/monitor/storekit/elasticsearch/index/loader"
)
type (
// RetentionStrategy .
RetentionStrategy interface {
GetTTL(*loader.IndexEntry) time.Duration
}
// RetentionStrategyLoader .
RetentionStrategyLoader interface {
Loading(ctx context.Context)
}
// Interface .
Interface interface {
CleanIndices(ctx context.Context, filter loader.Matcher) error
}
)
type (
config struct {
RequestTimeout time.Duration `file:"request_timeout" default:"1m"`
CheckInterval time.Duration `file:"check_interval" default:"1h"`
PrintOnly bool `file:"print_onluy"`
DiskClean struct {
Enable bool `file:"enable"`
CheckInterval time.Duration `file:"check_interval" default:"5m"`
MinIndicesStore string `file:"min_indices_store" default:"10GB"`
MinIndicesStorePercent float64 `file:"min_indices_store_percent" default:"10"`
HighDiskUsagePercent float64 `file:"high_disk_usage_percent" default:"85"`
LowDiskUsagePercent float64 `file:"low_disk_usage_percent" default:"70"`
RolloverBodyFile string `file:"rollover_body_file"`
RolloverAliasPatterns []struct {
Index string `file:"index"`
Alias string `file:"alias"`
} `file:"rollover_alias_patterns"`
} `file:"disk_clean"`
}
indexAliasPattern struct {
index *index.Pattern
alias *index.Pattern
}
provider struct {
Cfg *config
Log logs.Logger
election election.Interface
loader loader.Interface
retentions RetentionStrategy
clearCh chan *clearRequest
// for disk clean
minIndicesStoreInDisk int64
rolloverBodyForDiskClean string
rolloverAliasPatterns []*indexAliasPattern
}
)
var _ Interface = (*provider)(nil)
func (p *provider) Init(ctx servicehub.Context) error {
loader, err := loader.Find(ctx, p.Log, true)
if err != nil {
return err
}
p.loader = loader
election, err := index.FindElection(ctx, true)
if err != nil {
return err
}
p.election = election
if err := p.initRetentionStrategy(ctx); err != nil {
return err
}
if loader, ok := p.retentions.(RetentionStrategyLoader); ok {
p.election.OnLeader(loader.Loading)
}
if int64(p.Cfg.CheckInterval) <= 0 {
return fmt.Errorf("invalid check_interval: %v", p.Cfg.CheckInterval)
}
if !p.loader.QueryIndexTimeRange() {
p.Log.Warnf("index clean is enable, but QueryIndexTimeRange of elasticsearch.index.loader is disable")
}
p.election.OnLeader(p.runCleanIndices)
if p.Cfg.DiskClean.Enable {
if int64(p.Cfg.DiskClean.CheckInterval) <= 0 {
return fmt.Errorf("invalid disk_clean.check_interval: %v", p.Cfg.DiskClean.CheckInterval)
}
// rollover body for disk clean
if len(p.Cfg.DiskClean.RolloverBodyFile) > 0 {
body, err := ioutil.ReadFile(p.Cfg.DiskClean.RolloverBodyFile)
if err != nil {
return fmt.Errorf("failed to load rollover body file for disk clean: %s", err)
}
body = cfgpkg.EscapeEnv(body)
p.rolloverBodyForDiskClean = string(body)
if len(p.rolloverBodyForDiskClean) <= 0 {
return fmt.Errorf("RolloverBody is empty for disk clean")
}
var m map[string]interface{}
err = json.NewDecoder(strings.NewReader(p.rolloverBodyForDiskClean)).Decode(&m)
if err != nil {
return fmt.Errorf("invalid RolloverBody for disk clean: %v", string(body))
}
p.Log.Info("load rollover body for disk clean: \n", p.rolloverBodyForDiskClean)
}
minIndicesStore, err := size.ParseBytes(p.Cfg.DiskClean.MinIndicesStore)
if err != nil {
return fmt.Errorf("invalid min_indices_store: %s", err)
}
p.minIndicesStoreInDisk = minIndicesStore
if len(p.Cfg.DiskClean.RolloverAliasPatterns) <= 0 {
return fmt.Errorf("rollover_alias_patterns are required")
}
for i, ptn := range p.Cfg.DiskClean.RolloverAliasPatterns {
if len(ptn.Index) <= 0 || len(ptn.Alias) <= 0 {
return fmt.Errorf("pattern(%d) index and alias is required", i)
}
ip, err := index.BuildPattern(ptn.Index)
if err != nil {
return err
}
ap, err := index.BuildPattern(ptn.Alias)
if err != nil {
return err
}
if ap.VarNum > 0 {
return fmt.Errorf("pattern(%d) can't contains vars", i)
}
p.rolloverAliasPatterns = append(p.rolloverAliasPatterns, &indexAliasPattern{index: ip, alias: ap})
}
// run disk clean task on leader node
p.election.OnLeader(p.runDiskCheckAndClean)
}
// init manager routes
routeRrefix := "/api/elasticsearch/index"
if len(ctx.Label()) > 0 {
routeRrefix = routeRrefix + "/" + ctx.Label()
} else {
routeRrefix = routeRrefix + "/-"
}
routes := ctx.Service("http-router", interceptors.CORS()).(httpserver.Router)
err = p.intRoutes(routes, routeRrefix)
if err != nil {
return fmt.Errorf("failed to init routes: %s", err)
}
return nil
}
func (p *provider) initRetentionStrategy(ctx servicehub.Context) error {
obj, name := index.FindService(ctx, "elasticsearch.index.retention-strategy")
if obj == nil {
return fmt.Errorf("%q is required", name)
}
rs, ok := obj.(RetentionStrategy)
if !ok {
return fmt.Errorf("%q is not RetentionStrategy", name)
}
p.retentions = rs
p.Log.Debugf("use RetentionStrategy(%q) for index clean", name)
return nil
}
func init() {
servicehub.Register("elasticsearch.index.cleaner", &servicehub.Spec{
Services: []string{"elasticsearch.index.cleaner"},
Dependencies: []string{"http-router", "elasticsearch.index.loader", "elasticsearch.index.retention-strategy", "etcd-election"},
ConfigFunc: func() interface{} { return &config{} },
Creator: func() servicehub.Provider {
return &provider{
clearCh: make(chan *clearRequest),
}
},
})
}