2021-03-31 04:07:34 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2021-04-28 17:28:05 +00:00
|
|
|
"bytes"
|
2021-03-31 04:07:34 +00:00
|
|
|
"crypto/md5"
|
2022-02-21 16:15:00 +00:00
|
|
|
"fmt"
|
2022-04-18 02:35:43 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2021-03-31 04:07:34 +00:00
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2022-02-21 16:15:00 +00:00
|
|
|
"strconv"
|
2021-06-06 20:42:36 +00:00
|
|
|
"sync"
|
2021-06-07 06:05:17 +00:00
|
|
|
"sync/atomic"
|
2021-03-31 04:07:34 +00:00
|
|
|
"time"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2021-03-31 04:07:34 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 20:42:36 +00:00
|
|
|
var bufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-15 18:56:28 +00:00
|
|
|
func (fs *FilerServer) uploadReaderToChunks(w http.ResponseWriter, r *http.Request, reader io.Reader, chunkSize int32, fileName, contentType string, contentLength int64, so *operation.StorageOption) (fileChunks []*filer_pb.FileChunk, md5Hash hash.Hash, chunkOffset int64, uploadErr error, smallContent []byte) {
|
2022-02-21 16:15:00 +00:00
|
|
|
query := r.URL.Query()
|
|
|
|
|
2022-03-07 08:04:59 +00:00
|
|
|
isAppend := isAppend(r)
|
2022-02-21 16:15:00 +00:00
|
|
|
if query.Has("offset") {
|
|
|
|
offset := query.Get("offset")
|
|
|
|
offsetInt, err := strconv.ParseInt(offset, 10, 64)
|
|
|
|
if err != nil || offsetInt < 0 {
|
|
|
|
err = fmt.Errorf("invalid 'offset': '%s'", offset)
|
|
|
|
return nil, nil, 0, err, nil
|
|
|
|
}
|
2022-03-07 08:04:59 +00:00
|
|
|
if isAppend && offsetInt > 0 {
|
2022-02-21 16:15:00 +00:00
|
|
|
err = fmt.Errorf("cannot set offset when op=append")
|
|
|
|
return nil, nil, 0, err, nil
|
|
|
|
}
|
|
|
|
chunkOffset = offsetInt
|
|
|
|
}
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-07-15 18:56:28 +00:00
|
|
|
md5Hash = md5.New()
|
2021-10-14 04:27:58 +00:00
|
|
|
var partReader = io.NopCloser(io.TeeReader(reader, md5Hash))
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-06-07 03:23:36 +00:00
|
|
|
var wg sync.WaitGroup
|
2021-06-07 06:05:17 +00:00
|
|
|
var bytesBufferCounter int64
|
|
|
|
bytesBufferLimitCond := sync.NewCond(new(sync.Mutex))
|
2021-06-07 19:04:50 +00:00
|
|
|
var fileChunksLock sync.Mutex
|
2021-04-09 20:05:15 +00:00
|
|
|
for {
|
2021-06-07 03:23:36 +00:00
|
|
|
|
2021-06-07 06:05:17 +00:00
|
|
|
// need to throttle used byte buffer
|
|
|
|
bytesBufferLimitCond.L.Lock()
|
|
|
|
for atomic.LoadInt64(&bytesBufferCounter) >= 4 {
|
2022-08-27 07:19:13 +00:00
|
|
|
glog.V(4).Infof("waiting for byte buffer %d", atomic.LoadInt64(&bytesBufferCounter))
|
2021-06-07 06:05:17 +00:00
|
|
|
bytesBufferLimitCond.Wait()
|
|
|
|
}
|
|
|
|
atomic.AddInt64(&bytesBufferCounter, 1)
|
|
|
|
bytesBufferLimitCond.L.Unlock()
|
|
|
|
|
2021-06-07 03:23:36 +00:00
|
|
|
bytesBuffer := bufPool.Get().(*bytes.Buffer)
|
2022-08-27 07:19:13 +00:00
|
|
|
glog.V(4).Infof("received byte buffer %d", atomic.LoadInt64(&bytesBufferCounter))
|
2021-06-07 03:23:36 +00:00
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
limitedReader := io.LimitReader(partReader, int64(chunkSize))
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-06-06 20:42:36 +00:00
|
|
|
bytesBuffer.Reset()
|
|
|
|
|
|
|
|
dataSize, err := bytesBuffer.ReadFrom(limitedReader)
|
|
|
|
|
2021-10-14 04:27:58 +00:00
|
|
|
// data, err := io.ReadAll(limitedReader)
|
2021-06-07 03:23:36 +00:00
|
|
|
if err != nil || dataSize == 0 {
|
2021-06-07 06:05:17 +00:00
|
|
|
bufPool.Put(bytesBuffer)
|
|
|
|
atomic.AddInt64(&bytesBufferCounter, -1)
|
|
|
|
bytesBufferLimitCond.Signal()
|
2022-03-10 03:40:39 +00:00
|
|
|
uploadErr = err
|
2021-07-15 18:56:28 +00:00
|
|
|
break
|
2021-03-31 04:07:34 +00:00
|
|
|
}
|
2022-03-07 08:04:59 +00:00
|
|
|
if chunkOffset == 0 && !isAppend {
|
2022-08-22 00:02:16 +00:00
|
|
|
if dataSize < fs.option.SaveToFilerLimit {
|
2021-06-06 20:42:36 +00:00
|
|
|
chunkOffset += dataSize
|
|
|
|
smallContent = make([]byte, dataSize)
|
2021-06-07 03:57:03 +00:00
|
|
|
bytesBuffer.Read(smallContent)
|
2021-06-07 06:05:17 +00:00
|
|
|
bufPool.Put(bytesBuffer)
|
|
|
|
atomic.AddInt64(&bytesBufferCounter, -1)
|
|
|
|
bytesBufferLimitCond.Signal()
|
2022-08-04 08:44:54 +00:00
|
|
|
stats.FilerRequestCounter.WithLabelValues(stats.ContentSaveToFiler).Inc()
|
2021-04-09 20:05:15 +00:00
|
|
|
break
|
2021-04-01 09:21:40 +00:00
|
|
|
}
|
2022-08-04 08:44:54 +00:00
|
|
|
} else {
|
|
|
|
stats.FilerRequestCounter.WithLabelValues(stats.AutoChunk).Inc()
|
2021-04-09 20:05:15 +00:00
|
|
|
}
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-06-07 03:23:36 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(offset int64) {
|
2021-06-07 06:05:17 +00:00
|
|
|
defer func() {
|
|
|
|
bufPool.Put(bytesBuffer)
|
|
|
|
atomic.AddInt64(&bytesBufferCounter, -1)
|
|
|
|
bytesBufferLimitCond.Signal()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-06-07 05:16:32 +00:00
|
|
|
chunk, toChunkErr := fs.dataToChunk(fileName, contentType, bytesBuffer.Bytes(), offset, so)
|
2022-08-27 07:20:58 +00:00
|
|
|
if uploadErr == nil && toChunkErr != nil {
|
2021-06-07 03:23:36 +00:00
|
|
|
uploadErr = toChunkErr
|
|
|
|
}
|
|
|
|
if chunk != nil {
|
2021-06-07 19:04:50 +00:00
|
|
|
fileChunksLock.Lock()
|
2021-06-07 03:23:36 +00:00
|
|
|
fileChunks = append(fileChunks, chunk)
|
2021-06-07 19:04:50 +00:00
|
|
|
fileChunksLock.Unlock()
|
2021-06-07 03:23:36 +00:00
|
|
|
glog.V(4).Infof("uploaded %s chunk %d to %s [%d,%d)", fileName, len(fileChunks), chunk.FileId, offset, offset+int64(chunk.Size))
|
|
|
|
}
|
|
|
|
}(chunkOffset)
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
// reset variables for the next chunk
|
2021-06-07 03:23:36 +00:00
|
|
|
chunkOffset = chunkOffset + dataSize
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
// if last chunk was not at full chunk size, but already exhausted the reader
|
2021-06-07 03:23:36 +00:00
|
|
|
if dataSize < int64(chunkSize) {
|
2021-04-09 20:05:15 +00:00
|
|
|
break
|
2021-04-01 09:21:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-09 20:05:15 +00:00
|
|
|
|
2021-06-07 03:23:36 +00:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if uploadErr != nil {
|
2022-03-10 03:40:39 +00:00
|
|
|
fs.filer.DeleteChunks(fileChunks)
|
2021-06-07 03:23:36 +00:00
|
|
|
return nil, md5Hash, 0, uploadErr, nil
|
|
|
|
}
|
2022-04-18 02:35:43 +00:00
|
|
|
slices.SortFunc(fileChunks, func(a, b *filer_pb.FileChunk) bool {
|
|
|
|
return a.Offset < b.Offset
|
2021-06-07 03:23:36 +00:00
|
|
|
})
|
2021-04-09 20:05:15 +00:00
|
|
|
return fileChunks, md5Hash, chunkOffset, nil, smallContent
|
2021-03-31 04:07:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 01:43:04 +00:00
|
|
|
func (fs *FilerServer) doUpload(urlLocation string, limitedReader io.Reader, fileName string, contentType string, pairMap map[string]string, auth security.EncodedJwt) (*operation.UploadResult, error, []byte) {
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2022-08-04 08:44:54 +00:00
|
|
|
stats.FilerRequestCounter.WithLabelValues(stats.ChunkUpload).Inc()
|
2021-03-31 04:07:34 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
2022-08-04 08:44:54 +00:00
|
|
|
stats.FilerRequestHistogram.WithLabelValues(stats.ChunkUpload).Observe(time.Since(start).Seconds())
|
2021-03-31 04:07:34 +00:00
|
|
|
}()
|
|
|
|
|
2021-09-06 23:20:49 +00:00
|
|
|
uploadOption := &operation.UploadOption{
|
|
|
|
UploadUrl: urlLocation,
|
|
|
|
Filename: fileName,
|
|
|
|
Cipher: fs.option.Cipher,
|
|
|
|
IsInputCompressed: false,
|
|
|
|
MimeType: contentType,
|
|
|
|
PairMap: pairMap,
|
|
|
|
Jwt: auth,
|
|
|
|
}
|
|
|
|
uploadResult, err, data := operation.Upload(limitedReader, uploadOption)
|
2021-03-31 04:07:34 +00:00
|
|
|
if uploadResult != nil && uploadResult.RetryCount > 0 {
|
2022-08-04 08:44:54 +00:00
|
|
|
stats.FilerRequestCounter.WithLabelValues(stats.ChunkUploadRetry).Add(float64(uploadResult.RetryCount))
|
2021-03-31 04:07:34 +00:00
|
|
|
}
|
|
|
|
return uploadResult, err, data
|
|
|
|
}
|
2021-06-07 01:43:04 +00:00
|
|
|
|
2021-06-07 05:16:32 +00:00
|
|
|
func (fs *FilerServer) dataToChunk(fileName, contentType string, data []byte, chunkOffset int64, so *operation.StorageOption) (*filer_pb.FileChunk, error) {
|
2021-06-07 01:43:04 +00:00
|
|
|
dataReader := util.NewBytesReader(data)
|
|
|
|
|
|
|
|
// retry to assign a different file id
|
|
|
|
var fileId, urlLocation string
|
|
|
|
var auth security.EncodedJwt
|
|
|
|
var uploadErr error
|
|
|
|
var uploadResult *operation.UploadResult
|
2022-08-21 05:03:27 +00:00
|
|
|
|
|
|
|
err := util.Retry("filerDataToChunk", func() error {
|
2021-06-07 01:43:04 +00:00
|
|
|
// assign one file id for one chunk
|
|
|
|
fileId, urlLocation, auth, uploadErr = fs.assignNewFileInfo(so)
|
|
|
|
if uploadErr != nil {
|
|
|
|
glog.V(4).Infof("retry later due to assign error: %v", uploadErr)
|
2022-08-04 08:44:54 +00:00
|
|
|
stats.FilerRequestCounter.WithLabelValues(stats.ChunkAssignRetry).Inc()
|
2022-08-21 05:03:27 +00:00
|
|
|
return uploadErr
|
2021-06-07 01:43:04 +00:00
|
|
|
}
|
|
|
|
// upload the chunk to the volume server
|
|
|
|
uploadResult, uploadErr, _ = fs.doUpload(urlLocation, dataReader, fileName, contentType, nil, auth)
|
|
|
|
if uploadErr != nil {
|
|
|
|
glog.V(4).Infof("retry later due to upload error: %v", uploadErr)
|
2022-08-04 08:44:54 +00:00
|
|
|
stats.FilerRequestCounter.WithLabelValues(stats.ChunkDoUploadRetry).Inc()
|
2022-08-21 05:03:27 +00:00
|
|
|
return uploadErr
|
2021-06-07 01:43:04 +00:00
|
|
|
}
|
2022-08-21 05:03:27 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("upload error: %v", err)
|
|
|
|
return nil, err
|
2021-06-07 01:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if last chunk exhausted the reader exactly at the border
|
|
|
|
if uploadResult.Size == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return uploadResult.ToPbFileChunk(fileId, chunkOffset), nil
|
|
|
|
}
|