Skip to content

Commit

Permalink
feat: Remove DumpFile operations
Browse files Browse the repository at this point in the history
  • Loading branch information
fappy1234567 committed Sep 11, 2024
1 parent 8fa319b commit 606c148
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 40 deletions.
53 changes: 41 additions & 12 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,16 @@ func (d *Daemon) sharedFusedevMount(rafs *rafs.Rafs) error {
return err
}

c, err := daemonconfig.NewDaemonConfig(d.States.FsDriver, d.ConfigFile(rafs.SnapshotID))
if err != nil {
return errors.Wrapf(err, "Failed to reload instance configuration %s",
d.ConfigFile(rafs.SnapshotID))
}

// c, err := daemonconfig.NewDaemonConfig(d.States.FsDriver, d.ConfigFile(rafs.SnapshotID))
// if err != nil {
// return errors.Wrapf(err, "Failed to reload instance configuration %s",
// d.ConfigFile(rafs.SnapshotID))
// }
c := d.Config
cfg, err := c.DumpString()
if err != nil {
return errors.Wrap(err, "dump instance configuration")
}

err = client.Mount(rafs.RelaMountpoint(), bootstrap, cfg)
if err != nil {
return errors.Wrapf(err, "mount rafs instance")
Expand All @@ -280,11 +279,12 @@ func (d *Daemon) sharedErofsMount(ra *rafs.Rafs) error {
return errors.Wrapf(err, "failed to create fscache work dir %s", ra.FscacheWorkDir())
}

c, err := daemonconfig.NewDaemonConfig(d.States.FsDriver, d.ConfigFile(ra.SnapshotID))
if err != nil {
log.L.Errorf("Failed to reload daemon configuration %s, %s", d.ConfigFile(ra.SnapshotID), err)
return err
}
// c, err := daemonconfig.NewDaemonConfig(d.States.FsDriver, d.ConfigFile(ra.SnapshotID))
// if err != nil {
// log.L.Errorf("Failed to reload daemon configuration %s, %s", d.ConfigFile(ra.SnapshotID), err)
// return err
// }
c := d.Config

cfgStr, err := c.DumpString()
if err != nil {
Expand Down Expand Up @@ -650,3 +650,32 @@ func NewDaemon(opt ...NewDaemonOpt) (*Daemon, error) {

return d, nil
}

func (d *Daemon) MountByAPI() error {
rafs := d.RafsCache.Head()
if rafs == nil {
return errors.Wrapf(errdefs.ErrNotFound, "daemon %s no rafs instance associated", d.ID())
}
client, err := d.GetClient()
if err != nil {
return errors.Wrapf(err, "mount instance %s", rafs.SnapshotID)
}

bootstrap, err := rafs.BootstrapFile()
if err != nil {
return err
}
c := d.Config
cfg, err := c.DumpString()
if err != nil {
return errors.Wrap(err, "dump instance configuration")
}

err = client.Mount("/", bootstrap, cfg)
if err != nil {
return errors.Wrapf(err, "mount rafs instance")
}

return nil

}
54 changes: 30 additions & 24 deletions pkg/filesystem/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,26 +310,32 @@ func (fs *Filesystem) Mount(ctx context.Context, snapshotID string, labels map[s

// TODO: How to manage rafs configurations on-disk? separated json config file or DB record?
// In order to recover erofs mount, the configuration file has to be persisted.
var configSubDir string
if useSharedDaemon {
configSubDir = snapshotID
} else {
// Associate daemon config object when creating a new daemon object to avoid
// reading disk file again and again.
// For shared daemon, each rafs instance has its own configuration, so we don't
// attach a config interface to daemon in this case.
d.Config = cfg
}

err = cfg.DumpFile(d.ConfigFile(configSubDir))
if err != nil {
if errors.Is(err, errdefs.ErrAlreadyExists) {
log.L.Debugf("Configuration file %s already exits", d.ConfigFile(configSubDir))
} else {
return errors.Wrap(err, "dump daemon configuration file")
}
}

// var configSubDir string
// if useSharedDaemon {
// configSubDir = snapshotID
// } else {
// // Associate daemon config object when creating a new daemon object to avoid
// // reading disk file again and again.
// // For shared daemon, each rafs instance has its own configuration, so we don't
// // attach a config interface to daemon in this case.
// d.Config = cfg
// }

// err = cfg.DumpFile(d.ConfigFile(configSubDir))

// if err != nil {
// if errors.Is(err, errdefs.ErrAlreadyExists) {
// log.L.Debugf("Configuration file %s already exits", d.ConfigFile(configSubDir))
// } else {
// return errors.Wrap(err, "dump daemon configuration file")
// }
// }
// content, errs := cfg.DumpString()
// if errs != nil {
// return errors.Wrap(err, "dump instance configuration")
// }
// log.L.Infof("content %s", content)
d.Config = cfg
d.AddRafsInstance(rafs)

// if publicKey is not empty we should verify bootstrap file of image
Expand Down Expand Up @@ -596,10 +602,10 @@ func (fs *Filesystem) initSharedDaemon(fsManager *manager.Manager) (err error) {
// it is loaded when requesting mount api
// Dump the configuration file since it is reloaded when recovering the nydusd
d.Config = *fsManager.DaemonConfig
err = d.Config.DumpFile(d.ConfigFile(""))
if err != nil && !errors.Is(err, errdefs.ErrAlreadyExists) {
return errors.Wrapf(err, "dump configuration file %s", d.ConfigFile(""))
}
// err = d.Config.DumpFile(d.ConfigFile(""))
// if err != nil && !errors.Is(err, errdefs.ErrAlreadyExists) {
// return errors.Wrapf(err, "dump configuration file %s", d.ConfigFile(""))
// }

if err := fsManager.StartDaemon(d); err != nil {
return errors.Wrap(err, "start shared daemon")
Expand Down
18 changes: 14 additions & 4 deletions pkg/manager/daemon_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ func (m *Manager) StartDaemon(d *daemon.Daemon) error {
if err := cmd.Start(); err != nil {
return err
}
fsDriver := config.GetFsDriver()
isSharedFusedev := fsDriver == config.FsDriverFusedev && config.GetDaemonMode() == config.DaemonModeShared
useSharedDaemon := fsDriver == config.FsDriverFscache || isSharedFusedev

if !useSharedDaemon {
errs := d.MountByAPI()
if errs != nil {
return errors.Wrapf(err, "failed to mount")
}
}

d.Lock()
defer d.Unlock()
Expand Down Expand Up @@ -155,10 +165,10 @@ func (m *Manager) BuildDaemonCommand(d *daemon.Daemon, bin string, upgrade bool)
return nil, errors.Wrapf(err, "locate bootstrap %s", bootstrap)
}

cmdOpts = append(cmdOpts,
command.WithConfig(d.ConfigFile("")),
command.WithBootstrap(bootstrap),
)
// cmdOpts = append(cmdOpts,
// command.WithConfig(d.ConfigFile("")),
// command.WithBootstrap(bootstrap),
// )
if config.IsBackendSourceEnabled() {
configAPIPath := fmt.Sprintf(endpointGetBackend, d.States.ID)
cmdOpts = append(cmdOpts,
Expand Down

0 comments on commit 606c148

Please sign in to comment.