|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "google.golang.org/grpc/codes" |
| 14 | + "google.golang.org/grpc/status" |
| 15 | + "k8s.io/examples/AI/modelcloud/pkg/blobs" |
| 16 | + "k8s.io/klog/v2" |
| 17 | +) |
| 18 | + |
| 19 | +func main() { |
| 20 | + if err := run(context.Background()); err != nil { |
| 21 | + fmt.Fprintf(os.Stderr, "%v\n", err) |
| 22 | + os.Exit(1) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func run(ctx context.Context) error { |
| 27 | + log := klog.FromContext(ctx) |
| 28 | + |
| 29 | + listen := ":8080" |
| 30 | + cacheDir := os.Getenv("CACHE_DIR") |
| 31 | + if cacheDir == "" { |
| 32 | + // We expect CACHE_DIR to be set when running on kubernetes, but default sensibly for local dev |
| 33 | + cacheDir = "~/.cache/blob-server/blobs" |
| 34 | + } |
| 35 | + flag.StringVar(&listen, "listen", listen, "listen address") |
| 36 | + flag.StringVar(&cacheDir, "cache-dir", cacheDir, "cache directory") |
| 37 | + flag.Parse() |
| 38 | + |
| 39 | + if strings.HasPrefix(cacheDir, "~/") { |
| 40 | + homeDir, err := os.UserHomeDir() |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("getting home directory: %w", err) |
| 43 | + } |
| 44 | + cacheDir = filepath.Join(homeDir, strings.TrimPrefix(cacheDir, "~/")) |
| 45 | + } |
| 46 | + |
| 47 | + if err := os.MkdirAll(cacheDir, 0755); err != nil { |
| 48 | + return fmt.Errorf("creating cache directory %q: %w", cacheDir, err) |
| 49 | + } |
| 50 | + |
| 51 | + blobStore := &blobs.LocalBlobStore{ |
| 52 | + LocalDir: cacheDir, |
| 53 | + } |
| 54 | + |
| 55 | + blobCache := &blobCache{ |
| 56 | + CacheDir: cacheDir, |
| 57 | + blobStore: blobStore, |
| 58 | + } |
| 59 | + |
| 60 | + s := &httpServer{ |
| 61 | + blobCache: blobCache, |
| 62 | + tmpDir: filepath.Join(cacheDir, "tmp"), |
| 63 | + } |
| 64 | + |
| 65 | + log.Info("serving http", "endpoint", listen) |
| 66 | + if err := http.ListenAndServe(listen, s); err != nil { |
| 67 | + return fmt.Errorf("serving on %q: %w", listen, err) |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +type httpServer struct { |
| 74 | + blobCache *blobCache |
| 75 | + tmpDir string |
| 76 | +} |
| 77 | + |
| 78 | +func (s *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 79 | + tokens := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/") |
| 80 | + if len(tokens) == 1 { |
| 81 | + if r.Method == "GET" { |
| 82 | + hash := tokens[0] |
| 83 | + s.serveGETBlob(w, r, hash) |
| 84 | + return |
| 85 | + } |
| 86 | + if r.Method == "PUT" { |
| 87 | + hash := tokens[0] |
| 88 | + s.servePUTBlob(w, r, hash) |
| 89 | + return |
| 90 | + } |
| 91 | + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + http.Error(w, "not found", http.StatusNotFound) |
| 96 | +} |
| 97 | + |
| 98 | +func (s *httpServer) serveGETBlob(w http.ResponseWriter, r *http.Request, hash string) { |
| 99 | + ctx := r.Context() |
| 100 | + |
| 101 | + log := klog.FromContext(ctx) |
| 102 | + |
| 103 | + // TODO: Validate hash is hex, right length etc |
| 104 | + |
| 105 | + f, err := s.blobCache.GetBlob(ctx, hash) |
| 106 | + if err != nil { |
| 107 | + if status.Code(err) == codes.NotFound { |
| 108 | + log.Info("blob not found", "hash", hash) |
| 109 | + http.Error(w, "not found", http.StatusNotFound) |
| 110 | + return |
| 111 | + } |
| 112 | + log.Error(err, "error getting blob") |
| 113 | + http.Error(w, "internal server error", http.StatusInternalServerError) |
| 114 | + return |
| 115 | + } |
| 116 | + defer f.Close() |
| 117 | + p := f.Name() |
| 118 | + |
| 119 | + log.Info("serving blob", "hash", hash, "path", p) |
| 120 | + http.ServeFile(w, r, p) |
| 121 | +} |
| 122 | + |
| 123 | +func (s *httpServer) servePUTBlob(w http.ResponseWriter, r *http.Request, hash string) { |
| 124 | + ctx := r.Context() |
| 125 | + |
| 126 | + log := klog.FromContext(ctx) |
| 127 | + |
| 128 | + // TODO: Download to temp file first? |
| 129 | + |
| 130 | + if err := s.blobCache.PutBlob(ctx, hash, r.Body); err != nil { |
| 131 | + log.Error(err, "error stoing blob") |
| 132 | + http.Error(w, "internal server error", http.StatusInternalServerError) |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + log.Info("uploaded blob", "hash", hash) |
| 137 | + |
| 138 | + w.WriteHeader(http.StatusCreated) |
| 139 | +} |
| 140 | + |
| 141 | +type blobCache struct { |
| 142 | + CacheDir string |
| 143 | + blobStore blobs.BlobStore |
| 144 | +} |
| 145 | + |
| 146 | +func (c *blobCache) GetBlob(ctx context.Context, hash string) (*os.File, error) { |
| 147 | + log := klog.FromContext(ctx) |
| 148 | + |
| 149 | + localPath := filepath.Join(c.CacheDir, hash) |
| 150 | + f, err := os.Open(localPath) |
| 151 | + if err == nil { |
| 152 | + return f, nil |
| 153 | + } else if !os.IsNotExist(err) { |
| 154 | + return nil, fmt.Errorf("opening blob %q: %w", hash, err) |
| 155 | + } |
| 156 | + |
| 157 | + log.Info("blob not found in cache, downloading", "hash", hash) |
| 158 | + |
| 159 | + err = c.blobStore.Download(ctx, blobs.BlobInfo{Hash: hash}, localPath) |
| 160 | + if err == nil { |
| 161 | + f, err := os.Open(localPath) |
| 162 | + if err != nil { |
| 163 | + return nil, fmt.Errorf("opening blob %q after download: %w", hash, err) |
| 164 | + } |
| 165 | + return f, nil |
| 166 | + } |
| 167 | + |
| 168 | + return nil, err |
| 169 | +} |
| 170 | + |
| 171 | +func (c *blobCache) PutBlob(ctx context.Context, hash string, r io.Reader) error { |
| 172 | + log := klog.FromContext(ctx) |
| 173 | + |
| 174 | + if err := c.blobStore.Upload(ctx, r, blobs.BlobInfo{Hash: hash}); err != nil { |
| 175 | + log.Error(err, "error uploading blob") |
| 176 | + return fmt.Errorf("uploading blob %q: %w", hash, err) |
| 177 | + } |
| 178 | + |
| 179 | + // TODO: Side-load into local cache too? |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
0 commit comments