-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. add prefetchlist store prefetchlist storage service. 2. Modify the optimizer to publish the access file list as a prefetchlist to the storage service when obtaining it.
- Loading branch information
Showing
3 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,96 @@ | ||
/* | ||
* Copyright (c) 2023. Nydus Developers. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
type PrefetchFile struct { | ||
Path string | ||
} | ||
|
||
type CacheItem struct { | ||
ImageName string | ||
ContainerName string | ||
PrefetchFiles []PrefetchFile | ||
} | ||
|
||
type Cache struct { | ||
Items map[string]*CacheItem | ||
mutex sync.Mutex | ||
} | ||
|
||
func (cache *Cache) key(imageName, containerName string) string { | ||
return fmt.Sprintf("%s,%s", imageName, containerName) | ||
} | ||
|
||
func (cache *Cache) Get(imageName, containerName string) *CacheItem { | ||
cache.mutex.Lock() | ||
defer cache.mutex.Unlock() | ||
|
||
return cache.Items[cache.key(imageName, containerName)] | ||
} | ||
|
||
func (cache *Cache) Set(item CacheItem) { | ||
cache.mutex.Lock() | ||
defer cache.mutex.Unlock() | ||
|
||
cache.Items[cache.key(item.ImageName, item.ContainerName)] = &item | ||
} | ||
|
||
var serverCache Cache | ||
|
||
func uploadHandler(w http.ResponseWriter, r *http.Request) { | ||
var item CacheItem | ||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
http.Error(w, "Failed to read request body", http.StatusBadRequest) | ||
return | ||
} | ||
err = json.Unmarshal(body, &item) | ||
if err != nil { | ||
http.Error(w, "Invalid request payload", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
serverCache.Set(item) | ||
fmt.Fprintf(w, "Uploaded CacheItem for %s, %s successfully", item.ImageName, item.ContainerName) | ||
} | ||
|
||
func downloadHandler(w http.ResponseWriter, r *http.Request) { | ||
imageName := r.URL.Query().Get("imageName") | ||
containerName := r.URL.Query().Get("containerName") | ||
|
||
item := serverCache.Get(imageName, containerName) | ||
if item == nil { | ||
http.Error(w, "CacheItem not found", http.StatusNotFound) | ||
return | ||
} | ||
|
||
err := json.NewEncoder(w).Encode(item) | ||
if err != nil { | ||
http.Error(w, "Failed to encode CacheItem to JSON", http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
func main() { | ||
serverCache = Cache{Items: make(map[string]*CacheItem)} | ||
|
||
http.HandleFunc("/api/v1/prefetch/upload", uploadHandler) | ||
http.HandleFunc("/api/v1/prefetch/download", downloadHandler) | ||
|
||
fmt.Println("Server started on :1323") | ||
err := http.ListenAndServe(":1323", nil) | ||
if err != nil { | ||
fmt.Printf("Failed to start server: %v\n", err) | ||
} | ||
} |