forked from vmware-archive/atc
/
resource_config.go
253 lines (207 loc) · 6.15 KB
/
resource_config.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
package db
import (
"crypto/sha256"
"database/sql"
"encoding/json"
"errors"
"fmt"
"code.cloudfoundry.org/lager"
sq "github.com/Masterminds/squirrel"
"github.com/concourse/atc"
"github.com/lib/pq"
)
var ErrResourceConfigAlreadyExists = errors.New("resource config already exists")
var ErrResourceConfigDisappeared = errors.New("resource config disappeared")
var ErrResourceConfigParentDisappeared = errors.New("resource config parent disappeared")
var ErrBaseResourceTypeNotFound = errors.New("base resource type not found")
// ResourceConfig represents a resource type and config source.
//
// Resources in a pipeline, resource types in a pipeline, and `image_resource`
// fields in a task all result in a reference to a ResourceConfig.
//
// ResourceConfigs are garbage-collected by gc.ResourceConfigCollector.
type ResourceConfig struct {
// A resource type provided by a resource.
CreatedByResourceCache *ResourceCache
// A resource type provided by a worker.
CreatedByBaseResourceType *BaseResourceType
// The resource's source configuration.
Source atc.Source
}
// UsedResourceConfig is created whenever a ResourceConfig is Created and/or
// Used.
//
// So long as the UsedResourceConfig exists, the underlying ResourceConfig can
// not be removed.
//
// UsedResourceConfigs become unused by the gc.ResourceConfigCollector, which
// may then lead to the ResourceConfig being garbage-collected.
//
// See FindOrCreateForBuild, FindOrCreateForResource, and
// FindOrCreateForResourceType for more information on when it becomes unused.
type UsedResourceConfig struct {
ID int
CreatedByResourceCache *UsedResourceCache
CreatedByBaseResourceType *UsedBaseResourceType
}
func (resourceConfig *UsedResourceConfig) OriginBaseResourceType() *UsedBaseResourceType {
if resourceConfig.CreatedByBaseResourceType != nil {
return resourceConfig.CreatedByBaseResourceType
} else {
return resourceConfig.CreatedByResourceCache.ResourceConfig.OriginBaseResourceType()
}
}
func (resourceConfig ResourceConfig) findOrCreate(logger lager.Logger, tx Tx, user ResourceUser, forColumnName string, forColumnID int) (*UsedResourceConfig, error) {
urc := &UsedResourceConfig{}
var parentID int
var parentColumnName string
if resourceConfig.CreatedByResourceCache != nil {
parentColumnName = "resource_cache_id"
resourceCache, err := user.UseResourceCache(logger, tx, *resourceConfig.CreatedByResourceCache)
if err != nil {
return nil, err
}
parentID = resourceCache.ID
urc.CreatedByResourceCache = resourceCache
}
if resourceConfig.CreatedByBaseResourceType != nil {
parentColumnName = "base_resource_type_id"
var err error
var found bool
urc.CreatedByBaseResourceType, found, err = resourceConfig.CreatedByBaseResourceType.Find(tx)
if err != nil {
return nil, err
}
if !found {
return nil, ErrBaseResourceTypeNotFound
}
parentID = urc.CreatedByBaseResourceType.ID
}
id, found, err := resourceConfig.findWithParentID(tx, parentColumnName, parentID)
if err != nil {
return nil, err
}
if !found {
err := psql.Insert("resource_configs").
Columns(
parentColumnName,
"source_hash",
).
Values(
parentID,
mapHash(resourceConfig.Source),
).
Suffix("RETURNING id").
RunWith(tx).
QueryRow().
Scan(&id)
if err != nil {
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code.Name() == "unique_violation" {
return nil, ErrSafeRetryFindOrCreate
}
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code.Name() == "foreign_key_violation" {
return nil, ErrSafeRetryFindOrCreate
}
return nil, err
}
}
urc.ID = id
var resourceConfigUseExists int
err = psql.Select("1").
From("resource_config_uses").
Where(sq.Eq{
"resource_config_id": id,
forColumnName: forColumnID,
}).
RunWith(tx).
QueryRow().
Scan(&resourceConfigUseExists)
if err != nil {
if err == sql.ErrNoRows {
_, err = psql.Insert("resource_config_uses").
Columns(
"resource_config_id",
forColumnName,
).
Values(
id,
forColumnID,
).
RunWith(tx).
Exec()
if err != nil {
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code.Name() == "foreign_key_violation" {
if pqErr.Constraint == "resource_config_uses_resource_config_id_fkey" {
return nil, ErrSafeRetryFindOrCreate
} else {
return nil, UserDisappearedError{user}
}
}
return nil, err
}
return urc, nil
}
return nil, err
}
return urc, nil
}
func (resourceConfig ResourceConfig) Find(tx Tx) (*UsedResourceConfig, bool, error) {
urc := &UsedResourceConfig{}
var parentID int
var parentColumnName string
if resourceConfig.CreatedByResourceCache != nil {
parentColumnName = "resource_cache_id"
resourceCache, found, err := resourceConfig.CreatedByResourceCache.Find(tx)
if err != nil {
return nil, false, err
}
if !found {
return nil, false, nil
}
parentID = resourceCache.ID
urc.CreatedByResourceCache = resourceCache
}
if resourceConfig.CreatedByBaseResourceType != nil {
parentColumnName = "base_resource_type_id"
var err error
var found bool
urc.CreatedByBaseResourceType, found, err = resourceConfig.CreatedByBaseResourceType.Find(tx)
if err != nil {
return nil, false, err
}
if !found {
return nil, false, nil
}
parentID = urc.CreatedByBaseResourceType.ID
}
id, found, err := resourceConfig.findWithParentID(tx, parentColumnName, parentID)
if err != nil {
return nil, false, err
}
if !found {
return nil, false, nil
}
urc.ID = id
return urc, true, nil
}
func (resourceConfig ResourceConfig) lockName() (string, error) {
resourceConfigJSON, err := json.Marshal(resourceConfig)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", sha256.Sum256(resourceConfigJSON)), nil
}
func (resourceConfig ResourceConfig) findWithParentID(tx Tx, parentColumnName string, parentID int) (int, bool, error) {
var id int
err := psql.Select("id").From("resource_configs").Where(sq.Eq{
parentColumnName: parentID,
"source_hash": mapHash(resourceConfig.Source),
}).RunWith(tx).QueryRow().Scan(&id)
if err != nil {
if err == sql.ErrNoRows {
return 0, false, nil
}
return 0, false, err
}
return id, true, nil
}