2021-09-09 14:25:25 +02:00
|
|
|
//go:build !plan9 && !js
|
2017-11-12 18:54:25 +01:00
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
2017-11-12 18:54:25 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-01-29 23:05:04 +01:00
|
|
|
"path"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2018-10-13 15:41:15 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
2018-01-29 23:05:04 +01:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2017-11-12 18:54:25 +01:00
|
|
|
)
|
|
|
|
|
2018-01-29 23:05:04 +01:00
|
|
|
var uploaderMap = make(map[string]*backgroundWriter)
|
|
|
|
var uploaderMapMx sync.Mutex
|
|
|
|
|
|
|
|
// initBackgroundUploader returns a single instance
|
|
|
|
func initBackgroundUploader(fs *Fs) (*backgroundWriter, error) {
|
|
|
|
// write lock to create one
|
|
|
|
uploaderMapMx.Lock()
|
|
|
|
defer uploaderMapMx.Unlock()
|
|
|
|
if b, ok := uploaderMap[fs.String()]; ok {
|
|
|
|
// if it was already started we close it so that it can be started again
|
|
|
|
if b.running {
|
|
|
|
b.close()
|
|
|
|
} else {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bb := newBackgroundWriter(fs)
|
|
|
|
uploaderMap[fs.String()] = bb
|
|
|
|
return uploaderMap[fs.String()], nil
|
|
|
|
}
|
|
|
|
|
2017-11-12 18:54:25 +01:00
|
|
|
// Handle is managing the read/write/seek operations on an open handle
|
|
|
|
type Handle struct {
|
2019-06-17 10:34:30 +02:00
|
|
|
ctx context.Context
|
2017-12-09 22:54:26 +01:00
|
|
|
cachedObject *Object
|
2018-01-29 23:05:04 +01:00
|
|
|
cfs *Fs
|
|
|
|
memory *Memory
|
2017-12-09 22:54:26 +01:00
|
|
|
preloadQueue chan int64
|
|
|
|
preloadOffset int64
|
|
|
|
offset int64
|
|
|
|
seenOffsets map[int64]bool
|
|
|
|
mu sync.Mutex
|
2018-08-30 12:15:30 +02:00
|
|
|
workersWg sync.WaitGroup
|
2017-12-09 22:54:26 +01:00
|
|
|
confirmReading chan bool
|
2018-08-30 12:15:30 +02:00
|
|
|
workers int
|
|
|
|
maxWorkerID int
|
|
|
|
UseMemory bool
|
|
|
|
closed bool
|
|
|
|
reading bool
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewObjectHandle returns a new Handle for an existing Object
|
2019-06-17 10:34:30 +02:00
|
|
|
func NewObjectHandle(ctx context.Context, o *Object, cfs *Fs) *Handle {
|
2017-11-12 18:54:25 +01:00
|
|
|
r := &Handle{
|
2019-06-17 10:34:30 +02:00
|
|
|
ctx: ctx,
|
2017-11-12 18:54:25 +01:00
|
|
|
cachedObject: o,
|
2018-01-29 23:05:04 +01:00
|
|
|
cfs: cfs,
|
2017-11-12 18:54:25 +01:00
|
|
|
offset: 0,
|
|
|
|
preloadOffset: -1, // -1 to trigger the first preload
|
|
|
|
|
2018-05-14 19:06:57 +02:00
|
|
|
UseMemory: !cfs.opt.ChunkNoMemory,
|
2017-12-09 22:54:26 +01:00
|
|
|
reading: false,
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
r.seenOffsets = make(map[int64]bool)
|
2017-12-09 22:54:26 +01:00
|
|
|
r.memory = NewMemory(-1)
|
2017-11-12 18:54:25 +01:00
|
|
|
|
|
|
|
// create a larger buffer to queue up requests
|
2018-05-14 19:06:57 +02:00
|
|
|
r.preloadQueue = make(chan int64, r.cfs.opt.TotalWorkers*10)
|
2017-12-09 22:54:26 +01:00
|
|
|
r.confirmReading = make(chan bool)
|
2017-11-12 18:54:25 +01:00
|
|
|
r.startReadWorkers()
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// cacheFs is a convenience method to get the parent cache FS of the object's manager
|
|
|
|
func (r *Handle) cacheFs() *Fs {
|
2018-01-29 23:05:04 +01:00
|
|
|
return r.cfs
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// storage is a convenience method to get the persistent storage of the object's manager
|
2018-01-29 23:05:04 +01:00
|
|
|
func (r *Handle) storage() *Persistent {
|
2017-11-12 18:54:25 +01:00
|
|
|
return r.cacheFs().cache
|
|
|
|
}
|
|
|
|
|
|
|
|
// String representation of this reader
|
|
|
|
func (r *Handle) String() string {
|
|
|
|
return r.cachedObject.abs()
|
|
|
|
}
|
|
|
|
|
|
|
|
// startReadWorkers will start the worker pool
|
|
|
|
func (r *Handle) startReadWorkers() {
|
2018-08-30 12:15:30 +02:00
|
|
|
if r.workers > 0 {
|
2017-11-12 18:54:25 +01:00
|
|
|
return
|
|
|
|
}
|
2018-05-14 19:06:57 +02:00
|
|
|
totalWorkers := r.cacheFs().opt.TotalWorkers
|
2017-12-11 23:46:08 +01:00
|
|
|
|
|
|
|
if r.cacheFs().plexConnector.isConfigured() {
|
|
|
|
if !r.cacheFs().plexConnector.isConnected() {
|
|
|
|
err := r.cacheFs().plexConnector.authenticate()
|
|
|
|
if err != nil {
|
2018-01-29 23:05:04 +01:00
|
|
|
fs.Errorf(r, "failed to authenticate to Plex: %v", err)
|
2017-12-11 23:46:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.cacheFs().plexConnector.isConnected() {
|
|
|
|
totalWorkers = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.scaleWorkers(totalWorkers)
|
2017-12-09 22:54:26 +01:00
|
|
|
}
|
|
|
|
|
2024-04-14 14:04:35 +02:00
|
|
|
// scaleWorkers will increase the worker pool count by the provided amount
|
2017-12-09 22:54:26 +01:00
|
|
|
func (r *Handle) scaleWorkers(desired int) {
|
2018-08-30 12:15:30 +02:00
|
|
|
current := r.workers
|
2017-12-09 22:54:26 +01:00
|
|
|
if current == desired {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if current > desired {
|
|
|
|
// scale in gracefully
|
2018-08-30 12:15:30 +02:00
|
|
|
for r.workers > desired {
|
2017-12-09 22:54:26 +01:00
|
|
|
r.preloadQueue <- -1
|
2018-08-30 12:15:30 +02:00
|
|
|
r.workers--
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
2017-12-09 22:54:26 +01:00
|
|
|
} else {
|
|
|
|
// scale out
|
2018-08-30 12:15:30 +02:00
|
|
|
for r.workers < desired {
|
2017-12-09 22:54:26 +01:00
|
|
|
w := &worker{
|
|
|
|
r: r,
|
2018-08-30 12:15:30 +02:00
|
|
|
id: r.maxWorkerID,
|
2017-12-09 22:54:26 +01:00
|
|
|
}
|
2018-08-30 12:15:30 +02:00
|
|
|
r.workersWg.Add(1)
|
|
|
|
r.workers++
|
|
|
|
r.maxWorkerID++
|
2017-12-09 22:54:26 +01:00
|
|
|
go w.run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ignore first scale out from 0
|
|
|
|
if current != 0 {
|
2018-01-29 23:05:04 +01:00
|
|
|
fs.Debugf(r, "scale workers to %v", desired)
|
2017-12-09 22:54:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Handle) confirmExternalReading() {
|
|
|
|
// if we have a max value of workers
|
|
|
|
// then we skip this step
|
2018-08-30 12:15:30 +02:00
|
|
|
if r.workers > 1 ||
|
2018-06-13 14:03:06 +02:00
|
|
|
!r.cacheFs().plexConnector.isConfigured() {
|
2017-12-09 22:54:26 +01:00
|
|
|
return
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
2018-03-22 20:20:34 +01:00
|
|
|
if !r.cacheFs().plexConnector.isPlaying(r.cachedObject) {
|
2017-12-09 22:54:26 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fs.Infof(r, "confirmed reading by external reader")
|
2018-05-14 19:06:57 +02:00
|
|
|
r.scaleWorkers(r.cacheFs().opt.TotalWorkers)
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// queueOffset will send an offset to the workers if it's different from the last one
|
|
|
|
func (r *Handle) queueOffset(offset int64) {
|
|
|
|
if offset != r.preloadOffset {
|
2017-12-09 22:54:26 +01:00
|
|
|
// clean past in-memory chunks
|
|
|
|
if r.UseMemory {
|
|
|
|
go r.memory.CleanChunksByNeed(offset)
|
|
|
|
}
|
|
|
|
r.confirmExternalReading()
|
2017-11-12 18:54:25 +01:00
|
|
|
r.preloadOffset = offset
|
|
|
|
|
|
|
|
// clear the past seen chunks
|
|
|
|
// they will remain in our persistent storage but will be removed from transient
|
|
|
|
// so they need to be picked up by a worker
|
|
|
|
for k := range r.seenOffsets {
|
|
|
|
if k < offset {
|
|
|
|
r.seenOffsets[k] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:15:30 +02:00
|
|
|
for i := 0; i < r.workers; i++ {
|
2018-05-14 19:06:57 +02:00
|
|
|
o := r.preloadOffset + int64(r.cacheFs().opt.ChunkSize)*int64(i)
|
2017-11-12 18:54:25 +01:00
|
|
|
if o < 0 || o >= r.cachedObject.Size() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if v, ok := r.seenOffsets[o]; ok && v {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
r.seenOffsets[o] = true
|
|
|
|
r.preloadQueue <- o
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getChunk is called by the FS to retrieve a specific chunk of known start and size from where it can find it
|
|
|
|
// it can be from transient or persistent cache
|
|
|
|
// it will also build the chunk from the cache's specific chunk boundaries and build the final desired chunk in a buffer
|
|
|
|
func (r *Handle) getChunk(chunkStart int64) ([]byte, error) {
|
|
|
|
var data []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// we calculate the modulus of the requested offset with the size of a chunk
|
2018-05-14 19:06:57 +02:00
|
|
|
offset := chunkStart % int64(r.cacheFs().opt.ChunkSize)
|
2017-11-12 18:54:25 +01:00
|
|
|
|
|
|
|
// we align the start offset of the first chunk to a likely chunk in the storage
|
2024-05-31 14:37:21 +02:00
|
|
|
chunkStart -= offset
|
2017-11-12 18:54:25 +01:00
|
|
|
r.queueOffset(chunkStart)
|
|
|
|
found := false
|
|
|
|
|
|
|
|
if r.UseMemory {
|
|
|
|
data, err = r.memory.GetChunk(r.cachedObject, chunkStart)
|
|
|
|
if err == nil {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
// we're gonna give the workers a chance to pickup the chunk
|
|
|
|
// and retry a couple of times
|
2018-05-14 19:06:57 +02:00
|
|
|
for i := 0; i < r.cacheFs().opt.ReadRetries*8; i++ {
|
2017-11-12 18:54:25 +01:00
|
|
|
data, err = r.storage().GetChunk(r.cachedObject, chunkStart)
|
|
|
|
if err == nil {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.Debugf(r, "%v: chunk retry storage: %v", chunkStart, i)
|
2017-12-09 22:54:26 +01:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// not found in ram or
|
|
|
|
// the worker didn't managed to download the chunk in time so we abort and close the stream
|
|
|
|
if err != nil || len(data) == 0 || !found {
|
2018-08-30 12:15:30 +02:00
|
|
|
if r.workers == 0 {
|
2017-11-12 18:54:25 +01:00
|
|
|
fs.Errorf(r, "out of workers")
|
|
|
|
return nil, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("chunk not found %v", chunkStart)
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// first chunk will be aligned with the start
|
|
|
|
if offset > 0 {
|
2018-04-03 21:46:00 +02:00
|
|
|
if offset > int64(len(data)) {
|
2017-12-10 13:11:31 +01:00
|
|
|
fs.Errorf(r, "unexpected conditions during reading. current position: %v, current chunk position: %v, current chunk size: %v, offset: %v, chunk size: %v, file size: %v",
|
2018-05-14 19:06:57 +02:00
|
|
|
r.offset, chunkStart, len(data), offset, r.cacheFs().opt.ChunkSize, r.cachedObject.Size())
|
2017-12-10 13:11:31 +01:00
|
|
|
return nil, io.ErrUnexpectedEOF
|
|
|
|
}
|
2017-11-12 18:54:25 +01:00
|
|
|
data = data[int(offset):]
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a chunk from storage or len(p)
|
|
|
|
func (r *Handle) Read(p []byte) (n int, err error) {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
var buf []byte
|
|
|
|
|
2017-12-09 22:54:26 +01:00
|
|
|
// first reading
|
|
|
|
if !r.reading {
|
|
|
|
r.reading = true
|
|
|
|
}
|
|
|
|
// reached EOF
|
|
|
|
if r.offset >= r.cachedObject.Size() {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2017-11-12 18:54:25 +01:00
|
|
|
currentOffset := r.offset
|
|
|
|
buf, err = r.getChunk(currentOffset)
|
2018-04-03 21:46:00 +02:00
|
|
|
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
|
2017-12-10 13:11:31 +01:00
|
|
|
fs.Errorf(r, "(%v/%v) error (%v) response", currentOffset, r.cachedObject.Size(), err)
|
2018-04-03 21:46:00 +02:00
|
|
|
}
|
|
|
|
if len(buf) == 0 && err != io.ErrUnexpectedEOF {
|
2017-11-12 18:54:25 +01:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
readSize := copy(p, buf)
|
|
|
|
newOffset := currentOffset + int64(readSize)
|
|
|
|
r.offset = newOffset
|
|
|
|
|
|
|
|
return readSize, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close will tell the workers to stop
|
|
|
|
func (r *Handle) Close() error {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
if r.closed {
|
|
|
|
return errors.New("file already closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
close(r.preloadQueue)
|
|
|
|
r.closed = true
|
|
|
|
// wait for workers to complete their jobs before returning
|
2018-08-30 12:15:30 +02:00
|
|
|
r.workersWg.Wait()
|
2018-04-04 22:25:53 +02:00
|
|
|
r.memory.db.Flush()
|
2017-11-12 18:54:25 +01:00
|
|
|
|
|
|
|
fs.Debugf(r, "cache reader closed %v", r.offset)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek will move the current offset based on whence and instruct the workers to move there too
|
|
|
|
func (r *Handle) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
switch whence {
|
2018-04-06 20:53:06 +02:00
|
|
|
case io.SeekStart:
|
2017-11-12 18:54:25 +01:00
|
|
|
fs.Debugf(r, "moving offset set from %v to %v", r.offset, offset)
|
|
|
|
r.offset = offset
|
2018-04-06 20:53:06 +02:00
|
|
|
case io.SeekCurrent:
|
2017-11-12 18:54:25 +01:00
|
|
|
fs.Debugf(r, "moving offset cur from %v to %v", r.offset, r.offset+offset)
|
|
|
|
r.offset += offset
|
2018-04-06 20:53:06 +02:00
|
|
|
case io.SeekEnd:
|
2017-11-12 18:54:25 +01:00
|
|
|
fs.Debugf(r, "moving offset end (%v) from %v to %v", r.cachedObject.Size(), r.offset, r.cachedObject.Size()+offset)
|
|
|
|
r.offset = r.cachedObject.Size() + offset
|
|
|
|
default:
|
2021-11-04 11:12:57 +01:00
|
|
|
err = fmt.Errorf("cache: unimplemented seek whence %v", whence)
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 19:06:57 +02:00
|
|
|
chunkStart := r.offset - (r.offset % int64(r.cacheFs().opt.ChunkSize))
|
|
|
|
if chunkStart >= int64(r.cacheFs().opt.ChunkSize) {
|
2024-05-31 14:37:21 +02:00
|
|
|
chunkStart -= int64(r.cacheFs().opt.ChunkSize)
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
r.queueOffset(chunkStart)
|
|
|
|
|
|
|
|
return r.offset, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type worker struct {
|
2018-08-30 12:15:30 +02:00
|
|
|
r *Handle
|
|
|
|
rc io.ReadCloser
|
|
|
|
id int
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// String is a representation of this worker
|
|
|
|
func (w *worker) String() string {
|
|
|
|
return fmt.Sprintf("worker-%v <%v>", w.id, w.r.cachedObject.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// reader will return a reader depending on the capabilities of the source reader:
|
|
|
|
// - if it supports seeking it will seek to the desired offset and return the same reader
|
|
|
|
// - if it doesn't support seeking it will close a possible existing one and open at the desired offset
|
|
|
|
// - if there's no reader associated with this worker, it will create one
|
2018-01-29 23:05:04 +01:00
|
|
|
func (w *worker) reader(offset, end int64, closeOpen bool) (io.ReadCloser, error) {
|
2017-11-12 18:54:25 +01:00
|
|
|
var err error
|
|
|
|
r := w.rc
|
|
|
|
if w.rc == nil {
|
2018-01-29 23:05:04 +01:00
|
|
|
r, err = w.r.cacheFs().openRateLimited(func() (io.ReadCloser, error) {
|
2019-06-17 10:34:30 +02:00
|
|
|
return w.r.cachedObject.Object.Open(w.r.ctx, &fs.RangeOption{Start: offset, End: end - 1})
|
2017-11-12 18:54:25 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2018-01-29 23:05:04 +01:00
|
|
|
if !closeOpen {
|
2018-02-19 18:52:33 +01:00
|
|
|
if do, ok := r.(fs.RangeSeeker); ok {
|
2019-06-17 10:34:30 +02:00
|
|
|
_, err = do.RangeSeek(w.r.ctx, offset, io.SeekStart, end-offset)
|
2018-02-19 18:52:33 +01:00
|
|
|
return r, err
|
|
|
|
} else if do, ok := r.(io.Seeker); ok {
|
2018-04-06 20:53:06 +02:00
|
|
|
_, err = do.Seek(offset, io.SeekStart)
|
2018-01-29 23:05:04 +01:00
|
|
|
return r, err
|
|
|
|
}
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_ = w.rc.Close()
|
2018-01-29 23:05:04 +01:00
|
|
|
return w.r.cacheFs().openRateLimited(func() (io.ReadCloser, error) {
|
2019-06-17 10:34:30 +02:00
|
|
|
r, err = w.r.cachedObject.Object.Open(w.r.ctx, &fs.RangeOption{Start: offset, End: end - 1})
|
2017-11-12 18:54:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// run is the main loop for the worker which receives offsets to preload
|
|
|
|
func (w *worker) run() {
|
|
|
|
var err error
|
|
|
|
var data []byte
|
|
|
|
defer func() {
|
|
|
|
if w.rc != nil {
|
|
|
|
_ = w.rc.Close()
|
|
|
|
}
|
2018-08-30 12:15:30 +02:00
|
|
|
w.r.workersWg.Done()
|
2017-11-12 18:54:25 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
2018-08-30 12:15:30 +02:00
|
|
|
chunkStart, open := <-w.r.preloadQueue
|
2017-11-12 18:54:25 +01:00
|
|
|
if chunkStart < 0 || !open {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip if it exists
|
|
|
|
if w.r.UseMemory {
|
|
|
|
if w.r.memory.HasChunk(w.r.cachedObject, chunkStart) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// add it in ram if it's in the persistent storage
|
|
|
|
data, err = w.r.storage().GetChunk(w.r.cachedObject, chunkStart)
|
|
|
|
if err == nil {
|
2017-12-09 22:54:26 +01:00
|
|
|
err = w.r.memory.AddChunk(w.r.cachedObject.abs(), data, chunkStart)
|
2017-11-12 18:54:25 +01:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(w, "failed caching chunk in ram %v: %v", chunkStart, err)
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2024-05-31 15:18:56 +02:00
|
|
|
} else if w.r.storage().HasChunk(w.r.cachedObject, chunkStart) {
|
|
|
|
continue
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 19:06:57 +02:00
|
|
|
chunkEnd := chunkStart + int64(w.r.cacheFs().opt.ChunkSize)
|
2017-12-11 00:15:53 +01:00
|
|
|
// TODO: Remove this comment if it proves to be reliable for #1896
|
|
|
|
//if chunkEnd > w.r.cachedObject.Size() {
|
|
|
|
// chunkEnd = w.r.cachedObject.Size()
|
|
|
|
//}
|
2017-11-12 18:54:25 +01:00
|
|
|
|
2017-12-09 22:54:26 +01:00
|
|
|
w.download(chunkStart, chunkEnd, 0)
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 18:54:25 +01:00
|
|
|
|
2017-12-09 22:54:26 +01:00
|
|
|
func (w *worker) download(chunkStart, chunkEnd int64, retry int) {
|
|
|
|
var err error
|
|
|
|
var data []byte
|
2017-11-12 18:54:25 +01:00
|
|
|
|
2017-12-09 22:54:26 +01:00
|
|
|
// stop retries
|
2018-05-14 19:06:57 +02:00
|
|
|
if retry >= w.r.cacheFs().opt.ReadRetries {
|
2017-12-09 22:54:26 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// back-off between retries
|
|
|
|
if retry > 0 {
|
|
|
|
time.Sleep(time.Second * time.Duration(retry))
|
|
|
|
}
|
|
|
|
|
2018-01-29 23:05:04 +01:00
|
|
|
closeOpen := false
|
|
|
|
if retry > 0 {
|
|
|
|
closeOpen = true
|
|
|
|
}
|
|
|
|
w.rc, err = w.reader(chunkStart, chunkEnd, closeOpen)
|
2017-12-09 22:54:26 +01:00
|
|
|
// we seem to be getting only errors so we abort
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(w, "object open failed %v: %v", chunkStart, err)
|
2019-06-17 10:34:30 +02:00
|
|
|
err = w.r.cachedObject.refreshFromSource(w.r.ctx, true)
|
2018-01-29 23:05:04 +01:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(w, "%v", err)
|
|
|
|
}
|
2017-12-09 22:54:26 +01:00
|
|
|
w.download(chunkStart, chunkEnd, retry+1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data = make([]byte, chunkEnd-chunkStart)
|
2018-05-04 16:19:50 +02:00
|
|
|
var sourceRead int
|
2017-12-09 22:54:26 +01:00
|
|
|
sourceRead, err = io.ReadFull(w.rc, data)
|
|
|
|
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
|
|
|
|
fs.Errorf(w, "failed to read chunk %v: %v", chunkStart, err)
|
2019-06-17 10:34:30 +02:00
|
|
|
err = w.r.cachedObject.refreshFromSource(w.r.ctx, true)
|
2018-01-29 23:05:04 +01:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(w, "%v", err)
|
|
|
|
}
|
2017-12-09 22:54:26 +01:00
|
|
|
w.download(chunkStart, chunkEnd, retry+1)
|
|
|
|
return
|
|
|
|
}
|
2017-12-11 00:15:53 +01:00
|
|
|
data = data[:sourceRead] // reslice to remove extra garbage
|
2017-12-09 22:54:26 +01:00
|
|
|
if err == io.ErrUnexpectedEOF {
|
2017-12-11 00:15:53 +01:00
|
|
|
fs.Debugf(w, "partial downloaded chunk %v", fs.SizeSuffix(chunkStart))
|
|
|
|
} else {
|
2018-01-29 23:05:04 +01:00
|
|
|
fs.Debugf(w, "downloaded chunk %v", chunkStart)
|
2017-12-09 22:54:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if w.r.UseMemory {
|
|
|
|
err = w.r.memory.AddChunk(w.r.cachedObject.abs(), data, chunkStart)
|
2017-11-12 18:54:25 +01:00
|
|
|
if err != nil {
|
2017-12-09 22:54:26 +01:00
|
|
|
fs.Errorf(w, "failed caching chunk in ram %v: %v", chunkStart, err)
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-09 22:54:26 +01:00
|
|
|
|
|
|
|
err = w.r.storage().AddChunk(w.r.cachedObject.abs(), data, chunkStart)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(w, "failed caching chunk in storage %v: %v", chunkStart, err)
|
|
|
|
}
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 23:05:04 +01:00
|
|
|
const (
|
|
|
|
// BackgroundUploadStarted is a state for a temp file that has started upload
|
|
|
|
BackgroundUploadStarted = iota
|
|
|
|
// BackgroundUploadCompleted is a state for a temp file that has completed upload
|
|
|
|
BackgroundUploadCompleted
|
|
|
|
// BackgroundUploadError is a state for a temp file that has an error upload
|
|
|
|
BackgroundUploadError
|
|
|
|
)
|
|
|
|
|
|
|
|
// BackgroundUploadState is an entity that maps to an existing file which is stored on the temp fs
|
|
|
|
type BackgroundUploadState struct {
|
|
|
|
Remote string
|
|
|
|
Status int
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
type backgroundWriter struct {
|
|
|
|
fs *Fs
|
|
|
|
stateCh chan int
|
|
|
|
running bool
|
|
|
|
notifyCh chan BackgroundUploadState
|
2018-01-30 21:35:53 +01:00
|
|
|
mu sync.Mutex
|
2018-01-29 23:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newBackgroundWriter(f *Fs) *backgroundWriter {
|
|
|
|
b := &backgroundWriter{
|
|
|
|
fs: f,
|
|
|
|
stateCh: make(chan int),
|
|
|
|
notifyCh: make(chan BackgroundUploadState),
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backgroundWriter) close() {
|
|
|
|
b.stateCh <- 2
|
2018-01-30 21:35:53 +01:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
b.running = false
|
|
|
|
|
2018-01-29 23:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backgroundWriter) pause() {
|
|
|
|
b.stateCh <- 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backgroundWriter) play() {
|
|
|
|
b.stateCh <- 0
|
|
|
|
}
|
|
|
|
|
2018-01-30 21:35:53 +01:00
|
|
|
func (b *backgroundWriter) isRunning() bool {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
return b.running
|
|
|
|
}
|
|
|
|
|
2018-01-29 23:05:04 +01:00
|
|
|
func (b *backgroundWriter) notify(remote string, status int, err error) {
|
|
|
|
state := BackgroundUploadState{
|
|
|
|
Remote: remote,
|
|
|
|
Status: status,
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case b.notifyCh <- state:
|
|
|
|
fs.Debugf(remote, "notified background upload state: %v", state.Status)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backgroundWriter) run() {
|
|
|
|
state := 0
|
|
|
|
for {
|
2018-01-30 21:35:53 +01:00
|
|
|
b.mu.Lock()
|
2018-01-29 23:05:04 +01:00
|
|
|
b.running = true
|
2018-01-30 21:35:53 +01:00
|
|
|
b.mu.Unlock()
|
2018-01-29 23:05:04 +01:00
|
|
|
select {
|
|
|
|
case s := <-b.stateCh:
|
|
|
|
state = s
|
|
|
|
default:
|
|
|
|
//
|
|
|
|
}
|
|
|
|
switch state {
|
|
|
|
case 1:
|
|
|
|
runtime.Gosched()
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
continue
|
|
|
|
case 2:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:06:57 +02:00
|
|
|
absPath, err := b.fs.cache.getPendingUpload(b.fs.Root(), time.Duration(b.fs.opt.TempWaitTime))
|
2018-01-29 23:05:04 +01:00
|
|
|
if err != nil || absPath == "" || !b.fs.isRootInPath(absPath) {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
remote := b.fs.cleanRootFromPath(absPath)
|
|
|
|
b.notify(remote, BackgroundUploadStarted, nil)
|
|
|
|
fs.Infof(remote, "background upload: started upload")
|
2019-06-17 10:34:30 +02:00
|
|
|
err = operations.MoveFile(context.TODO(), b.fs.UnWrap(), b.fs.tempFs, remote, remote)
|
2018-01-29 23:05:04 +01:00
|
|
|
if err != nil {
|
|
|
|
b.notify(remote, BackgroundUploadError, err)
|
|
|
|
_ = b.fs.cache.rollbackPendingUpload(absPath)
|
|
|
|
fs.Errorf(remote, "background upload: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2018-06-09 13:52:31 +02:00
|
|
|
// clean empty dirs up to root
|
|
|
|
thisDir := cleanPath(path.Dir(remote))
|
|
|
|
for thisDir != "" {
|
2019-06-17 10:34:30 +02:00
|
|
|
thisList, err := b.fs.tempFs.List(context.TODO(), thisDir)
|
2018-06-09 13:52:31 +02:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(thisList) > 0 {
|
|
|
|
break
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
err = b.fs.tempFs.Rmdir(context.TODO(), thisDir)
|
2018-06-09 13:52:31 +02:00
|
|
|
fs.Debugf(thisDir, "cleaned from temp path")
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
thisDir = cleanPath(path.Dir(thisDir))
|
|
|
|
}
|
2018-01-29 23:05:04 +01:00
|
|
|
fs.Infof(remote, "background upload: uploaded entry")
|
|
|
|
err = b.fs.cache.removePendingUpload(absPath)
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "pending upload not found") {
|
|
|
|
fs.Errorf(remote, "background upload: %v", err)
|
|
|
|
}
|
|
|
|
parentCd := NewDirectory(b.fs, cleanPath(path.Dir(remote)))
|
|
|
|
err = b.fs.cache.ExpireDir(parentCd)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(parentCd, "background upload: cache expire error: %v", err)
|
|
|
|
}
|
2018-03-08 21:03:34 +01:00
|
|
|
b.fs.notifyChangeUpstream(remote, fs.EntryObject)
|
2018-01-29 23:05:04 +01:00
|
|
|
fs.Infof(remote, "finished background upload")
|
|
|
|
b.notify(remote, BackgroundUploadCompleted, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 18:54:25 +01:00
|
|
|
// Check the interfaces are satisfied
|
|
|
|
var (
|
|
|
|
_ io.ReadCloser = (*Handle)(nil)
|
|
|
|
_ io.Seeker = (*Handle)(nil)
|
|
|
|
)
|