2018-01-02 18:06:50 +00:00
|
|
|
package solver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-02-02 00:10:38 +00:00
|
|
|
"time"
|
2018-01-02 18:06:50 +00:00
|
|
|
|
2018-02-13 23:54:00 +00:00
|
|
|
"github.com/containerd/containerd/content"
|
2018-01-02 18:06:50 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2018-02-13 23:54:00 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2018-01-02 18:06:50 +00:00
|
|
|
)
|
|
|
|
|
2019-11-07 18:00:20 +00:00
|
|
|
// Vertex is a node in a build graph. It defines an interface for a
|
2019-11-05 22:21:04 +00:00
|
|
|
// content-addressable operation and its inputs.
|
2018-01-02 18:06:50 +00:00
|
|
|
type Vertex interface {
|
2019-11-05 22:21:04 +00:00
|
|
|
// Digest returns a checksum of the definition up to the vertex including
|
2019-11-07 18:00:20 +00:00
|
|
|
// all of its inputs.
|
2018-01-02 18:06:50 +00:00
|
|
|
Digest() digest.Digest
|
2019-11-05 22:21:04 +00:00
|
|
|
|
|
|
|
// Sys returns an object used to resolve the executor for this vertex.
|
|
|
|
// In LLB solver, this value would be of type `llb.Op`.
|
2018-01-02 18:06:50 +00:00
|
|
|
Sys() interface{}
|
2019-11-05 22:21:04 +00:00
|
|
|
|
|
|
|
// Options return metadata associated with the vertex that doesn't change the
|
|
|
|
// definition or equality check of it.
|
2018-02-01 23:41:14 +00:00
|
|
|
Options() VertexOptions
|
2019-11-05 22:21:04 +00:00
|
|
|
|
|
|
|
// Inputs returns an array of edges the vertex depends on. An input edge is
|
|
|
|
// a vertex and an index from the returned array of results from an executor
|
|
|
|
// returned by Sys(). A vertex may have zero inputs.
|
2018-01-02 18:06:50 +00:00
|
|
|
Inputs() []Edge
|
2019-11-05 22:21:04 +00:00
|
|
|
|
2018-01-02 18:06:50 +00:00
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
2019-11-05 22:21:04 +00:00
|
|
|
// Index is an index value for the return array of an operation. Index starts
|
|
|
|
// counting from zero.
|
2018-01-02 18:06:50 +00:00
|
|
|
type Index int
|
|
|
|
|
2019-11-05 22:21:04 +00:00
|
|
|
// Edge is a connection point between vertexes. An edge references a specific
|
|
|
|
// output of a vertex's operation. Edges are used as inputs to other vertexes.
|
2018-01-02 18:06:50 +00:00
|
|
|
type Edge struct {
|
|
|
|
Index Index
|
|
|
|
Vertex Vertex
|
|
|
|
}
|
|
|
|
|
2019-11-05 22:21:04 +00:00
|
|
|
// VertexOptions define optional metadata for a vertex that doesn't change the
|
|
|
|
// definition or equality check of it. These options are not contained in the
|
|
|
|
// vertex digest.
|
2018-02-01 23:41:14 +00:00
|
|
|
type VertexOptions struct {
|
2018-04-25 17:49:15 +00:00
|
|
|
IgnoreCache bool
|
|
|
|
CacheSources []CacheManager
|
|
|
|
Description map[string]string // text values with no special meaning for solver
|
|
|
|
ExportCache *bool
|
2018-02-01 22:23:42 +00:00
|
|
|
// WorkerConstraint
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:06:50 +00:00
|
|
|
// Result is an abstract return value for a solve
|
|
|
|
type Result interface {
|
|
|
|
ID() string
|
|
|
|
Release(context.Context) error
|
|
|
|
Sys() interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CachedResult is a result connected with its cache key
|
|
|
|
type CachedResult interface {
|
|
|
|
Result
|
2018-07-23 21:27:35 +00:00
|
|
|
CacheKeys() []ExportableCacheKey
|
2018-02-13 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 04:52:30 +00:00
|
|
|
// CacheExportMode is the type for setting cache exporting modes
|
2018-05-07 21:19:02 +00:00
|
|
|
type CacheExportMode int
|
|
|
|
|
|
|
|
const (
|
2018-05-09 04:52:30 +00:00
|
|
|
// CacheExportModeMin exports a topmost allowed vertex and its dependencies
|
|
|
|
// that already have transferable layers
|
2018-05-07 21:19:02 +00:00
|
|
|
CacheExportModeMin CacheExportMode = iota
|
2018-05-09 04:52:30 +00:00
|
|
|
// CacheExportModeMax exports all possible non-root vertexes
|
2018-05-07 21:19:02 +00:00
|
|
|
CacheExportModeMax
|
2018-05-09 04:52:30 +00:00
|
|
|
// CacheExportModeRemoteOnly only exports vertexes that already have
|
|
|
|
// transferable layers
|
2018-05-07 23:17:03 +00:00
|
|
|
CacheExportModeRemoteOnly
|
2018-05-07 21:19:02 +00:00
|
|
|
)
|
|
|
|
|
2018-05-09 04:52:30 +00:00
|
|
|
// CacheExportOpt defines options for exporting build cache
|
2018-05-07 21:19:02 +00:00
|
|
|
type CacheExportOpt struct {
|
2018-05-09 04:52:30 +00:00
|
|
|
// Convert can convert a build result to transferable object
|
2018-05-07 21:19:02 +00:00
|
|
|
Convert func(context.Context, Result) (*Remote, error)
|
2018-05-09 04:52:30 +00:00
|
|
|
// Mode defines a cache export algorithm
|
|
|
|
Mode CacheExportMode
|
2018-05-07 21:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CacheExporter can export the artifacts of the build chain
|
|
|
|
type CacheExporter interface {
|
|
|
|
ExportTo(ctx context.Context, t CacheExporterTarget, opt CacheExportOpt) ([]CacheExporterRecord, error)
|
2018-02-13 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 21:19:02 +00:00
|
|
|
// CacheExporterTarget defines object capable of receiving exports
|
|
|
|
type CacheExporterTarget interface {
|
|
|
|
Add(dgst digest.Digest) CacheExporterRecord
|
2018-04-13 20:52:27 +00:00
|
|
|
Visit(interface{})
|
|
|
|
Visited(interface{}) bool
|
|
|
|
}
|
|
|
|
|
2018-05-07 21:19:02 +00:00
|
|
|
// CacheExporterRecord is a single object being exported
|
|
|
|
type CacheExporterRecord interface {
|
2018-04-13 20:52:27 +00:00
|
|
|
AddResult(createdAt time.Time, result *Remote)
|
2018-05-07 21:19:02 +00:00
|
|
|
LinkFrom(src CacheExporterRecord, index int, selector string)
|
2018-02-13 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remote is a descriptor or a list of stacked descriptors that can be pulled
|
|
|
|
// from a content provider
|
2018-04-13 20:52:27 +00:00
|
|
|
// TODO: add closer to keep referenced data from getting deleted
|
2018-02-13 23:54:00 +00:00
|
|
|
type Remote struct {
|
|
|
|
Descriptors []ocispec.Descriptor
|
|
|
|
Provider content.Provider
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheLink is a link between two cache records
|
|
|
|
type CacheLink struct {
|
2018-04-13 20:52:27 +00:00
|
|
|
Source digest.Digest `json:",omitempty"`
|
|
|
|
Input Index `json:",omitempty"`
|
|
|
|
Output Index `json:",omitempty"`
|
|
|
|
Base digest.Digest `json:",omitempty"`
|
|
|
|
Selector digest.Digest `json:",omitempty"`
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 22:21:04 +00:00
|
|
|
// Op defines how the solver can evaluate the properties of a vertex operation.
|
|
|
|
// An op is executed in the worker, and is retrieved from the vertex by the
|
|
|
|
// value of `vertex.Sys()`. The solver is configured with a resolve function to
|
|
|
|
// convert a `vertex.Sys()` into an `Op`.
|
2018-01-02 18:06:50 +00:00
|
|
|
type Op interface {
|
2018-04-24 19:01:50 +00:00
|
|
|
// CacheMap returns structure describing how the operation is cached.
|
|
|
|
// Currently only roots are allowed to return multiple cache maps per op.
|
|
|
|
CacheMap(context.Context, int) (*CacheMap, bool, error)
|
2019-11-05 22:21:04 +00:00
|
|
|
|
2018-01-02 18:06:50 +00:00
|
|
|
// Exec runs an operation given results from previous operations.
|
|
|
|
Exec(ctx context.Context, inputs []Result) (outputs []Result, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResultBasedCacheFunc func(context.Context, Result) (digest.Digest, error)
|
|
|
|
|
2019-11-05 22:21:04 +00:00
|
|
|
// CacheMap is a description for calculating the cache key of an operation.
|
2018-01-02 18:06:50 +00:00
|
|
|
type CacheMap struct {
|
2019-11-05 22:21:04 +00:00
|
|
|
// Digest returns a checksum for the operation. The operation result can be
|
|
|
|
// cached by a checksum that combines this digest and the cache keys of the
|
|
|
|
// operation's inputs.
|
|
|
|
//
|
|
|
|
// For example, in LLB this digest is a manifest digest for OCI images, or
|
|
|
|
// commit SHA for git sources.
|
2018-01-02 18:06:50 +00:00
|
|
|
Digest digest.Digest
|
2019-11-05 22:21:04 +00:00
|
|
|
|
|
|
|
// Deps contain optional selectors or content-based cache functions for its
|
|
|
|
// inputs.
|
|
|
|
Deps []struct {
|
|
|
|
// Selector is a digest that is merged with the cache key of the input.
|
2019-11-07 18:00:20 +00:00
|
|
|
// Selectors are not merged with the result of the `ComputeDigestFunc` for
|
|
|
|
// this input.
|
2018-01-02 18:06:50 +00:00
|
|
|
Selector digest.Digest
|
2019-11-05 22:21:04 +00:00
|
|
|
|
2019-11-07 18:00:20 +00:00
|
|
|
// ComputeDigestFunc should return a digest for the input based on its return
|
|
|
|
// value.
|
2019-11-05 22:21:04 +00:00
|
|
|
//
|
|
|
|
// For example, in LLB this is invoked to calculate the cache key based on
|
|
|
|
// the checksum of file contents from input snapshots.
|
2018-01-02 18:06:50 +00:00
|
|
|
ComputeDigestFunc ResultBasedCacheFunc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:54:00 +00:00
|
|
|
// ExportableCacheKey is a cache key connected with an exporter that can export
|
|
|
|
// a chain of cacherecords pointing to that key
|
|
|
|
type ExportableCacheKey struct {
|
2018-04-13 20:52:27 +00:00
|
|
|
*CacheKey
|
2018-05-07 21:19:02 +00:00
|
|
|
Exporter CacheExporter
|
2018-02-13 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:06:50 +00:00
|
|
|
// CacheRecord is an identifier for loading in cache
|
|
|
|
type CacheRecord struct {
|
2018-04-13 20:52:27 +00:00
|
|
|
ID string
|
|
|
|
Size int
|
2018-02-02 00:10:38 +00:00
|
|
|
CreatedAt time.Time
|
|
|
|
Priority int
|
2018-04-13 20:52:27 +00:00
|
|
|
|
|
|
|
cacheManager *cacheManager
|
|
|
|
key *CacheKey
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 18:00:20 +00:00
|
|
|
// CacheManager determines if there is a result that matches the cache keys
|
2019-11-05 22:21:04 +00:00
|
|
|
// generated during the build that could be reused instead of fully
|
|
|
|
// reevaluating the vertex and its inputs. There can be multiple cache
|
|
|
|
// managers, and specific managers can be defined per vertex using
|
|
|
|
// `VertexOptions`.
|
2018-01-02 18:06:50 +00:00
|
|
|
type CacheManager interface {
|
2018-02-01 22:23:42 +00:00
|
|
|
// ID is used to identify cache providers that are backed by same source
|
2019-11-05 22:21:04 +00:00
|
|
|
// to avoid duplicate calls to the same provider.
|
2018-02-01 22:23:42 +00:00
|
|
|
ID() string
|
2019-11-05 22:21:04 +00:00
|
|
|
|
2018-02-01 22:23:42 +00:00
|
|
|
// Query searches for cache paths from one cache key to the output of a
|
|
|
|
// possible match.
|
2018-04-13 20:52:27 +00:00
|
|
|
Query(inp []CacheKeyWithSelector, inputIndex Index, dgst digest.Digest, outputIndex Index) ([]*CacheKey, error)
|
|
|
|
Records(ck *CacheKey) ([]*CacheRecord, error)
|
2019-11-05 22:21:04 +00:00
|
|
|
|
|
|
|
// Load loads a cache record into a result reference.
|
2018-01-02 18:06:50 +00:00
|
|
|
Load(ctx context.Context, rec *CacheRecord) (Result, error)
|
2019-11-05 22:21:04 +00:00
|
|
|
|
2018-01-02 18:06:50 +00:00
|
|
|
// Save saves a result based on a cache key
|
2019-01-10 16:43:27 +00:00
|
|
|
Save(key *CacheKey, s Result, createdAt time.Time) (*ExportableCacheKey, error)
|
2018-02-13 23:54:00 +00:00
|
|
|
}
|