Repository: containerd/containerd
File: plugins/metadata/plugin.go (Go)

Code before:
		InitFn: func(ic *plugin.InitContext) (interface{}, error) {
			root := ic.Properties[plugins.PropertyRootDir]
			if err := os.MkdirAll(root, 0711); err != nil {
				return nil, err
			}
			cs, err := ic.GetSingle(plugins.ContentPlugin)
			if err != nil {
				return nil, err
			}

			snapshottersRaw, err := ic.GetByType(plugins.SnapshotPlugin)
			if err != nil {
				return nil, err
			}

			snapshotters := make(map[string]snapshots.Snapshotter)
			for name, sn := range snapshottersRaw {
				snapshotters[name] = sn.(snapshots.Snapshotter)
			}

			ep, err := ic.GetSingle(plugins.EventPlugin)
			if err != nil {
				return nil, err
			}

			config := *bolt.DefaultOptions
			// Without the timeout, bbolt.Open would block indefinitely due to flock(2).
			config.Timeout = timeout.Get(boltOpenTimeout)

			shared := true
			ic.Meta.Exports["policy"] = SharingPolicyShared
			if cfg, ok := ic.Config.(*BoltConfig); ok {
				if cfg.ContentSharingPolicy != "" {
					if err := cfg.Validate(); err != nil {
						return nil, err
					}
					if cfg.ContentSharingPolicy == SharingPolicyIsolated {
						ic.Meta.Exports["policy"] = SharingPolicyIsolated
						shared = false
					}

					log.G(ic.Context).WithField("policy", cfg.ContentSharingPolicy).Info("metadata content store policy set")

					// Apply options from config
					config.NoFreelistSync = cfg.DB.NoFreelistSync
					config.PreLoadFreelist = cfg.DB.PreLoadFreelist
					config.NoSync = cfg.DB.NoSync
					config.Mlock = cfg.DB.Mlock
					config.NoGrowSync = cfg.DB.NoGrowSync
					config.MmapFlags = cfg.DB.MmapFlags
					config.InitialMmapSize = cfg.DB.InitialMmapSize

Review comment (suggestion):
Do we need to call `defaultBoltOptions` here instead of using the default options provided by bolt (as we overwrote `NoFreelistSync` at https://github.com/containerd/containerd/pull/10745/files#diff-9c09b2ff8e763fbd2736030258f48ef2115b4cf95d00dc1e32871cedde707fc0R80-R84)

Code after:
						ic.Meta.Exports["policy"] = SharingPolicyIsolated
						shared = false
					}

					log.G(ic.Context).WithField("policy", cfg.ContentSharingPolicy).Info("metadata content store policy set")

					if cfg.NoSync {
						options.NoSync = true
						options.NoGrowSync = true

						log.G(ic.Context).Warn("using async mode for boltdb")
					}
				}
			}

			path := filepath.Join(root, "meta.db")
			ic.Meta.Exports["path"] = path

			doneCh := make(chan struct{})
			go func() {
				t := time.NewTimer(10 * time.Second)
				defer t.Stop()
				select {
				case <-t.C:
					log.G(ic.Context).WithField("plugin", "bolt").Warn("waiting for response from boltdb open")
				case <-doneCh:
					return
				}
			}()

			db, err := bolt.Open(path, 0644, &options)
			close(doneCh)
			if err != nil {
				return nil, err
			}

			dbopts := []metadata.DBOpt{
				metadata.WithEventsPublisher(ep.(events.Publisher)),
			}

			if !shared {
				dbopts = append(dbopts, metadata.WithPolicyIsolated)
			}

			mdb := metadata.NewDB(db, cs.(content.Store), snapshotters, dbopts...)
			if err := mdb.Init(ic.Context); err != nil {
				return nil, err
			}
			return mdb, nil
		},
	})