syncthing/model/model.go

872 lines
22 KiB
Go
Raw Normal View History

2014-07-12 22:45:33 +00:00
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
// All rights reserved. Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2014-06-01 20:50:14 +00:00
2014-05-15 03:26:55 +00:00
package model
2013-12-15 10:43:31 +00:00
import (
"errors"
2013-12-23 17:12:44 +00:00
"fmt"
"io"
2014-01-05 22:54:57 +00:00
"net"
2013-12-15 10:43:31 +00:00
"os"
"path/filepath"
"strconv"
2013-12-15 10:43:31 +00:00
"sync"
"time"
2014-06-21 07:43:12 +00:00
2014-05-15 00:18:09 +00:00
"github.com/calmh/syncthing/config"
2014-07-13 19:07:24 +00:00
"github.com/calmh/syncthing/events"
"github.com/calmh/syncthing/files"
"github.com/calmh/syncthing/lamport"
2013-12-15 10:43:31 +00:00
"github.com/calmh/syncthing/protocol"
"github.com/calmh/syncthing/scanner"
2014-07-06 12:46:48 +00:00
"github.com/syndtr/goleveldb/leveldb"
2013-12-15 10:43:31 +00:00
)
type repoState int
const (
RepoIdle repoState = iota
RepoScanning
RepoSyncing
RepoCleaning
)
// Somewhat arbitrary amount of bytes that we choose to let represent the size
// of an unsynchronized directory entry or a deleted file. We need it to be
// larger than zero so that it's visible that there is some amount of bytes to
// transfer to bring the systems into synchronization.
const zeroEntrySize = 128
// How many files to send in each Index/IndexUpdate message.
const indexBatchSize = 1000
2013-12-15 10:43:31 +00:00
type Model struct {
2014-05-15 03:26:55 +00:00
indexDir string
cfg *config.Configuration
2014-07-06 12:46:48 +00:00
db *leveldb.DB
2014-05-15 03:26:55 +00:00
clientName string
clientVersion string
2014-05-23 12:31:16 +00:00
repoCfgs map[string]config.RepositoryConfiguration // repo -> cfg
repoFiles map[string]*files.Set // repo -> files
repoNodes map[string][]protocol.NodeID // repo -> nodeIDs
nodeRepos map[protocol.NodeID][]string // nodeID -> repos
2014-05-23 12:31:16 +00:00
suppressor map[string]*suppressor // repo -> suppressor
rmut sync.RWMutex // protects the above
repoState map[string]repoState // repo -> state
smut sync.RWMutex
protoConn map[protocol.NodeID]protocol.Connection
rawConn map[protocol.NodeID]io.Closer
nodeVer map[protocol.NodeID]string
2014-01-18 03:06:44 +00:00
pmut sync.RWMutex // protects protoConn and rawConn
2013-12-30 14:30:29 +00:00
sentLocalVer map[protocol.NodeID]map[string]uint64
slMut sync.Mutex
sup suppressor
2014-01-12 15:59:35 +00:00
addedRepo bool
started bool
2013-12-15 10:43:31 +00:00
}
var (
ErrNoSuchFile = errors.New("no such file")
ErrInvalid = errors.New("file is invalid")
)
// NewModel creates and starts a new model. The model starts in read-only mode,
// where it sends index information to connected peers and responds to requests
// for file data without altering the local repository in any way.
2014-07-06 12:46:48 +00:00
func NewModel(indexDir string, cfg *config.Configuration, clientName, clientVersion string, db *leveldb.DB) *Model {
2013-12-15 10:43:31 +00:00
m := &Model{
2014-05-15 03:26:55 +00:00
indexDir: indexDir,
cfg: cfg,
2014-07-06 12:46:48 +00:00
db: db,
2014-05-15 03:26:55 +00:00
clientName: clientName,
clientVersion: clientVersion,
2014-05-23 12:31:16 +00:00
repoCfgs: make(map[string]config.RepositoryConfiguration),
2014-05-15 03:26:55 +00:00
repoFiles: make(map[string]*files.Set),
repoNodes: make(map[string][]protocol.NodeID),
nodeRepos: make(map[protocol.NodeID][]string),
2014-05-15 03:26:55 +00:00
repoState: make(map[string]repoState),
suppressor: make(map[string]*suppressor),
protoConn: make(map[protocol.NodeID]protocol.Connection),
rawConn: make(map[protocol.NodeID]io.Closer),
nodeVer: make(map[protocol.NodeID]string),
sentLocalVer: make(map[protocol.NodeID]map[string]uint64),
2014-05-15 03:26:55 +00:00
sup: suppressor{threshold: int64(cfg.Options.MaxChangeKbps)},
2013-12-15 10:43:31 +00:00
}
var timeout = 20 * 60 // seconds
if t := os.Getenv("STDEADLOCKTIMEOUT"); len(t) > 0 {
it, err := strconv.Atoi(t)
if err == nil {
timeout = it
}
}
deadlockDetect(&m.rmut, time.Duration(timeout)*time.Second)
deadlockDetect(&m.smut, time.Duration(timeout)*time.Second)
deadlockDetect(&m.pmut, time.Duration(timeout)*time.Second)
2013-12-15 10:43:31 +00:00
return m
}
// StartRW starts read/write processing on the current model. When in
// read/write mode the model will attempt to keep in sync with the cluster by
// pulling needed files from peer nodes.
func (m *Model) StartRepoRW(repo string, threads int) {
m.rmut.RLock()
defer m.rmut.RUnlock()
2014-05-23 12:31:16 +00:00
if cfg, ok := m.repoCfgs[repo]; !ok {
panic("cannot start without repo")
} else {
2014-05-23 12:31:16 +00:00
newPuller(cfg, m, threads, m.cfg)
}
}
// StartRO starts read only processing on the current model. When in
// read only mode the model will announce files to the cluster but not
// pull in any external changes.
func (m *Model) StartRepoRO(repo string) {
m.StartRepoRW(repo, 0) // zero threads => read only
2014-01-20 21:22:27 +00:00
}
2014-01-05 22:54:57 +00:00
type ConnectionInfo struct {
protocol.Statistics
2014-01-23 12:12:45 +00:00
Address string
ClientVersion string
Completion int
2014-01-05 22:54:57 +00:00
}
// ConnectionStats returns a map with connection statistics for each connected node.
2014-01-05 22:54:57 +00:00
func (m *Model) ConnectionStats() map[string]ConnectionInfo {
type remoteAddrer interface {
RemoteAddr() net.Addr
}
2014-01-18 03:06:44 +00:00
m.pmut.RLock()
m.rmut.RLock()
2014-01-05 15:16:37 +00:00
2014-01-05 22:54:57 +00:00
var res = make(map[string]ConnectionInfo)
2014-01-09 12:58:35 +00:00
for node, conn := range m.protoConn {
2014-01-05 22:54:57 +00:00
ci := ConnectionInfo{
2014-01-23 12:12:45 +00:00
Statistics: conn.Statistics(),
ClientVersion: m.nodeVer[node],
2014-01-05 22:54:57 +00:00
}
if nc, ok := m.rawConn[node].(remoteAddrer); ok {
ci.Address = nc.RemoteAddr().String()
}
var tot int64
var have int64
for _, repo := range m.nodeRepos[node] {
2014-07-12 21:06:48 +00:00
m.repoFiles[repo].WithGlobal(func(f protocol.FileInfo) bool {
2014-05-23 10:53:26 +00:00
if !protocol.IsDeleted(f.Flags) {
2014-07-12 21:06:48 +00:00
var size int64
2014-05-23 10:53:26 +00:00
if protocol.IsDirectory(f.Flags) {
size = zeroEntrySize
2014-07-12 21:06:48 +00:00
} else {
size = f.Size()
}
tot += size
have += size
}
2014-07-06 12:46:48 +00:00
return true
})
2014-07-12 21:06:48 +00:00
m.repoFiles[repo].WithNeed(node, func(f protocol.FileInfo) bool {
2014-05-23 10:53:26 +00:00
if !protocol.IsDeleted(f.Flags) {
2014-07-12 21:06:48 +00:00
var size int64
2014-05-23 10:53:26 +00:00
if protocol.IsDirectory(f.Flags) {
size = zeroEntrySize
2014-07-12 21:06:48 +00:00
} else {
size = f.Size()
}
have -= size
}
2014-07-06 12:46:48 +00:00
return true
})
}
ci.Completion = 100
if tot != 0 {
ci.Completion = int(100 * have / tot)
}
res[node.String()] = ci
2013-12-30 14:30:29 +00:00
}
2014-01-18 03:06:44 +00:00
m.rmut.RUnlock()
2014-01-18 03:06:44 +00:00
m.pmut.RUnlock()
in, out := protocol.TotalInOut()
res["total"] = ConnectionInfo{
Statistics: protocol.Statistics{
At: time.Now(),
InBytesTotal: in,
OutBytesTotal: out,
},
}
2014-01-05 15:16:37 +00:00
return res
2013-12-30 14:30:29 +00:00
}
2014-07-12 21:06:48 +00:00
func sizeOf(fs []protocol.FileInfo) (files, deleted int, bytes int64) {
for _, f := range fs {
2014-07-06 12:46:48 +00:00
fs, de, by := sizeOfFile(f)
files += fs
deleted += de
bytes += by
}
return
}
2014-07-12 21:06:48 +00:00
func sizeOfFile(f protocol.FileInfo) (files, deleted int, bytes int64) {
2014-07-06 12:46:48 +00:00
if !protocol.IsDeleted(f.Flags) {
files++
if !protocol.IsDirectory(f.Flags) {
2014-07-12 21:06:48 +00:00
bytes += f.Size()
2014-01-05 22:54:57 +00:00
} else {
bytes += zeroEntrySize
2014-01-05 22:54:57 +00:00
}
2014-07-06 12:46:48 +00:00
} else {
deleted++
bytes += zeroEntrySize
2013-12-30 14:30:29 +00:00
}
2014-01-05 15:16:37 +00:00
return
}
2013-12-30 14:30:29 +00:00
// GlobalSize returns the number of files, deleted files and total bytes for all
// files in the global model.
func (m *Model) GlobalSize(repo string) (files, deleted int, bytes int64) {
m.rmut.RLock()
defer m.rmut.RUnlock()
if rf, ok := m.repoFiles[repo]; ok {
2014-07-12 21:06:48 +00:00
rf.WithGlobal(func(f protocol.FileInfo) bool {
2014-07-06 12:46:48 +00:00
fs, de, by := sizeOfFile(f)
files += fs
deleted += de
bytes += by
return true
})
}
2014-07-06 12:46:48 +00:00
return
}
// LocalSize returns the number of files, deleted files and total bytes for all
// files in the local repository.
func (m *Model) LocalSize(repo string) (files, deleted int, bytes int64) {
m.rmut.RLock()
defer m.rmut.RUnlock()
if rf, ok := m.repoFiles[repo]; ok {
2014-07-12 21:06:48 +00:00
rf.WithHave(protocol.LocalNodeID, func(f protocol.FileInfo) bool {
2014-07-06 12:46:48 +00:00
fs, de, by := sizeOfFile(f)
files += fs
deleted += de
bytes += by
return true
})
}
2014-07-06 21:15:28 +00:00
return
2014-01-06 05:38:01 +00:00
}
// NeedSize returns the number and total size of currently needed files.
func (m *Model) NeedSize(repo string) (files int, bytes int64) {
2014-07-15 15:54:00 +00:00
m.rmut.RLock()
defer m.rmut.RUnlock()
if rf, ok := m.repoFiles[repo]; ok {
rf.WithNeed(protocol.LocalNodeID, func(f protocol.FileInfo) bool {
fs, de, by := sizeOfFile(f)
files += fs + de
bytes += by
return true
})
}
return
2013-12-23 17:12:44 +00:00
}
2014-07-06 12:46:48 +00:00
// NeedFiles returns the list of currently needed files
2014-07-12 21:06:48 +00:00
func (m *Model) NeedFilesRepo(repo string) []protocol.FileInfo {
m.rmut.RLock()
defer m.rmut.RUnlock()
if rf, ok := m.repoFiles[repo]; ok {
2014-07-15 15:54:00 +00:00
fs := make([]protocol.FileInfo, 0, indexBatchSize)
2014-07-12 21:06:48 +00:00
rf.WithNeed(protocol.LocalNodeID, func(f protocol.FileInfo) bool {
2014-07-06 12:46:48 +00:00
fs = append(fs, f)
2014-07-15 15:54:00 +00:00
return len(fs) < indexBatchSize
2014-07-06 12:46:48 +00:00
})
if r := m.repoCfgs[repo].FileRanker(); r != nil {
2014-07-06 12:46:48 +00:00
files.SortBy(r).Sort(fs)
}
2014-07-06 12:46:48 +00:00
return fs
}
return nil
}
2013-12-30 14:30:29 +00:00
// Index is called when a new node is connected and we receive their full index.
// Implements the protocol.Model interface.
func (m *Model) Index(nodeID protocol.NodeID, repo string, fs []protocol.FileInfo) {
2014-05-15 03:26:55 +00:00
if debug {
l.Debugf("IDX(in): %s %q: %d files", nodeID, repo, len(fs))
}
if !m.repoSharedWith(repo, nodeID) {
l.Warnf("Unexpected repository ID %q sent from node %q; ensure that the repository exists and that this node is selected under \"Share With\" in the repository configuration.", repo, nodeID)
return
}
m.rmut.RLock()
if r, ok := m.repoFiles[repo]; ok {
2014-07-12 21:06:48 +00:00
r.Replace(nodeID, fs)
} else {
l.Fatalf("Index for nonexistant repo %q", repo)
2013-12-15 10:43:31 +00:00
}
m.rmut.RUnlock()
2014-07-13 19:07:24 +00:00
events.Default.Log(events.RemoteIndexUpdated, map[string]string{
"node": nodeID.String(),
"repo": repo,
})
2013-12-28 13:10:36 +00:00
}
2013-12-30 14:30:29 +00:00
// IndexUpdate is called for incremental updates to connected nodes' indexes.
// Implements the protocol.Model interface.
func (m *Model) IndexUpdate(nodeID protocol.NodeID, repo string, fs []protocol.FileInfo) {
2014-05-15 03:26:55 +00:00
if debug {
2014-05-15 00:08:56 +00:00
l.Debugf("IDXUP(in): %s / %q: %d files", nodeID, repo, len(fs))
}
if !m.repoSharedWith(repo, nodeID) {
l.Warnf("Unexpected repository ID %q sent from node %q; ensure that the repository exists and that this node is selected under \"Share With\" in the repository configuration.", repo, nodeID)
return
}
m.rmut.RLock()
if r, ok := m.repoFiles[repo]; ok {
2014-07-12 21:06:48 +00:00
r.Update(nodeID, fs)
} else {
l.Fatalf("IndexUpdate for nonexistant repo %q", repo)
2013-12-28 13:10:36 +00:00
}
m.rmut.RUnlock()
2014-07-13 19:07:24 +00:00
events.Default.Log(events.RemoteIndexUpdated, map[string]string{
"node": nodeID.String(),
"repo": repo,
})
}
func (m *Model) repoSharedWith(repo string, nodeID protocol.NodeID) bool {
m.rmut.RLock()
defer m.rmut.RUnlock()
for _, nrepo := range m.nodeRepos[nodeID] {
if nrepo == repo {
return true
}
}
return false
}
func (m *Model) ClusterConfig(nodeID protocol.NodeID, config protocol.ClusterConfigMessage) {
compErr := compareClusterConfig(m.clusterConfig(nodeID), config)
2014-05-15 03:26:55 +00:00
if debug {
2014-05-15 00:08:56 +00:00
l.Debugf("ClusterConfig: %s: %#v", nodeID, config)
l.Debugf(" ... compare: %s: %v", nodeID, compErr)
}
if compErr != nil {
2014-05-15 00:08:56 +00:00
l.Warnf("%s: %v", nodeID, compErr)
m.Close(nodeID, compErr)
}
m.pmut.Lock()
if config.ClientName == "syncthing" {
m.nodeVer[nodeID] = config.ClientVersion
} else {
m.nodeVer[nodeID] = config.ClientName + " " + config.ClientVersion
}
m.pmut.Unlock()
2014-07-02 18:43:16 +00:00
l.Infof(`Node %s client is "%s %s"`, nodeID, config.ClientName, config.ClientVersion)
}
2014-01-20 21:22:27 +00:00
// Close removes the peer from the model and closes the underlying connection if possible.
// Implements the protocol.Model interface.
func (m *Model) Close(node protocol.NodeID, err error) {
2014-06-23 13:06:20 +00:00
l.Infof("Connection to %s closed: %v", node, err)
2014-07-13 19:07:24 +00:00
events.Default.Log(events.NodeDisconnected, map[string]string{
"id": node.String(),
"error": err.Error(),
})
m.pmut.Lock()
m.rmut.RLock()
for _, repo := range m.nodeRepos[node] {
2014-07-06 12:46:48 +00:00
m.repoFiles[repo].Replace(node, nil)
}
m.rmut.RUnlock()
2014-01-20 21:22:27 +00:00
conn, ok := m.rawConn[node]
if ok {
conn.Close()
}
2014-01-09 12:58:35 +00:00
delete(m.protoConn, node)
delete(m.rawConn, node)
delete(m.nodeVer, node)
2014-01-18 03:06:44 +00:00
m.pmut.Unlock()
2013-12-15 10:43:31 +00:00
}
// Request returns the specified data segment by reading it from local disk.
// Implements the protocol.Model interface.
func (m *Model) Request(nodeID protocol.NodeID, repo, name string, offset int64, size int) ([]byte, error) {
// Verify that the requested file exists in the local model.
m.rmut.RLock()
r, ok := m.repoFiles[repo]
m.rmut.RUnlock()
if !ok {
2014-05-15 00:08:56 +00:00
l.Warnf("Request from %s for file %s in nonexistent repo %q", nodeID, name, repo)
return nil, ErrNoSuchFile
}
2014-07-06 12:46:48 +00:00
lf := r.Get(protocol.LocalNodeID, name)
2014-07-12 21:06:48 +00:00
if protocol.IsInvalid(lf.Flags) || protocol.IsDeleted(lf.Flags) {
if debug {
l.Debugf("REQ(in): %s: %q / %q o=%d s=%d; invalid: %v", nodeID, repo, name, offset, size, lf)
}
return nil, ErrInvalid
}
2014-07-12 21:06:48 +00:00
if offset > lf.Size() {
2014-05-15 03:26:55 +00:00
if debug {
2014-05-15 00:08:56 +00:00
l.Debugf("REQ(in; nonexistent): %s: %q o=%d s=%d", nodeID, name, offset, size)
}
return nil, ErrNoSuchFile
}
2014-07-06 12:46:48 +00:00
if debug && nodeID != protocol.LocalNodeID {
2014-05-15 00:08:56 +00:00
l.Debugf("REQ(in): %s: %q / %q o=%d s=%d", nodeID, repo, name, offset, size)
2013-12-15 10:43:31 +00:00
}
m.rmut.RLock()
2014-05-23 12:31:16 +00:00
fn := filepath.Join(m.repoCfgs[repo].Directory, name)
m.rmut.RUnlock()
2013-12-15 10:43:31 +00:00
fd, err := os.Open(fn) // XXX: Inefficient, should cache fd?
if err != nil {
return nil, err
}
defer fd.Close()
2014-06-18 21:57:22 +00:00
buf := make([]byte, size)
_, err = fd.ReadAt(buf, offset)
2013-12-15 10:43:31 +00:00
if err != nil {
return nil, err
}
return buf, nil
}
// ReplaceLocal replaces the local repository index with the given list of files.
2014-07-12 21:06:48 +00:00
func (m *Model) ReplaceLocal(repo string, fs []protocol.FileInfo) {
m.rmut.RLock()
2014-07-06 12:46:48 +00:00
m.repoFiles[repo].ReplaceWithDelete(protocol.LocalNodeID, fs)
m.rmut.RUnlock()
2013-12-15 10:43:31 +00:00
}
2014-07-12 21:06:48 +00:00
func (m *Model) CurrentRepoFile(repo string, file string) protocol.FileInfo {
m.rmut.RLock()
2014-07-06 12:46:48 +00:00
f := m.repoFiles[repo].Get(protocol.LocalNodeID, file)
m.rmut.RUnlock()
return f
}
2014-07-12 21:06:48 +00:00
func (m *Model) CurrentGlobalFile(repo string, file string) protocol.FileInfo {
m.rmut.RLock()
f := m.repoFiles[repo].GetGlobal(file)
m.rmut.RUnlock()
return f
}
type cFiler struct {
m *Model
r string
}
2014-03-16 07:14:55 +00:00
// Implements scanner.CurrentFiler
2014-07-12 21:06:48 +00:00
func (cf cFiler) CurrentFile(file string) protocol.FileInfo {
return cf.m.CurrentRepoFile(cf.r, file)
2014-03-16 07:14:55 +00:00
}
// ConnectedTo returns true if we are connected to the named node.
func (m *Model) ConnectedTo(nodeID protocol.NodeID) bool {
2014-01-18 03:06:44 +00:00
m.pmut.RLock()
2014-01-09 12:58:35 +00:00
_, ok := m.protoConn[nodeID]
2014-01-18 03:06:44 +00:00
m.pmut.RUnlock()
return ok
}
// AddConnection adds a new peer connection to the model. An initial index will
// be sent to the connected peer, thereafter index updates whenever the local
// repository changes.
func (m *Model) AddConnection(rawConn io.Closer, protoConn protocol.Connection) {
2014-01-09 12:58:35 +00:00
nodeID := protoConn.ID()
2014-01-18 03:06:44 +00:00
m.pmut.Lock()
if _, ok := m.protoConn[nodeID]; ok {
panic("add existing node")
}
2014-01-09 12:58:35 +00:00
m.protoConn[nodeID] = protoConn
if _, ok := m.rawConn[nodeID]; ok {
panic("add existing node")
}
2014-01-09 12:58:35 +00:00
m.rawConn[nodeID] = rawConn
cm := m.clusterConfig(nodeID)
protoConn.ClusterConfig(cm)
2014-05-04 15:18:58 +00:00
m.rmut.RLock()
for _, repo := range m.nodeRepos[nodeID] {
fs := m.repoFiles[repo]
go sendIndexes(protoConn, repo, fs)
2014-05-04 15:18:58 +00:00
}
m.rmut.RUnlock()
m.pmut.Unlock()
}
func sendIndexes(conn protocol.Connection, repo string, fs *files.Set) {
nodeID := conn.ID()
name := conn.Name()
if debug {
l.Debugf("sendIndexes for %s-%s@/%q starting", nodeID, name, repo)
}
2014-05-04 15:18:58 +00:00
initial := true
minLocalVer := uint64(0)
var err error
defer func() {
if debug {
l.Debugf("sendIndexes for %s-%s@/%q exiting: %v", nodeID, name, repo, err)
}
}()
for err == nil {
if !initial && fs.LocalVersion(protocol.LocalNodeID) <= minLocalVer {
time.Sleep(1 * time.Second)
continue
}
batch := make([]protocol.FileInfo, 0, indexBatchSize)
maxLocalVer := uint64(0)
fs.WithHave(protocol.LocalNodeID, func(f protocol.FileInfo) bool {
if f.LocalVersion <= minLocalVer {
return true
}
if f.LocalVersion > maxLocalVer {
maxLocalVer = f.LocalVersion
}
if len(batch) == indexBatchSize {
if initial {
if err = conn.Index(repo, batch); err != nil {
return false
}
if debug {
l.Debugf("sendIndexes for %s-%s/%q: %d files (initial index)", nodeID, name, repo, len(batch))
}
initial = false
2014-07-03 10:30:10 +00:00
} else {
if err = conn.IndexUpdate(repo, batch); err != nil {
return false
}
if debug {
l.Debugf("sendIndexes for %s-%s/%q: %d files (batched update)", nodeID, name, repo, len(batch))
}
2014-07-03 10:30:10 +00:00
}
batch = make([]protocol.FileInfo, 0, indexBatchSize)
2014-07-03 10:30:10 +00:00
}
batch = append(batch, f)
return true
})
2014-01-18 03:06:44 +00:00
if initial {
err = conn.Index(repo, batch)
if debug && err == nil {
l.Debugf("sendIndexes for %s-%s/%q: %d files (small initial index)", nodeID, name, repo, len(batch))
}
initial = false
} else if len(batch) > 0 {
err = conn.IndexUpdate(repo, batch)
if debug && err == nil {
l.Debugf("sendIndexes for %s-%s/%q: %d files (last batch)", nodeID, name, repo, len(batch))
}
}
minLocalVer = maxLocalVer
}
}
2014-07-12 21:06:48 +00:00
func (m *Model) updateLocal(repo string, f protocol.FileInfo) {
f.LocalVersion = 0
m.rmut.RLock()
2014-07-12 21:06:48 +00:00
m.repoFiles[repo].Update(protocol.LocalNodeID, []protocol.FileInfo{f})
m.rmut.RUnlock()
2014-07-13 19:07:24 +00:00
events.Default.Log(events.LocalIndexUpdated, map[string]string{"repo": repo})
}
func (m *Model) requestGlobal(nodeID protocol.NodeID, repo, name string, offset int64, size int, hash []byte) ([]byte, error) {
2014-01-18 03:06:44 +00:00
m.pmut.RLock()
2014-01-09 12:58:35 +00:00
nc, ok := m.protoConn[nodeID]
2014-01-18 03:06:44 +00:00
m.pmut.RUnlock()
if !ok {
return nil, fmt.Errorf("requestGlobal: no such node: %s", nodeID)
}
2014-05-15 03:26:55 +00:00
if debug {
2014-05-15 00:08:56 +00:00
l.Debugf("REQ(out): %s: %q / %q o=%d s=%d h=%x", nodeID, repo, name, offset, size, hash)
}
return nc.Request(repo, name, offset, size)
}
2014-05-23 12:31:16 +00:00
func (m *Model) AddRepo(cfg config.RepositoryConfiguration) {
if m.started {
panic("cannot add repo to started model")
}
2014-05-23 12:31:16 +00:00
if len(cfg.ID) == 0 {
panic("cannot add empty repo id")
}
m.rmut.Lock()
2014-05-23 12:31:16 +00:00
m.repoCfgs[cfg.ID] = cfg
2014-07-06 12:46:48 +00:00
m.repoFiles[cfg.ID] = files.NewSet(cfg.ID, m.db)
2014-05-23 12:31:16 +00:00
m.suppressor[cfg.ID] = &suppressor{threshold: int64(m.cfg.Options.MaxChangeKbps)}
2013-12-15 10:43:31 +00:00
m.repoNodes[cfg.ID] = make([]protocol.NodeID, len(cfg.Nodes))
2014-05-23 12:31:16 +00:00
for i, node := range cfg.Nodes {
m.repoNodes[cfg.ID][i] = node.NodeID
m.nodeRepos[node.NodeID] = append(m.nodeRepos[node.NodeID], cfg.ID)
}
2014-01-23 21:20:15 +00:00
m.addedRepo = true
m.rmut.Unlock()
}
2014-01-23 21:20:15 +00:00
func (m *Model) ScanRepos() {
m.rmut.RLock()
2014-05-23 12:31:16 +00:00
var repos = make([]string, 0, len(m.repoCfgs))
for repo := range m.repoCfgs {
repos = append(repos, repo)
}
m.rmut.RUnlock()
var wg sync.WaitGroup
wg.Add(len(repos))
for _, repo := range repos {
repo := repo
go func() {
err := m.ScanRepo(repo)
if err != nil {
invalidateRepo(m.cfg, repo, err)
}
wg.Done()
}()
}
wg.Wait()
}
2013-12-15 10:43:31 +00:00
2014-05-15 03:26:55 +00:00
func (m *Model) CleanRepos() {
m.rmut.RLock()
2014-05-23 12:31:16 +00:00
var dirs = make([]string, 0, len(m.repoCfgs))
for _, cfg := range m.repoCfgs {
dirs = append(dirs, cfg.Directory)
2014-05-15 03:26:55 +00:00
}
m.rmut.RUnlock()
var wg sync.WaitGroup
wg.Add(len(dirs))
for _, dir := range dirs {
w := &scanner.Walker{
Dir: dir,
TempNamer: defTempNamer,
}
go func() {
w.CleanTempFiles()
wg.Done()
}()
}
wg.Wait()
}
func (m *Model) ScanRepo(repo string) error {
m.rmut.RLock()
fs := m.repoFiles[repo]
dir := m.repoCfgs[repo].Directory
w := &scanner.Walker{
Dir: dir,
IgnoreFile: ".stignore",
2014-05-15 03:26:55 +00:00
BlockSize: scanner.StandardBlockSize,
TempNamer: defTempNamer,
2014-05-15 03:26:55 +00:00
Suppressor: m.suppressor[repo],
CurrentFiler: cFiler{m, repo},
2014-05-23 12:31:16 +00:00
IgnorePerms: m.repoCfgs[repo].IgnorePerms,
}
m.rmut.RUnlock()
m.setState(repo, RepoScanning)
fchan, _, err := w.Walk()
if err != nil {
return err
}
2014-07-15 15:54:00 +00:00
batchSize := 100
batch := make([]protocol.FileInfo, 0, 00)
for f := range fchan {
2014-07-15 15:54:00 +00:00
if len(batch) == batchSize {
fs.Update(protocol.LocalNodeID, batch)
batch = batch[:0]
}
batch = append(batch, f)
}
if len(batch) > 0 {
fs.Update(protocol.LocalNodeID, batch)
}
batch = batch[:0]
fs.WithHave(protocol.LocalNodeID, func(f protocol.FileInfo) bool {
if !protocol.IsDeleted(f.Flags) {
2014-07-15 15:54:00 +00:00
if len(batch) == batchSize {
fs.Update(protocol.LocalNodeID, batch)
batch = batch[:0]
}
if _, err := os.Stat(filepath.Join(dir, f.Name)); err != nil && os.IsNotExist(err) {
// File has been deleted
f.Blocks = nil
f.Flags |= protocol.FlagDeleted
f.Version = lamport.Default.Tick(f.Version)
f.LocalVersion = 0
batch = append(batch, f)
}
}
return true
})
if len(batch) > 0 {
fs.Update(protocol.LocalNodeID, batch)
}
m.setState(repo, RepoIdle)
return nil
}
// clusterConfig returns a ClusterConfigMessage that is correct for the given peer node
func (m *Model) clusterConfig(node protocol.NodeID) protocol.ClusterConfigMessage {
cm := protocol.ClusterConfigMessage{
2014-05-15 03:26:55 +00:00
ClientName: m.clientName,
ClientVersion: m.clientVersion,
}
m.rmut.RLock()
for _, repo := range m.nodeRepos[node] {
cr := protocol.Repository{
ID: repo,
2014-01-09 12:58:35 +00:00
}
for _, node := range m.repoNodes[repo] {
// TODO: Set read only bit when relevant
cr.Nodes = append(cr.Nodes, protocol.Node{
ID: node[:],
Flags: protocol.FlagShareTrusted,
})
2014-01-09 12:58:35 +00:00
}
cm.Repositories = append(cm.Repositories, cr)
2013-12-30 01:33:57 +00:00
}
m.rmut.RUnlock()
return cm
2013-12-30 01:33:57 +00:00
}
func (m *Model) setState(repo string, state repoState) {
m.smut.Lock()
m.repoState[repo] = state
m.smut.Unlock()
}
func (m *Model) State(repo string) string {
m.smut.RLock()
state := m.repoState[repo]
m.smut.RUnlock()
switch state {
case RepoIdle:
return "idle"
case RepoScanning:
return "scanning"
case RepoCleaning:
return "cleaning"
case RepoSyncing:
return "syncing"
default:
return "unknown"
}
}
func (m *Model) Override(repo string) {
m.rmut.RLock()
2014-07-15 15:54:00 +00:00
fs := m.repoFiles[repo]
m.rmut.RUnlock()
2014-07-15 15:54:00 +00:00
batch := make([]protocol.FileInfo, 0, indexBatchSize)
fs.WithNeed(protocol.LocalNodeID, func(need protocol.FileInfo) bool {
if len(batch) == indexBatchSize {
fs.Update(protocol.LocalNodeID, batch)
batch = batch[:0]
}
have := fs.Get(protocol.LocalNodeID, need.Name)
if have.Name != need.Name {
// We are missing the file
2014-07-15 15:54:00 +00:00
need.Flags |= protocol.FlagDeleted
need.Blocks = nil
} else {
// We have the file, replace with our version
2014-07-15 15:54:00 +00:00
need = have
}
2014-07-15 15:54:00 +00:00
need.Version = lamport.Default.Tick(need.Version)
need.LocalVersion = 0
batch = append(batch, need)
return true
})
if len(batch) > 0 {
fs.Update(protocol.LocalNodeID, batch)
}
}
// Version returns the change version for the given repository. This is
// guaranteed to increment if the contents of the local or global repository
// has changed.
func (m *Model) LocalVersion(repo string) uint64 {
m.rmut.Lock()
2014-07-15 15:54:00 +00:00
defer m.rmut.Unlock()
fs, ok := m.repoFiles[repo]
if !ok {
return 0
}
ver := fs.LocalVersion(protocol.LocalNodeID)
for _, n := range m.repoNodes[repo] {
2014-07-15 15:54:00 +00:00
ver += fs.LocalVersion(n)
}
return ver
}