-
Notifications
You must be signed in to change notification settings - Fork 81
storage: add splitfdstream for efficient layer transfer with reflink support #651
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
Open
giuseppe
wants to merge
12
commits into
containers:main
Choose a base branch
from
giuseppe:splitfdstream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8085f08
pkg/fileutils: use CopyFileRange if possible
giuseppe fa6bb30
storage, chunked: new function GenerateDumpFromTarHeaders
giuseppe 3e31b05
storage/vendor: add github.com/cgwalters/jsonrpc-fdpass-go
giuseppe bf6cc4f
storage/pkg/splitfdstream: new package
giuseppe 1ef55d9
storage/store: add new APIs to resolve image ID
giuseppe f491453
storage: add SplitFDStreamStore interface
giuseppe e23a04d
storage/archive,chrootarchive: add support for splitfdstream
giuseppe be78f57
storage, overlay: use openat2 instead of using procfs
giuseppe aec5e5d
storage, overlay: factor function out
giuseppe cab0694
storage/overlay: implement SplitFDStreamDriver
giuseppe 040b2e2
storage/cmd: new commands json-rpc-server and apply-splitfdstream
giuseppe 42fcc4e
storage/tests: add tests for splitfdstream
giuseppe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| //go:build linux | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "os" | ||
| "os/signal" | ||
| "path/filepath" | ||
| "syscall" | ||
|
|
||
| "go.podman.io/storage" | ||
| graphdriver "go.podman.io/storage/drivers" | ||
| "go.podman.io/storage/pkg/archive" | ||
| "go.podman.io/storage/pkg/mflag" | ||
| "go.podman.io/storage/pkg/splitfdstream" | ||
| ) | ||
|
|
||
| const defaultJSONRPCSocket = "json-rpc.sock" | ||
|
|
||
| var ( | ||
| splitfdstreamSocket = "" | ||
| applyFdstreamSocket = "" | ||
| applyFdstreamParent = "" | ||
| applyFdstreamMountLabel = "" | ||
| ) | ||
|
|
||
| // splitFDStreamDiffer implements graphdriver.Differ for splitfdstream data | ||
| type splitFDStreamDiffer struct { | ||
| streamData []byte | ||
| fds []*os.File | ||
| store storage.Store | ||
| } | ||
|
|
||
| func (d *splitFDStreamDiffer) ApplyDiff(dest string, options *archive.TarOptions, differOpts *graphdriver.DifferOptions) (graphdriver.DriverWithDifferOutput, error) { | ||
| driver, err := d.store.GraphDriver() | ||
| if err != nil { | ||
| return graphdriver.DriverWithDifferOutput{}, fmt.Errorf("failed to get graph driver: %w", err) | ||
| } | ||
|
|
||
| splitDriver, ok := driver.(splitfdstream.SplitFDStreamDriver) | ||
| if !ok { | ||
| return graphdriver.DriverWithDifferOutput{}, fmt.Errorf("driver %s does not support splitfdstream", driver.String()) | ||
| } | ||
|
|
||
| opts := &splitfdstream.ApplySplitFDStreamOpts{ | ||
| Stream: bytes.NewReader(d.streamData), | ||
| FileDescriptors: d.fds, | ||
| StagingDir: dest, | ||
| } | ||
|
|
||
| size, err := splitDriver.ApplySplitFDStream(opts) | ||
| if err != nil { | ||
| return graphdriver.DriverWithDifferOutput{}, fmt.Errorf("failed to apply splitfdstream to staging dir %s: %w", dest, err) | ||
| } | ||
|
|
||
| return graphdriver.DriverWithDifferOutput{ | ||
| Target: dest, | ||
| Size: size, | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *splitFDStreamDiffer) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| func splitfdstreamServer(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) { | ||
| driver, err := m.GraphDriver() | ||
| if err != nil { | ||
| return 1, fmt.Errorf("failed to get graph driver: %w", err) | ||
| } | ||
|
|
||
| splitDriver, ok := driver.(splitfdstream.SplitFDStreamDriver) | ||
| if !ok { | ||
| return 1, fmt.Errorf("driver %s does not support splitfdstream", driver.String()) | ||
| } | ||
| server := splitfdstream.NewJSONRPCServer(splitDriver, m) | ||
|
|
||
| socketPath := splitfdstreamSocket | ||
| if socketPath == "" { | ||
| socketPath = filepath.Join(m.RunRoot(), defaultJSONRPCSocket) | ||
| } | ||
|
|
||
| if err := server.Start(socketPath); err != nil { | ||
| return 1, fmt.Errorf("failed to start server: %w", err) | ||
| } | ||
| defer func() { _ = server.Stop() }() | ||
|
|
||
| fmt.Printf("%s\n", socketPath) | ||
|
|
||
| // Wait for interrupt signal | ||
| sigCh := make(chan os.Signal, 1) | ||
| signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) | ||
| <-sigCh | ||
|
|
||
| return 0, nil | ||
| } | ||
|
|
||
| func applySplitfdstream(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) { | ||
| layerID := args[0] | ||
|
|
||
| socketPath := applyFdstreamSocket | ||
| if socketPath == "" { | ||
| socketPath = filepath.Join(m.RunRoot(), defaultJSONRPCSocket) | ||
| } | ||
|
|
||
| defer func() { | ||
| if _, err := m.Shutdown(false); err != nil { | ||
| fmt.Fprintf(os.Stderr, "warning: failed to shutdown storage: %v\n", err) | ||
| } | ||
| }() | ||
|
|
||
| client, err := splitfdstream.NewJSONRPCClient(socketPath) | ||
| if err != nil { | ||
| return 1, fmt.Errorf("failed to connect to server: %w", err) | ||
| } | ||
| defer client.Close() | ||
|
|
||
| // Get splitfdstream data from remote server | ||
| streamData, fds, err := client.GetSplitFDStream(layerID, "") | ||
| if err != nil { | ||
| return 1, fmt.Errorf("failed to get splitfdstream from server: %w", err) | ||
| } | ||
|
|
||
| // Close received FDs when done | ||
| defer func() { | ||
| for _, fd := range fds { | ||
| fd.Close() | ||
| } | ||
| }() | ||
|
|
||
| // Create a custom differ for splitfdstream data | ||
| differ := &splitFDStreamDiffer{ | ||
| streamData: streamData, | ||
| fds: fds, | ||
| store: m, | ||
| } | ||
| defer differ.Close() | ||
|
|
||
| // Prepare the staged layer | ||
| diffOptions := &graphdriver.ApplyDiffWithDifferOpts{} | ||
| diffOutput, err := m.PrepareStagedLayer(diffOptions, differ) | ||
| if err != nil { | ||
| return 1, fmt.Errorf("failed to prepare staged layer: %w", err) | ||
| } | ||
|
|
||
| // Apply the staged layer to create the final layer | ||
| applyArgs := storage.ApplyStagedLayerOptions{ | ||
| ID: layerID, | ||
| ParentLayer: applyFdstreamParent, | ||
| MountLabel: applyFdstreamMountLabel, | ||
| Writeable: false, | ||
| LayerOptions: &storage.LayerOptions{}, | ||
| DiffOutput: diffOutput, | ||
| DiffOptions: diffOptions, | ||
| } | ||
|
|
||
| layer, err := m.ApplyStagedLayer(applyArgs) | ||
| if err != nil { | ||
| // Clean up the staged layer on failure | ||
| if cleanupErr := m.CleanupStagedLayer(diffOutput); cleanupErr != nil { | ||
| fmt.Fprintf(os.Stderr, "warning: failed to cleanup staged layer: %v\n", cleanupErr) | ||
| } | ||
| return 1, fmt.Errorf("failed to apply staged layer: %w", err) | ||
| } | ||
|
|
||
| // Output the result | ||
| if jsonOutput { | ||
| return outputJSON(map[string]interface{}{"id": layer.ID, "size": diffOutput.Size}) | ||
| } | ||
| fmt.Printf("%s\n", layer.ID) | ||
| return 0, nil | ||
| } | ||
|
|
||
| func init() { | ||
| commands = append(commands, command{ | ||
| names: []string{"json-rpc-server"}, | ||
| optionsHelp: "[options]", | ||
| usage: "Start a JSON-RPC server", | ||
| minArgs: 0, | ||
| maxArgs: 0, | ||
| action: splitfdstreamServer, | ||
| addFlags: func(flags *mflag.FlagSet, cmd *command) { | ||
| flags.StringVar(&splitfdstreamSocket, []string{"-socket"}, "", | ||
| "Path to UNIX socket") | ||
| }, | ||
| }) | ||
| commands = append(commands, command{ | ||
| names: []string{"apply-splitfdstream"}, | ||
| optionsHelp: "[options] layerID", | ||
| usage: "Fetch a layer from remote server and apply it locally", | ||
| minArgs: 1, | ||
| maxArgs: 1, | ||
| action: applySplitfdstream, | ||
| addFlags: func(flags *mflag.FlagSet, cmd *command) { | ||
| flags.StringVar(&applyFdstreamSocket, []string{"-socket"}, "", | ||
| "Path to remote UNIX socket") | ||
| flags.StringVar(&applyFdstreamParent, []string{"-parent"}, "", | ||
| "Parent layer ID for the new layer") | ||
| flags.StringVar(&applyFdstreamMountLabel, []string{"-mount-label"}, "", | ||
| "SELinux mount label for the layer") | ||
| flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output") | ||
| }, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
AI: When Openat2 returns any error, the code does
continueto try the next diffDir. This silently ignores errors like EACCES, ENOMEM, etc. Only ENOENT (and possibly ELOOP for RESOLVE_NO_SYMLINKS) should trigger continue; other errors should be returned.Also, RESOLVE_NO_SYMLINKS is a behavior change from the old procfs approach which followed symlinks. If any legitimate composefs path contains a symlink component, this will silently skip the entry. Worth documenting that this is intentional.