-
Notifications
You must be signed in to change notification settings - Fork 143
/
deployment_store.go
364 lines (311 loc) · 9.61 KB
/
deployment_store.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright 2023 The PipeCD Authors.
//
// 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 insightstore
import (
"context"
"encoding/json"
"errors"
"fmt"
"sort"
"time"
"go.uber.org/zap"
"github.com/pipe-cd/pipecd/pkg/filestore"
"github.com/pipe-cd/pipecd/pkg/insight"
)
type DeploymentBlockMetadata struct {
BlockID string `json:"block_id"`
ChunkMetadata []*DeploymentChunkMetadata `json:"chunk_metadata"`
}
type DeploymentChunkMetadata struct {
ChunkID string `json:"chunk_id"`
ChunkIndex int `json:"chunk_index"`
MinTimestamp int64 `json:"min_timestamp"`
MaxTimestamp int64 `json:"max_timestamp"`
Count int `json:"count"`
Completed bool `json:"completed"`
}
type DeploymentChunk struct {
ChunkID string `json:"chunk_id"`
Deployments []*insight.DeploymentData `json:"deployments"`
}
func (c *DeploymentChunk) Size() (int64, error) {
raw, err := json.Marshal(c)
if err != nil {
return 0, err
}
return int64(len(raw)), nil
}
func (c *DeploymentChunk) FindDeployments(from, to int64) []*insight.DeploymentData {
out := make([]*insight.DeploymentData, 0, len(c.Deployments))
for _, d := range c.Deployments {
if from <= d.CompletedAt && d.CompletedAt <= to {
out = append(out, d)
}
}
return out
}
func (m *DeploymentBlockMetadata) FindChunks(from, to int64) []DeploymentChunkMetadata {
var out []DeploymentChunkMetadata
for _, m := range m.ChunkMetadata {
if overlap(from, to, m.MinTimestamp, m.MaxTimestamp) {
out = append(out, *m)
}
}
return out
}
func overlap(firstFrom, firstTo, secondFrom, secondTo int64) bool {
return !(firstTo < secondFrom || firstFrom > secondTo)
}
func (s *store) ListCompletedDeployments(ctx context.Context, projectID string, from, to int64) ([]*insight.DeploymentData, error) {
const rangeLimit time.Duration = 2 * 365 * 24 * time.Hour // 2 year
if from > to {
return nil, errInvalidArg
}
if float64(to-from) > rangeLimit.Seconds() {
return nil, errLargeDuration
}
var (
fromYear = time.Unix(from, 0).Year()
toYear = time.Unix(to, 0).Year()
out = make([]*insight.DeploymentData, 0)
)
for year := fromYear; year <= toYear; year++ {
blockID := makeDeploymentBlockID(year)
blockMetadata, err := s.loadBlockMetadata(ctx, projectID, blockID)
if errors.Is(err, filestore.ErrNotFound) {
continue
}
if err != nil {
return nil, err
}
chunkMDs := blockMetadata.FindChunks(from, to)
if len(chunkMDs) == 0 {
continue
}
for _, chunkMD := range chunkMDs {
chunk, err := s.loadChunk(ctx, projectID, blockID, chunkMD.ChunkID, chunkMD.Completed)
if errors.Is(err, filestore.ErrNotFound) {
continue
}
if err != nil {
return nil, err
}
deployments := chunk.FindDeployments(from, to)
out = append(out, deployments...)
}
}
return out, nil
}
func (s *store) PutCompletedDeployments(ctx context.Context, projectID string, ds []*insight.DeploymentData) error {
var lastErr error
blocks := groupCompletedDeploymentsByBlock(ds)
for _, b := range blocks {
if err := s.putCompletedDeploymentsBlock(ctx, projectID, b.BlockID, b.Deployments); err != nil {
lastErr = err
}
}
return lastErr
}
type blockData struct {
BlockID string
Deployments []*insight.DeploymentData
}
func groupCompletedDeploymentsByBlock(ds []*insight.DeploymentData) []*blockData {
if len(ds) == 0 {
return nil
}
var (
out []*blockData
curBlock *blockData
)
// To be sure, sort the deployment list again in order of CompletedAt.
sort.Slice(ds, func(i, j int) bool {
return ds[i].CompletedAt < ds[j].CompletedAt
})
for _, d := range ds {
var (
timestamp = d.CompletedAt
blockID = makeDeploymentBlockID(time.Unix(timestamp, 0).Year())
)
if curBlock == nil || curBlock.BlockID != blockID {
curBlock = &blockData{BlockID: blockID}
out = append(out, curBlock)
}
curBlock.Deployments = append(curBlock.Deployments, d)
}
return out
}
func (s *store) putCompletedDeploymentsBlock(ctx context.Context, projectID, blockID string, ds []*insight.DeploymentData) error {
block, err := s.loadBlockMetadata(ctx, projectID, blockID)
if err != nil {
if !errors.Is(err, filestore.ErrNotFound) {
return err
}
block = &DeploymentBlockMetadata{
BlockID: blockID,
}
if err := s.saveBlockMetadata(ctx, projectID, block); err != nil {
return fmt.Errorf("could not save metadata for new block: %w", err)
}
s.logger.Info(fmt.Sprintf("saved a new block metadata file: %s", blockID), zap.String("project", projectID))
}
for index, chunkIndex := 0, 0; ; chunkIndex++ {
// There is no more deployment to add.
if index >= len(ds) {
break
}
// Append new chunk if needed.
if chunkIndex >= len(block.ChunkMetadata) {
block.ChunkMetadata = append(block.ChunkMetadata, &DeploymentChunkMetadata{
ChunkID: makeDeploymentChunkID(chunkIndex),
ChunkIndex: chunkIndex,
MinTimestamp: ds[index].CompletedAt,
})
}
chunkMD := block.ChunkMetadata[chunkIndex]
if chunkMD.Completed {
continue
}
if chunkMD.Count >= s.chunkMaxCount {
chunkMD.Completed = true
continue
}
var (
from = index
addCount = len(ds) - from
)
if chunkMD.Count+addCount > s.chunkMaxCount {
addCount = s.chunkMaxCount - chunkMD.Count
}
index = from + addCount
adds := ds[from:index]
if err := s.putCompletedDeploymentsToChunk(ctx, projectID, blockID, chunkMD.ChunkID, adds); err != nil {
s.logger.Error("could not put deployments to chunk",
zap.String("project", projectID),
zap.String("block", blockID),
zap.String("chunk", chunkMD.ChunkID),
)
return fmt.Errorf("could not put deployments to chunk %s: %w", chunkMD.ChunkID, err)
}
// Update chunk metadata in block.
chunkMD.Count += len(adds)
if chunkMD.Count >= s.chunkMaxCount {
chunkMD.Completed = true
}
if chunkMD.MinTimestamp > ds[from].CompletedAt {
chunkMD.MinTimestamp = ds[from].CompletedAt
}
if chunkMD.MaxTimestamp < ds[index-1].CompletedAt {
chunkMD.MaxTimestamp = ds[index-1].CompletedAt
}
}
// Save block metadata.
if err := s.saveBlockMetadata(ctx, projectID, block); err != nil {
return fmt.Errorf("could not save block metadata: %w", err)
}
return nil
}
func (s *store) putCompletedDeploymentsToChunk(ctx context.Context, projectID, blockID, chunkID string, ds []*insight.DeploymentData) error {
chunk, err := s.loadChunk(ctx, projectID, blockID, chunkID, false)
if err != nil {
if !errors.Is(err, filestore.ErrNotFound) {
return err
}
chunk = &DeploymentChunk{
ChunkID: chunkID,
}
}
var (
mergedList = make([]*insight.DeploymentData, 0, len(chunk.Deployments)+len(ds))
dsMap = make(map[string]struct{}, len(ds))
duplicates = 0
)
for _, d := range chunk.Deployments {
dsMap[d.Id] = struct{}{}
mergedList = append(mergedList, d)
}
for _, d := range ds {
if _, ok := dsMap[d.Id]; ok {
duplicates++
continue
}
mergedList = append(mergedList, d)
}
chunk.Deployments = mergedList
if err := s.saveChunk(ctx, projectID, blockID, chunk); err != nil {
return fmt.Errorf("could not save chunk: %w", err)
}
s.logger.Info(fmt.Sprintf("stored %d deployments (%d duplicates) into chunk file: %s", len(ds), duplicates, chunkID))
return nil
}
func (s *store) loadBlockMetadata(ctx context.Context, projectID, blockID string) (*DeploymentBlockMetadata, error) {
path := makeCompletedDeploymentsBlockMetaFilePath(projectID, blockID)
data, err := s.fileStore.Get(ctx, path)
if err != nil {
return nil, err
}
var md DeploymentBlockMetadata
if err := json.Unmarshal(data, &md); err != nil {
return nil, err
}
return &md, nil
}
func (s *store) saveBlockMetadata(ctx context.Context, projectID string, block *DeploymentBlockMetadata) error {
data, err := json.Marshal(block)
if err != nil {
return err
}
path := makeCompletedDeploymentsBlockMetaFilePath(projectID, block.BlockID)
return s.fileStore.Put(ctx, path, data)
}
func (s *store) loadChunk(ctx context.Context, projectID, blockID, chunkID string, useCache bool) (*DeploymentChunk, error) {
var (
cacheKey = makeCompletedDeploymentChunkCacheKey(projectID, blockID, chunkID)
path = makeCompletedDeploymentsChunkFilePath(projectID, blockID, chunkID)
data []byte
)
if useCache {
if cdata, err := s.deploymentChunkCache.Get(cacheKey); err == nil {
data = cdata.([]byte)
s.logger.Info("successfully loaded deployment chunk from cache", zap.String("key", cacheKey))
} else {
data, err = s.fileStore.Get(ctx, path)
if err != nil {
return nil, err
}
if err := s.deploymentChunkCache.Put(cacheKey, data); err != nil {
s.logger.Error("failed to put deployment chunk to cache", zap.Error(err))
}
}
} else {
var err error
data, err = s.fileStore.Get(ctx, path)
if err != nil {
return nil, err
}
}
var c DeploymentChunk
if err := json.Unmarshal(data, &c); err != nil {
return nil, err
}
return &c, nil
}
func (s *store) saveChunk(ctx context.Context, projectID, blockID string, chunk *DeploymentChunk) error {
data, err := json.Marshal(chunk)
if err != nil {
return err
}
path := makeCompletedDeploymentsChunkFilePath(projectID, blockID, chunk.ChunkID)
return s.fileStore.Put(ctx, path, data)
}