refactor(server): Clean up cache implementation

A couple of minor fixes and improvements to the cache implementation.
This commit is contained in:
Vincent Ambo 2019-10-02 18:00:35 +01:00 committed by Vincent Ambo
parent 0698d7f2aa
commit 1308a6e1fd
3 changed files with 26 additions and 27 deletions

View file

@ -355,7 +355,7 @@ func uploadHashLayer(ctx context.Context, s *State, key string, data io.Reader)
return nil, err return nil, err
} }
cacheBuild(ctx, &s.Cache, s.Bucket, key, build) cacheBuild(ctx, s, key, build)
log.Printf("Uploaded layer sha256:%s (%v bytes written)", build.SHA256, size) log.Printf("Uploaded layer sha256:%s (%v bytes written)", build.SHA256, size)

View file

@ -21,8 +21,6 @@ import (
"log" "log"
"os" "os"
"sync" "sync"
"cloud.google.com/go/storage"
) )
type Build struct { type Build struct {
@ -72,29 +70,29 @@ func (c *LocalCache) localCacheManifest(key, path string) {
// Retrieve a cached build from the local cache. // Retrieve a cached build from the local cache.
func (c *LocalCache) buildFromLocalCache(key string) (*Build, bool) { func (c *LocalCache) buildFromLocalCache(key string) (*Build, bool) {
c.bmtx.RLock() c.lmtx.RLock()
b, ok := c.lcache[key] b, ok := c.lcache[key]
c.bmtx.RUnlock() c.lmtx.RUnlock()
return &b, ok return &b, ok
} }
// Add a build result to the local cache. // Add a build result to the local cache.
func (c *LocalCache) localCacheBuild(key string, b Build) { func (c *LocalCache) localCacheBuild(key string, b Build) {
c.bmtx.Lock() c.lmtx.Lock()
c.lcache[key] = b c.lcache[key] = b
c.bmtx.Unlock() c.lmtx.Unlock()
} }
// Retrieve a manifest from the cache(s). First the local cache is // Retrieve a manifest from the cache(s). First the local cache is
// checked, then the GCS-bucket cache. // checked, then the GCS-bucket cache.
func manifestFromCache(ctx context.Context, cache *LocalCache, bucket *storage.BucketHandle, key string) (string, bool) { func manifestFromCache(ctx context.Context, s *State, key string) (string, bool) {
path, cached := cache.manifestFromLocalCache(key) path, cached := s.Cache.manifestFromLocalCache(key)
if cached { if cached {
return path, true return path, true
} }
obj := bucket.Object("manifests/" + key) obj := s.Bucket.Object("manifests/" + key)
// Probe whether the file exists before trying to fetch it. // Probe whether the file exists before trying to fetch it.
_, err := obj.Attrs(ctx) _, err := obj.Attrs(ctx)
@ -119,17 +117,17 @@ func manifestFromCache(ctx context.Context, cache *LocalCache, bucket *storage.B
} }
log.Printf("Retrieved manifest for sha1:%s from GCS\n", key) log.Printf("Retrieved manifest for sha1:%s from GCS\n", key)
cache.localCacheManifest(key, path) go s.Cache.localCacheManifest(key, path)
return path, true return path, true
} }
// Add a manifest to the bucket & local caches // Add a manifest to the bucket & local caches
func cacheManifest(ctx *context.Context, cache *LocalCache, bucket *storage.BucketHandle, key, path string) { func cacheManifest(ctx context.Context, s *State, key, path string) {
cache.localCacheManifest(key, path) go s.Cache.localCacheManifest(key, path)
obj := bucket.Object("manifests/" + key) obj := s.Bucket.Object("manifests/" + key)
w := obj.NewWriter(*ctx) w := obj.NewWriter(ctx)
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
@ -154,19 +152,19 @@ func cacheManifest(ctx *context.Context, cache *LocalCache, bucket *storage.Buck
// Retrieve a build from the cache, first checking the local cache // Retrieve a build from the cache, first checking the local cache
// followed by the bucket cache. // followed by the bucket cache.
func buildFromCache(ctx *context.Context, cache *LocalCache, bucket *storage.BucketHandle, key string) (*Build, bool) { func buildFromCache(ctx context.Context, s *State, key string) (*Build, bool) {
build, cached := cache.buildFromLocalCache(key) build, cached := s.Cache.buildFromLocalCache(key)
if cached { if cached {
return build, true return build, true
} }
obj := bucket.Object("builds/" + key) obj := s.Bucket.Object("builds/" + key)
_, err := obj.Attrs(*ctx) _, err := obj.Attrs(ctx)
if err != nil { if err != nil {
return nil, false return nil, false
} }
r, err := obj.NewReader(*ctx) r, err := obj.NewReader(ctx)
if err != nil { if err != nil {
log.Printf("Failed to retrieve build '%s' from cache: %s\n", key, err) log.Printf("Failed to retrieve build '%s' from cache: %s\n", key, err)
return nil, false return nil, false
@ -187,13 +185,14 @@ func buildFromCache(ctx *context.Context, cache *LocalCache, bucket *storage.Buc
return nil, false return nil, false
} }
go s.Cache.localCacheBuild(key, b)
return &b, true return &b, true
} }
func cacheBuild(ctx context.Context, cache *LocalCache, bucket *storage.BucketHandle, key string, build Build) { func cacheBuild(ctx context.Context, s *State, key string, build Build) {
cache.localCacheBuild(key, build) go s.Cache.localCacheBuild(key, build)
obj := bucket.Object("builds/" + key) obj := s.Bucket.Object("builds/" + key)
j, _ := json.Marshal(&build) j, _ := json.Marshal(&build)

View file

@ -81,15 +81,15 @@ func constructLayerUrl(cfg *config.Config, digest string) (string, error) {
// //
// The bucket is required for Nixery to function correctly, hence fatal errors // The bucket is required for Nixery to function correctly, hence fatal errors
// are generated in case it fails to be set up correctly. // are generated in case it fails to be set up correctly.
func prepareBucket(ctx *context.Context, cfg *config.Config) *storage.BucketHandle { func prepareBucket(ctx context.Context, cfg *config.Config) *storage.BucketHandle {
client, err := storage.NewClient(*ctx) client, err := storage.NewClient(ctx)
if err != nil { if err != nil {
log.Fatalln("Failed to set up Cloud Storage client:", err) log.Fatalln("Failed to set up Cloud Storage client:", err)
} }
bkt := client.Bucket(cfg.Bucket) bkt := client.Bucket(cfg.Bucket)
if _, err := bkt.Attrs(*ctx); err != nil { if _, err := bkt.Attrs(ctx); err != nil {
log.Fatalln("Could not access configured bucket", err) log.Fatalln("Could not access configured bucket", err)
} }
@ -194,7 +194,7 @@ func main() {
} }
ctx := context.Background() ctx := context.Background()
bucket := prepareBucket(&ctx, cfg) bucket := prepareBucket(ctx, cfg)
state := builder.NewState(bucket, *cfg) state := builder.NewState(bucket, *cfg)
log.Printf("Starting Nixery on port %s\n", cfg.Port) log.Printf("Starting Nixery on port %s\n", cfg.Port)