Skip to content

Commit f044e0a

Browse files
committed
llbsolver: move history blobs to a separate namespace
Migrate history objects to separate namespace to holding reference to a blob does not interfer with the GC labels held for same blobs by the containerd image store. Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 81d19ad commit f044e0a

File tree

7 files changed

+288
-51
lines changed

7 files changed

+288
-51
lines changed

control/control.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
contentapi "github.com/containerd/containerd/api/services/content/v1"
1212
"github.com/containerd/containerd/content"
13-
"github.com/containerd/containerd/leases"
1413
"github.com/containerd/containerd/services/content/contentserver"
1514
"github.com/docker/distribution/reference"
1615
"github.com/mitchellh/hashstructure/v2"
@@ -26,12 +25,14 @@ import (
2625
"github.com/moby/buildkit/frontend/attestations"
2726
"github.com/moby/buildkit/session"
2827
"github.com/moby/buildkit/session/grpchijack"
28+
containerdsnapshot "github.com/moby/buildkit/snapshot/containerd"
2929
"github.com/moby/buildkit/solver"
3030
"github.com/moby/buildkit/solver/llbsolver"
3131
"github.com/moby/buildkit/solver/llbsolver/proc"
3232
"github.com/moby/buildkit/solver/pb"
3333
"github.com/moby/buildkit/util/bklog"
3434
"github.com/moby/buildkit/util/imageutil"
35+
"github.com/moby/buildkit/util/leaseutil"
3536
"github.com/moby/buildkit/util/throttle"
3637
"github.com/moby/buildkit/util/tracing/transform"
3738
"github.com/moby/buildkit/version"
@@ -58,8 +59,8 @@ type Opt struct {
5859
Entitlements []string
5960
TraceCollector sdktrace.SpanExporter
6061
HistoryDB *bbolt.DB
61-
LeaseManager leases.Manager
62-
ContentStore content.Store
62+
LeaseManager *leaseutil.Manager
63+
ContentStore *containerdsnapshot.Store
6364
HistoryConfig *config.HistoryConfig
6465
}
6566

@@ -79,12 +80,15 @@ type Controller struct { // TODO: ControlService
7980
func NewController(opt Opt) (*Controller, error) {
8081
gatewayForwarder := controlgateway.NewGatewayForwarder()
8182

82-
hq := llbsolver.NewHistoryQueue(llbsolver.HistoryQueueOpt{
83+
hq, err := llbsolver.NewHistoryQueue(llbsolver.HistoryQueueOpt{
8384
DB: opt.HistoryDB,
8485
LeaseManager: opt.LeaseManager,
8586
ContentStore: opt.ContentStore,
8687
CleanConfig: opt.HistoryConfig,
8788
})
89+
if err != nil {
90+
return nil, errors.Wrap(err, "failed to create history queue")
91+
}
8892

8993
s, err := llbsolver.New(llbsolver.Opt{
9094
WorkerController: opt.WorkerController,
@@ -125,7 +129,7 @@ func (c *Controller) Register(server *grpc.Server) {
125129
c.gatewayForwarder.Register(server)
126130
tracev1.RegisterTraceServiceServer(server, c)
127131

128-
store := &roContentStore{c.opt.ContentStore}
132+
store := &roContentStore{c.opt.ContentStore.WithFallbackNS(c.opt.ContentStore.Namespace() + "_history")}
129133
contentapi.RegisterContentServer(server, contentserver.New(store))
130134
}
131135

snapshot/containerd/content.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55

66
"github.com/containerd/containerd/content"
77
"github.com/containerd/containerd/namespaces"
8+
"github.com/containerd/nydus-snapshotter/pkg/errdefs"
89
digest "github.com/opencontainers/go-digest"
910
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
1011
"github.com/pkg/errors"
1112
)
1213

13-
func NewContentStore(store content.Store, ns string) content.Store {
14+
type Store = nsContent
15+
16+
func NewContentStore(store content.Store, ns string) *Store {
1417
return &nsContent{ns, store}
1518
}
1619

@@ -19,6 +22,14 @@ type nsContent struct {
1922
content.Store
2023
}
2124

25+
func (c *nsContent) Namespace() string {
26+
return c.ns
27+
}
28+
29+
func (c *nsContent) WithNamespace(ns string) *Store {
30+
return NewContentStore(c.Store, ns)
31+
}
32+
2233
func (c *nsContent) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
2334
ctx = namespaces.WithNamespace(ctx, c.ns)
2435
return c.Store.Info(ctx, dgst)
@@ -62,6 +73,13 @@ func (c *nsContent) Writer(ctx context.Context, opts ...content.WriterOpt) (cont
6273
return c.writer(ctx, 3, opts...)
6374
}
6475

76+
func (c *nsContent) WithFallbackNS(ns string) content.Store {
77+
return &nsFallbackStore{
78+
main: c,
79+
fb: c.WithNamespace(ns),
80+
}
81+
}
82+
6583
func (c *nsContent) writer(ctx context.Context, retries int, opts ...content.WriterOpt) (content.Writer, error) {
6684
ctx = namespaces.WithNamespace(ctx, c.ns)
6785
w, err := c.Store.Writer(ctx, opts...)
@@ -80,3 +98,58 @@ func (w *nsWriter) Commit(ctx context.Context, size int64, expected digest.Diges
8098
ctx = namespaces.WithNamespace(ctx, w.ns)
8199
return w.Writer.Commit(ctx, size, expected, opts...)
82100
}
101+
102+
type nsFallbackStore struct {
103+
main *nsContent
104+
fb *nsContent
105+
}
106+
107+
var _ content.Store = &nsFallbackStore{}
108+
109+
func (c *nsFallbackStore) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
110+
info, err := c.main.Info(ctx, dgst)
111+
if err != nil {
112+
if errdefs.IsNotFound(err) {
113+
return c.fb.Info(ctx, dgst)
114+
}
115+
}
116+
return info, err
117+
}
118+
119+
func (c *nsFallbackStore) Update(ctx context.Context, info content.Info, fieldpaths ...string) (content.Info, error) {
120+
return c.main.Update(ctx, info, fieldpaths...)
121+
}
122+
123+
func (c *nsFallbackStore) Walk(ctx context.Context, fn content.WalkFunc, filters ...string) error {
124+
return c.main.Walk(ctx, fn, filters...)
125+
}
126+
127+
func (c *nsFallbackStore) Delete(ctx context.Context, dgst digest.Digest) error {
128+
return c.main.Delete(ctx, dgst)
129+
}
130+
131+
func (c *nsFallbackStore) Status(ctx context.Context, ref string) (content.Status, error) {
132+
return c.main.Status(ctx, ref)
133+
}
134+
135+
func (c *nsFallbackStore) ListStatuses(ctx context.Context, filters ...string) ([]content.Status, error) {
136+
return c.main.ListStatuses(ctx, filters...)
137+
}
138+
139+
func (c *nsFallbackStore) Abort(ctx context.Context, ref string) error {
140+
return c.main.Abort(ctx, ref)
141+
}
142+
143+
func (c *nsFallbackStore) ReaderAt(ctx context.Context, desc ocispecs.Descriptor) (content.ReaderAt, error) {
144+
ra, err := c.main.ReaderAt(ctx, desc)
145+
if err != nil {
146+
if errdefs.IsNotFound(err) {
147+
return c.fb.ReaderAt(ctx, desc)
148+
}
149+
}
150+
return ra, err
151+
}
152+
153+
func (c *nsFallbackStore) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
154+
return c.main.Writer(ctx, opts...)
155+
}

0 commit comments

Comments
 (0)