forked from concourse/concourse
/
resource_cache.go
247 lines (210 loc) · 5.88 KB
/
resource_cache.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
package db
import (
"crypto/sha256"
"database/sql"
"encoding/json"
"errors"
"fmt"
sq "github.com/Masterminds/squirrel"
"github.com/chenbh/concourse/v6/atc"
"github.com/chenbh/concourse/v6/atc/db/lock"
)
var ErrResourceCacheAlreadyExists = errors.New("resource-cache-already-exists")
var ErrResourceCacheDisappeared = errors.New("resource-cache-disappeared")
// ResourceCache represents an instance of a ResourceConfig's version.
//
// A ResourceCache is created by a `get`, an `image_resource`, or a resource
// type in a pipeline.
//
// ResourceCaches are garbage-collected by gc.ResourceCacheCollector.
type ResourceCacheDescriptor struct {
ResourceConfigDescriptor ResourceConfigDescriptor // The resource configuration.
Version atc.Version // The version of the resource.
Params atc.Params // The params used when fetching the version.
}
func (cache *ResourceCacheDescriptor) find(tx Tx, lockFactory lock.LockFactory, conn Conn) (UsedResourceCache, bool, error) {
resourceConfig, found, err := cache.ResourceConfigDescriptor.find(tx, lockFactory, conn)
if err != nil {
return nil, false, err
}
if !found {
return nil, false, nil
}
return cache.findWithResourceConfig(tx, resourceConfig, lockFactory, conn)
}
func (cache *ResourceCacheDescriptor) findOrCreate(
tx Tx,
lockFactory lock.LockFactory,
conn Conn,
) (UsedResourceCache, error) {
resourceConfig, err := cache.ResourceConfigDescriptor.findOrCreate(tx, lockFactory, conn)
if err != nil {
return nil, err
}
rc, found, err := cache.findWithResourceConfig(tx, resourceConfig, lockFactory, conn)
if err != nil {
return nil, err
}
if !found {
var id int
err = psql.Insert("resource_caches").
Columns(
"resource_config_id",
"version",
"version_md5",
"params_hash",
).
Values(
resourceConfig.ID(),
cache.version(),
sq.Expr("md5(?)", cache.version()),
paramsHash(cache.Params),
).
Suffix(`
ON CONFLICT (resource_config_id, version_md5, params_hash) DO UPDATE SET
resource_config_id = EXCLUDED.resource_config_id,
version = EXCLUDED.version,
version_md5 = EXCLUDED.version_md5,
params_hash = EXCLUDED.params_hash
RETURNING id
`).
RunWith(tx).
QueryRow().
Scan(&id)
if err != nil {
return nil, err
}
rc = &usedResourceCache{
id: id,
version: cache.Version,
resourceConfig: resourceConfig,
lockFactory: lockFactory,
conn: conn,
}
}
return rc, nil
}
func (cache *ResourceCacheDescriptor) use(
tx Tx,
rc UsedResourceCache,
user ResourceCacheUser,
) error {
cols := user.SQLMap()
cols["resource_cache_id"] = rc.ID()
var resourceCacheUseExists int
err := psql.Select("1").
From("resource_cache_uses").
Where(sq.Eq(cols)).
RunWith(tx).
QueryRow().
Scan(&resourceCacheUseExists)
if err != nil {
if err != sql.ErrNoRows {
return err
}
}
if err == nil {
// use already exists
return nil
}
_, err = psql.Insert("resource_cache_uses").
SetMap(cols).
RunWith(tx).
Exec()
return err
}
func (cache *ResourceCacheDescriptor) findWithResourceConfig(tx Tx, resourceConfig ResourceConfig, lockFactory lock.LockFactory, conn Conn) (UsedResourceCache, bool, error) {
var id int
err := psql.Select("id").
From("resource_caches").
Where(sq.Eq{
"resource_config_id": resourceConfig.ID(),
"params_hash": paramsHash(cache.Params),
}).
Where(sq.Expr("version_md5 = md5(?)", cache.version())).
Suffix("FOR SHARE").
RunWith(tx).
QueryRow().
Scan(&id)
if err != nil {
if err == sql.ErrNoRows {
return nil, false, nil
}
return nil, false, err
}
return &usedResourceCache{
id: id,
version: cache.Version,
resourceConfig: resourceConfig,
lockFactory: lockFactory,
conn: conn,
}, true, nil
}
func (cache *ResourceCacheDescriptor) version() string {
j, _ := json.Marshal(cache.Version)
return string(j)
}
func paramsHash(p atc.Params) string {
if p != nil {
return mapHash(p)
}
return mapHash(atc.Params{})
}
// UsedResourceCache is created whenever a ResourceCache is Created and/or
// Used.
//
// So long as the UsedResourceCache exists, the underlying ResourceCache can
// not be removed.
//
// UsedResourceCaches become unused by the gc.ResourceCacheCollector, which may
// then lead to the ResourceCache being garbage-collected.
//
// See FindOrCreateForBuild, FindOrCreateForResource, and
// FindOrCreateForResourceType for more information on when it becomes unused.
//go:generate counterfeiter . UsedResourceCache
type UsedResourceCache interface {
ID() int
Version() atc.Version
ResourceConfig() ResourceConfig
Destroy(Tx) (bool, error)
BaseResourceType() *UsedBaseResourceType
}
type usedResourceCache struct {
id int
resourceConfig ResourceConfig
version atc.Version
lockFactory lock.LockFactory
conn Conn
}
func (cache *usedResourceCache) ID() int { return cache.id }
func (cache *usedResourceCache) ResourceConfig() ResourceConfig { return cache.resourceConfig }
func (cache *usedResourceCache) Version() atc.Version { return cache.version }
func (cache *usedResourceCache) Destroy(tx Tx) (bool, error) {
rows, err := psql.Delete("resource_caches").
Where(sq.Eq{
"id": cache.id,
}).
RunWith(tx).
Exec()
if err != nil {
return false, err
}
affected, err := rows.RowsAffected()
if err != nil {
return false, err
}
if affected == 0 {
return false, ErrResourceCacheDisappeared
}
return true, nil
}
func (cache *usedResourceCache) BaseResourceType() *UsedBaseResourceType {
if cache.resourceConfig.CreatedByBaseResourceType() != nil {
return cache.resourceConfig.CreatedByBaseResourceType()
}
return cache.resourceConfig.CreatedByResourceCache().BaseResourceType()
}
func mapHash(m map[string]interface{}) string {
j, _ := json.Marshal(m)
return fmt.Sprintf("%x", sha256.Sum256(j))
}