-
Notifications
You must be signed in to change notification settings - Fork 0
/
logicalindex_timebased.go
184 lines (151 loc) · 5.48 KB
/
logicalindex_timebased.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
package coordinator
import (
"context"
"errors"
"fmt"
"github.com/myrteametrics/myrtea-sdk/v5/elasticsearch"
"sort"
"sync"
"time"
"github.com/myrteametrics/myrtea-sdk/v5/modeler"
"github.com/robfig/cron/v3"
"go.uber.org/zap"
)
// LogicalIndexTimeBased abstracts a group a technical elasticsearchv8 indices, which are accessibles with specific aliases
type LogicalIndexTimeBased struct {
Initialized bool
Name string
Cron *cron.Cron
Model modeler.Model
LiveIndices []string
mu sync.RWMutex
}
func NewLogicalIndexTimeBased(instanceName string, model modeler.Model) (*LogicalIndexTimeBased, error) {
logicalIndexName := fmt.Sprintf("%s-%s", instanceName, model.Name)
zap.L().Info("Initialize logicalIndex (LogicalIndexTimeBased)", zap.String("name", logicalIndexName), zap.String("model", model.Name), zap.Any("options", model.ElasticsearchOptions))
if model.ElasticsearchOptions.Rollmode != "timebased" {
return nil, errors.New("invalid rollmode for this logicalIndex type")
}
templateName := fmt.Sprintf("template-%s", logicalIndexName)
logicalIndex := &LogicalIndexTimeBased{
Initialized: false,
Name: logicalIndexName,
Cron: nil,
Model: model,
LiveIndices: make([]string, 0),
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
templateExists, err := elasticsearch.C().Indices.ExistsTemplate(templateName).IsSuccess(ctx)
if err != nil {
zap.L().Error("IndexTemplateExists()", zap.Error(err))
return nil, err
}
if !templateExists {
zap.L().Info("template doesn't exists, creating it", zap.String("logicalIndexName", logicalIndexName))
indexPatern := fmt.Sprintf("%s-*", logicalIndexName)
logicalIndex.putTemplate(templateName, indexPatern, model)
}
logicalIndex.FetchIndices()
c := cron.New()
_, err = c.AddFunc("*/30 * * * *", logicalIndex.FetchIndices)
if err != nil {
zap.L().Error("Cron add function logicalIndex.updateAliases", zap.Error(err))
return nil, err
}
if logicalIndex.Model.ElasticsearchOptions.EnablePurge {
_, err = c.AddFunc(logicalIndex.Model.ElasticsearchOptions.Rollcron, logicalIndex.purge)
if err != nil {
zap.L().Error("Cron add function logicalIndex.updateAliases", zap.Error(err))
return nil, err
}
}
logicalIndex.Cron = c
zap.L().Info("Cron started", zap.String("logicalIndex", logicalIndex.Name), zap.String("cron", logicalIndex.Model.ElasticsearchOptions.Rollcron))
return logicalIndex, nil
}
func (logicalIndex *LogicalIndexTimeBased) GetCron() *cron.Cron {
return logicalIndex.Cron
}
func (logicalIndex *LogicalIndexTimeBased) purge() {
if !logicalIndex.Model.ElasticsearchOptions.EnablePurge {
return
}
tsStart := time.Now().Add(time.Duration(logicalIndex.Model.ElasticsearchOptions.PurgeMaxConcurrentIndices) * -1 * 24 * time.Hour)
indexStart := fmt.Sprintf("%s-%s", logicalIndex.Name, tsStart.Format("2006-01-02"))
allIndices := logicalIndex.GetAllIndices()
indices := make([]string, 0)
for _, index := range allIndices {
if index < indexStart { // purge selection condition
indices = append(indices, index)
}
}
sort.Strings(indices)
zap.L().Info("Purging indices older than", zap.String("indexStart", indexStart), zap.Strings("indices", indices))
if len(indices) > 0 {
for _, index := range indices {
logicalIndex.deleteIndex(index)
}
}
logicalIndex.FetchIndices()
}
func (logicalIndex *LogicalIndexTimeBased) putTemplate(name string, indexPatern string, model modeler.Model) {
req := elasticsearch.NewPutTemplateRequestV8([]string{indexPatern}, model)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
response, err := elasticsearch.C().Indices.PutTemplate(name).Request(req).Do(ctx)
if err != nil {
zap.L().Error("PutTemplate", zap.Error(err))
}
if !response.Acknowledged {
zap.L().Error("PutTemplate failed")
}
}
func (logicalIndex *LogicalIndexTimeBased) deleteIndex(index string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
success, err := elasticsearch.C().Indices.Delete(index).IsSuccess(ctx)
if err != nil {
zap.L().Warn("Delete index failed", zap.Error(err))
}
if !success {
zap.L().Warn("Delete index failed")
}
}
func (logicalIndex *LogicalIndexTimeBased) FetchIndices() {
indices := logicalIndex.GetAllIndices()
logicalIndex.mu.Lock()
logicalIndex.LiveIndices = indices
logicalIndex.mu.Unlock()
}
func (logicalIndex *LogicalIndexTimeBased) GetAllIndices() []string {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
response, err := elasticsearch.C().Cat.Indices().Index(fmt.Sprintf("%s-*", logicalIndex.Name)).Do(ctx)
if err != nil {
zap.L().Error("elasticsearchv8 CatIndices", zap.Error(err))
return make([]string, 0)
}
indices := make([]string, 0)
for _, index := range response {
if index.Index != nil {
indices = append(indices, *index.Index)
}
}
sort.Strings(indices)
return indices
}
func (logicalIndex *LogicalIndexTimeBased) FindIndices(t time.Time, depthDays int64) ([]string, error) {
tsStart := t.Add(time.Duration(depthDays) * -1 * 24 * time.Hour)
indexEnd := fmt.Sprintf("%s-%s", logicalIndex.Name, t.Format("2006-01-02"))
indexStart := fmt.Sprintf("%s-%s", logicalIndex.Name, tsStart.Format("2006-01-02"))
indices := make([]string, 0)
logicalIndex.mu.RLock()
for _, index := range logicalIndex.LiveIndices {
if index >= indexStart && index <= indexEnd {
indices = append(indices, index)
}
}
logicalIndex.mu.RUnlock()
return indices, nil
}