2020-06-22 23:31:08 +08:00
|
|
|
// Package writeback keeps track of the files which need to be written
|
|
|
|
// back to storage
|
|
|
|
package writeback
|
2020-04-17 18:18:58 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"context"
|
|
|
|
"sync"
|
2020-06-22 23:31:08 +08:00
|
|
|
"sync/atomic"
|
2020-04-17 18:18:58 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
2020-06-15 22:09:33 +08:00
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
2020-04-17 18:18:58 +08:00
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 08:17:24 +08:00
|
|
|
maxUploadDelay = 5 * time.Minute // max delay between upload attempts
|
2020-04-17 18:18:58 +08:00
|
|
|
)
|
|
|
|
|
2020-06-22 23:31:08 +08:00
|
|
|
// PutFn is the interface that item provides to store the data
|
|
|
|
type PutFn func(context.Context) error
|
|
|
|
|
|
|
|
// Handle is returned for callers to keep track of writeback items
|
|
|
|
type Handle uint64
|
2020-06-19 22:53:21 +08:00
|
|
|
|
2020-06-22 23:31:08 +08:00
|
|
|
// WriteBack keeps track of the items which need to be written back to the disk at some point
|
|
|
|
type WriteBack struct {
|
2020-06-20 17:26:25 +08:00
|
|
|
ctx context.Context
|
2020-04-17 18:18:58 +08:00
|
|
|
mu sync.Mutex
|
2020-06-22 23:31:08 +08:00
|
|
|
items writeBackItems // priority queue of *writeBackItem - writeBackItems are in here while awaiting transfer only
|
|
|
|
lookup map[Handle]*writeBackItem // for getting a *writeBackItem from a Handle - writeBackItems are in here until cancelled
|
|
|
|
opt *vfscommon.Options // VFS options
|
|
|
|
timer *time.Timer // next scheduled time for the uploader
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 08:17:24 +08:00
|
|
|
expiry time.Time // time the next item expires or IsZero
|
2020-06-22 23:31:08 +08:00
|
|
|
uploads int // number of uploads in progress
|
|
|
|
|
|
|
|
// read and written with atomic
|
|
|
|
id Handle // id of the last writeBackItem created
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 23:31:08 +08:00
|
|
|
// New make a new WriteBack
|
2020-04-17 18:18:58 +08:00
|
|
|
//
|
2020-06-22 23:31:08 +08:00
|
|
|
// cancel the context to stop the background processing
|
|
|
|
func New(ctx context.Context, opt *vfscommon.Options) *WriteBack {
|
|
|
|
wb := &WriteBack{
|
2020-06-20 17:26:25 +08:00
|
|
|
ctx: ctx,
|
2020-04-17 18:18:58 +08:00
|
|
|
items: writeBackItems{},
|
2020-06-22 23:31:08 +08:00
|
|
|
lookup: make(map[Handle]*writeBackItem),
|
2020-04-17 18:18:58 +08:00
|
|
|
opt: opt,
|
|
|
|
}
|
|
|
|
heap.Init(&wb.items)
|
|
|
|
return wb
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeBackItem stores an Item awaiting writeback
|
|
|
|
//
|
2020-06-03 22:49:41 +08:00
|
|
|
// These are stored on the items heap when awaiting transfer but
|
|
|
|
// removed from the items heap when transferring. They remain in the
|
|
|
|
// lookup map until cancelled.
|
|
|
|
//
|
2020-04-17 18:18:58 +08:00
|
|
|
// writeBack.mu must be held to manipulate this
|
|
|
|
type writeBackItem struct {
|
2020-06-03 22:49:41 +08:00
|
|
|
name string // name of the item so we don't have to read it from item
|
2020-06-22 23:31:08 +08:00
|
|
|
id Handle // id of the item
|
2020-04-17 18:18:58 +08:00
|
|
|
index int // index into the priority queue for update
|
|
|
|
expiry time.Time // When this expires we will write it back
|
2020-06-15 22:09:33 +08:00
|
|
|
uploading bool // True if item is being processed by upload() method
|
|
|
|
onHeap bool // true if this item is on the items heap
|
2020-04-17 18:18:58 +08:00
|
|
|
cancel context.CancelFunc // To cancel the upload with
|
2020-06-03 22:49:41 +08:00
|
|
|
done chan struct{} // closed when the cancellation completes
|
2020-06-22 23:31:08 +08:00
|
|
|
putFn PutFn // To write the object data
|
2020-04-17 18:18:58 +08:00
|
|
|
tries int // number of times we have tried to upload
|
|
|
|
delay time.Duration // delay between upload attempts
|
|
|
|
}
|
|
|
|
|
|
|
|
// A writeBackItems implements a priority queue by implementing
|
|
|
|
// heap.Interface and holds writeBackItems.
|
|
|
|
type writeBackItems []*writeBackItem
|
|
|
|
|
|
|
|
func (ws writeBackItems) Len() int { return len(ws) }
|
|
|
|
|
|
|
|
func (ws writeBackItems) Less(i, j int) bool {
|
2020-06-21 18:45:15 +08:00
|
|
|
a, b := ws[i], ws[j]
|
|
|
|
// If times are equal then use ID to disambiguate
|
|
|
|
if a.expiry.Equal(b.expiry) {
|
|
|
|
return a.id < b.id
|
|
|
|
}
|
|
|
|
return a.expiry.Before(b.expiry)
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ws writeBackItems) Swap(i, j int) {
|
|
|
|
ws[i], ws[j] = ws[j], ws[i]
|
|
|
|
ws[i].index = i
|
|
|
|
ws[j].index = j
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *writeBackItems) Push(x interface{}) {
|
|
|
|
n := len(*ws)
|
|
|
|
item := x.(*writeBackItem)
|
|
|
|
item.index = n
|
|
|
|
*ws = append(*ws, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *writeBackItems) Pop() interface{} {
|
|
|
|
old := *ws
|
|
|
|
n := len(old)
|
|
|
|
item := old[n-1]
|
|
|
|
old[n-1] = nil // avoid memory leak
|
|
|
|
item.index = -1 // for safety
|
|
|
|
*ws = old[0 : n-1]
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// update modifies the expiry of an Item in the queue.
|
|
|
|
//
|
|
|
|
// call with lock held
|
|
|
|
func (ws *writeBackItems) _update(item *writeBackItem, expiry time.Time) {
|
|
|
|
item.expiry = expiry
|
|
|
|
heap.Fix(ws, item.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a new expiry time based from now until the WriteBack timeout
|
|
|
|
//
|
|
|
|
// call with lock held
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) _newExpiry() time.Time {
|
2020-04-17 18:18:58 +08:00
|
|
|
expiry := time.Now()
|
|
|
|
if wb.opt.WriteBack > 0 {
|
|
|
|
expiry = expiry.Add(wb.opt.WriteBack)
|
|
|
|
}
|
2020-06-21 18:45:15 +08:00
|
|
|
// expiry = expiry.Round(time.Millisecond)
|
2020-04-17 18:18:58 +08:00
|
|
|
return expiry
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a new writeBackItem
|
|
|
|
//
|
|
|
|
// call with the lock held
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) _newItem(id Handle, name string) *writeBackItem {
|
|
|
|
wb.SetID(&id)
|
2020-04-17 18:18:58 +08:00
|
|
|
wbItem := &writeBackItem{
|
2020-06-03 22:49:41 +08:00
|
|
|
name: name,
|
2020-04-17 18:18:58 +08:00
|
|
|
expiry: wb._newExpiry(),
|
2020-06-03 22:49:41 +08:00
|
|
|
delay: wb.opt.WriteBack,
|
2020-06-22 23:31:08 +08:00
|
|
|
id: id,
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
wb._addItem(wbItem)
|
|
|
|
wb._pushItem(wbItem)
|
|
|
|
return wbItem
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a writeBackItem to the lookup map
|
|
|
|
//
|
|
|
|
// call with the lock held
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) _addItem(wbItem *writeBackItem) {
|
|
|
|
wb.lookup[wbItem.id] = wbItem
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete a writeBackItem from the lookup map
|
|
|
|
//
|
|
|
|
// call with the lock held
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) _delItem(wbItem *writeBackItem) {
|
|
|
|
delete(wb.lookup, wbItem.id)
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// pop a writeBackItem from the items heap
|
|
|
|
//
|
|
|
|
// call with the lock held
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) _popItem() (wbItem *writeBackItem) {
|
2020-06-15 22:09:33 +08:00
|
|
|
wbItem = heap.Pop(&wb.items).(*writeBackItem)
|
|
|
|
wbItem.onHeap = false
|
|
|
|
return wbItem
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// push a writeBackItem onto the items heap
|
|
|
|
//
|
|
|
|
// call with the lock held
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) _pushItem(wbItem *writeBackItem) {
|
2020-06-15 22:09:33 +08:00
|
|
|
if !wbItem.onHeap {
|
|
|
|
heap.Push(&wb.items, wbItem)
|
|
|
|
wbItem.onHeap = true
|
|
|
|
}
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
|
2020-06-03 22:49:41 +08:00
|
|
|
// remove a writeBackItem from the items heap
|
|
|
|
//
|
|
|
|
// call with the lock held
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) _removeItem(wbItem *writeBackItem) {
|
2020-06-15 22:09:33 +08:00
|
|
|
if wbItem.onHeap {
|
|
|
|
heap.Remove(&wb.items, wbItem.index)
|
|
|
|
wbItem.onHeap = false
|
|
|
|
}
|
2020-06-03 22:49:41 +08:00
|
|
|
}
|
|
|
|
|
2020-04-17 18:18:58 +08:00
|
|
|
// peek the oldest writeBackItem - may be nil
|
|
|
|
//
|
|
|
|
// call with the lock held
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) _peekItem() (wbItem *writeBackItem) {
|
2020-04-17 18:18:58 +08:00
|
|
|
if len(wb.items) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return wb.items[0]
|
|
|
|
}
|
|
|
|
|
2020-06-20 19:25:33 +08:00
|
|
|
// stop the timer which runs the expiries
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) _stopTimer() {
|
2020-06-20 19:25:33 +08:00
|
|
|
if wb.expiry.IsZero() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wb.expiry = time.Time{}
|
2020-07-06 23:06:42 +08:00
|
|
|
// fs.Debugf(nil, "resetTimer STOP")
|
2020-06-20 19:25:33 +08:00
|
|
|
if wb.timer != nil {
|
|
|
|
wb.timer.Stop()
|
|
|
|
wb.timer = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 18:18:58 +08:00
|
|
|
// reset the timer which runs the expiries
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) _resetTimer() {
|
2020-04-17 18:18:58 +08:00
|
|
|
wbItem := wb._peekItem()
|
|
|
|
if wbItem == nil {
|
2020-06-20 19:25:33 +08:00
|
|
|
wb._stopTimer()
|
2020-04-17 18:18:58 +08:00
|
|
|
} else {
|
2020-06-20 17:26:25 +08:00
|
|
|
if wb.expiry.Equal(wbItem.expiry) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wb.expiry = wbItem.expiry
|
2020-04-17 18:18:58 +08:00
|
|
|
dt := time.Until(wbItem.expiry)
|
|
|
|
if dt < 0 {
|
|
|
|
dt = 0
|
|
|
|
}
|
2020-07-06 23:06:42 +08:00
|
|
|
// fs.Debugf(nil, "resetTimer dt=%v", dt)
|
2020-06-20 17:26:25 +08:00
|
|
|
if wb.timer != nil {
|
|
|
|
wb.timer.Stop()
|
|
|
|
}
|
|
|
|
wb.timer = time.AfterFunc(dt, func() {
|
|
|
|
wb.processItems(wb.ctx)
|
|
|
|
})
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:31:08 +08:00
|
|
|
// SetID sets the Handle pointed to if it is non zero to the next
|
|
|
|
// handle.
|
|
|
|
func (wb *WriteBack) SetID(pid *Handle) {
|
|
|
|
if *pid == 0 {
|
|
|
|
*pid = Handle(atomic.AddUint64((*uint64)(&wb.id), 1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds an item to the writeback queue or resets its timer if it
|
2020-06-15 22:09:33 +08:00
|
|
|
// is already there.
|
|
|
|
//
|
2020-06-22 23:31:08 +08:00
|
|
|
// If id is 0 then a new item will always be created and the new
|
|
|
|
// Handle will be returned.
|
|
|
|
//
|
|
|
|
// Use SetID to create Handles in advance of calling Add
|
|
|
|
//
|
|
|
|
// If modified is false then it it doesn't cancel a pending upload if
|
|
|
|
// there is one as there is no need.
|
|
|
|
func (wb *WriteBack) Add(id Handle, name string, modified bool, putFn PutFn) Handle {
|
2020-04-17 18:18:58 +08:00
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
|
|
|
|
2020-06-22 23:31:08 +08:00
|
|
|
wbItem, ok := wb.lookup[id]
|
2020-04-17 18:18:58 +08:00
|
|
|
if !ok {
|
2020-06-22 23:31:08 +08:00
|
|
|
wbItem = wb._newItem(id, name)
|
2020-04-17 18:18:58 +08:00
|
|
|
} else {
|
2020-06-15 22:09:33 +08:00
|
|
|
if wbItem.uploading && modified {
|
2020-04-17 18:18:58 +08:00
|
|
|
// We are uploading already so cancel the upload
|
|
|
|
wb._cancelUpload(wbItem)
|
|
|
|
}
|
|
|
|
// Kick the timer on
|
|
|
|
wb.items._update(wbItem, wb._newExpiry())
|
|
|
|
}
|
2020-06-19 22:53:21 +08:00
|
|
|
wbItem.putFn = putFn
|
2020-04-17 18:18:58 +08:00
|
|
|
wb._resetTimer()
|
2020-06-22 23:31:08 +08:00
|
|
|
return wbItem.id
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 23:31:08 +08:00
|
|
|
// Remove should be called when a file should be removed from the
|
|
|
|
// writeback queue. This cancels a writeback if there is one and
|
|
|
|
// doesn't return the item to the queue.
|
|
|
|
func (wb *WriteBack) Remove(id Handle) (found bool) {
|
2020-06-03 22:49:41 +08:00
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
|
|
|
|
2020-06-22 23:31:08 +08:00
|
|
|
wbItem, found := wb.lookup[id]
|
2020-06-03 22:49:41 +08:00
|
|
|
if found {
|
2020-06-22 23:31:08 +08:00
|
|
|
fs.Debugf(wbItem.name, "vfs cache: cancelling writeback (uploading %v) %p item %d", wbItem.uploading, wbItem, wbItem.id)
|
2020-06-03 22:49:41 +08:00
|
|
|
if wbItem.uploading {
|
|
|
|
// We are uploading already so cancel the upload
|
|
|
|
wb._cancelUpload(wbItem)
|
|
|
|
}
|
2020-06-15 22:09:33 +08:00
|
|
|
// Remove the item from the heap
|
|
|
|
wb._removeItem(wbItem)
|
2020-06-03 22:49:41 +08:00
|
|
|
// Remove the item from the lookup map
|
|
|
|
wb._delItem(wbItem)
|
|
|
|
}
|
|
|
|
wb._resetTimer()
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2020-07-04 00:16:28 +08:00
|
|
|
// Rename should be called when a file might be uploading and it gains
|
|
|
|
// a new name. This will cancel the upload and put it back in the
|
|
|
|
// queue.
|
|
|
|
func (wb *WriteBack) Rename(id Handle, name string) {
|
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
|
|
|
|
|
|
|
wbItem, ok := wb.lookup[id]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if wbItem.uploading {
|
|
|
|
// We are uploading already so cancel the upload
|
|
|
|
wb._cancelUpload(wbItem)
|
|
|
|
}
|
|
|
|
wbItem.name = name
|
|
|
|
// Kick the timer on
|
|
|
|
wb.items._update(wbItem, wb._newExpiry())
|
|
|
|
|
|
|
|
wb._resetTimer()
|
|
|
|
}
|
|
|
|
|
2020-04-17 18:18:58 +08:00
|
|
|
// upload the item - called as a goroutine
|
2020-06-15 22:09:33 +08:00
|
|
|
//
|
|
|
|
// uploading will have been incremented here already
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) upload(ctx context.Context, wbItem *writeBackItem) {
|
2020-04-17 18:18:58 +08:00
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
2020-06-19 22:53:21 +08:00
|
|
|
putFn := wbItem.putFn
|
2020-04-17 18:18:58 +08:00
|
|
|
wbItem.tries++
|
|
|
|
|
2020-07-06 23:06:42 +08:00
|
|
|
fs.Debugf(wbItem.name, "vfs cache: starting upload")
|
|
|
|
|
2020-04-17 18:18:58 +08:00
|
|
|
wb.mu.Unlock()
|
2020-06-19 22:53:21 +08:00
|
|
|
err := putFn(ctx)
|
2020-04-17 18:18:58 +08:00
|
|
|
wb.mu.Lock()
|
|
|
|
|
|
|
|
wbItem.cancel() // cancel context to release resources since store done
|
2020-06-15 22:09:33 +08:00
|
|
|
|
|
|
|
wbItem.uploading = false
|
|
|
|
wb.uploads--
|
2020-04-17 18:18:58 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2020-06-03 22:49:41 +08:00
|
|
|
// FIXME should this have a max number of transfer attempts?
|
|
|
|
wbItem.delay *= 2
|
|
|
|
if wbItem.delay > maxUploadDelay {
|
|
|
|
wbItem.delay = maxUploadDelay
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
2020-06-15 22:09:33 +08:00
|
|
|
if _, uerr := fserrors.Cause(err); uerr == context.Canceled {
|
2020-07-06 23:06:42 +08:00
|
|
|
fs.Infof(wbItem.name, "vfs cache: upload canceled")
|
2020-06-15 22:09:33 +08:00
|
|
|
// Upload was cancelled so reset timer
|
|
|
|
wbItem.delay = wb.opt.WriteBack
|
|
|
|
} else {
|
|
|
|
fs.Errorf(wbItem.name, "vfs cache: failed to upload try #%d, will retry in %v: %v", wbItem.tries, wbItem.delay, err)
|
|
|
|
}
|
2020-06-03 22:49:41 +08:00
|
|
|
// push the item back on the queue for retry
|
|
|
|
wb._pushItem(wbItem)
|
|
|
|
wb.items._update(wbItem, time.Now().Add(wbItem.delay))
|
2020-04-17 18:18:58 +08:00
|
|
|
} else {
|
2020-06-03 22:49:41 +08:00
|
|
|
fs.Infof(wbItem.name, "vfs cache: upload succeeded try #%d", wbItem.tries)
|
2020-04-17 18:18:58 +08:00
|
|
|
// show that we are done with the item
|
|
|
|
wb._delItem(wbItem)
|
|
|
|
}
|
2020-06-20 17:26:25 +08:00
|
|
|
wb._resetTimer()
|
2020-06-03 22:49:41 +08:00
|
|
|
close(wbItem.done)
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
|
2020-06-15 22:09:33 +08:00
|
|
|
// cancel the upload - the item should be on the heap after this returns
|
2020-04-17 18:18:58 +08:00
|
|
|
//
|
|
|
|
// call with lock held
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) _cancelUpload(wbItem *writeBackItem) {
|
2020-04-17 18:18:58 +08:00
|
|
|
if !wbItem.uploading {
|
|
|
|
return
|
|
|
|
}
|
2020-07-06 23:06:42 +08:00
|
|
|
fs.Debugf(wbItem.name, "vfs cache: cancelling upload")
|
2020-04-17 18:18:58 +08:00
|
|
|
if wbItem.cancel != nil {
|
|
|
|
// Cancel the upload - this may or may not be effective
|
|
|
|
wbItem.cancel()
|
2020-06-03 22:49:41 +08:00
|
|
|
// wait for the uploader to finish
|
2020-06-15 22:09:33 +08:00
|
|
|
//
|
|
|
|
// we need to wait without the lock otherwise the
|
|
|
|
// background part will never run.
|
|
|
|
wb.mu.Unlock()
|
2020-06-03 22:49:41 +08:00
|
|
|
<-wbItem.done
|
2020-06-15 22:09:33 +08:00
|
|
|
wb.mu.Lock()
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
// uploading items are not on the heap so add them back
|
|
|
|
wb._pushItem(wbItem)
|
2020-07-06 23:06:42 +08:00
|
|
|
fs.Debugf(wbItem.name, "vfs cache: cancelled upload")
|
2020-06-15 22:09:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cancelUpload cancels the upload of the item if there is one in progress
|
|
|
|
//
|
|
|
|
// it returns true if there was an upload in progress
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) cancelUpload(id Handle) bool {
|
2020-06-15 22:09:33 +08:00
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
2020-06-22 23:31:08 +08:00
|
|
|
wbItem, ok := wb.lookup[id]
|
2020-06-15 22:09:33 +08:00
|
|
|
if !ok || !wbItem.uploading {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
wb._cancelUpload(wbItem)
|
|
|
|
return true
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// this uploads as many items as possible
|
2020-06-22 23:31:08 +08:00
|
|
|
func (wb *WriteBack) processItems(ctx context.Context) {
|
2020-04-17 18:18:58 +08:00
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
|
|
|
|
2020-06-20 17:26:25 +08:00
|
|
|
if wb.ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resetTimer := true
|
2020-04-17 18:18:58 +08:00
|
|
|
for wbItem := wb._peekItem(); wbItem != nil && time.Until(wbItem.expiry) <= 0; wbItem = wb._peekItem() {
|
|
|
|
// If reached transfer limit don't restart the timer
|
|
|
|
if wb.uploads >= fs.Config.Transfers {
|
2020-06-03 22:49:41 +08:00
|
|
|
fs.Debugf(wbItem.name, "vfs cache: delaying writeback as --transfers exceeded")
|
2020-04-17 18:18:58 +08:00
|
|
|
resetTimer = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Pop the item, mark as uploading and start the uploader
|
|
|
|
wbItem = wb._popItem()
|
2020-06-20 17:26:25 +08:00
|
|
|
//fs.Debugf(wbItem.name, "uploading = true %p item %p", wbItem, wbItem.item)
|
2020-04-17 18:18:58 +08:00
|
|
|
wbItem.uploading = true
|
|
|
|
wb.uploads++
|
|
|
|
newCtx, cancel := context.WithCancel(ctx)
|
|
|
|
wbItem.cancel = cancel
|
2020-06-03 22:49:41 +08:00
|
|
|
wbItem.done = make(chan struct{})
|
2020-04-17 18:18:58 +08:00
|
|
|
go wb.upload(newCtx, wbItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resetTimer {
|
|
|
|
wb._resetTimer()
|
2020-06-20 19:25:33 +08:00
|
|
|
} else {
|
|
|
|
wb._stopTimer()
|
2020-04-17 18:18:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:31:08 +08:00
|
|
|
// Stats return the number of uploads in progress and queued
|
|
|
|
func (wb *WriteBack) Stats() (uploadsInProgress, uploadsQueued int) {
|
2020-04-22 19:25:40 +08:00
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
|
|
|
return wb.uploads, len(wb.items)
|
|
|
|
}
|