forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_cache.go
218 lines (185 loc) · 4.9 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
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 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 ResourceCache struct {
ResourceConfig ResourceConfig // The resource configuration.
Version atc.Version // The version of the resource.
Params atc.Params // The params used when fetching the version.
}
// 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.
type UsedResourceCache struct {
ID int
ResourceConfig *UsedResourceConfig
Version atc.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 ResourceCache) Find(tx Tx) (*UsedResourceCache, bool, error) {
usedResourceConfig, found, err := cache.ResourceConfig.Find(tx)
if err != nil {
return nil, false, err
}
if !found {
return nil, false, nil
}
return cache.findWithResourceConfig(tx, usedResourceConfig)
}
func (cache ResourceCache) findOrCreate(
logger lager.Logger,
tx Tx,
) (*UsedResourceCache, error) {
usedResourceConfig, err := cache.ResourceConfig.findOrCreate(logger, tx)
if err != nil {
return nil, err
}
rc, found, err := cache.findWithResourceConfig(tx, usedResourceConfig)
if err != nil {
return nil, err
}
if !found {
var id int
err = psql.Insert("resource_caches").
Columns(
"resource_config_id",
"version",
"params_hash",
).
Values(
usedResourceConfig.ID,
cache.version(),
paramsHash(cache.Params),
).
Suffix("RETURNING id").
RunWith(tx).
QueryRow().
Scan(&id)
if err != nil {
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code.Name() == pqFKeyViolationErrCode {
return nil, ErrSafeRetryFindOrCreate
}
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code.Name() == pqUniqueViolationErrCode {
return nil, ErrSafeRetryFindOrCreate
}
return nil, err
}
rc = &UsedResourceCache{
ID: id,
ResourceConfig: usedResourceConfig,
Version: cache.Version,
}
}
return rc, nil
}
func (cache ResourceCache) use(
logger lager.Logger,
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 ResourceCache) findWithResourceConfig(tx Tx, resourceConfig *UsedResourceConfig) (*UsedResourceCache, bool, error) {
var id int
err := psql.Select("id").From("resource_caches").Where(sq.Eq{
"resource_config_id": resourceConfig.ID,
"version": cache.version(),
"params_hash": paramsHash(cache.Params),
}).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,
ResourceConfig: resourceConfig,
Version: cache.Version,
}, true, nil
}
func (cache ResourceCache) version() string {
j, _ := json.Marshal(cache.Version)
return string(j)
}
func (cache *UsedResourceCache) BaseResourceType() *UsedBaseResourceType {
if cache.ResourceConfig.CreatedByBaseResourceType != nil {
return cache.ResourceConfig.CreatedByBaseResourceType
}
return cache.ResourceConfig.CreatedByResourceCache.BaseResourceType()
}
func paramsHash(p atc.Params) string {
if p != nil {
return mapHash(p)
}
return mapHash(atc.Params{})
}
func mapHash(m map[string]interface{}) string {
j, _ := json.Marshal(m)
return fmt.Sprintf("%x", sha256.Sum256(j))
}