flatfs.go 23.7 KB
Newer Older
Tommi Virtanen's avatar
Tommi Virtanen committed
1 2 3 4 5 6
// Package flatfs is a Datastore implementation that stores all
// objects in a two-level directory structure in the local file
// system, regardless of the hierarchy of the keys.
package flatfs

import (
7
	"encoding/json"
Tommi Virtanen's avatar
Tommi Virtanen committed
8
	"errors"
9
	"fmt"
Tommi Virtanen's avatar
Tommi Virtanen committed
10
	"io/ioutil"
11 12
	"math"
	"math/rand"
Tommi Virtanen's avatar
Tommi Virtanen committed
13
	"os"
Jeromy's avatar
Jeromy committed
14
	"path/filepath"
Tommi Virtanen's avatar
Tommi Virtanen committed
15
	"strings"
16 17
	"sync"
	"sync/atomic"
18
	"time"
Tommi Virtanen's avatar
Tommi Virtanen committed
19

Jeromy's avatar
Jeromy committed
20 21
	"github.com/ipfs/go-datastore"
	"github.com/ipfs/go-datastore/query"
22

Jakub Sztandera's avatar
Jakub Sztandera committed
23
	logging "github.com/ipfs/go-log"
Tommi Virtanen's avatar
Tommi Virtanen committed
24 25
)

26 27
var log = logging.Logger("flatfs")

Tommi Virtanen's avatar
Tommi Virtanen committed
28
const (
29
	extension                  = ".data"
30
	diskUsageMessageTimeout    = 5 * time.Second
31
	diskUsageCheckpointPercent = 1.0
32
	diskUsageCheckpointTimeout = 2 * time.Second
Tommi Virtanen's avatar
Tommi Virtanen committed
33 34
)

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
var (
	// DiskUsageFile is the name of the file to cache the size of the
	// datastore in disk
	DiskUsageFile = "diskUsage.cache"
	// DiskUsageFilesAverage is the maximum number of files per folder
	// to stat in order to calculate the size of the datastore.
	// The size of the rest of the files in a folder will be assumed
	// to be the average of the values obtained. This includes
	// regular files and directories.
	DiskUsageFilesAverage = 2000
	// DiskUsageCalcTimeout is the maximum time to spend
	// calculating the DiskUsage upon a start when no
	// DiskUsageFile is present.
	// If this period did not suffice to read the size of the datastore,
	// the remaining sizes will be stimated.
	DiskUsageCalcTimeout = 5 * time.Minute
)

const (
	opPut = iota
	opDelete
	opRename
)

59 60 61
type initAccuracy string

const (
62
	unknownA  initAccuracy = "unknown"
63 64 65 66 67 68
	exactA    initAccuracy = "initial-exact"
	approxA   initAccuracy = "initial-approximate"
	timedoutA initAccuracy = "initial-timed-out"
)

func combineAccuracy(a, b initAccuracy) initAccuracy {
69 70 71
	if a == unknownA || b == unknownA {
		return unknownA
	}
72 73 74 75 76 77 78 79 80
	if a == timedoutA || b == timedoutA {
		return timedoutA
	}
	if a == approxA || b == approxA {
		return approxA
	}
	if a == exactA && b == exactA {
		return exactA
	}
81 82 83 84 85 86 87
	if a == "" {
		return b
	}
	if b == "" {
		return a
	}
	return unknownA
88 89
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
var _ datastore.Datastore = (*Datastore)(nil)

var (
	ErrDatastoreExists       = errors.New("datastore already exists")
	ErrDatastoreDoesNotExist = errors.New("datastore directory does not exist")
	ErrShardingFileMissing   = fmt.Errorf("%s file not found in datastore", SHARDING_FN)
)

func init() {
	rand.Seed(time.Now().UTC().UnixNano())
}

// Datastore implements the go-datastore Interface.
// Note this datastore cannot guarantee order of concurrent
// write operations to the same key. See the explanation in
// Put().
Tommi Virtanen's avatar
Tommi Virtanen committed
106
type Datastore struct {
107 108 109 110 111
	// atmoic operations should always be used with diskUsage.
	// Must be first in struct to ensure correct alignment
	// (see https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
	diskUsage int64

Tommi Virtanen's avatar
Tommi Virtanen committed
112
	path string
113

114 115
	shardStr string
	getDir   ShardFunc
Jeromy's avatar
Jeromy committed
116 117 118

	// sychronize all writes and directory changes for added safety
	sync bool
119

120 121 122
	// these values should only be used during internalization or
	// inside the checkpoint loop
	dirty       bool
123
	storedValue diskUsageValue
124

Kevin Atkinson's avatar
Kevin Atkinson committed
125 126
	checkpointCh chan struct{}
	done         chan struct{}
127 128 129 130

	// opMap handles concurrent write operations (put/delete)
	// to the same key
	opMap *opMap
Tommi Virtanen's avatar
Tommi Virtanen committed
131 132
}

133
type diskUsageValue struct {
134 135
	DiskUsage int64        `json:"diskUsage"`
	Accuracy  initAccuracy `json:"accuracy"`
136 137
}

138 139
type ShardFunc func(string) string

140
type opT int
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
// op wraps useful arguments of write operations
type op struct {
	typ  opT           // operation type
	key  datastore.Key // datastore key. Mandatory.
	tmp  string        // temp file path
	path string        // file path
	v    []byte        // value
}

type opMap struct {
	ops sync.Map
}

type opResult struct {
	mu      sync.RWMutex
	success bool

	opMap *opMap
	name  string
}

// Returns nil if there's nothing to do.
func (m *opMap) Begin(name string) *opResult {
	for {
		myOp := &opResult{opMap: m, name: name}
		myOp.mu.Lock()
		opIface, loaded := m.ops.LoadOrStore(name, myOp)
		if !loaded { // no one else doing ops with this key
			return myOp
		}

		op := opIface.(*opResult)
		// someone else doing ops with this key, wait for
		// the result
		op.mu.RLock()
		if op.success {
			return nil
		}

		// if we are here, we will retry the operation
	}
}

func (o *opResult) Finish(ok bool) {
	o.success = ok
	o.opMap.ops.Delete(o.name)
	o.mu.Unlock()
}
190

191
func Create(path string, fun *ShardIdV1) error {
192

kpcyrd's avatar
kpcyrd committed
193
	err := os.Mkdir(path, 0755)
194 195
	if err != nil && !os.IsExist(err) {
		return err
Tommi Virtanen's avatar
Tommi Virtanen committed
196 197
	}

198 199
	dsFun, err := ReadShardFunc(path)
	switch err {
Kevin Atkinson's avatar
Kevin Atkinson committed
200
	case ErrShardingFileMissing:
201 202 203 204 205
		isEmpty, err := DirIsEmpty(path)
		if err != nil {
			return err
		}
		if !isEmpty {
206
			return fmt.Errorf("directory missing %s file: %s", SHARDING_FN, path)
207 208 209
		}

		err = WriteShardFunc(path, fun)
210 211 212
		if err != nil {
			return err
		}
213 214
		err = WriteReadme(path, fun)
		return err
215
	case nil:
216
		if fun.String() != dsFun.String() {
217
			return fmt.Errorf("specified shard func '%s' does not match repo shard func '%s'",
218
				fun.String(), dsFun.String())
219
		}
Kevin Atkinson's avatar
Kevin Atkinson committed
220
		return ErrDatastoreExists
221
	default:
222
		return err
223
	}
224 225
}

226
func Open(path string, syncFiles bool) (*Datastore, error) {
227 228
	_, err := os.Stat(path)
	if os.IsNotExist(err) {
Kevin Atkinson's avatar
Kevin Atkinson committed
229
		return nil, ErrDatastoreDoesNotExist
230 231 232 233
	} else if err != nil {
		return nil, err
	}

234
	shardId, err := ReadShardFunc(path)
235
	if err != nil {
236
		return nil, err
237 238
	}

239
	fs := &Datastore{
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
		path:      path,
		shardStr:  shardId.String(),
		getDir:    shardId.Func(),
		sync:      syncFiles,
		diskUsage: 0,
		opMap:     new(opMap),
	}

	// This sets diskUsage to the correct value
	// It might be slow, but allowing it to happen
	// while the datastore is usable might
	// cause diskUsage to not be accurate.
	err = fs.calculateDiskUsage()
	if err != nil {
		// Cannot stat() all
		// elements in the datastore.
		return nil, err
257
	}
258

Kevin Atkinson's avatar
Kevin Atkinson committed
259 260 261
	fs.checkpointCh = make(chan struct{}, 1)
	fs.done = make(chan struct{})
	go fs.checkpointLoop()
262 263 264
	return fs, nil
}

265
// convenience method
266
func CreateOrOpen(path string, fun *ShardIdV1, sync bool) (*Datastore, error) {
267
	err := Create(path, fun)
Kevin Atkinson's avatar
Kevin Atkinson committed
268
	if err != nil && err != ErrDatastoreExists {
269 270 271 272 273
		return nil, err
	}
	return Open(path, sync)
}

274 275
func (fs *Datastore) ShardStr() string {
	return fs.shardStr
276 277
}

Tommi Virtanen's avatar
Tommi Virtanen committed
278
func (fs *Datastore) encode(key datastore.Key) (dir, file string) {
279
	noslash := key.String()[1:]
Jeromy's avatar
Jeromy committed
280 281
	dir = filepath.Join(fs.path, fs.getDir(noslash))
	file = filepath.Join(dir, noslash+extension)
Tommi Virtanen's avatar
Tommi Virtanen committed
282 283 284
	return dir, file
}

285
func (fs *Datastore) decode(file string) (key datastore.Key, ok bool) {
Jeromy's avatar
Jeromy committed
286
	if filepath.Ext(file) != extension {
287 288 289
		return datastore.Key{}, false
	}
	name := file[:len(file)-len(extension)]
Jeromy's avatar
Jeromy committed
290
	return datastore.NewKey(name), true
291 292
}

293 294
func (fs *Datastore) makeDir(dir string) error {
	if err := fs.makeDirNoSync(dir); err != nil {
Jeromy's avatar
Jeromy committed
295
		return err
296 297 298 299 300 301
	}

	// In theory, if we create a new prefix dir and add a file to
	// it, the creation of the prefix dir itself might not be
	// durable yet. Sync the root dir after a successful mkdir of
	// a prefix dir, just to be paranoid.
Jeromy's avatar
Jeromy committed
302 303 304 305
	if fs.sync {
		if err := syncDir(fs.path); err != nil {
			return err
		}
306 307 308 309
	}
	return nil
}

310
func (fs *Datastore) makeDirNoSync(dir string) error {
kpcyrd's avatar
kpcyrd committed
311
	if err := os.Mkdir(dir, 0755); err != nil {
Jeromy's avatar
Jeromy committed
312 313 314 315 316
		// EEXIST is safe to ignore here, that just means the prefix
		// directory already existed.
		if !os.IsExist(err) {
			return err
		}
317
		return nil
Jeromy's avatar
Jeromy committed
318
	}
319 320 321

	// Track DiskUsage of this NEW folder
	fs.updateDiskUsage(dir, true)
Jeromy's avatar
Jeromy committed
322 323 324
	return nil
}

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
// This function always runs under an opLock. Therefore, only one thread is
// touching the affected files.
func (fs *Datastore) renameAndUpdateDiskUsage(tmpPath, path string) error {
	fi, err := os.Stat(path)

	// Destination exists, we need to discount it from diskUsage
	if fs != nil && err == nil {
		atomic.AddInt64(&fs.diskUsage, -fi.Size())
	} else if !os.IsNotExist(err) {
		return err
	}

	// Rename and add new file's diskUsage. If the rename fails,
	// it will either a) Re-add the size of an existing file, which
	// was sustracted before b) Add 0 if there is no existing file.
340
	err = os.Rename(tmpPath, path)
341 342 343 344
	fs.updateDiskUsage(path, true)
	return err
}

Jeromy's avatar
Jeromy committed
345
var putMaxRetries = 6
346

347 348 349 350 351 352 353 354 355 356
// Put stores a key/value in the datastore.
//
// Note, that we do not guarantee order of write operations (Put or Delete)
// to the same key in this datastore.
//
// For example. i.e. in the case of two concurrent Put, we only guarantee
// that one of them will come through, but cannot assure which one even if
// one arrived slightly later than the other. In the case of a
// concurrent Put and a Delete operation, we cannot guarantee which one
// will win.
357
func (fs *Datastore) Put(key datastore.Key, value []byte) error {
358
	var err error
Jeromy's avatar
Jeromy committed
359
	for i := 1; i <= putMaxRetries; i++ {
360 361 362
		err = fs.doWriteOp(&op{
			typ: opPut,
			key: key,
363
			v:   value,
364
		})
365
		if err == nil {
Jeromy's avatar
Jeromy committed
366
			break
367 368 369
		}

		if !strings.Contains(err.Error(), "too many open files") {
Jeromy's avatar
Jeromy committed
370
			break
371 372
		}

Or Rikon's avatar
Or Rikon committed
373
		log.Errorf("too many open files, retrying in %dms", 100*i)
Jeromy's avatar
Jeromy committed
374
		time.Sleep(time.Millisecond * 100 * time.Duration(i))
375 376 377 378
	}
	return err
}

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
func (fs *Datastore) doOp(oper *op) error {
	switch oper.typ {
	case opPut:
		return fs.doPut(oper.key, oper.v)
	case opDelete:
		return fs.doDelete(oper.key)
	case opRename:
		return fs.renameAndUpdateDiskUsage(oper.tmp, oper.path)
	default:
		panic("bad operation, this is a bug")
	}
}

// doWrite optmizes out write operations (put/delete) to the same
// key by queueing them and suceeding all queued
// operations if one of them does. In such case,
// we assume that the first suceeding operation
// on that key was the last one to happen after
// all successful others.
func (fs *Datastore) doWriteOp(oper *op) error {
	keyStr := oper.key.String()

	opRes := fs.opMap.Begin(keyStr)
	if opRes == nil { // nothing to do, a concurrent op succeeded
		return nil
	}

	// Do the operation
	err := fs.doOp(oper)

	// Finish it. If no error, it will signal other operations
	// waiting on this result to succeed. Otherwise, they will
	// retry.
	opRes.Finish(err == nil)
	return err
}

416
func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
417

Tommi Virtanen's avatar
Tommi Virtanen committed
418
	dir, path := fs.encode(key)
419
	if err := fs.makeDir(dir); err != nil {
420
		return err
Tommi Virtanen's avatar
Tommi Virtanen committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	}

	tmp, err := ioutil.TempFile(dir, "put-")
	if err != nil {
		return err
	}
	closed := false
	removed := false
	defer func() {
		if !closed {
			// silence errcheck
			_ = tmp.Close()
		}
		if !removed {
			// silence errcheck
			_ = os.Remove(tmp.Name())
		}
	}()

	if _, err := tmp.Write(val); err != nil {
		return err
	}
Jeromy's avatar
Jeromy committed
443
	if fs.sync {
444
		if err := syncFile(tmp); err != nil {
Jeromy's avatar
Jeromy committed
445 446
			return err
		}
447
	}
Tommi Virtanen's avatar
Tommi Virtanen committed
448 449 450 451 452
	if err := tmp.Close(); err != nil {
		return err
	}
	closed = true

453
	err = fs.renameAndUpdateDiskUsage(tmp.Name(), path)
Tommi Virtanen's avatar
Tommi Virtanen committed
454 455 456 457 458
	if err != nil {
		return err
	}
	removed = true

Jeromy's avatar
Jeromy committed
459 460 461 462
	if fs.sync {
		if err := syncDir(dir); err != nil {
			return err
		}
463
	}
Tommi Virtanen's avatar
Tommi Virtanen committed
464 465 466
	return nil
}

Jeromy's avatar
Jeromy committed
467 468
func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
	var dirsToSync []string
469
	files := make(map[*os.File]*op)
Jeromy's avatar
Jeromy committed
470 471 472 473 474 475 476

	for key, value := range data {
		val, ok := value.([]byte)
		if !ok {
			return datastore.ErrInvalidType
		}
		dir, path := fs.encode(key)
477
		if err := fs.makeDirNoSync(dir); err != nil {
Jeromy's avatar
Jeromy committed
478 479 480 481 482 483 484 485 486 487 488 489 490
			return err
		}
		dirsToSync = append(dirsToSync, dir)

		tmp, err := ioutil.TempFile(dir, "put-")
		if err != nil {
			return err
		}

		if _, err := tmp.Write(val); err != nil {
			return err
		}

491 492 493 494 495 496
		files[tmp] = &op{
			typ:  opRename,
			path: path,
			tmp:  tmp.Name(),
			key:  key,
		}
Jeromy's avatar
Jeromy committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	}

	ops := make(map[*os.File]int)

	defer func() {
		for fi, _ := range files {
			val, _ := ops[fi]
			switch val {
			case 0:
				_ = fi.Close()
				fallthrough
			case 1:
				_ = os.Remove(fi.Name())
			}
		}
	}()

	// Now we sync everything
	// sync and close files
	for fi, _ := range files {
Jeromy's avatar
Jeromy committed
517
		if fs.sync {
518
			if err := syncFile(fi); err != nil {
Jeromy's avatar
Jeromy committed
519 520
				return err
			}
Jeromy's avatar
Jeromy committed
521 522 523 524 525 526 527 528 529 530 531
		}

		if err := fi.Close(); err != nil {
			return err
		}

		// signify closed
		ops[fi] = 1
	}

	// move files to their proper places
532 533
	for fi, op := range files {
		fs.doWriteOp(op)
Jeromy's avatar
Jeromy committed
534 535 536 537 538
		// signify removed
		ops[fi] = 2
	}

	// now sync the dirs for those files
Jeromy's avatar
Jeromy committed
539 540 541 542 543
	if fs.sync {
		for _, dir := range dirsToSync {
			if err := syncDir(dir); err != nil {
				return err
			}
Jeromy's avatar
Jeromy committed
544 545
		}

Jeromy's avatar
Jeromy committed
546 547 548 549
		// sync top flatfs dir
		if err := syncDir(fs.path); err != nil {
			return err
		}
Jeromy's avatar
Jeromy committed
550 551 552 553 554
	}

	return nil
}

555
func (fs *Datastore) Get(key datastore.Key) (value []byte, err error) {
Tommi Virtanen's avatar
Tommi Virtanen committed
556 557 558 559 560 561 562 563 564 565 566 567 568
	_, path := fs.encode(key)
	data, err := ioutil.ReadFile(path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, datastore.ErrNotFound
		}
		// no specific error to return, so just pass it through
		return nil, err
	}
	return data, nil
}

func (fs *Datastore) Has(key datastore.Key) (exists bool, err error) {
Tommi Virtanen's avatar
Tommi Virtanen committed
569 570 571 572 573 574 575 576 577
	_, path := fs.encode(key)
	switch _, err := os.Stat(path); {
	case err == nil:
		return true, nil
	case os.IsNotExist(err):
		return false, nil
	default:
		return false, err
	}
Tommi Virtanen's avatar
Tommi Virtanen committed
578 579
}

Steven Allen's avatar
Steven Allen committed
580 581 582 583 584 585 586 587 588 589 590 591
func (fs *Datastore) GetSize(key datastore.Key) (size int, err error) {
	_, path := fs.encode(key)
	switch s, err := os.Stat(path); {
	case err == nil:
		return int(s.Size()), nil
	case os.IsNotExist(err):
		return -1, datastore.ErrNotFound
	default:
		return -1, err
	}
}

592 593 594
// Delete removes a key/value from the Datastore. Please read
// the Put() explanation about the handling of concurrent write
// operations to the same key.
Tommi Virtanen's avatar
Tommi Virtanen committed
595
func (fs *Datastore) Delete(key datastore.Key) error {
596 597 598 599 600 601 602 603 604 605
	return fs.doWriteOp(&op{
		typ: opDelete,
		key: key,
		v:   nil,
	})
}

// This function always runs within an opLock for the given
// key, and not concurrently.
func (fs *Datastore) doDelete(key datastore.Key) error {
Tommi Virtanen's avatar
Tommi Virtanen committed
606
	_, path := fs.encode(key)
607 608 609

	fSize := fileSize(path)

Tommi Virtanen's avatar
Tommi Virtanen committed
610 611
	switch err := os.Remove(path); {
	case err == nil:
612 613
		atomic.AddInt64(&fs.diskUsage, -fSize)
		fs.checkpointDiskUsage()
Tommi Virtanen's avatar
Tommi Virtanen committed
614 615 616 617 618 619
		return nil
	case os.IsNotExist(err):
		return datastore.ErrNotFound
	default:
		return err
	}
Tommi Virtanen's avatar
Tommi Virtanen committed
620 621 622
}

func (fs *Datastore) Query(q query.Query) (query.Results, error) {
623 624 625 626 627 628 629 630 631 632 633
	if (q.Prefix != "" && q.Prefix != "/") ||
		len(q.Filters) > 0 ||
		len(q.Orders) > 0 ||
		q.Limit > 0 ||
		q.Offset > 0 ||
		!q.KeysOnly {
		// TODO this is overly simplistic, but the only caller is
		// `ipfs refs local` for now, and this gets us moving.
		return nil, errors.New("flatfs only supports listing all keys in random order")
	}

634
	reschan := make(chan query.Result, query.KeysOnlyBufSize)
Jeromy's avatar
Jeromy committed
635 636
	go func() {
		defer close(reschan)
637
		err := fs.walkTopLevel(fs.path, reschan)
Jeromy's avatar
Jeromy committed
638
		if err != nil {
639
			reschan <- query.Result{Error: errors.New("walk failed: " + err.Error())}
640
		}
Jeromy's avatar
Jeromy committed
641 642
	}()
	return query.ResultsWithChan(q, reschan), nil
Tommi Virtanen's avatar
Tommi Virtanen committed
643 644
}

645 646 647 648 649
func (fs *Datastore) walkTopLevel(path string, reschan chan query.Result) error {
	dir, err := os.Open(path)
	if err != nil {
		return err
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
650
	defer dir.Close()
651 652 653 654 655
	names, err := dir.Readdirnames(-1)
	if err != nil {
		return err
	}
	for _, dir := range names {
Kevin Atkinson's avatar
Kevin Atkinson committed
656 657 658 659 660

		if len(dir) == 0 || dir[0] == '.' {
			continue
		}

661 662 663 664
		err = fs.walk(filepath.Join(path, dir), reschan)
		if err != nil {
			return err
		}
Kevin Atkinson's avatar
Kevin Atkinson committed
665

666 667 668 669
	}
	return nil
}

670 671 672
// folderSize estimates the diskUsage of a folder by reading
// up to DiskUsageFilesAverage entries in it and assumming any
// other files will have an avereage size.
673
func folderSize(path string, deadline time.Time) (int64, initAccuracy, error) {
674 675 676 677
	var du int64

	folder, err := os.Open(path)
	if err != nil {
678
		return 0, "", err
679 680 681 682 683
	}
	defer folder.Close()

	stat, err := folder.Stat()
	if err != nil {
684
		return 0, "", err
685 686 687 688
	}

	files, err := folder.Readdirnames(-1)
	if err != nil {
689
		return 0, "", err
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
	}

	totalFiles := len(files)
	i := 0
	filesProcessed := 0
	maxFiles := DiskUsageFilesAverage
	if maxFiles <= 0 {
		maxFiles = totalFiles
	}

	// randomize file order
	// https://stackoverflow.com/a/42776696
	for i := len(files) - 1; i > 0; i-- {
		j := rand.Intn(i + 1)
		files[i], files[j] = files[j], files[i]
	}

707
	accuracy := exactA
708
	for {
709 710 711
		// Do not process any files after deadline is over
		if time.Now().After(deadline) {
			accuracy = timedoutA
712 713 714
			break
		}

715 716 717 718
		if i >= totalFiles || filesProcessed >= maxFiles {
			if filesProcessed >= maxFiles {
				accuracy = approxA
			}
719 720 721 722 723 724 725 726
			break
		}

		// Stat the file
		fname := files[i]
		subpath := filepath.Join(path, fname)
		st, err := os.Stat(subpath)
		if err != nil {
727
			return 0, "", err
728 729 730 731
		}

		// Find folder size recursively
		if st.IsDir() {
732
			du2, acc, err := folderSize(filepath.Join(subpath), deadline)
733
			if err != nil {
734
				return 0, "", err
735
			}
736
			accuracy = combineAccuracy(acc, accuracy)
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
			du += du2
			filesProcessed++
		} else { // in any other case, add the file size
			du += st.Size()
			filesProcessed++
		}

		i++
	}

	nonProcessed := totalFiles - filesProcessed

	// Avg is total size in this folder up to now / total files processed
	// it includes folders ant not folders
	avg := 0.0
	if filesProcessed > 0 {
		avg = float64(du) / float64(filesProcessed)
	}
	duEstimation := int64(avg * float64(nonProcessed))
	du += duEstimation
	du += stat.Size()
	//fmt.Println(path, "total:", totalFiles, "totalStat:", i, "totalFile:", filesProcessed, "left:", nonProcessed, "avg:", int(avg), "est:", int(duEstimation), "du:", du)
759
	return du, accuracy, nil
760 761 762 763
}

// calculateDiskUsage tries to read the DiskUsageFile for a cached
// diskUsage value, otherwise walks the datastore files.
764
// it is only safe to call in Open()
765 766 767
func (fs *Datastore) calculateDiskUsage() error {
	// Try to obtain a previously stored value from disk
	if persDu := fs.readDiskUsageFile(); persDu > 0 {
768
		fs.diskUsage = persDu
769 770 771
		return nil
	}

772 773 774 775 776 777 778
	msgDone := make(chan struct{}, 1) // prevent race condition
	msgTimer := time.AfterFunc(diskUsageMessageTimeout, func() {
		fmt.Printf("Calculating datastore size. This might take %s at most and will happen only once\n",
			DiskUsageCalcTimeout.String())
		msgDone <- struct{}{}
	})
	defer msgTimer.Stop()
779
	deadline := time.Now().Add(DiskUsageCalcTimeout)
780
	du, accuracy, err := folderSize(fs.path, deadline)
781 782 783
	if err != nil {
		return err
	}
784 785 786
	if !msgTimer.Stop() {
		<-msgDone
	}
787
	if accuracy == timedoutA {
788 789 790 791 792 793 794
		fmt.Println("WARN: It took to long to calculate the datastore size")
		fmt.Printf("WARN: The total size (%d) is an estimation. You can fix errors by\n", du)
		fmt.Printf("WARN: replacing the %s file with the right disk usage in bytes and\n",
			filepath.Join(fs.path, DiskUsageFile))
		fmt.Println("WARN: re-opening the datastore")
	}

795
	fs.storedValue.Accuracy = accuracy
796
	fs.diskUsage = du
797
	fs.writeDiskUsageFile(du, true)
798

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
	return nil
}

func fileSize(path string) int64 {
	fi, err := os.Stat(path)
	if err != nil {
		return 0
	}
	return fi.Size()
}

// updateDiskUsage reads the size of path and atomically
// increases or decreases the diskUsage variable.
// setting add to false will subtract from disk usage.
func (fs *Datastore) updateDiskUsage(path string, add bool) {
	fsize := fileSize(path)
	if !add {
		fsize = -fsize
	}

	if fsize != 0 {
820 821
		atomic.AddInt64(&fs.diskUsage, fsize)
		fs.checkpointDiskUsage()
822 823 824
	}
}

825
func (fs *Datastore) checkpointDiskUsage() {
826
	select {
Kevin Atkinson's avatar
Kevin Atkinson committed
827
	case fs.checkpointCh <- struct{}{}:
Kevin Atkinson's avatar
Tweaks.  
Kevin Atkinson committed
828
		// msg sent
829 830
	default:
		// checkpoint request already pending
831 832 833
	}
}

834
func (fs *Datastore) checkpointLoop() {
835 836
	timerActive := true
	timer := time.NewTimer(0)
Kevin Atkinson's avatar
Tweaks.  
Kevin Atkinson committed
837
	defer timer.Stop()
838
	for {
839 840 841
		select {
		case _, more := <-fs.checkpointCh:
			du := atomic.LoadInt64(&fs.diskUsage)
Kevin Atkinson's avatar
Tweaks.  
Kevin Atkinson committed
842
			fs.dirty = true
843
			if !more { // shutting down
844
				fs.writeDiskUsageFile(du, true)
845
				if fs.dirty {
Kevin Atkinson's avatar
Tweaks.  
Kevin Atkinson committed
846
					log.Errorf("could not store final value of disk usage to file, future estimates may be inaccurate")
847
				}
Kevin Atkinson's avatar
Kevin Atkinson committed
848
				fs.done <- struct{}{}
849 850 851 852 853
				return
			}
			// If the difference between the checkpointed disk usage and
			// current one is larger than than `diskUsageCheckpointPercent`
			// of the checkpointed: store it.
854
			newDu := float64(du)
855
			lastCheckpointDu := float64(fs.storedValue.DiskUsage)
856
			diff := math.Abs(newDu - lastCheckpointDu)
857 858
			if lastCheckpointDu*diskUsageCheckpointPercent < diff*100.0 {
				fs.writeDiskUsageFile(du, false)
859
			}
860 861 862 863
			// Otherwise insure the value will be written to disk after
			// `diskUsageCheckpointTimeout`
			if fs.dirty && !timerActive {
				timer.Reset(diskUsageCheckpointTimeout)
Kevin Atkinson's avatar
Kevin Atkinson committed
864
				timerActive = true
865 866 867 868 869
			}
		case <-timer.C:
			timerActive = false
			if fs.dirty {
				du := atomic.LoadInt64(&fs.diskUsage)
870
				fs.writeDiskUsageFile(du, false)
871
			}
872
		}
873
	}
874 875
}

876
func (fs *Datastore) writeDiskUsageFile(du int64, doSync bool) {
877
	tmp, err := ioutil.TempFile(fs.path, "du-")
878 879 880 881 882
	if err != nil {
		log.Warningf("cound not write disk usage: %v", err)
		return
	}

883 884 885 886 887 888 889
	removed := false
	defer func() {
		if !removed {
			// silence errcheck
			_ = os.Remove(tmp.Name())
		}
	}()
890

891 892
	toWrite := fs.storedValue
	toWrite.DiskUsage = du
893
	encoder := json.NewEncoder(tmp)
894
	if err := encoder.Encode(&toWrite); err != nil {
895
		log.Warningf("cound not write disk usage: %v", err)
896 897
		return
	}
898

899 900 901 902 903 904 905
	if doSync {
		if err := tmp.Sync(); err != nil {
			log.Warningf("cound not sync %s: %v", DiskUsageFile, err)
			return
		}
	}

906
	if err := tmp.Close(); err != nil {
907
		log.Warningf("cound not write disk usage: %v", err)
908 909 910
		return
	}

911
	if err := os.Rename(tmp.Name(), filepath.Join(fs.path, DiskUsageFile)); err != nil {
912
		log.Warningf("cound not write disk usage: %v", err)
913
		return
914
	}
915
	removed = true
916

917
	fs.storedValue = toWrite
918
	fs.dirty = false
919 920
}

921
// readDiskUsageFile is only safe to call in Open()
922 923 924 925 926 927
func (fs *Datastore) readDiskUsageFile() int64 {
	fpath := filepath.Join(fs.path, DiskUsageFile)
	duB, err := ioutil.ReadFile(fpath)
	if err != nil {
		return 0
	}
928
	err = json.Unmarshal(duB, &fs.storedValue)
929 930 931
	if err != nil {
		return 0
	}
932
	return fs.storedValue.DiskUsage
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
}

// DiskUsage implements the PersistentDatastore interface
// and returns the current disk usage in bytes used by
// this datastore.
//
// The size is approximative and may slightly differ from
// the real disk values.
func (fs *Datastore) DiskUsage() (uint64, error) {
	// it may differ from real disk values if
	// the filesystem has allocated for blocks
	// for a directory because it has many files in it
	// we don't account for "resized" directories.
	// In a large datastore, the differences should be
	// are negligible though.

	du := atomic.LoadInt64(&fs.diskUsage)
	return uint64(du), nil
}

953 954 955 956
// Accuracy returns a string representing the accuracy of the
// DiskUsage() result, the value returned is implementation defined
// and for informational purposes only
func (fs *Datastore) Accuracy() string {
957
	return string(fs.storedValue.Accuracy)
958 959
}

960 961 962
func (fs *Datastore) walk(path string, reschan chan query.Result) error {
	dir, err := os.Open(path)
	if err != nil {
963 964 965 966
		if os.IsNotExist(err) {
			// not an error if the file disappeared
			return nil
		}
967 968
		return err
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
969
	defer dir.Close()
970 971 972 973 974 975 976 977 978 979

	// ignore non-directories
	fileInfo, err := dir.Stat()
	if err != nil {
		return err
	}
	if !fileInfo.IsDir() {
		return nil
	}

980 981 982 983 984 985 986 987 988 989 990 991
	names, err := dir.Readdirnames(-1)
	if err != nil {
		return err
	}
	for _, fn := range names {

		if len(fn) == 0 || fn[0] == '.' {
			continue
		}

		key, ok := fs.decode(fn)
		if !ok {
992
			log.Warningf("failed to decode flatfs entry: %s", fn)
993 994 995 996 997 998 999 1000 1001 1002 1003 1004
			continue
		}

		reschan <- query.Result{
			Entry: query.Entry{
				Key: key.String(),
			},
		}
	}
	return nil
}

1005 1006 1007 1008 1009 1010 1011 1012 1013
// Deactivate closes background maintenance threads, most write
// operations will fail but readonly operations will continue to
// function
func (fs *Datastore) deactivate() error {
	if fs.checkpointCh != nil {
		close(fs.checkpointCh)
		<-fs.done
		fs.checkpointCh = nil
	}
Jeromy's avatar
Jeromy committed
1014 1015 1016
	return nil
}

1017 1018 1019 1020
func (fs *Datastore) Close() error {
	return fs.deactivate()
}

Jeromy's avatar
Jeromy committed
1021
type flatfsBatch struct {
Jeromy's avatar
Jeromy committed
1022 1023 1024 1025 1026 1027
	puts    map[datastore.Key]interface{}
	deletes map[datastore.Key]struct{}

	ds *Datastore
}

Jeromy's avatar
Jeromy committed
1028
func (fs *Datastore) Batch() (datastore.Batch, error) {
Jeromy's avatar
Jeromy committed
1029
	return &flatfsBatch{
Jeromy's avatar
Jeromy committed
1030 1031 1032
		puts:    make(map[datastore.Key]interface{}),
		deletes: make(map[datastore.Key]struct{}),
		ds:      fs,
Jeromy's avatar
Jeromy committed
1033
	}, nil
Jeromy's avatar
Jeromy committed
1034 1035
}

1036
func (bt *flatfsBatch) Put(key datastore.Key, val []byte) error {
Jeromy's avatar
Jeromy committed
1037 1038 1039 1040
	bt.puts[key] = val
	return nil
}

Jeromy's avatar
Jeromy committed
1041
func (bt *flatfsBatch) Delete(key datastore.Key) error {
Jeromy's avatar
Jeromy committed
1042 1043 1044 1045
	bt.deletes[key] = struct{}{}
	return nil
}

Jeromy's avatar
Jeromy committed
1046
func (bt *flatfsBatch) Commit() error {
Jeromy's avatar
Jeromy committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
	if err := bt.ds.putMany(bt.puts); err != nil {
		return err
	}

	for k, _ := range bt.deletes {
		if err := bt.ds.Delete(k); err != nil {
			return err
		}
	}

	return nil
}

Tommi Virtanen's avatar
Tommi Virtanen committed
1060 1061 1062
var _ datastore.ThreadSafeDatastore = (*Datastore)(nil)

func (*Datastore) IsThreadSafe() {}