| 1 |
package cacheimport |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"slices" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/moby/buildkit/session" |
| 9 |
"github.com/moby/buildkit/solver" |
| 10 |
"github.com/moby/buildkit/util/compression" |
| 11 |
"github.com/moby/buildkit/worker" |
| 12 |
digest "github.com/opencontainers/go-digest" |
| 13 |
"github.com/pkg/errors" |
| 14 |
) |
| 15 |
|
| 16 |
func NewCacheKeyStorage(cc *CacheChains, w worker.Worker) (solver.CacheKeyStorage, solver.CacheResultStorage, error) { |
| 17 |
storage := &cacheKeyStorage{ |
| 18 |
byID: map[string]*itemWithOutgoingLinks{}, |
| 19 |
byItem: map[*item]string{}, |
| 20 |
byResult: map[string]map[string]struct{}{}, |
| 21 |
} |
| 22 |
|
| 23 |
cc.computeIDs() |
| 24 |
|
| 25 |
for it := range cc.leaves() { |
| 26 |
visited := make(map[*item]*itemWithOutgoingLinks) |
| 27 |
if _, err := addItemToStorage(storage, it, visited); err != nil { |
| 28 |
return nil, nil, err |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
results := &cacheResultStorage{ |
| 33 |
w: w, |
| 34 |
byID: storage.byID, |
| 35 |
byItem: storage.byItem, |
| 36 |
byResult: storage.byResult, |
| 37 |
} |
| 38 |
|
| 39 |
return storage, results, nil |
| 40 |
} |
| 41 |
|
| 42 |
func addItemToStorage(k *cacheKeyStorage, it *item, visited map[*item]*itemWithOutgoingLinks) (*itemWithOutgoingLinks, error) { |
| 43 |
if v, ok := visited[it]; ok { |
| 44 |
return v, nil |
| 45 |
} |
| 46 |
visited[it] = nil |
| 47 |
|
| 48 |
if id, ok := k.byItem[it]; ok { |
| 49 |
if id == "" { |
| 50 |
return nil, errors.Errorf("invalid loop") |
| 51 |
} |
| 52 |
return k.byID[id], nil |
| 53 |
} |
| 54 |
|
| 55 |
id := it.id |
| 56 |
k.byItem[it] = "" |
| 57 |
|
| 58 |
for i, m := range it.parents { |
| 59 |
for l := range m { |
| 60 |
src, err := addItemToStorage(k, l.src, visited) |
| 61 |
if err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
if src == nil { |
| 65 |
continue |
| 66 |
} |
| 67 |
cl := nlink{ |
| 68 |
input: i, |
| 69 |
dgst: it.dgst, |
| 70 |
selector: l.selector, |
| 71 |
} |
| 72 |
src.links[cl] = append(src.links[cl], id) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
k.byItem[it] = id |
| 77 |
|
| 78 |
itl := &itemWithOutgoingLinks{ |
| 79 |
item: it, |
| 80 |
links: map[nlink][]string{}, |
| 81 |
} |
| 82 |
|
| 83 |
k.byID[id] = itl |
| 84 |
|
| 85 |
seen := map[string]struct{}{} |
| 86 |
for _, res := range it.results { |
| 87 |
resultID := remoteID(res.Result) |
| 88 |
if _, ok := seen[resultID]; ok { |
| 89 |
continue |
| 90 |
} |
| 91 |
seen[resultID] = struct{}{} |
| 92 |
ids, ok := k.byResult[resultID] |
| 93 |
if !ok { |
| 94 |
ids = map[string]struct{}{} |
| 95 |
k.byResult[resultID] = ids |
| 96 |
} |
| 97 |
ids[id] = struct{}{} |
| 98 |
} |
| 99 |
visited[it] = itl |
| 100 |
return itl, nil |
| 101 |
} |
| 102 |
|
| 103 |
type cacheKeyStorage struct { |
| 104 |
byID map[string]*itemWithOutgoingLinks |
| 105 |
byItem map[*item]string |
| 106 |
byResult map[string]map[string]struct{} |
| 107 |
} |
| 108 |
|
| 109 |
type itemWithOutgoingLinks struct { |
| 110 |
*item |
| 111 |
links map[nlink][]string |
| 112 |
} |
| 113 |
|
| 114 |
func (cs *cacheKeyStorage) Exists(id string) bool { |
| 115 |
_, ok := cs.byID[id] |
| 116 |
return ok |
| 117 |
} |
| 118 |
|
| 119 |
func (cs *cacheKeyStorage) Walk(cb func(id string) error) error { |
| 120 |
for id := range cs.byID { |
| 121 |
if err := cb(id); err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
} |
| 125 |
return nil |
| 126 |
} |
| 127 |
|
| 128 |
func (cs *cacheKeyStorage) WalkResults(id string, fn func(solver.CacheResult) error) error { |
| 129 |
it, ok := cs.byID[id] |
| 130 |
if !ok { |
| 131 |
return nil |
| 132 |
} |
| 133 |
seen := map[string]struct{}{} |
| 134 |
for _, res := range it.results { |
| 135 |
id := remoteID(res.Result) |
| 136 |
if _, ok := seen[id]; ok { |
| 137 |
continue |
| 138 |
} |
| 139 |
if err := fn(solver.CacheResult{ID: id, CreatedAt: res.CreatedAt}); err != nil { |
| 140 |
return err |
| 141 |
} |
| 142 |
seen[id] = struct{}{} |
| 143 |
} |
| 144 |
return nil |
| 145 |
} |
| 146 |
|
| 147 |
func (cs *cacheKeyStorage) Load(id string, resultID string) (solver.CacheResult, error) { |
| 148 |
var res solver.CacheResult |
| 149 |
if err := cs.WalkResults(id, func(r solver.CacheResult) error { |
| 150 |
if r.ID == resultID { |
| 151 |
res = r |
| 152 |
return nil |
| 153 |
} |
| 154 |
return nil |
| 155 |
}); err != nil { |
| 156 |
return solver.CacheResult{}, errors.Wrapf(err, "failed to load cache result for %s", id) |
| 157 |
} |
| 158 |
return res, nil |
| 159 |
} |
| 160 |
|
| 161 |
func (cs *cacheKeyStorage) AddResult(id string, res solver.CacheResult) error { |
| 162 |
return nil |
| 163 |
} |
| 164 |
|
| 165 |
func (cs *cacheKeyStorage) Release(resultID string) error { |
| 166 |
return nil |
| 167 |
} |
| 168 |
func (cs *cacheKeyStorage) AddLink(id string, link solver.CacheInfoLink, target string) error { |
| 169 |
return nil |
| 170 |
} |
| 171 |
|
| 172 |
func (cs *cacheKeyStorage) WalkLinksAll(id string, fn func(id string, link solver.CacheInfoLink) error) error { |
| 173 |
it, ok := cs.byID[id] |
| 174 |
if !ok { |
| 175 |
return nil |
| 176 |
} |
| 177 |
for nl, ids := range it.links { |
| 178 |
for _, id2 := range ids { |
| 179 |
if err := fn(id2, solver.CacheInfoLink{ |
| 180 |
Input: solver.Index(nl.input), |
| 181 |
Selector: digest.Digest(nl.selector), |
| 182 |
Digest: nl.dgst, |
| 183 |
}); err != nil { |
| 184 |
return err |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
return nil |
| 189 |
} |
| 190 |
|
| 191 |
func (cs *cacheKeyStorage) WalkLinks(id string, link solver.CacheInfoLink, fn func(id string) error) error { |
| 192 |
it, ok := cs.byID[id] |
| 193 |
if !ok { |
| 194 |
return nil |
| 195 |
} |
| 196 |
for _, id := range it.links[nlink{ |
| 197 |
dgst: outputKey(link.Digest, int(link.Output)), |
| 198 |
input: int(link.Input), |
| 199 |
selector: link.Selector.String(), |
| 200 |
}] { |
| 201 |
if err := fn(id); err != nil { |
| 202 |
return err |
| 203 |
} |
| 204 |
} |
| 205 |
return nil |
| 206 |
} |
| 207 |
|
| 208 |
func (cs *cacheKeyStorage) WalkBacklinks(id string, fn func(id string, link solver.CacheInfoLink) error) error { |
| 209 |
for k, it := range cs.byID { |
| 210 |
for nl, ids := range it.links { |
| 211 |
for _, id2 := range ids { |
| 212 |
if id == id2 { |
| 213 |
if err := fn(k, solver.CacheInfoLink{ |
| 214 |
Input: solver.Index(nl.input), |
| 215 |
Selector: digest.Digest(nl.selector), |
| 216 |
Digest: nl.dgst, |
| 217 |
}); err != nil { |
| 218 |
return err |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
return nil |
| 225 |
} |
| 226 |
|
| 227 |
func (cs *cacheKeyStorage) WalkIDsByResult(id string, fn func(id string) error) error { |
| 228 |
ids := cs.byResult[id] |
| 229 |
for id := range ids { |
| 230 |
if err := fn(id); err != nil { |
| 231 |
return err |
| 232 |
} |
| 233 |
} |
| 234 |
return nil |
| 235 |
} |
| 236 |
|
| 237 |
func (cs *cacheKeyStorage) HasLink(id string, link solver.CacheInfoLink, target string) bool { |
| 238 |
l := nlink{ |
| 239 |
dgst: outputKey(link.Digest, int(link.Output)), |
| 240 |
input: int(link.Input), |
| 241 |
selector: link.Selector.String(), |
| 242 |
} |
| 243 |
if it, ok := cs.byID[id]; ok { |
| 244 |
if slices.Contains(it.links[l], target) { |
| 245 |
return true |
| 246 |
} |
| 247 |
} |
| 248 |
return false |
| 249 |
} |
| 250 |
|
| 251 |
type cacheResultStorage struct { |
| 252 |
w worker.Worker |
| 253 |
byID map[string]*itemWithOutgoingLinks |
| 254 |
byResult map[string]map[string]struct{} |
| 255 |
byItem map[*item]string |
| 256 |
} |
| 257 |
|
| 258 |
func (cs *cacheResultStorage) Save(res solver.Result, createdAt time.Time) (solver.CacheResult, error) { |
| 259 |
return solver.CacheResult{}, errors.Errorf("importer is immutable") |
| 260 |
} |
| 261 |
|
| 262 |
func (cs *cacheResultStorage) LoadWithParents(ctx context.Context, res solver.CacheResult) (map[string]solver.Result, error) { |
| 263 |
m := map[string]solver.Result{} |
| 264 |
|
| 265 |
visited := make(map[*item]struct{}) |
| 266 |
|
| 267 |
ids, ok := cs.byResult[res.ID] |
| 268 |
if !ok || len(ids) == 0 { |
| 269 |
return nil, errors.WithStack(solver.ErrNotFound) |
| 270 |
} |
| 271 |
|
| 272 |
for id := range ids { |
| 273 |
v, ok := cs.byID[id] |
| 274 |
if ok { |
| 275 |
if _, ok := visited[v.item]; ok { |
| 276 |
continue |
| 277 |
} |
| 278 |
for _, result := range v.results { |
| 279 |
resultID := remoteID(result.Result) |
| 280 |
if resultID == res.ID { |
| 281 |
if err := v.walkAllResults(func(i *item) error { |
| 282 |
for _, subRes := range i.results { |
| 283 |
id, ok := cs.byItem[i] |
| 284 |
if !ok { |
| 285 |
return nil |
| 286 |
} |
| 287 |
if isSubRemote(*subRes.Result, *result.Result) { |
| 288 |
ref, err := cs.w.FromRemote(ctx, subRes.Result) |
| 289 |
if err != nil { |
| 290 |
return err |
| 291 |
} |
| 292 |
m[id] = worker.NewWorkerRefResult(ref, cs.w) |
| 293 |
} |
| 294 |
} |
| 295 |
return nil |
| 296 |
}, visited); err != nil { |
| 297 |
for _, v := range m { |
| 298 |
v.Release(context.TODO()) |
| 299 |
} |
| 300 |
return nil, err |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
return m, nil |
| 308 |
} |
| 309 |
|
| 310 |
func (cs *cacheResultStorage) Load(ctx context.Context, res solver.CacheResult) (solver.Result, error) { |
| 311 |
item := cs.byResultID(res.ID) |
| 312 |
for _, r := range item.results { |
| 313 |
resultID := remoteID(r.Result) |
| 314 |
if resultID != res.ID { |
| 315 |
continue |
| 316 |
} |
| 317 |
ref, err := cs.w.FromRemote(ctx, r.Result) |
| 318 |
if err != nil { |
| 319 |
return nil, errors.Wrap(err, "failed to load result from remote") |
| 320 |
} |
| 321 |
return worker.NewWorkerRefResult(ref, cs.w), nil |
| 322 |
} |
| 323 |
return nil, errors.WithStack(solver.ErrNotFound) |
| 324 |
} |
| 325 |
|
| 326 |
func (cs *cacheResultStorage) LoadRemotes(ctx context.Context, res solver.CacheResult, compressionopts *compression.Config, _ session.Group) ([]*solver.Remote, error) { |
| 327 |
if it := cs.byResultID(res.ID); it != nil { |
| 328 |
for _, r := range it.results { |
| 329 |
if compressionopts == nil { |
| 330 |
resultID := remoteID(r.Result) |
| 331 |
if resultID != res.ID { |
| 332 |
continue |
| 333 |
} |
| 334 |
return []*solver.Remote{r.Result}, nil |
| 335 |
} |
| 336 |
// Any of blobs in the remote must meet the specified compression option. |
| 337 |
match := false |
| 338 |
for _, desc := range r.Result.Descriptors { |
| 339 |
m := compression.IsMediaType(compressionopts.Type, desc.MediaType) |
| 340 |
match = match || m |
| 341 |
if compressionopts.Force && !m { |
| 342 |
match = false |
| 343 |
break |
| 344 |
} |
| 345 |
} |
| 346 |
if match { |
| 347 |
return []*solver.Remote{r.Result}, nil |
| 348 |
} |
| 349 |
} |
| 350 |
return nil, nil // return nil as it's best effort. |
| 351 |
} |
| 352 |
return nil, errors.WithStack(solver.ErrNotFound) |
| 353 |
} |
| 354 |
|
| 355 |
func (cs *cacheResultStorage) Exists(ctx context.Context, id string) bool { |
| 356 |
return cs.byResultID(id) != nil |
| 357 |
} |
| 358 |
|
| 359 |
func (cs *cacheResultStorage) byResultID(resultID string) *itemWithOutgoingLinks { |
| 360 |
m, ok := cs.byResult[resultID] |
| 361 |
if !ok || len(m) == 0 { |
| 362 |
return nil |
| 363 |
} |
| 364 |
|
| 365 |
for id := range m { |
| 366 |
it, ok := cs.byID[id] |
| 367 |
if ok { |
| 368 |
return it |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
return nil |
| 373 |
} |
| 374 |
|
| 375 |
// unique ID per remote. this ID is not stable. |
| 376 |
func remoteID(r *solver.Remote) string { |
| 377 |
dgstr := digest.Canonical.Digester() |
| 378 |
for _, desc := range r.Descriptors { |
| 379 |
dgstr.Hash().Write([]byte(desc.Digest)) |
| 380 |
} |
| 381 |
return dgstr.Digest().String() |
| 382 |
} |