Skip to content

Commit

Permalink
prefetch separate fanotify nydus-snapshotter side
Browse files Browse the repository at this point in the history
  • Loading branch information
billie60 committed Apr 18, 2024
1 parent de38ec6 commit 5e6e626
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 44 deletions.
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ type SystemControllerConfig struct {
DebugConfig DebugConfig `toml:"debug"`
}

type PrefetchControllerConfig struct {
Enable bool `toml:"enable"`
PrefetchConfig string `toml:"get_prefetch_endpoint"`
}

type SnapshotterConfig struct {
// Configuration format version
Version int `toml:"version"`
Expand All @@ -229,6 +234,8 @@ type SnapshotterConfig struct {
LoggingConfig LoggingConfig `toml:"log"`
CgroupConfig CgroupConfig `toml:"cgroup"`
Experimental Experimental `toml:"experimental"`
// Get prefetch list from http server
PrefetchControllerConfig PrefetchControllerConfig `toml:"prefetch"`
}

func LoadSnapshotterConfig(path string) (*SnapshotterConfig, error) {
Expand Down
8 changes: 8 additions & 0 deletions config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ func GetPrefetchRoot() string {
return globalConfig.PrefetchRoot
}

func GetPrefetchEndpoint() string {
return globalConfig.origin.PrefetchControllerConfig.PrefetchConfig
}

func IsPrefetchEnabled() bool {
return globalConfig.origin.PrefetchControllerConfig.Enable
}

func GetMirrorsConfigDir() string {
return globalConfig.MirrorsConfig.Dir
}
Expand Down
4 changes: 4 additions & 0 deletions misc/snapshotter/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ daemon_mode = "dedicated"
# Whether snapshotter should try to clean up resources when it is closed
cleanup_on_close = false

[prefetch]
enable = false
get_prefetch_endpoint = "http://localhost:1323/api/v1/prefetch/download"

[system]
# Snapshotter's debug and trace HTTP server interface
enable = true
Expand Down
2 changes: 1 addition & 1 deletion pkg/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func WithPrefetchDir(dir, imageID string) NewDaemonOpt {
return func(d *Daemon) error {
s := filepath.Join(dir, d.ID())
prefetchDir, err := prefetch.GetPrefetchList(s, imageID)
if err != nil && !errors.Is(err, prefetch.ErrUds) {
if err != nil {
return errors.Wrapf(err, "failed to get prefetchList for image %s in path %s", imageID, s)
}
if prefetchDir != "" {
Expand Down
70 changes: 27 additions & 43 deletions pkg/prefetch/prefetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,56 @@
package prefetch

import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/containerd/containerd/log"
"github.com/pkg/errors"

"github.com/containerd/nydus-snapshotter/config"
)

type prefetchlist struct {
FilePaths []string `json:"files"`
}

const (
endpointPrefetch = "/api/v1/imagename"
udsSocket = "/run/optimizer/prefetch.sock"
)

var ErrUds = errors.New("failed to connect unix domain socket")

func GetPrefetchList(prefetchDir, imageRepo string) (string, error) {
url := fmt.Sprintf("http://unix%s", endpointPrefetch)

req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(imageRepo))
if err != nil {
return "", err
}
if config.IsPrefetchEnabled() {
url := config.GetPrefetchEndpoint()
getURL := fmt.Sprintf("%s?imageName=%s", url, imageRepo)

client := &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", udsSocket)
},
},
}
resp, err := client.Do(req)
if err != nil {
log.L.Infof("failed to connect unix domain socket. Skipping prefetch for image: %s\n", imageRepo)
return "", ErrUds
}
defer resp.Body.Close()
resp, err := http.Get(getURL)
if err != nil {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to send data, status code: %v", resp.StatusCode)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound {
return "", fmt.Errorf("get from server returned a non-OK status code: %d, HTTP Status Error", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}

if strings.Contains(string(body), "CacheItem not found") {
log.L.Infof("Cache item not found for image: %s\n", imageRepo)
return "", nil
}
if strings.Contains(string(body), "CacheItem not found") {
log.L.Infof("Cache item not found for image: %s\n", imageRepo)
return "", nil
}

prefetchfilePath, err := storePrefetchList(prefetchDir, body)
if err != nil {
return "", err
prefetchfilePath, err := storePrefetchList(prefetchDir, body)
if err != nil {
return "", err
}
return prefetchfilePath, nil
}
return prefetchfilePath, nil
return "", nil
}

func storePrefetchList(prefetchDir string, list []byte) (string, error) {
Expand Down

0 comments on commit 5e6e626

Please sign in to comment.