2024-02-01 02:27:38 +00:00
|
|
|
package renter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-02-01 07:03:04 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/cron"
|
2024-02-01 02:27:38 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-02-01 07:03:04 +00:00
|
|
|
rhpv2 "go.sia.tech/core/rhp/v2"
|
2024-02-01 02:27:38 +00:00
|
|
|
"go.sia.tech/renterd/api"
|
|
|
|
busClient "go.sia.tech/renterd/bus/client"
|
2024-02-01 07:03:04 +00:00
|
|
|
"go.sia.tech/renterd/object"
|
2024-02-01 02:27:38 +00:00
|
|
|
workerClient "go.sia.tech/renterd/worker/client"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"io"
|
2024-02-01 07:03:04 +00:00
|
|
|
"math"
|
2024-02-01 02:27:38 +00:00
|
|
|
"net/url"
|
2024-02-01 07:03:04 +00:00
|
|
|
"strconv"
|
2024-02-01 02:27:38 +00:00
|
|
|
)
|
|
|
|
|
2024-02-01 07:03:04 +00:00
|
|
|
type ReaderFactory func(start uint, end uint) (io.ReadCloser, error)
|
|
|
|
type UploadIDHandler func(uploadID string)
|
|
|
|
|
2024-02-01 02:27:38 +00:00
|
|
|
type RenterServiceParams struct {
|
|
|
|
fx.In
|
|
|
|
Config *viper.Viper
|
|
|
|
Logger *zap.Logger
|
2024-02-01 07:03:04 +00:00
|
|
|
Cron *cron.CronServiceDefault
|
2024-02-01 02:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RenterDefault struct {
|
|
|
|
busClient *busClient.Client
|
|
|
|
workerClient *workerClient.Client
|
|
|
|
config *viper.Viper
|
|
|
|
logger *zap.Logger
|
2024-02-01 07:03:04 +00:00
|
|
|
cron *cron.CronServiceDefault
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiPartUploadParams struct {
|
|
|
|
ReaderFactory ReaderFactory
|
|
|
|
Bucket string
|
|
|
|
FileName string
|
|
|
|
Size uint64
|
|
|
|
ExistingUploadID string
|
|
|
|
UploadIDHandler UploadIDHandler
|
2024-02-01 02:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var Module = fx.Module("renter",
|
|
|
|
fx.Options(
|
|
|
|
fx.Provide(NewRenterService),
|
|
|
|
fx.Invoke(func(r *RenterDefault) error {
|
|
|
|
return r.init()
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewRenterService(params RenterServiceParams) *RenterDefault {
|
|
|
|
return &RenterDefault{
|
|
|
|
config: params.Config,
|
|
|
|
logger: params.Logger,
|
2024-02-01 23:33:17 +00:00
|
|
|
cron: params.Cron,
|
2024-02-01 02:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RenterDefault) CreateBucketIfNotExists(bucket string) error {
|
|
|
|
_, err := r.busClient.Bucket(context.Background(), bucket)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, api.ErrBucketNotFound) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.busClient.CreateBucket(context.Background(), bucket, api.CreateBucketOptions{
|
|
|
|
Policy: api.BucketPolicy{
|
|
|
|
PublicReadAccess: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RenterDefault) UploadObject(ctx context.Context, file io.Reader, bucket string, hash string) error {
|
|
|
|
_, err := r.workerClient.UploadObject(ctx, file, bucket, hash, api.UploadObjectOptions{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RenterDefault) init() error {
|
|
|
|
addr := r.config.GetString("core.sia.url")
|
|
|
|
passwd := r.config.GetString("core.sia.key")
|
|
|
|
|
|
|
|
addrURL, err := url.Parse(addr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
addrURL.Path = "/api/worker"
|
|
|
|
|
|
|
|
r.workerClient = workerClient.New(addrURL.String(), passwd)
|
|
|
|
|
|
|
|
addrURL.Path = "/api/bus"
|
|
|
|
|
|
|
|
r.busClient = busClient.New(addrURL.String(), passwd)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RenterDefault) GetObject(ctx context.Context, protocol string, hash string, options api.DownloadObjectOptions) (*api.GetObjectResponse, error) {
|
|
|
|
return r.workerClient.GetObject(ctx, protocol, hash, options)
|
|
|
|
}
|
2024-02-01 07:03:04 +00:00
|
|
|
|
|
|
|
func (r *RenterDefault) GetSetting(ctx context.Context, setting string, out any) error {
|
|
|
|
err := r.busClient.Setting(ctx, setting, out)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RenterDefault) MultipartUpload(params MultiPartUploadParams) error {
|
|
|
|
size := params.Size
|
|
|
|
rf := params.ReaderFactory
|
|
|
|
bucket := params.Bucket
|
|
|
|
fileName := params.FileName
|
|
|
|
ctx := context.Background()
|
|
|
|
idHandler := params.UploadIDHandler
|
|
|
|
|
|
|
|
var redundancy api.RedundancySettings
|
|
|
|
|
2024-02-01 23:25:32 +00:00
|
|
|
err := r.GetSetting(ctx, "redundancy", &redundancy)
|
2024-02-01 07:03:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
slabSize := uint64(redundancy.MinShards * rhpv2.SectorSize)
|
|
|
|
parts := uint64(math.Ceil(float64(size) / float64(slabSize)))
|
|
|
|
uploadParts := make([]api.MultipartCompletedPart, parts)
|
|
|
|
|
|
|
|
upload, err := r.busClient.CreateMultipartUpload(ctx, bucket, fileName, api.CreateMultipartOptions{Key: object.NoOpKey})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if idHandler != nil {
|
|
|
|
idHandler(upload.UploadID)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint64(0); i < parts; i++ {
|
|
|
|
start := i * slabSize
|
|
|
|
|
|
|
|
end := start + slabSize
|
|
|
|
if end > size {
|
|
|
|
end = size
|
|
|
|
}
|
|
|
|
next := make(chan struct{}, 0)
|
2024-02-02 00:02:24 +00:00
|
|
|
|
2024-02-01 07:03:04 +00:00
|
|
|
job := r.cron.RetryableTask(cron.RetryableTaskParams{
|
|
|
|
Name: fileName + "-part-" + strconv.FormatUint(i, 10),
|
|
|
|
Function: func() error {
|
2024-02-02 00:02:24 +00:00
|
|
|
reader, err := rf(uint(start), uint(end))
|
|
|
|
defer func(reader io.ReadCloser) {
|
|
|
|
err := reader.Close()
|
|
|
|
if err != nil {
|
|
|
|
r.logger.Error("failed to close reader", zap.Error(err))
|
|
|
|
}
|
|
|
|
}(reader)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.workerClient.UploadMultipartUploadPart(context.Background(), reader, bucket, fileName, upload.UploadID, int(i), api.UploadMultipartUploadPartOptions{})
|
2024-02-01 07:03:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
next <- struct{}{}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Limit: 10,
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = r.cron.CreateJob(job)
|
|
|
|
if err != nil {
|
|
|
|
r.logger.Error("failed to create job", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadParts[i] = api.MultipartCompletedPart{
|
|
|
|
PartNumber: int(i),
|
|
|
|
}
|
|
|
|
|
|
|
|
<-next
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.busClient.CompleteMultipartUpload(ctx, bucket, fileName, upload.UploadID, uploadParts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|