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"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/stats"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
2021-04-09 20:05:15 +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) ([]*filer_pb.FileChunk, hash.Hash, int64, error, []byte) {
|
|
|
|
var fileChunks []*filer_pb.FileChunk
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
md5Hash := md5.New()
|
2021-03-31 04:07:34 +00:00
|
|
|
var partReader = ioutil.NopCloser(io.TeeReader(reader, md5Hash))
|
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
chunkOffset := int64(0)
|
|
|
|
var smallContent []byte
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
for {
|
|
|
|
limitedReader := io.LimitReader(partReader, int64(chunkSize))
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
data, err := ioutil.ReadAll(limitedReader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, 0, err, nil
|
2021-03-31 04:07:34 +00:00
|
|
|
}
|
2021-04-09 20:05:15 +00:00
|
|
|
if chunkOffset == 0 && !isAppend(r) {
|
|
|
|
if len(data) < int(fs.option.SaveToFilerLimit) || strings.HasPrefix(r.URL.Path, filer.DirectoryEtcRoot) && len(data) < 4*1024 {
|
|
|
|
smallContent = data
|
|
|
|
chunkOffset += int64(len(data))
|
|
|
|
break
|
2021-04-01 09:21:40 +00:00
|
|
|
}
|
2021-04-09 20:05:15 +00:00
|
|
|
}
|
|
|
|
dataReader := util.NewBytesReader(data)
|
|
|
|
|
|
|
|
// retry to assign a different file id
|
|
|
|
var fileId, urlLocation string
|
|
|
|
var auth security.EncodedJwt
|
|
|
|
var assignErr, uploadErr error
|
|
|
|
var uploadResult *operation.UploadResult
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
// assign one file id for one chunk
|
|
|
|
fileId, urlLocation, auth, assignErr = fs.assignNewFileInfo(so)
|
|
|
|
if assignErr != nil {
|
|
|
|
return nil, nil, 0, assignErr, nil
|
2021-03-31 04:07:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
// upload the chunk to the volume server
|
|
|
|
uploadResult, uploadErr, _ = fs.doUpload(urlLocation, w, r, dataReader, fileName, contentType, nil, auth)
|
2021-03-31 04:07:34 +00:00
|
|
|
if uploadErr != nil {
|
2021-04-09 20:05:15 +00:00
|
|
|
time.Sleep(251 * time.Millisecond)
|
|
|
|
continue
|
2021-04-01 09:21:40 +00:00
|
|
|
}
|
2021-04-09 20:05:15 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if uploadErr != nil {
|
|
|
|
return nil, nil, 0, uploadErr, nil
|
2021-04-05 01:37:56 +00:00
|
|
|
}
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
// if last chunk exhausted the reader exactly at the border
|
|
|
|
if uploadResult.Size == 0 {
|
|
|
|
break
|
|
|
|
}
|
2021-05-06 10:03:00 +00:00
|
|
|
if chunkOffset == 0 {
|
|
|
|
uploadedMd5 := util.Base64Md5ToBytes(uploadResult.ContentMd5)
|
|
|
|
readedMd5 := md5Hash.Sum(nil)
|
|
|
|
if !bytes.Equal(uploadedMd5, readedMd5) {
|
|
|
|
glog.Errorf("md5 %x does not match %x uploaded chunk %s to the volume server", readedMd5, uploadedMd5, uploadResult.Name)
|
|
|
|
}
|
2021-04-28 17:28:05 +00:00
|
|
|
}
|
2021-04-06 06:24:26 +00:00
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
// Save to chunk manifest structure
|
|
|
|
fileChunks = append(fileChunks, uploadResult.ToPbFileChunk(fileId, chunkOffset))
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
glog.V(4).Infof("uploaded %s chunk %d to %s [%d,%d)", fileName, len(fileChunks), fileId, chunkOffset, chunkOffset+int64(uploadResult.Size))
|
2021-03-31 04:07:34 +00:00
|
|
|
|
2021-04-09 20:05:15 +00:00
|
|
|
// reset variables for the next chunk
|
|
|
|
chunkOffset = chunkOffset + int64(uploadResult.Size)
|
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
|
|
|
|
if int64(uploadResult.Size) < int64(chunkSize) {
|
|
|
|
break
|
2021-04-01 09:21:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-09 20:05:15 +00:00
|
|
|
|
|
|
|
return fileChunks, md5Hash, chunkOffset, nil, smallContent
|
2021-03-31 04:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FilerServer) doUpload(urlLocation string, w http.ResponseWriter, r *http.Request, limitedReader io.Reader, fileName string, contentType string, pairMap map[string]string, auth security.EncodedJwt) (*operation.UploadResult, error, []byte) {
|
|
|
|
|
|
|
|
stats.FilerRequestCounter.WithLabelValues("chunkUpload").Inc()
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
stats.FilerRequestHistogram.WithLabelValues("chunkUpload").Observe(time.Since(start).Seconds())
|
|
|
|
}()
|
|
|
|
|
|
|
|
uploadResult, err, data := operation.Upload(urlLocation, fileName, fs.option.Cipher, limitedReader, false, contentType, pairMap, auth)
|
|
|
|
if uploadResult != nil && uploadResult.RetryCount > 0 {
|
|
|
|
stats.FilerRequestCounter.WithLabelValues("chunkUploadRetry").Add(float64(uploadResult.RetryCount))
|
|
|
|
}
|
|
|
|
return uploadResult, err, data
|
|
|
|
}
|