Skip to content

Commit ecae985

Browse files
authored
Merge pull request moby#3833 from tonistiigi/history-ns-migrate
llbsolver: move history blobs to a separate namespace
2 parents 3d5efca + f5ca0c5 commit ecae985

File tree

7 files changed

+304
-71
lines changed

7 files changed

+304
-71
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: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,80 @@ 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-
return &nsContent{ns, store}
14+
func NewContentStore(store content.Store, ns string) *Store {
15+
return &Store{ns, store}
1516
}
1617

17-
type nsContent struct {
18+
type Store struct {
1819
ns string
1920
content.Store
2021
}
2122

22-
func (c *nsContent) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
23+
func (c *Store) Namespace() string {
24+
return c.ns
25+
}
26+
27+
func (c *Store) WithNamespace(ns string) *Store {
28+
return NewContentStore(c.Store, ns)
29+
}
30+
31+
func (c *Store) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
2332
ctx = namespaces.WithNamespace(ctx, c.ns)
2433
return c.Store.Info(ctx, dgst)
2534
}
2635

27-
func (c *nsContent) Update(ctx context.Context, info content.Info, fieldpaths ...string) (content.Info, error) {
36+
func (c *Store) Update(ctx context.Context, info content.Info, fieldpaths ...string) (content.Info, error) {
2837
ctx = namespaces.WithNamespace(ctx, c.ns)
2938
return c.Store.Update(ctx, info, fieldpaths...)
3039
}
3140

32-
func (c *nsContent) Walk(ctx context.Context, fn content.WalkFunc, filters ...string) error {
41+
func (c *Store) Walk(ctx context.Context, fn content.WalkFunc, filters ...string) error {
3342
ctx = namespaces.WithNamespace(ctx, c.ns)
3443
return c.Store.Walk(ctx, fn, filters...)
3544
}
3645

37-
func (c *nsContent) Delete(ctx context.Context, dgst digest.Digest) error {
46+
func (c *Store) Delete(ctx context.Context, dgst digest.Digest) error {
3847
return errors.Errorf("contentstore.Delete usage is forbidden")
3948
}
4049

41-
func (c *nsContent) Status(ctx context.Context, ref string) (content.Status, error) {
50+
func (c *Store) Status(ctx context.Context, ref string) (content.Status, error) {
4251
ctx = namespaces.WithNamespace(ctx, c.ns)
4352
return c.Store.Status(ctx, ref)
4453
}
4554

46-
func (c *nsContent) ListStatuses(ctx context.Context, filters ...string) ([]content.Status, error) {
55+
func (c *Store) ListStatuses(ctx context.Context, filters ...string) ([]content.Status, error) {
4756
ctx = namespaces.WithNamespace(ctx, c.ns)
4857
return c.Store.ListStatuses(ctx, filters...)
4958
}
5059

51-
func (c *nsContent) Abort(ctx context.Context, ref string) error {
60+
func (c *Store) Abort(ctx context.Context, ref string) error {
5261
ctx = namespaces.WithNamespace(ctx, c.ns)
5362
return c.Store.Abort(ctx, ref)
5463
}
5564

56-
func (c *nsContent) ReaderAt(ctx context.Context, desc ocispecs.Descriptor) (content.ReaderAt, error) {
65+
func (c *Store) ReaderAt(ctx context.Context, desc ocispecs.Descriptor) (content.ReaderAt, error) {
5766
ctx = namespaces.WithNamespace(ctx, c.ns)
5867
return c.Store.ReaderAt(ctx, desc)
5968
}
6069

61-
func (c *nsContent) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
70+
func (c *Store) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
6271
return c.writer(ctx, 3, opts...)
6372
}
6473

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

0 commit comments

Comments
 (0)