about summary refs log tree commit diff
path: root/tvix/nar-bridge/pkg/http/narinfo_put.go
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-10-11T10·28+0200
committerflokli <flokli@flokli.de>2023-10-11T11·41+0000
commit98c17147c64f9898b656f56ab139d4d52248743e (patch)
treea7fefb1eb20353c78375282387ad32ebb36dfbc5 /tvix/nar-bridge/pkg/http/narinfo_put.go
parentceb1674e9f66bcbf382130bb195d367d269ddf86 (diff)
refactor(tvix/nar-bridge): have Export return root node r/6780
… and nar size / sha256 digest.

Instead of producing sparse PathInfo messages when NARs are sent to
nar-bridge, the nar-bridge http server now keeps a lookup table
(narsha256) -> (rootNode, narSize)

This removes a whole bunch of noise, because we don't need to keep
sparse fields around.

A convenience function
`GenPathInfo(rootNode *castorev1pb.Node, narInfo *narinfo.NarInfo)` is
added, which is used to produce PathInfo messages, either when receiving
a NAR file over http and uploading it to a remote PathInfoService, or to
synthesize the PathInfoMessage to return to the client, if nar-bridge is
acting as a PathInfoService for a remove Nix HTTP Binary cache.

Change-Id: Ibba1ab6238a050816c4fab29cb21ae88877d8613
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9651
Tested-by: BuildkiteCI
Reviewed-by: Brian McGee <brian@bmcgee.ie>
Diffstat (limited to 'tvix/nar-bridge/pkg/http/narinfo_put.go')
-rw-r--r--tvix/nar-bridge/pkg/http/narinfo_put.go110
1 files changed, 19 insertions, 91 deletions
diff --git a/tvix/nar-bridge/pkg/http/narinfo_put.go b/tvix/nar-bridge/pkg/http/narinfo_put.go
index 6494bca6f20d..fd588bec8644 100644
--- a/tvix/nar-bridge/pkg/http/narinfo_put.go
+++ b/tvix/nar-bridge/pkg/http/narinfo_put.go
@@ -2,14 +2,11 @@ package http
 
 import (
 	"net/http"
-	"path"
 
-	castorev1pb "code.tvl.fyi/tvix/castore/protos"
-	storev1pb "code.tvl.fyi/tvix/store/protos"
+	"code.tvl.fyi/tvix/nar-bridge/pkg/importer"
 	"github.com/go-chi/chi/v5"
 	"github.com/nix-community/go-nix/pkg/narinfo"
 	"github.com/nix-community/go-nix/pkg/nixbase32"
-	"github.com/nix-community/go-nix/pkg/storepath"
 	"github.com/sirupsen/logrus"
 	log "github.com/sirupsen/logrus"
 )
@@ -43,12 +40,10 @@ func registerNarinfoPut(s *Server) {
 			"output_path": narInfo.StorePath,
 		})
 
-		var pathInfo *storev1pb.PathInfo
-
 		// look up the narHash in our temporary map
-		s.narHashToPathInfoMu.Lock()
-		pathInfo, found := s.narHashToPathInfo[narInfo.NarHash.SRIString()]
-		s.narHashToPathInfoMu.Unlock()
+		s.narDbMu.Lock()
+		narData, found := s.narDb[narInfo.NarHash.SRIString()]
+		s.narDbMu.Unlock()
 		if !found {
 			log.Error("unable to find referred NAR")
 			w.WriteHeader(http.StatusBadRequest)
@@ -60,10 +55,12 @@ func registerNarinfoPut(s *Server) {
 			return
 		}
 
+		rootNode := narData.rootNode
+
 		// compare fields with what we computed while receiving the NAR file
 
 		// NarSize needs to match
-		if pathInfo.Narinfo.NarSize != narInfo.NarSize {
+		if narData.narSize != narInfo.NarSize {
 			log.Error("narsize mismatch")
 			w.WriteHeader(http.StatusBadRequest)
 			_, err := w.Write([]byte("unable to parse narinfo"))
@@ -73,90 +70,23 @@ func registerNarinfoPut(s *Server) {
 
 			return
 		}
-		// We know the narhash in the .narinfo matches one of the two narhashes in the partial pathInfo,
-		// because that's how we found it.
-
-		// FUTUREWORK: We can't compare References yet, but it'd be a good idea to
-		// do reference checking on .nar files server-side during upload.
-		// We however still need to be parse them, because we store
-		// the bytes in pathInfo.References, and the full strings in pathInfo.Narinfo.ReferenceNames.
-		referencesBytes := make([][]byte, 0)
-		for _, reference := range narInfo.References {
-			storePath, err := storepath.FromString(reference)
-			if err != nil {
-				log.WithField("reference", reference).WithError(err).Error("unable to parse reference")
-				w.WriteHeader(http.StatusBadRequest)
-				_, err := w.Write([]byte("unable to parse reference"))
-				if err != nil {
-					log.WithError(err).Errorf("unable to write error message to client")
-				}
-
-				return
-			}
-			referencesBytes = append(referencesBytes, storePath.Digest)
-		}
 
-		// assemble the []*storev1pb.NARInfo_Signature{} from narinfo.Signatures.
-		pbNarinfoSignatures := make([]*storev1pb.NARInfo_Signature, 0)
-		for _, narinfoSig := range narInfo.Signatures {
+		pathInfo, err := importer.GenPathInfo(rootNode, narInfo)
+		if err != nil {
+			log.WithError(err).Error("unable to generate PathInfo")
 
-			pbNarinfoSignatures = append(pbNarinfoSignatures, &storev1pb.NARInfo_Signature{
-				Name: narinfoSig.Name,
-				Data: narinfoSig.Data,
-			})
-		}
+			w.WriteHeader(http.StatusInternalServerError)
+			_, err := w.Write([]byte("unable to generate PathInfo"))
+			if err != nil {
+				log.WithError(err).Errorf("unable to write error message to client")
+			}
 
-		// If everything matches, We will add References, NAR signatures and the
-		// output path name, and then upload to the pathinfo service.
-		// We want a copy here, because we don't want to mutate the contents in the lookup table
-		// until we get things back from the remote store.
-		pathInfoToUpload := &storev1pb.PathInfo{
-			Node:       nil, // set below
-			References: referencesBytes,
-			Narinfo: &storev1pb.NARInfo{
-				NarSize:        pathInfo.Narinfo.NarSize,
-				NarSha256:      pathInfo.Narinfo.NarSha256,
-				Signatures:     pbNarinfoSignatures,
-				ReferenceNames: narInfo.References,
-			},
+			return
 		}
 
-		// We need to add the basename of the storepath from the .narinfo
-		// to the pathInfo to be sent.
-		switch v := (pathInfo.GetNode().GetNode()).(type) {
-		case *castorev1pb.Node_File:
-			pathInfoToUpload.Node = &castorev1pb.Node{
-				Node: &castorev1pb.Node_File{
-					File: &castorev1pb.FileNode{
-						Name:       []byte(path.Base(narInfo.StorePath)),
-						Digest:     v.File.Digest,
-						Size:       v.File.Size,
-						Executable: v.File.Executable,
-					},
-				},
-			}
-		case *castorev1pb.Node_Symlink:
-			pathInfoToUpload.Node = &castorev1pb.Node{
-				Node: &castorev1pb.Node_Symlink{
-					Symlink: &castorev1pb.SymlinkNode{
-						Name:   []byte(path.Base(narInfo.StorePath)),
-						Target: v.Symlink.Target,
-					},
-				},
-			}
-		case *castorev1pb.Node_Directory:
-			pathInfoToUpload.Node = &castorev1pb.Node{
-				Node: &castorev1pb.Node_Directory{
-					Directory: &castorev1pb.DirectoryNode{
-						Name:   []byte(path.Base(narInfo.StorePath)),
-						Digest: v.Directory.Digest,
-						Size:   v.Directory.Size,
-					},
-				},
-			}
-		}
+		log.WithField("pathInfo", pathInfo).Debug("inserted new pathInfo")
 
-		receivedPathInfo, err := s.pathInfoServiceClient.Put(ctx, pathInfoToUpload)
+		receivedPathInfo, err := s.pathInfoServiceClient.Put(ctx, pathInfo)
 		if err != nil {
 			log.WithError(err).Error("unable to upload pathinfo to service")
 			w.WriteHeader(http.StatusInternalServerError)
@@ -168,8 +98,6 @@ func registerNarinfoPut(s *Server) {
 			return
 		}
 
-		log.Debugf("received new pathInfo: %v+", receivedPathInfo)
-
-		// TODO: update the local temporary pathinfo with this?
+		log.WithField("pathInfo", receivedPathInfo).Debug("got back PathInfo")
 	})
 }