lib/model: Properly schedule pull on reconnect (fixes #4504)
We need to reset prevSeq so that we force a full check when someone reconnects - the sequence number may not have changed due to the reconnect. (This is a regression; we did this before f6ea2a7.) Also add an optimization: we schedule a pull after scanning, but there is no need to do so if no changes were detected. This matters now because the scheduled pull actually traverses the database which is expensive. This, however, makes the pull not happen on initial scan if there were no changes during the initial scan. Compensate by always scheduling a pull after initial scan in the rwfolder itself. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4508 LGTM: imsodin, AudriusButkevicius
This commit is contained in:
committed by
Audrius Butkevicius
parent
ee5d0dd43f
commit
5f4ed66aa1
@@ -147,7 +147,6 @@ func (f *sendReceiveFolder) Serve() {
|
||||
f.setState(FolderIdle)
|
||||
}()
|
||||
|
||||
var prevSeq int64
|
||||
var prevIgnoreHash string
|
||||
var success bool
|
||||
pullFailTimer := time.NewTimer(time.Duration(0))
|
||||
@@ -157,6 +156,8 @@ func (f *sendReceiveFolder) Serve() {
|
||||
f.startWatch()
|
||||
}
|
||||
|
||||
initialCompleted := f.initialScanFinished
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-f.ctx.Done():
|
||||
@@ -169,13 +170,13 @@ func (f *sendReceiveFolder) Serve() {
|
||||
default:
|
||||
}
|
||||
|
||||
if prevSeq, prevIgnoreHash, success = f.pull(prevSeq, prevIgnoreHash); !success {
|
||||
if prevIgnoreHash, success = f.pull(prevIgnoreHash); !success {
|
||||
// Pulling failed, try again later.
|
||||
pullFailTimer.Reset(f.pause)
|
||||
}
|
||||
|
||||
case <-pullFailTimer.C:
|
||||
if prevSeq, prevIgnoreHash, success = f.pull(prevSeq, prevIgnoreHash); !success {
|
||||
if prevIgnoreHash, success = f.pull(prevIgnoreHash); !success {
|
||||
// Pulling failed, try again later.
|
||||
pullFailTimer.Reset(f.pause)
|
||||
// Back off from retrying to pull with an upper limit.
|
||||
@@ -184,6 +185,14 @@ func (f *sendReceiveFolder) Serve() {
|
||||
}
|
||||
}
|
||||
|
||||
case <-initialCompleted:
|
||||
// Initial scan has completed, we should do a pull
|
||||
initialCompleted = nil // never hit this case again
|
||||
if prevIgnoreHash, success = f.pull(prevIgnoreHash); !success {
|
||||
// Pulling failed, try again later.
|
||||
pullFailTimer.Reset(f.pause)
|
||||
}
|
||||
|
||||
// The reason for running the scanner from within the puller is that
|
||||
// this is the easiest way to make sure we are not doing both at the
|
||||
// same time.
|
||||
@@ -222,41 +231,27 @@ func (f *sendReceiveFolder) String() string {
|
||||
return fmt.Sprintf("sendReceiveFolder/%s@%p", f.folderID, f)
|
||||
}
|
||||
|
||||
func (f *sendReceiveFolder) pull(prevSeq int64, prevIgnoreHash string) (curSeq int64, curIgnoreHash string, success bool) {
|
||||
func (f *sendReceiveFolder) pull(prevIgnoreHash string) (curIgnoreHash string, success bool) {
|
||||
select {
|
||||
case <-f.initialScanFinished:
|
||||
default:
|
||||
// Once the initial scan finished, a pull will be scheduled
|
||||
return prevSeq, prevIgnoreHash, true
|
||||
return prevIgnoreHash, true
|
||||
}
|
||||
|
||||
f.model.fmut.RLock()
|
||||
curIgnores := f.model.folderIgnores[f.folderID]
|
||||
f.model.fmut.RUnlock()
|
||||
|
||||
curSeq = prevSeq
|
||||
curIgnoreHash = curIgnores.Hash()
|
||||
ignoresChanged := curIgnoreHash != prevIgnoreHash
|
||||
if ignoresChanged {
|
||||
// The ignore patterns have changed. We need to re-evaluate if
|
||||
// there are files we need now that were ignored before.
|
||||
l.Debugln(f, "ignore patterns have changed, resetting curSeq")
|
||||
curSeq = 0
|
||||
}
|
||||
|
||||
// RemoteSequence() is a fast call, doesn't touch the database.
|
||||
remoteSeq, ok := f.model.RemoteSequence(f.folderID)
|
||||
if !ok || remoteSeq == curSeq {
|
||||
l.Debugln(f, "skip (remoteSeq == curSeq)", curSeq, ok)
|
||||
return curSeq, curIgnoreHash, true
|
||||
}
|
||||
|
||||
if err := f.CheckHealth(); err != nil {
|
||||
l.Debugln("Skipping pull of", f.Description(), "due to folder error:", err)
|
||||
return curSeq, curIgnoreHash, true
|
||||
return curIgnoreHash, true
|
||||
}
|
||||
|
||||
l.Debugln(f, "pulling", curSeq, remoteSeq)
|
||||
l.Debugln(f, "pulling")
|
||||
|
||||
f.setState(FolderSyncing)
|
||||
f.clearErrors()
|
||||
@@ -273,20 +268,6 @@ func (f *sendReceiveFolder) pull(prevSeq int64, prevIgnoreHash string) (curSeq i
|
||||
// No files were changed by the puller, so we are in
|
||||
// sync. Update the local version number.
|
||||
|
||||
if lv, ok := f.model.RemoteSequence(f.folderID); ok && lv < remoteSeq {
|
||||
// There's a corner case where the device we needed
|
||||
// files from disconnected during the puller
|
||||
// iteration. The files will have been removed from
|
||||
// the index, so we've concluded that we don't need
|
||||
// them, but at the same time we have the old remote sequence
|
||||
// that includes those files in remoteSeq. So we
|
||||
// catch the case that this sequence might have
|
||||
// decreased here.
|
||||
l.Debugf("%v adjusting remoteSeq from %d to %d", remoteSeq, lv)
|
||||
remoteSeq = lv
|
||||
}
|
||||
curSeq = remoteSeq
|
||||
|
||||
f.pause = f.basePause()
|
||||
|
||||
break
|
||||
@@ -313,7 +294,7 @@ func (f *sendReceiveFolder) pull(prevSeq int64, prevIgnoreHash string) (curSeq i
|
||||
|
||||
f.setState(FolderIdle)
|
||||
|
||||
return curSeq, curIgnoreHash, changed == 0
|
||||
return curIgnoreHash, changed == 0
|
||||
}
|
||||
|
||||
// pullerIteration runs a single puller iteration for the given folder and
|
||||
|
||||
Reference in New Issue
Block a user