2019-08-14 21:02:52 +02:00
|
|
|
// Copyright 2019 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
// use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
// the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations under
|
|
|
|
// the License.
|
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
2019-09-30 15:25:55 +02:00
|
|
|
"bytes"
|
2019-09-09 17:41:52 +02:00
|
|
|
"context"
|
2019-09-30 15:25:55 +02:00
|
|
|
"encoding/json"
|
2019-09-09 17:41:52 +02:00
|
|
|
"io"
|
2019-10-03 12:23:04 +02:00
|
|
|
"io/ioutil"
|
2019-10-03 13:49:26 +02:00
|
|
|
"os"
|
2019-08-14 21:02:52 +02:00
|
|
|
"sync"
|
|
|
|
|
2019-10-05 15:54:49 +02:00
|
|
|
"github.com/google/nixery/server/manifest"
|
2019-10-04 23:17:11 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-10-03 13:09:24 +02:00
|
|
|
)
|
2019-09-30 15:25:55 +02:00
|
|
|
|
2019-09-09 17:41:52 +02:00
|
|
|
// LocalCache implements the structure used for local caching of
|
|
|
|
// manifests and layer uploads.
|
|
|
|
type LocalCache struct {
|
2019-09-30 15:25:55 +02:00
|
|
|
// Manifest cache
|
2019-10-03 13:49:26 +02:00
|
|
|
mmtx sync.RWMutex
|
|
|
|
mdir string
|
2019-08-14 21:02:52 +02:00
|
|
|
|
2019-10-02 18:48:58 +02:00
|
|
|
// Layer cache
|
2019-08-14 21:02:52 +02:00
|
|
|
lmtx sync.RWMutex
|
2019-10-03 13:09:24 +02:00
|
|
|
lcache map[string]manifest.Entry
|
2019-08-14 21:02:52 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 13:49:26 +02:00
|
|
|
// Creates an in-memory cache and ensures that the local file path for
|
|
|
|
// manifest caching exists.
|
|
|
|
func NewCache() (LocalCache, error) {
|
|
|
|
path := os.TempDir() + "/nixery"
|
|
|
|
err := os.MkdirAll(path, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return LocalCache{}, err
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:41:52 +02:00
|
|
|
return LocalCache{
|
2019-10-03 13:49:26 +02:00
|
|
|
mdir: path + "/",
|
2019-10-03 13:09:24 +02:00
|
|
|
lcache: make(map[string]manifest.Entry),
|
2019-10-03 13:49:26 +02:00
|
|
|
}, nil
|
2019-08-14 21:02:52 +02:00
|
|
|
}
|
|
|
|
|
2019-09-08 23:21:14 +02:00
|
|
|
// Retrieve a cached manifest if the build is cacheable and it exists.
|
2019-10-03 13:49:26 +02:00
|
|
|
func (c *LocalCache) manifestFromLocalCache(key string) (json.RawMessage, bool) {
|
2019-09-08 23:21:14 +02:00
|
|
|
c.mmtx.RLock()
|
2019-10-03 13:49:26 +02:00
|
|
|
defer c.mmtx.RUnlock()
|
2019-08-14 21:02:52 +02:00
|
|
|
|
2019-10-03 13:49:26 +02:00
|
|
|
f, err := os.Open(c.mdir + key)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(tazjin): Once log levels are available, this
|
|
|
|
// might warrant a debug log.
|
|
|
|
return nil, false
|
2019-08-14 21:02:52 +02:00
|
|
|
}
|
2019-10-03 13:49:26 +02:00
|
|
|
defer f.Close()
|
2019-08-14 21:02:52 +02:00
|
|
|
|
2019-10-03 13:49:26 +02:00
|
|
|
m, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to read manifest '%s' from local cache: %s\n", key, err)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.RawMessage(m), true
|
2019-08-14 21:02:52 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:41:52 +02:00
|
|
|
// Adds the result of a manifest build to the local cache, if the
|
|
|
|
// manifest is considered cacheable.
|
2019-10-03 13:49:26 +02:00
|
|
|
//
|
|
|
|
// Manifests can be quite large and are cached on disk instead of in
|
|
|
|
// memory.
|
|
|
|
func (c *LocalCache) localCacheManifest(key string, m json.RawMessage) {
|
2019-09-09 17:41:52 +02:00
|
|
|
c.mmtx.Lock()
|
2019-10-03 13:49:26 +02:00
|
|
|
defer c.mmtx.Unlock()
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(c.mdir+key, []byte(m), 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to locally cache manifest for '%s': %s\n", key, err)
|
|
|
|
}
|
2019-09-09 17:41:52 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 13:09:24 +02:00
|
|
|
// Retrieve a layer build from the local cache.
|
|
|
|
func (c *LocalCache) layerFromLocalCache(key string) (*manifest.Entry, bool) {
|
2019-10-02 19:00:35 +02:00
|
|
|
c.lmtx.RLock()
|
2019-10-03 13:09:24 +02:00
|
|
|
e, ok := c.lcache[key]
|
2019-10-02 19:00:35 +02:00
|
|
|
c.lmtx.RUnlock()
|
2019-09-30 15:25:55 +02:00
|
|
|
|
2019-10-03 13:09:24 +02:00
|
|
|
return &e, ok
|
2019-09-30 15:25:55 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 13:09:24 +02:00
|
|
|
// Add a layer build result to the local cache.
|
|
|
|
func (c *LocalCache) localCacheLayer(key string, e manifest.Entry) {
|
2019-10-02 19:00:35 +02:00
|
|
|
c.lmtx.Lock()
|
2019-10-03 13:09:24 +02:00
|
|
|
c.lcache[key] = e
|
2019-10-02 19:00:35 +02:00
|
|
|
c.lmtx.Unlock()
|
2019-09-30 15:25:55 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:41:52 +02:00
|
|
|
// Retrieve a manifest from the cache(s). First the local cache is
|
|
|
|
// checked, then the GCS-bucket cache.
|
2019-10-03 12:23:04 +02:00
|
|
|
func manifestFromCache(ctx context.Context, s *State, key string) (json.RawMessage, bool) {
|
2019-10-03 13:49:26 +02:00
|
|
|
if m, cached := s.Cache.manifestFromLocalCache(key); cached {
|
|
|
|
return m, true
|
|
|
|
}
|
2019-09-09 17:41:52 +02:00
|
|
|
|
2019-10-02 19:00:35 +02:00
|
|
|
obj := s.Bucket.Object("manifests/" + key)
|
2019-09-09 17:41:52 +02:00
|
|
|
|
|
|
|
// Probe whether the file exists before trying to fetch it.
|
2019-10-02 00:26:09 +02:00
|
|
|
_, err := obj.Attrs(ctx)
|
2019-09-09 17:41:52 +02:00
|
|
|
if err != nil {
|
2019-10-03 12:23:04 +02:00
|
|
|
return nil, false
|
2019-09-09 17:41:52 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 00:26:09 +02:00
|
|
|
r, err := obj.NewReader(ctx)
|
2019-09-09 17:41:52 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to retrieve manifest '%s' from cache: %s\n", key, err)
|
2019-10-03 12:23:04 +02:00
|
|
|
return nil, false
|
2019-09-09 17:41:52 +02:00
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
|
2019-10-03 12:23:04 +02:00
|
|
|
m, err := ioutil.ReadAll(r)
|
2019-09-09 17:41:52 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to read cached manifest for '%s': %s\n", key, err)
|
|
|
|
}
|
|
|
|
|
2019-10-03 13:49:26 +02:00
|
|
|
go s.Cache.localCacheManifest(key, m)
|
2019-09-10 12:13:10 +02:00
|
|
|
log.Printf("Retrieved manifest for sha1:%s from GCS\n", key)
|
2019-10-03 13:49:26 +02:00
|
|
|
|
2019-10-03 12:23:04 +02:00
|
|
|
return json.RawMessage(m), true
|
2019-09-09 17:41:52 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 15:25:55 +02:00
|
|
|
// Add a manifest to the bucket & local caches
|
2019-10-03 12:23:04 +02:00
|
|
|
func cacheManifest(ctx context.Context, s *State, key string, m json.RawMessage) {
|
2019-10-03 13:49:26 +02:00
|
|
|
go s.Cache.localCacheManifest(key, m)
|
2019-09-09 17:41:52 +02:00
|
|
|
|
2019-10-02 19:00:35 +02:00
|
|
|
obj := s.Bucket.Object("manifests/" + key)
|
|
|
|
w := obj.NewWriter(ctx)
|
2019-10-03 12:23:04 +02:00
|
|
|
r := bytes.NewReader([]byte(m))
|
2019-09-09 17:41:52 +02:00
|
|
|
|
2019-10-03 12:23:04 +02:00
|
|
|
size, err := io.Copy(w, r)
|
2019-09-09 17:41:52 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to cache manifest sha1:%s: %s\n", key, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = w.Close(); err != nil {
|
|
|
|
log.Printf("failed to cache manifest sha1:%s: %s\n", key, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Cached manifest sha1:%s (%v bytes written)\n", key, size)
|
2019-08-14 21:02:52 +02:00
|
|
|
}
|
2019-09-30 15:25:55 +02:00
|
|
|
|
2019-10-03 13:09:24 +02:00
|
|
|
// Retrieve a layer build from the cache, first checking the local
|
|
|
|
// cache followed by the bucket cache.
|
|
|
|
func layerFromCache(ctx context.Context, s *State, key string) (*manifest.Entry, bool) {
|
|
|
|
if entry, cached := s.Cache.layerFromLocalCache(key); cached {
|
|
|
|
return entry, true
|
2019-09-30 15:25:55 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 19:00:35 +02:00
|
|
|
obj := s.Bucket.Object("builds/" + key)
|
|
|
|
_, err := obj.Attrs(ctx)
|
2019-09-30 15:25:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-10-02 19:00:35 +02:00
|
|
|
r, err := obj.NewReader(ctx)
|
2019-09-30 15:25:55 +02:00
|
|
|
if err != nil {
|
2019-10-03 13:09:24 +02:00
|
|
|
log.Printf("Failed to retrieve layer build '%s' from cache: %s\n", key, err)
|
2019-09-30 15:25:55 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
jb := bytes.NewBuffer([]byte{})
|
|
|
|
_, err = io.Copy(jb, r)
|
|
|
|
if err != nil {
|
2019-10-03 13:09:24 +02:00
|
|
|
log.Printf("Failed to read layer build '%s' from cache: %s\n", key, err)
|
2019-09-30 15:25:55 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-10-03 13:09:24 +02:00
|
|
|
var entry manifest.Entry
|
|
|
|
err = json.Unmarshal(jb.Bytes(), &entry)
|
2019-09-30 15:25:55 +02:00
|
|
|
if err != nil {
|
2019-10-03 13:09:24 +02:00
|
|
|
log.Printf("Failed to unmarshal layer build '%s' from cache: %s\n", key, err)
|
2019-09-30 15:25:55 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-10-03 13:09:24 +02:00
|
|
|
go s.Cache.localCacheLayer(key, entry)
|
|
|
|
return &entry, true
|
2019-09-30 15:25:55 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 13:09:24 +02:00
|
|
|
func cacheLayer(ctx context.Context, s *State, key string, entry manifest.Entry) {
|
|
|
|
s.Cache.localCacheLayer(key, entry)
|
2019-09-30 15:25:55 +02:00
|
|
|
|
2019-10-02 19:00:35 +02:00
|
|
|
obj := s.Bucket.Object("builds/" + key)
|
2019-09-30 15:25:55 +02:00
|
|
|
|
2019-10-03 13:09:24 +02:00
|
|
|
j, _ := json.Marshal(&entry)
|
2019-09-30 15:25:55 +02:00
|
|
|
|
|
|
|
w := obj.NewWriter(ctx)
|
|
|
|
|
2019-10-02 00:26:09 +02:00
|
|
|
_, err := io.Copy(w, bytes.NewReader(j))
|
2019-09-30 15:25:55 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to cache build '%s': %s\n", key, err)
|
|
|
|
return
|
|
|
|
}
|
2019-10-03 23:13:13 +02:00
|
|
|
|
|
|
|
if err = w.Close(); err != nil {
|
|
|
|
log.Printf("failed to cache build '%s': %s\n", key, err)
|
|
|
|
return
|
|
|
|
}
|
2019-09-30 15:25:55 +02:00
|
|
|
}
|