syncthing/model_puller.go

249 lines
4.3 KiB
Go
Raw Normal View History

2013-12-15 10:43:31 +00:00
package main
/*
Locking
=======
These methods are never called from the outside so don't follow the locking
2013-12-29 17:18:59 +00:00
policy in model.go.
2013-12-15 10:43:31 +00:00
TODO(jb): Refactor this into smaller and cleaner pieces.
2013-12-29 17:18:59 +00:00
TODO(jb): Increase performance by taking apparent peer bandwidth into account.
2013-12-15 10:43:31 +00:00
*/
import (
"bytes"
2013-12-30 14:36:41 +00:00
"errors"
2013-12-15 10:43:31 +00:00
"fmt"
"io"
"os"
"path"
"sync"
"time"
"github.com/calmh/syncthing/buffers"
)
func (m *Model) pullFile(name string) error {
m.RLock()
var localFile = m.local[name]
var globalFile = m.global[name]
2013-12-31 00:49:25 +00:00
var nodeIDs = m.whoHas(name)
2013-12-15 10:43:31 +00:00
m.RUnlock()
2013-12-31 00:49:25 +00:00
if len(nodeIDs) == 0 {
2013-12-31 01:55:33 +00:00
return fmt.Errorf("%s: no connected nodes with file available", name)
2013-12-31 00:49:25 +00:00
}
2013-12-15 10:43:31 +00:00
filename := path.Join(m.dir, name)
sdir := path.Dir(filename)
_, err := os.Stat(sdir)
if err != nil && os.IsNotExist(err) {
os.MkdirAll(sdir, 0777)
}
tmpFilename := tempName(filename, globalFile.Modified)
tmpFile, err := os.Create(tmpFilename)
if err != nil {
return err
}
contentChan := make(chan content, 32)
var applyDone sync.WaitGroup
applyDone.Add(1)
go func() {
applyContent(contentChan, tmpFile)
2014-01-01 21:31:52 +00:00
tmpFile.Close()
2013-12-15 10:43:31 +00:00
applyDone.Done()
}()
local, remote := localFile.Blocks.To(globalFile.Blocks)
var fetchDone sync.WaitGroup
// One local copy routine
2013-12-15 10:43:31 +00:00
fetchDone.Add(1)
go func() {
for _, block := range local {
data, err := m.Request("<local>", name, block.Offset, block.Length, block.Hash)
if err != nil {
break
}
contentChan <- content{
offset: int64(block.Offset),
data: data,
}
}
fetchDone.Done()
}()
// N remote copy routines
2013-12-29 17:18:59 +00:00
var remoteBlocks = blockIterator{blocks: remote}
for i := 0; i < opts.Advanced.RequestsInFlight; i++ {
curNode := nodeIDs[i%len(nodeIDs)]
fetchDone.Add(1)
go func(nodeID string) {
for {
block, ok := remoteBlocks.Next()
if !ok {
break
2013-12-15 10:43:31 +00:00
}
2013-12-29 17:18:59 +00:00
data, err := m.RequestGlobal(nodeID, name, block.Offset, block.Length, block.Hash)
if err != nil {
break
}
contentChan <- content{
offset: int64(block.Offset),
data: data,
}
}
fetchDone.Done()
}(curNode)
2013-12-15 10:43:31 +00:00
}
fetchDone.Wait()
close(contentChan)
applyDone.Wait()
2013-12-29 17:18:59 +00:00
err = hashCheck(tmpFilename, globalFile.Blocks)
2013-12-15 10:43:31 +00:00
if err != nil {
2013-12-30 20:27:46 +00:00
return fmt.Errorf("%s: %s (deleting)", path.Base(name), err.Error())
2013-12-15 10:43:31 +00:00
}
err = os.Chtimes(tmpFilename, time.Unix(globalFile.Modified, 0), time.Unix(globalFile.Modified, 0))
if err != nil {
return err
}
err = os.Rename(tmpFilename, filename)
if err != nil {
return err
}
return nil
}
func (m *Model) puller() {
for {
2013-12-29 17:18:59 +00:00
time.Sleep(time.Second)
2013-12-15 10:43:31 +00:00
2013-12-29 17:18:59 +00:00
var ns []string
m.RLock()
for n := range m.need {
ns = append(ns, n)
}
m.RUnlock()
2013-12-15 10:43:31 +00:00
2013-12-29 17:18:59 +00:00
if len(ns) == 0 {
continue
}
var limiter = make(chan bool, opts.Advanced.FilesInFlight)
var allDone sync.WaitGroup
2013-12-29 17:18:59 +00:00
for _, n := range ns {
limiter <- true
allDone.Add(1)
2013-12-29 17:18:59 +00:00
2013-12-30 00:20:36 +00:00
go func(n string) {
defer func() {
allDone.Done()
2013-12-30 00:20:36 +00:00
<-limiter
}()
2013-12-15 10:43:31 +00:00
2013-12-30 00:20:36 +00:00
f, ok := m.GlobalFile(n)
if !ok {
return
2013-12-15 10:43:31 +00:00
}
2013-12-29 17:18:59 +00:00
2013-12-30 00:20:36 +00:00
var err error
if f.Flags&FlagDeleted == 0 {
if opts.Debug.TraceFile {
debugf("FILE: Pull %q", n)
}
err = m.pullFile(n)
} else {
if opts.Debug.TraceFile {
debugf("FILE: Remove %q", n)
}
// Cheerfully ignore errors here
_ = os.Remove(path.Join(m.dir, n))
}
if err == nil {
m.UpdateLocal(f)
} else {
warnln(err)
}
}(n)
2013-12-15 10:43:31 +00:00
}
allDone.Wait()
2013-12-15 10:43:31 +00:00
}
}
type content struct {
offset int64
data []byte
}
func applyContent(cc <-chan content, dst io.WriterAt) error {
var err error
for c := range cc {
_, err = dst.WriteAt(c.data, c.offset)
2014-01-01 21:31:52 +00:00
buffers.Put(c.data)
2013-12-15 10:43:31 +00:00
if err != nil {
return err
}
}
return nil
}
2013-12-29 17:18:59 +00:00
func hashCheck(name string, correct []Block) error {
rf, err := os.Open(name)
if err != nil {
return err
}
defer rf.Close()
current, err := Blocks(rf, BlockSize)
if err != nil {
return err
}
if len(current) != len(correct) {
2013-12-30 14:36:41 +00:00
return errors.New("incorrect number of blocks")
2013-12-29 17:18:59 +00:00
}
for i := range current {
if bytes.Compare(current[i].Hash, correct[i].Hash) != 0 {
2013-12-30 14:36:41 +00:00
return fmt.Errorf("hash mismatch: %x != %x", current[i], correct[i])
2013-12-29 17:18:59 +00:00
}
}
return nil
}
type blockIterator struct {
sync.Mutex
blocks []Block
}
func (i *blockIterator) Next() (b Block, ok bool) {
i.Lock()
defer i.Unlock()
if len(i.blocks) == 0 {
return
}
b, i.blocks = i.blocks[0], i.blocks[1:]
ok = true
return
}