2018-02-21 19:01:43 +00:00
|
|
|
package solver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2018-02-21 19:01:43 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewInMemoryCacheStorage() CacheKeyStorage {
|
2018-02-27 01:39:18 +00:00
|
|
|
return &inMemoryStore{
|
|
|
|
byID: map[string]*inMemoryKey{},
|
|
|
|
byResult: map[string]map[string]struct{}{},
|
|
|
|
}
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type inMemoryStore struct {
|
2018-02-27 01:39:18 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
byID map[string]*inMemoryKey
|
|
|
|
byResult map[string]map[string]struct{}
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type inMemoryKey struct {
|
2018-03-17 04:07:18 +00:00
|
|
|
id string
|
2018-02-27 01:39:18 +00:00
|
|
|
results map[string]CacheResult
|
|
|
|
links map[CacheInfoLink]map[string]struct{}
|
|
|
|
backlinks map[string]struct{}
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
func (s *inMemoryStore) Exists(id string) bool {
|
2018-02-21 19:01:43 +00:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2018-03-17 04:07:18 +00:00
|
|
|
if k, ok := s.byID[id]; ok {
|
2018-04-13 20:52:27 +00:00
|
|
|
return len(k.links) > 0 || len(k.results) > 0
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
2018-03-17 04:07:18 +00:00
|
|
|
return false
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:52:27 +00:00
|
|
|
func newInMemoryKey(id string) *inMemoryKey {
|
2018-03-17 04:07:18 +00:00
|
|
|
return &inMemoryKey{
|
|
|
|
results: map[string]CacheResult{},
|
|
|
|
links: map[CacheInfoLink]map[string]struct{}{},
|
|
|
|
backlinks: map[string]struct{}{},
|
|
|
|
id: id,
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 01:39:18 +00:00
|
|
|
func (s *inMemoryStore) Walk(fn func(string) error) error {
|
|
|
|
s.mu.RLock()
|
|
|
|
ids := make([]string, 0, len(s.byID))
|
|
|
|
for id := range s.byID {
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
s.mu.RUnlock()
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
if err := fn(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-21 19:01:43 +00:00
|
|
|
func (s *inMemoryStore) WalkResults(id string, fn func(CacheResult) error) error {
|
|
|
|
s.mu.RLock()
|
2018-02-27 01:39:18 +00:00
|
|
|
|
2018-02-21 19:01:43 +00:00
|
|
|
k, ok := s.byID[id]
|
|
|
|
if !ok {
|
2018-02-27 01:39:18 +00:00
|
|
|
s.mu.RUnlock()
|
2018-02-21 19:01:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-27 01:39:18 +00:00
|
|
|
copy := make([]CacheResult, 0, len(k.results))
|
2018-02-21 19:01:43 +00:00
|
|
|
for _, res := range k.results {
|
2018-02-27 01:39:18 +00:00
|
|
|
copy = append(copy, res)
|
|
|
|
}
|
|
|
|
s.mu.RUnlock()
|
|
|
|
|
|
|
|
for _, res := range copy {
|
2018-02-21 19:01:43 +00:00
|
|
|
if err := fn(res); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemoryStore) Load(id string, resultID string) (CacheResult, error) {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
k, ok := s.byID[id]
|
|
|
|
if !ok {
|
|
|
|
return CacheResult{}, errors.Wrapf(ErrNotFound, "no such key %s", id)
|
|
|
|
}
|
|
|
|
r, ok := k.results[resultID]
|
|
|
|
if !ok {
|
|
|
|
return CacheResult{}, errors.WithStack(ErrNotFound)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemoryStore) AddResult(id string, res CacheResult) error {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
k, ok := s.byID[id]
|
|
|
|
if !ok {
|
2018-04-13 20:52:27 +00:00
|
|
|
k = newInMemoryKey(id)
|
2018-03-17 04:07:18 +00:00
|
|
|
s.byID[id] = k
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
k.results[res.ID] = res
|
2018-02-27 01:39:18 +00:00
|
|
|
m, ok := s.byResult[res.ID]
|
|
|
|
if !ok {
|
|
|
|
m = map[string]struct{}{}
|
|
|
|
s.byResult[res.ID] = m
|
|
|
|
}
|
|
|
|
m[id] = struct{}{}
|
2018-02-21 19:01:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:01:50 +00:00
|
|
|
func (s *inMemoryStore) WalkIDsByResult(resultID string, fn func(string) error) error {
|
|
|
|
s.mu.Lock()
|
|
|
|
|
|
|
|
ids := map[string]struct{}{}
|
|
|
|
for id := range s.byResult[resultID] {
|
|
|
|
ids[id] = struct{}{}
|
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
for id := range ids {
|
|
|
|
if err := fn(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-21 19:01:43 +00:00
|
|
|
func (s *inMemoryStore) Release(resultID string) error {
|
2018-02-27 01:39:18 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
ids, ok := s.byResult[resultID]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for id := range ids {
|
|
|
|
k, ok := s.byID[id]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(k.results, resultID)
|
|
|
|
delete(s.byResult[resultID], id)
|
|
|
|
if len(s.byResult[resultID]) == 0 {
|
|
|
|
delete(s.byResult, resultID)
|
|
|
|
}
|
|
|
|
|
2018-02-27 22:48:08 +00:00
|
|
|
s.emptyBranchWithParents(k)
|
2018-02-27 01:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemoryStore) emptyBranchWithParents(k *inMemoryKey) {
|
2018-02-27 22:48:08 +00:00
|
|
|
if len(k.results) != 0 || len(k.links) != 0 {
|
2018-02-27 01:39:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for id := range k.backlinks {
|
|
|
|
p, ok := s.byID[id]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for l := range p.links {
|
2018-03-17 04:07:18 +00:00
|
|
|
delete(p.links[l], k.id)
|
2018-02-27 01:39:18 +00:00
|
|
|
if len(p.links[l]) == 0 {
|
|
|
|
delete(p.links, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.emptyBranchWithParents(p)
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
delete(s.byID, k.id)
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemoryStore) AddLink(id string, link CacheInfoLink, target string) error {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
k, ok := s.byID[id]
|
|
|
|
if !ok {
|
2018-04-13 20:52:27 +00:00
|
|
|
k = newInMemoryKey(id)
|
2018-03-17 04:07:18 +00:00
|
|
|
s.byID[id] = k
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
2018-02-27 01:39:18 +00:00
|
|
|
k2, ok := s.byID[target]
|
|
|
|
if !ok {
|
2018-04-13 20:52:27 +00:00
|
|
|
k2 = newInMemoryKey(target)
|
2018-03-17 04:07:18 +00:00
|
|
|
s.byID[target] = k2
|
2018-02-27 01:39:18 +00:00
|
|
|
}
|
2018-02-21 19:01:43 +00:00
|
|
|
m, ok := k.links[link]
|
|
|
|
if !ok {
|
|
|
|
m = map[string]struct{}{}
|
|
|
|
k.links[link] = m
|
|
|
|
}
|
|
|
|
|
2018-02-27 01:39:18 +00:00
|
|
|
k2.backlinks[id] = struct{}{}
|
2018-02-21 19:01:43 +00:00
|
|
|
m[target] = struct{}{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemoryStore) WalkLinks(id string, link CacheInfoLink, fn func(id string) error) error {
|
|
|
|
s.mu.RLock()
|
|
|
|
k, ok := s.byID[id]
|
|
|
|
if !ok {
|
2018-03-01 19:09:21 +00:00
|
|
|
s.mu.RUnlock()
|
2018-03-17 04:07:18 +00:00
|
|
|
return nil
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
2018-03-01 19:09:21 +00:00
|
|
|
var links []string
|
2018-02-21 19:01:43 +00:00
|
|
|
for target := range k.links[link] {
|
2018-03-01 19:09:21 +00:00
|
|
|
links = append(links, target)
|
|
|
|
}
|
|
|
|
s.mu.RUnlock()
|
|
|
|
|
|
|
|
for _, t := range links {
|
|
|
|
if err := fn(t); err != nil {
|
2018-02-21 19:01:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:52:27 +00:00
|
|
|
func (s *inMemoryStore) HasLink(id string, link CacheInfoLink, target string) bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
if k, ok := s.byID[id]; ok {
|
|
|
|
if v, ok := k.links[link]; ok {
|
|
|
|
if _, ok := v[target]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemoryStore) WalkBacklinks(id string, fn func(id string, link CacheInfoLink) error) error {
|
|
|
|
s.mu.RLock()
|
|
|
|
k, ok := s.byID[id]
|
|
|
|
if !ok {
|
|
|
|
s.mu.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var outIDs []string
|
|
|
|
var outLinks []CacheInfoLink
|
|
|
|
for bid := range k.backlinks {
|
|
|
|
b, ok := s.byID[bid]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for l, m := range b.links {
|
|
|
|
if _, ok := m[id]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
outIDs = append(outIDs, bid)
|
|
|
|
outLinks = append(outLinks, CacheInfoLink{
|
|
|
|
Digest: rootKey(l.Digest, l.Output),
|
|
|
|
Input: l.Input,
|
|
|
|
Selector: l.Selector,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.mu.RUnlock()
|
|
|
|
|
|
|
|
for i := range outIDs {
|
|
|
|
if err := fn(outIDs[i], outLinks[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-21 19:01:43 +00:00
|
|
|
func NewInMemoryResultStorage() CacheResultStorage {
|
|
|
|
return &inMemoryResultStore{m: &sync.Map{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
type inMemoryResultStore struct {
|
|
|
|
m *sync.Map
|
|
|
|
}
|
|
|
|
|
2019-01-10 16:43:27 +00:00
|
|
|
func (s *inMemoryResultStore) Save(r Result, createdAt time.Time) (CacheResult, error) {
|
2018-02-21 19:01:43 +00:00
|
|
|
s.m.Store(r.ID(), r)
|
2019-01-10 16:43:27 +00:00
|
|
|
return CacheResult{ID: r.ID(), CreatedAt: createdAt}, nil
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemoryResultStore) Load(ctx context.Context, res CacheResult) (Result, error) {
|
|
|
|
v, ok := s.m.Load(res.ID)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.WithStack(ErrNotFound)
|
|
|
|
}
|
|
|
|
return v.(Result), nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
func (s *inMemoryResultStore) LoadRemote(_ context.Context, _ CacheResult, _ session.Group) (*Remote, error) {
|
2018-02-21 19:01:43 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2018-02-27 01:39:18 +00:00
|
|
|
|
|
|
|
func (s *inMemoryResultStore) Exists(id string) bool {
|
|
|
|
_, ok := s.m.Load(id)
|
|
|
|
return ok
|
|
|
|
}
|