/
index.go
134 lines (124 loc) · 3.41 KB
/
index.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package slsimport
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/olivere/elastic"
)
const timeForSplitIndex int64 = 24 * int64(time.Hour)
// getIndex .
func (c *Consumer) getIndex(project string, timestamp int64) string {
timestamp = (timestamp - timestamp%timeForSplitIndex) / 1000000
return c.outputs.indexPrefix + strings.Replace(project, "-", "_", -1) + "-" + strconv.FormatInt(timestamp, 10)
}
func (p *provider) startIndexManager() {
defer p.wg.Done()
tick := time.Tick(p.C.AccountsReloadInterval)
for {
err := p.cleanIndices()
if err != nil {
p.L.Error(err)
}
select {
case <-tick:
continue
case <-p.closeCh:
return
}
}
}
func (p *provider) cleanIndices() error {
indices, err := p.loadIndices()
if err != nil {
return fmt.Errorf("fail to load indices: %s", err)
}
now := time.Now().Add(-p.C.Output.Elasticsearch.IndexTTL)
var removeList []string
for _, item := range indices {
if now.After(item.Timestamp) {
removeList = append(removeList, item.Index)
}
}
if len(removeList) > 0 {
const size = 10 // 一次性删太多,请求太大会被拒绝
for len(removeList) >= size {
err := p.deleteIndices(removeList[:size])
if err != nil {
return err
}
removeList = removeList[size:]
}
if len(removeList) > 0 {
err := p.deleteIndices(removeList)
if err != nil {
return err
}
}
}
return nil
}
// deleteIndices
func (p *provider) deleteIndices(indices []string) error {
ctx, cancel := context.WithTimeout(context.Background(), p.C.Output.Elasticsearch.RequestTimeout)
defer cancel()
resp, err := p.es.DeleteIndex(indices...).Do(ctx)
if err != nil {
if e, ok := err.(*elastic.Error); ok {
if e.Status == 404 {
return nil
}
}
return err
}
if !resp.Acknowledged {
return fmt.Errorf("delete indices Acknowledged=false")
}
p.L.Infof("clean indices %d, %v", len(indices), indices)
return nil
}
type indexEntry struct {
Index string
Timestamp time.Time
}
func (p *provider) loadIndices() ([]*indexEntry, error) {
ctx, cancel := context.WithTimeout(context.Background(), p.C.Output.Elasticsearch.RequestTimeout)
defer cancel()
resps, err := p.es.CatIndices().Index(p.C.Output.Elasticsearch.IndexPrefix + "*").Columns("index").Do(ctx)
if err != nil {
return nil, err
}
var indices []*indexEntry
for _, item := range resps {
parts := strings.Split(item.Index, "-")
if len(parts) != 3 {
p.L.Debugf("invalid index format %s", item.Index)
continue
}
last := len(parts) - 1
timestamp, err := strconv.ParseInt(parts[last], 10, 64)
if err != nil {
p.L.Warnf("invalid timestamp in index %s, %s", item.Index, err)
continue
}
ts := time.Unix(timestamp/1000, (timestamp%1000)*int64(time.Millisecond))
indices = append(indices, &indexEntry{
Index: item.Index,
Timestamp: ts,
})
}
return indices, nil
}