forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offload_node_status_repo.go
229 lines (204 loc) · 6.17 KB
/
offload_node_status_repo.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
package sqldb
import (
"encoding/json"
"fmt"
"hash/fnv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"upper.io/db.v3"
"upper.io/db.v3/lib/sqlbuilder"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/util/env"
)
const OffloadNodeStatusDisabled = "Workflow has offloaded nodes, but offloading has been disabled"
type UUIDVersion struct {
UID string `db:"uid"`
Version string `db:"version"`
}
type OffloadNodeStatusRepo interface {
Save(uid, namespace string, nodes wfv1.Nodes) (string, error)
Get(uid, version string) (wfv1.Nodes, error)
List(namespace string) (map[UUIDVersion]wfv1.Nodes, error)
ListOldOffloads(namespace string) (map[string][]string, error)
Delete(uid, version string) error
IsEnabled() bool
}
func NewOffloadNodeStatusRepo(session sqlbuilder.Database, clusterName, tableName string) (OffloadNodeStatusRepo, error) {
// this environment variable allows you to make Argo Workflows delete offloaded data more or less aggressively,
// useful for testing
ttl := env.LookupEnvDurationOr("OFFLOAD_NODE_STATUS_TTL", 5*time.Minute)
log.WithField("ttl", ttl).Info("Node status offloading config")
return &nodeOffloadRepo{session: session, clusterName: clusterName, tableName: tableName, ttl: ttl}, nil
}
type nodesRecord struct {
ClusterName string `db:"clustername"`
UUIDVersion
Namespace string `db:"namespace"`
Nodes string `db:"nodes"`
}
type nodeOffloadRepo struct {
session sqlbuilder.Database
clusterName string
tableName string
// time to live - at what ttl an offload becomes old
ttl time.Duration
}
func (wdc *nodeOffloadRepo) IsEnabled() bool {
return true
}
func nodeStatusVersion(s wfv1.Nodes) (string, string, error) {
marshalled, err := json.Marshal(s)
if err != nil {
return "", "", err
}
h := fnv.New32()
_, _ = h.Write(marshalled)
return string(marshalled), fmt.Sprintf("fnv:%v", h.Sum32()), nil
}
func (wdc *nodeOffloadRepo) Save(uid, namespace string, nodes wfv1.Nodes) (string, error) {
marshalled, version, err := nodeStatusVersion(nodes)
if err != nil {
return "", err
}
record := &nodesRecord{
ClusterName: wdc.clusterName,
UUIDVersion: UUIDVersion{
UID: uid,
Version: version,
},
Namespace: namespace,
Nodes: marshalled,
}
logCtx := log.WithFields(log.Fields{"uid": uid, "version": version})
logCtx.Debug("Offloading nodes")
_, err = wdc.session.Collection(wdc.tableName).Insert(record)
if err != nil {
// if we have a duplicate, then it must have the same clustername+uid+version, which MUST mean that we
// have already written this record
if !isDuplicateKeyError(err) {
return "", err
}
logCtx.WithField("err", err).Info("Ignoring duplicate key error")
}
logCtx.Debug("Nodes offloaded, cleaning up old offloads")
// This might fail, which kind of fine (maybe a bug).
// It might not delete all records, which is also fine, as we always key on resource version.
// We also want to keep enough around so that we can service watches.
rs, err := wdc.session.
DeleteFrom(wdc.tableName).
Where(db.Cond{"clustername": wdc.clusterName}).
And(db.Cond{"uid": uid}).
And(db.Cond{"version <>": version}).
And(wdc.oldOffload()).
Exec()
if err != nil {
return "", err
}
rowsAffected, err := rs.RowsAffected()
if err != nil {
return "", err
}
logCtx.WithField("rowsAffected", rowsAffected).Debug("Deleted offloaded nodes")
return version, nil
}
func isDuplicateKeyError(err error) bool {
// postgres
if strings.Contains(err.Error(), "duplicate key") {
return true
}
// mysql
if strings.Contains(err.Error(), "Duplicate entry") {
return true
}
return false
}
func (wdc *nodeOffloadRepo) Get(uid, version string) (wfv1.Nodes, error) {
log.WithFields(log.Fields{"uid": uid, "version": version}).Debug("Getting offloaded nodes")
r := &nodesRecord{}
err := wdc.session.
SelectFrom(wdc.tableName).
Where(db.Cond{"clustername": wdc.clusterName}).
And(db.Cond{"uid": uid}).
And(db.Cond{"version": version}).
One(r)
if err != nil {
return nil, err
}
nodes := &wfv1.Nodes{}
err = json.Unmarshal([]byte(r.Nodes), nodes)
if err != nil {
return nil, err
}
return *nodes, nil
}
func (wdc *nodeOffloadRepo) List(namespace string) (map[UUIDVersion]wfv1.Nodes, error) {
log.WithFields(log.Fields{"namespace": namespace}).Debug("Listing offloaded nodes")
var records []nodesRecord
err := wdc.session.
Select("uid", "version", "nodes").
From(wdc.tableName).
Where(db.Cond{"clustername": wdc.clusterName}).
And(namespaceEqual(namespace)).
All(&records)
if err != nil {
return nil, err
}
res := make(map[UUIDVersion]wfv1.Nodes)
for _, r := range records {
nodes := &wfv1.Nodes{}
err = json.Unmarshal([]byte(r.Nodes), nodes)
if err != nil {
return nil, err
}
res[UUIDVersion{UID: r.UID, Version: r.Version}] = *nodes
}
return res, nil
}
func (wdc *nodeOffloadRepo) ListOldOffloads(namespace string) (map[string][]string, error) {
log.WithFields(log.Fields{"namespace": namespace}).Debug("Listing old offloaded nodes")
var records []UUIDVersion
err := wdc.session.
Select("uid", "version").
From(wdc.tableName).
Where(db.Cond{"clustername": wdc.clusterName}).
And(namespaceEqual(namespace)).
And(wdc.oldOffload()).
All(&records)
if err != nil {
return nil, err
}
x := make(map[string][]string)
for _, r := range records {
x[r.UID] = append(x[r.UID], r.Version)
}
return x, nil
}
func (wdc *nodeOffloadRepo) Delete(uid, version string) error {
if uid == "" {
return fmt.Errorf("invalid uid")
}
if version == "" {
return fmt.Errorf("invalid version")
}
logCtx := log.WithFields(log.Fields{"uid": uid, "version": version})
logCtx.Debug("Deleting offloaded nodes")
rs, err := wdc.session.
DeleteFrom(wdc.tableName).
Where(db.Cond{"clustername": wdc.clusterName}).
And(db.Cond{"uid": uid}).
And(db.Cond{"version": version}).
Exec()
if err != nil {
return err
}
rowsAffected, err := rs.RowsAffected()
if err != nil {
return err
}
logCtx.WithField("rowsAffected", rowsAffected).Debug("Deleted offloaded nodes")
return nil
}
func (wdc *nodeOffloadRepo) oldOffload() string {
return fmt.Sprintf("updatedat < current_timestamp - interval '%d' second", int(wdc.ttl.Seconds()))
}