-
Notifications
You must be signed in to change notification settings - Fork 143
add hot reloading for fs config #2179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /* | ||
| Copyright The containerd Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "path/filepath" | ||
| "reflect" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/containerd/containerd/v2/core/snapshots" | ||
| "github.com/containerd/log" | ||
| fsconfig "github.com/containerd/stargz-snapshotter/fs/config" | ||
| "github.com/fsnotify/fsnotify" | ||
| "github.com/pelletier/go-toml" | ||
| ) | ||
|
|
||
| // WatchConfig monitors the specified configuration file for changes. | ||
| // It triggers the config reload when a change is detected. | ||
| func WatchConfig( | ||
| ctx context.Context, | ||
| filePath string, | ||
| rs snapshots.Snapshotter, | ||
| initialConfig *fsconfig.Config, | ||
| ) error { | ||
| absFilePath, err := filepath.Abs(filePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| watchDir := filepath.Dir(absFilePath) | ||
|
|
||
| watcher, err := fsnotify.NewWatcher() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work in the rootless settings? |
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := watcher.Add(watchDir); err != nil { | ||
| watcher.Close() | ||
| return err | ||
| } | ||
|
|
||
| log.G(ctx).Infof("started monitoring config file: %s", absFilePath) | ||
|
|
||
| cw := &configWatcher{ | ||
| lastConfig: initialConfig, | ||
| } | ||
|
|
||
| go func() { | ||
| defer watcher.Close() | ||
|
|
||
| var ( | ||
| debounceTimer *time.Timer | ||
| mu sync.Mutex | ||
|
Comment on lines
+67
to
+69
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is |
||
| ) | ||
|
|
||
| for { | ||
| select { | ||
| case event, ok := <-watcher.Events: | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| if event.Name == absFilePath { | ||
| // Trigger on Write, Create, Rename, or Chmod events | ||
| // such as vim, nano, etc. | ||
| if event.Has(fsnotify.Write) || event.Has(fsnotify.Create) || | ||
| event.Has(fsnotify.Rename) || event.Has(fsnotify.Chmod) { | ||
|
Comment on lines
+79
to
+83
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If the file is renamed, the next change won't be detected, will it? Is this an expected behaviour? |
||
|
|
||
| mu.Lock() | ||
| if debounceTimer != nil { | ||
| debounceTimer.Stop() | ||
| } | ||
| // Debounce changes with a 50ms delay | ||
| debounceTimer = time.AfterFunc(50*time.Millisecond, func() { | ||
| log.G(ctx).Infof("config file modification detected: %s", absFilePath) | ||
| cw.reload(ctx, absFilePath, rs) | ||
| }) | ||
| mu.Unlock() | ||
| } | ||
| } | ||
|
|
||
| case err, ok := <-watcher.Errors: | ||
| if !ok { | ||
| return | ||
| } | ||
| log.G(ctx).WithError(err).Error("config watcher encountered an error") | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type configWatcher struct { | ||
| lastConfig *fsconfig.Config | ||
| mu sync.Mutex | ||
| } | ||
|
|
||
| func (w *configWatcher) reload(ctx context.Context, configPath string, rs snapshots.Snapshotter) { | ||
| log.G(ctx).Infof("Config file %s changed, reloading...", configPath) | ||
| var newConfig snapshotterConfig | ||
| tree, err := toml.LoadFile(configPath) | ||
| if err != nil { | ||
| log.G(ctx).WithError(err).Error("failed to reload config file") | ||
| return | ||
| } | ||
| if err := tree.Unmarshal(&newConfig); err != nil { | ||
| log.G(ctx).WithError(err).Error("failed to unmarshal config") | ||
| return | ||
| } | ||
|
|
||
| newFsConfig := newConfig.Config.Config | ||
|
|
||
| w.mu.Lock() | ||
| defer w.mu.Unlock() | ||
|
|
||
| if w.lastConfig != nil && reflect.DeepEqual(*w.lastConfig, newFsConfig) { | ||
| log.G(ctx).Info("Config content unchanged, skipping update") | ||
| return | ||
| } | ||
|
|
||
| if updater, ok := rs.(interface { | ||
| UpdateConfig(context.Context, fsconfig.Config) error | ||
| }); ok { | ||
| log.G(ctx).Debugf("applying new config: %+v", newFsConfig) | ||
| if err := updater.UpdateConfig(ctx, newFsConfig); err != nil { | ||
| log.G(ctx).WithError(err).Error("failed to update config") | ||
| } else { | ||
| log.G(ctx).Info("Config updated successfully") | ||
| cfgCopy := newFsConfig | ||
| w.lastConfig = &cfgCopy | ||
| } | ||
| } else { | ||
| log.G(ctx).Warn("snapshotter does not support config update") | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,6 +260,14 @@ insecure = true | |
|
|
||
| The config file can be passed to stargz snapshotter using `containerd-stargz-grpc`'s `--config` option. | ||
|
|
||
| ## Configuration hot reload | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The benefit of this feature compared to the FUSE manager (which already supports safe restarts) should be documented.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| [Fs configurations](/fs/config/config.go) supports hot reloading. When the configuration file is modified, the snapshotter detects the change and applies the new configuration without restarting the process. | ||
| This enables instant performance tuning (e.g. concurrency, timeouts) without I/O suspension, and allows updating FUSE parameters that cannot be changed by simply restarting the main process when FUSE manager is enabled. | ||
|
|
||
| Note that other configurations (e.g. `proxy_plugins`, `fuse_manager`, `resolver`, `mount_options`) require a restart to take effect. | ||
| Also, some specific fields in `[stargz]` section (e.g. `no_prometheus`) do not support hot reloading and changes to them will be ignored until restart. | ||
|
|
||
| ## Make your remote snapshotter | ||
|
|
||
| It isn't difficult for you to implement your remote snapshotter using [our general snapshotter package](/snapshot) without considering the protocol between that and containerd. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to export this symbol.