MCPcopy
hub / github.com/containerd/containerd / garbageCollect

Method garbageCollect

core/metadata/content.go:844–960  ·  view source on GitHub ↗

garbageCollect removes all contents that are no longer used.

(ctx context.Context)

Source from the content-addressed store, hash-verified

842
843// garbageCollect removes all contents that are no longer used.
844func (cs *contentStore) garbageCollect(ctx context.Context) (d time.Duration, err error) {
845 cs.l.Lock()
846 t1 := time.Now()
847 defer func() {
848 if err == nil {
849 d = time.Since(t1)
850 }
851 cs.l.Unlock()
852 }()
853
854 contentSeen := map[string]struct{}{}
855 ingestSeen := map[string]struct{}{}
856 if err := cs.db.View(func(tx *bolt.Tx) error {
857 v1bkt := tx.Bucket(bucketKeyVersion)
858 if v1bkt == nil {
859 return nil
860 }
861
862 // iterate through each namespace
863 v1c := v1bkt.Cursor()
864
865 for k, v := v1c.First(); k != nil; k, v = v1c.Next() {
866 if v != nil {
867 continue
868 }
869
870 cbkt := v1bkt.Bucket(k).Bucket(bucketKeyObjectContent)
871 if cbkt == nil {
872 continue
873 }
874 bbkt := cbkt.Bucket(bucketKeyObjectBlob)
875 if bbkt != nil {
876 if err := bbkt.ForEach(func(ck, cv []byte) error {
877 if cv == nil {
878 contentSeen[string(ck)] = struct{}{}
879 }
880 return nil
881 }); err != nil {
882 return err
883 }
884 }
885
886 ibkt := cbkt.Bucket(bucketKeyObjectIngests)
887 if ibkt != nil {
888 if err := ibkt.ForEach(func(ref, v []byte) error {
889 if v == nil {
890 bkt := ibkt.Bucket(ref)
891 // expected here may be from a different namespace
892 // so much be explicitly retained from the ingest
893 // in case it was removed from the other namespace
894 expected := bkt.Get(bucketKeyExpected)
895 if len(expected) > 0 {
896 contentSeen[string(expected)] = struct{}{}
897 }
898 bref := bkt.Get(bucketKeyRef)
899 if len(bref) > 0 {
900 ingestSeen[string(bref)] = struct{}{}
901 }

Callers 2

cleanupSnapshotterMethod · 0.45
cleanupContentMethod · 0.45

Calls 10

LockMethod · 0.65
UnlockMethod · 0.65
ViewMethod · 0.65
GetMethod · 0.65
WalkMethod · 0.65
DeleteMethod · 0.65
WalkStatusRefsMethod · 0.65
AbortMethod · 0.65
ListStatusesMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected