This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
/
migrator.go
235 lines (215 loc) · 6.21 KB
/
migrator.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
package db
import (
"context"
"github.com/globalsign/mgo/bson"
"github.com/keptn/keptn/statistics-service/operations"
"github.com/mitchellh/copystructure"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"strings"
"time"
)
// Migrator goes through each and every document and escapes keys which are known to dots (".")
// https://github.com/keptn/keptn/issues/6250
type Migrator struct {
dbConnection MongoDBConnection
statsCollection *mongo.Collection
batchSize int
interval time.Duration
migratedCount uint
nextBatchNum int
}
// NewMigrator creates a new instance of a Migrator
func NewMigrator(batchSize int, interval time.Duration) *Migrator {
return &Migrator{
dbConnection: MongoDBConnection{},
batchSize: batchSize,
interval: interval,
nextBatchNum: 1,
}
}
func (m *Migrator) Run(ctx context.Context) (uint, error) {
err := m.getCollection()
if err != nil {
return m.migratedCount, err
}
for {
select {
case <-ctx.Done():
return m.migratedCount, nil
case <-time.After(m.interval):
done, err := m.migrateBatch(ctx)
if done || err != nil {
return m.migratedCount, err
}
log.Infof("Migrated documents: %d", m.migratedCount)
}
}
}
func (m *Migrator) migrateBatch(ctx context.Context) (bool, error) {
skips := m.batchSize * (m.nextBatchNum - 1)
m.nextBatchNum++
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
cur, err := m.statsCollection.Find(ctx, bson.M{}, &options.FindOptions{
Limit: crateInt64P(m.batchSize),
Skip: crateInt64P(skips),
})
defer func() {
if cur == nil {
return
}
if err := cur.Close(ctx); err != nil {
log.Errorf("could not close cursor: %v", err)
}
}()
if err != nil {
return false, err
}
currBatchSize := 0
for cur.Next(ctx) {
stats := &operations.Statistics{}
err := cur.Decode(stats)
if err != nil {
return false, err
}
currBatchSize++
// if there is no project data, there is nothing to do
if len(stats.Projects) == 0 {
continue
}
// if there are no dots in keys, there is nothing to do
if noDotsInKeys(stats) {
continue
}
hexID := &HexID{}
err = cur.Decode(hexID)
if err != nil {
return false, err
}
decodedKeys, err := decodeKeys([]operations.Statistics{*stats})
if err != nil {
return false, err
}
statsWithEncodedKeys, err := encodeKeys(&decodedKeys[0])
if err != nil {
return false, err
}
_, err = m.statsCollection.ReplaceOne(ctx, bson.M{"_id": hexID.ID}, statsWithEncodedKeys)
if err != nil {
return false, err
}
m.migratedCount++
}
if currBatchSize < m.batchSize {
return true, nil
}
return false, nil
}
func noDotsInKeys(statistics *operations.Statistics) bool {
for projKey, proj := range statistics.Projects {
if strings.Contains(projKey, ".") {
return false
}
for svcKey, svc := range proj.Services {
if strings.Contains(svcKey, ".") {
return false
}
for eventType := range svc.ExecutedSequencesPerType {
if strings.Contains(eventType, ".") {
return false
}
}
for eventType := range svc.Events {
if strings.Contains(eventType, ".") {
return false
}
}
for keptnServiceExecutionsKey, keptnService := range svc.KeptnServiceExecutions {
if strings.Contains(keptnServiceExecutionsKey, ".") {
return false
}
for eventType := range keptnService.Executions {
if strings.Contains(eventType, ".") {
return false
}
}
}
}
}
return true
}
func encodeKeys(statistics *operations.Statistics) (*operations.Statistics, error) {
return transform(statistics, encodeKey)
}
func decodeKeys(statistics []operations.Statistics) ([]operations.Statistics, error) {
newStatistics := []operations.Statistics{}
for _, stat := range statistics {
s, err := transform(&stat, decodeKey)
if err != nil {
return nil, err
}
newStatistics = append(newStatistics, *s)
}
return newStatistics, nil
}
func transform(statistics *operations.Statistics, transformFn func(string) string) (*operations.Statistics, error) {
copiedStatistics, err := copystructure.Copy(statistics)
if err != nil {
return nil, err
}
newProjects := make(map[string]*operations.Project)
for projKey, proj := range copiedStatistics.(*operations.Statistics).Projects {
newServices := make(map[string]*operations.Service)
for serviceKey, service := range proj.Services {
newExecutedSequencesPerType := make(map[string]int)
for eventType, numExecutedSequencesPerType := range service.ExecutedSequencesPerType {
newExecutedSequencesPerType[transformFn(eventType)] = numExecutedSequencesPerType
}
service.ExecutedSequencesPerType = newExecutedSequencesPerType
newEvents := make(map[string]int)
for eventType, event := range service.Events {
newEvents[transformFn(eventType)] = event
}
service.Events = newEvents
newKeptnServiceExecutions := make(map[string]*operations.KeptnService)
for keptnServiceExecutionKey, keptnService := range service.KeptnServiceExecutions {
newServiceExecutions := make(map[string]int)
for eventType2, numExecutions := range keptnService.Executions {
newServiceExecutions[transformFn(eventType2)] = numExecutions
}
keptnService.Executions = newServiceExecutions
newKeptnServiceExecutions[transformFn(keptnServiceExecutionKey)] = keptnService
}
service.KeptnServiceExecutions = newKeptnServiceExecutions
newServices[transformFn(serviceKey)] = service
}
proj.Services = newServices
newProjects[transformFn(projKey)] = proj
}
copiedStatistics.(*operations.Statistics).Projects = newProjects
return copiedStatistics.(*operations.Statistics), nil
}
func encodeKey(key string) string {
encodedKey := strings.ReplaceAll(strings.ReplaceAll(key, "~", "~t"), ".", "~p")
return encodedKey
}
func decodeKey(key string) string {
decodedKey := strings.ReplaceAll(strings.ReplaceAll(key, "~p", "."), "~t", "~")
return decodedKey
}
func (m *Migrator) getCollection() error {
err := m.dbConnection.EnsureDBConnection()
if err != nil {
return err
}
if m.statsCollection == nil {
m.statsCollection = m.dbConnection.Client.Database(databaseName).Collection(keptnStatsCollection)
}
return nil
}
func crateInt64P(x int) *int64 {
i := int64(x)
return &i
}