@@ -8,53 +8,106 @@ package prefetch
88
99import (
1010 "encoding/json"
11- "sync"
11+ "fmt"
12+ "io"
13+ "net/http"
14+ "os"
15+ "path/filepath"
16+ "strings"
1217
18+ << << << < HEAD
1319 "github.com/containerd/log"
20+ == == == =
21+ "github.com/containerd/containerd/log"
22+ "github.com/pkg/errors"
23+
24+ "github.com/containerd/nydus-snapshotter/config"
25+ >> >> >> > 5977 f28 (unawareness prefetch implementation on snapshotter side )
1426)
1527
16- type prefetchInfo struct {
17- prefetchMap map [string ]string
18- prefetchMutex sync.Mutex
28+ type prefetchlist struct {
29+ FilePaths []string `json:"files"`
1930}
2031
21- var Pm prefetchInfo
32+ func GetPrefetchList (prefetchDir , imageRepo string ) (string , error ) {
33+ if config .IsPrefetchEnabled () {
34+ url := config .GetPrefetchEndpoint ()
35+ getURL := fmt .Sprintf ("%s?imageName=%s" , url , imageRepo )
36+
37+ resp , err := http .Get (getURL )
38+ if err != nil {
39+ return "" , err
40+ }
41+ defer resp .Body .Close ()
42+
43+ if resp .StatusCode != http .StatusOK && resp .StatusCode != http .StatusNotFound {
44+ return "" , fmt .Errorf ("get from server returned a non-OK status code: %d, HTTP Status Error" , resp .StatusCode )
45+ }
46+
47+ body , err := io .ReadAll (resp .Body )
48+ if err != nil {
49+ return "" , err
50+ }
2251
23- func (p * prefetchInfo ) SetPrefetchFiles (body []byte ) error {
24- p .prefetchMutex .Lock ()
25- defer p .prefetchMutex .Unlock ()
52+ if strings .Contains (string (body ), "CacheItem not found" ) {
53+ log .L .Infof ("Cache item not found for image: %s\n " , imageRepo )
54+ return "" , nil
55+ }
56+
57+ prefetchfilePath , err := storePrefetchList (prefetchDir , body )
58+ if err != nil {
59+ return "" , err
60+ }
61+ return prefetchfilePath , nil
62+ }
63+ return "" , nil
64+ }
2665
27- var prefetchMsg [] map [ string ] string
28- if err := json . Unmarshal ( body , & prefetchMsg ); err != nil {
29- return err
66+ func storePrefetchList ( prefetchDir string , list [] byte ) ( string , error ) {
67+ if err := os . MkdirAll ( prefetchDir , 0755 ); err != nil {
68+ return "" , errors . Wrapf ( err , "create prefetch dir %s" , prefetchDir )
3069 }
3170
32- if p .prefetchMap == nil {
33- p .prefetchMap = make (map [string ]string )
71+ filePath := filepath .Join (prefetchDir , "list" )
72+ jsonfilePath := filepath .Join (prefetchDir , "list.json" )
73+
74+ file , err := os .OpenFile (filePath , os .O_WRONLY | os .O_APPEND | os .O_CREATE , 0644 )
75+ if err != nil {
76+ fmt .Println ("Error opening file:" , err )
77+ return "" , errors .Wrap (err , "error opening prefetch file" )
3478 }
35- for _ , item := range prefetchMsg {
36- image := item ["image" ]
37- prefetchfiles := item ["prefetch" ]
38- p .prefetchMap [image ] = prefetchfiles
79+ defer file .Close ()
80+
81+ var prefetchSlice []string
82+ err = json .Unmarshal (list , & prefetchSlice )
83+ if err != nil {
84+ return "" , errors .Wrap (err , "failed to parse prefetch list" )
3985 }
4086
41- log .L .Infof ("received prefetch list from nri plugin: %v " , p .prefetchMap )
42- return nil
43- }
87+ for _ , path := range prefetchSlice {
88+ content := path + "\n "
89+ _ , err := file .WriteString (content )
90+ if err != nil {
91+ return "" , errors .Wrap (err , "error writing to prefetch file" )
92+ }
93+ }
4494
45- func (p * prefetchInfo ) GetPrefetchInfo (image string ) string {
46- p .prefetchMutex .Lock ()
47- defer p .prefetchMutex .Unlock ()
95+ prefetchStruct := prefetchlist {FilePaths : prefetchSlice }
96+ jsonByte , err := json .Marshal (prefetchStruct )
97+ if err != nil {
98+ return "" , errors .Wrap (err , "failed to marshal to JSON" )
99+ }
48100
49- if prefetchfiles , ok := p .prefetchMap [image ]; ok {
50- return prefetchfiles
101+ jsonfile , err := os .Create (jsonfilePath )
102+ if err != nil {
103+ return "" , errors .Wrapf (err , "failed to create file %s" , jsonfilePath )
51104 }
52- return ""
53- }
105+ defer jsonfile .Close ()
54106
55- func (p * prefetchInfo ) DeleteFromPrefetchMap (image string ) {
56- p .prefetchMutex .Lock ()
57- defer p .prefetchMutex .Unlock ()
107+ _ , err = jsonfile .Write (jsonByte )
108+ if err != nil {
109+ return "" , errors .Wrap (err , "error writing JSON to file" )
110+ }
58111
59- delete ( p . prefetchMap , image )
112+ return filePath , nil
60113}
0 commit comments