package objectstoreplugin
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"slices"
"go.ate.dev/substrate/api/objectstore/v1"
"go.ate.dev/substrate/internal/resources"
"go.ate.dev/substrate/pkg/objectstorage"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const manifestFile = "manifest.json"
type NodePlugin struct {
objectstore.UnimplementedNodeProviderServer
creds Credentials
uploader *objectstorage.Uploader
root string
}
func NewNodePlugin(client objectstorage.ObjectStorage, root string, uploadMemory int64, opts ...NodeOption) (*NodePlugin, error) {
if !filepath.IsAbs(root) {
return nil, fmt.Errorf("root %q is not an absolute path", root)
}
uploader, err := objectstorage.NewUploader(client, uploadMemory)
if err != nil {
return nil, err
}
p := &NodePlugin{creds: Ambient(client), uploader: uploader, root: filepath.Clean(root)}
for _, opt := range opts {
opt(p)
}
return p, nil
}
type NodeOption func(*NodePlugin)
func WithCredentials(creds Credentials) NodeOption {
return func(p *NodePlugin) { p.creds = creds }
}
func (p *NodePlugin) FetchSnapshot(ctx context.Context, req *objectstore.FetchSnapshotRequest) (*objectstore.FetchSnapshotResponse, error) {
uri, dstDir, err := p.validate(req.GetSnapshotUri(), req.GetWritePath(), req.GetFiles())
if err != nil {
return nil, err
}
root, err := p.openDir(dstDir)
if err != nil {
return nil, toStatus(fmt.Errorf("while opening restore directory: %w", err))
}
defer root.Close()
tr := p.begin(req.GetActorJwt())
g, gCtx := errgroup.WithContext(ctx)
for _, fileName := range req.GetFiles() {
fileName := fileName
g.Go(func() error {
if fileName == manifestFile {
return p.fetchManifest(gCtx, tr, uri, root)
}
objectURI, err := uri.ObjectURI(fileName + ".zstd")
if err != nil {
return fmt.Errorf("while addressing %s in GCS: %w", fileName, err)
}
if dir := filepath.Dir(fileName); dir != "." {
if err := root.MkdirAll(dir, 0o700); err != nil {
return fmt.Errorf("while creating %s in restore directory: %w", dir, err)
}
}
local, err := root.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return fmt.Errorf("while opening %s in restore directory: %w", fileName, err)
}
fetchErr := tr.do(gCtx, func(client objectstorage.ObjectStorage, _ *objectstorage.Uploader) error {
if err := truncate(local); err != nil {
return err
}
return objectstorage.FetchFileFromGCSWithZstd(gCtx, client, objectURI, local)
})
closeErr := local.Close()
if err := errors.Join(fetchErr, closeErr); err != nil {
return fmt.Errorf("while downloading %s from GCS: %w", fileName, err)
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, toStatus(err)
}
return &objectstore.FetchSnapshotResponse{}, nil
}
func (p *NodePlugin) UploadSnapshot(ctx context.Context, req *objectstore.UploadSnapshotRequest) (*objectstore.UploadSnapshotResponse, error) {
uri, srcDir, err := p.validate(req.GetSnapshotUri(), req.GetLocalPath(), req.GetFiles())
if err != nil {
return nil, err
}
root, err := p.openDir(srcDir)
if err != nil {
return nil, toStatus(fmt.Errorf("while opening snapshot directory: %w", err))
}
defer root.Close()
tr := p.begin(req.GetActorJwt())
upload := func(ctx context.Context, fileName string) error {
local, err := root.Open(fileName)
if err != nil {
return fmt.Errorf("while opening %s in snapshot directory: %w", fileName, err)
}
defer local.Close()
info, err := local.Stat()
if err != nil {
return fmt.Errorf("while inspecting %s in snapshot directory: %w", fileName, err)
}
if !info.Mode().IsRegular() {
return fmt.Errorf("snapshot file %s is not a regular file", fileName)
}
if fileName == manifestFile {
return p.uploadManifest(ctx, tr, uri, local)
}
objectURI, err := uri.ObjectURI(fileName + ".zstd")
if err != nil {
return fmt.Errorf("while addressing %s in GCS: %w", fileName, err)
}
err = tr.do(ctx, func(_ objectstorage.ObjectStorage, uploader *objectstorage.Uploader) error {
if err := rewind(local); err != nil {
return err
}
return uploader.SendFile(ctx, objectURI, local)
})
if err != nil {
return fmt.Errorf("while uploading %s to GCS: %w", fileName, err)
}
return nil
}
g, gCtx := errgroup.WithContext(ctx)
for _, fileName := range req.GetFiles() {
if fileName != manifestFile {
g.Go(func() error { return upload(gCtx, fileName) })
}
}
if err := g.Wait(); err != nil {
return nil, toStatus(err)
}
if slices.Contains(req.GetFiles(), manifestFile) {
if err := upload(ctx, manifestFile); err != nil {
return nil, toStatus(err)
}
}
return &objectstore.UploadSnapshotResponse{}, nil
}
func (p *NodePlugin) fetchManifest(ctx context.Context, tr *transfer, uri resources.SnapshotURI, root *os.Root) error {
manifestURI, err := uri.ObjectURI(manifestFile)
if err != nil {
return err
}
var manifest []byte
err = tr.do(ctx, func(client objectstorage.ObjectStorage, _ *objectstorage.Uploader) (err error) {
manifest, err = objectstorage.FetchFromGCS(ctx, client, manifestURI)
return err
})
if err != nil {
return fmt.Errorf("while fetching snapshot manifest: %w", err)
}
if err := root.WriteFile(manifestFile, manifest, 0o600); err != nil {
return fmt.Errorf("while writing %s in restore directory: %w", manifestFile, err)
}
return nil
}
func (p *NodePlugin) uploadManifest(ctx context.Context, tr *transfer, uri resources.SnapshotURI, local *os.File) error {
manifestURI, err := uri.ObjectURI(manifestFile)
if err != nil {
return fmt.Errorf("while addressing snapshot manifest in GCS: %w", err)
}
err = tr.do(ctx, func(_ objectstorage.ObjectStorage, uploader *objectstorage.Uploader) error {
if err := rewind(local); err != nil {
return err
}
return uploader.Send(ctx, manifestURI, local)
})
if err != nil {
return fmt.Errorf("while uploading snapshot manifest: %w", err)
}
return nil
}
func rewind(f *os.File) error {
_, err := f.Seek(0, io.SeekStart)
return err
}
func truncate(f *os.File) error {
if err := f.Truncate(0); err != nil {
return err
}
return rewind(f)
}
func (p *NodePlugin) validate(snapshotURI, dir string, files []string) (resources.SnapshotURI, string, error) {
uri, err := resources.ParseSnapshotURI(snapshotURI)
if err != nil {
return resources.SnapshotURI{}, "", status.Error(codes.InvalidArgument, err.Error())
}
if !filepath.IsAbs(dir) {
return resources.SnapshotURI{}, "", status.Errorf(codes.InvalidArgument, "local path %q is not absolute", dir)
}
rel, err := filepath.Rel(p.root, dir)
if err != nil || !filepath.IsLocal(rel) {
return resources.SnapshotURI{}, "", status.Errorf(codes.InvalidArgument, "local path %q is outside %q", dir, p.root)
}
if len(files) == 0 {
return resources.SnapshotURI{}, "", status.Error(codes.InvalidArgument, "no files requested")
}
if err := resources.ValidateSnapshotFileNames(files); err != nil {
return resources.SnapshotURI{}, "", status.Error(codes.InvalidArgument, err.Error())
}
return uri, rel, nil
}
func (p *NodePlugin) openDir(rel string) (*os.Root, error) {
base, err := os.OpenRoot(p.root)
if err != nil {
return nil, err
}
defer base.Close()
return base.OpenRoot(rel)
}