flatfs.go 21.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 30
	extension                  = ".data"
	diskUsageCheckpointPercent = 1.0
Tommi Virtanen's avatar
Tommi Virtanen committed
31 32
)

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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
)

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
type initAccuracy string

const (
	exactA    initAccuracy = "initial-exact"
	approxA   initAccuracy = "initial-approximate"
	timedoutA initAccuracy = "initial-timed-out"
)

func combineAccuracy(a, b initAccuracy) initAccuracy {
	if a == timedoutA || b == timedoutA {
		return timedoutA
	}
	if a == approxA || b == approxA {
		return approxA
	}
	if a == exactA && b == exactA {
		return exactA
	}
	return ""
}

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
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
94 95
type Datastore struct {
	path string
96

97 98
	shardStr string
	getDir   ShardFunc
Jeromy's avatar
Jeromy committed
99 100 101

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

103 104 105 106 107 108 109
	diskUsage   int64
	storedValue diskUsageValue
	// updateLock must be held when updating storedValue or writing
	// to a file, it doesn't need to be held when reading
	// checkpoint.DiskUsage atomically (or when the datastore is
	// initializing)
	updateLock sync.Mutex
110 111 112 113

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

116 117 118 119 120
type diskUsageValue struct {
	diskUsage int64
	accuracy  initAccuracy
}

121 122
type ShardFunc func(string) string

123
type opT int
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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
// 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()
}
173

174
func Create(path string, fun *ShardIdV1) error {
175

kpcyrd's avatar
kpcyrd committed
176
	err := os.Mkdir(path, 0755)
177 178
	if err != nil && !os.IsExist(err) {
		return err
Tommi Virtanen's avatar
Tommi Virtanen committed
179 180
	}

181 182
	dsFun, err := ReadShardFunc(path)
	switch err {
Kevin Atkinson's avatar
Kevin Atkinson committed
183
	case ErrShardingFileMissing:
184 185 186 187 188
		isEmpty, err := DirIsEmpty(path)
		if err != nil {
			return err
		}
		if !isEmpty {
189
			return fmt.Errorf("directory missing %s file: %s", SHARDING_FN, path)
190 191 192
		}

		err = WriteShardFunc(path, fun)
193 194 195
		if err != nil {
			return err
		}
196 197
		err = WriteReadme(path, fun)
		return err
198
	case nil:
199
		if fun.String() != dsFun.String() {
200
			return fmt.Errorf("specified shard func '%s' does not match repo shard func '%s'",
201
				fun.String(), dsFun.String())
202
		}
Kevin Atkinson's avatar
Kevin Atkinson committed
203
		return ErrDatastoreExists
204
	default:
205
		return err
206
	}
207 208
}

209
func Open(path string, syncFiles bool) (*Datastore, error) {
210 211
	_, err := os.Stat(path)
	if os.IsNotExist(err) {
Kevin Atkinson's avatar
Kevin Atkinson committed
212
		return nil, ErrDatastoreDoesNotExist
213 214 215 216
	} else if err != nil {
		return nil, err
	}

217
	shardId, err := ReadShardFunc(path)
218
	if err != nil {
219
		return nil, err
220 221
	}

222
	fs := &Datastore{
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
		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
240
	}
241 242 243
	return fs, nil
}

244
// convenience method
245
func CreateOrOpen(path string, fun *ShardIdV1, sync bool) (*Datastore, error) {
246
	err := Create(path, fun)
Kevin Atkinson's avatar
Kevin Atkinson committed
247
	if err != nil && err != ErrDatastoreExists {
248 249 250 251 252
		return nil, err
	}
	return Open(path, sync)
}

253 254
func (fs *Datastore) ShardStr() string {
	return fs.shardStr
255 256
}

Tommi Virtanen's avatar
Tommi Virtanen committed
257
func (fs *Datastore) encode(key datastore.Key) (dir, file string) {
258
	noslash := key.String()[1:]
Jeromy's avatar
Jeromy committed
259 260
	dir = filepath.Join(fs.path, fs.getDir(noslash))
	file = filepath.Join(dir, noslash+extension)
Tommi Virtanen's avatar
Tommi Virtanen committed
261 262 263
	return dir, file
}

264
func (fs *Datastore) decode(file string) (key datastore.Key, ok bool) {
Jeromy's avatar
Jeromy committed
265
	if filepath.Ext(file) != extension {
266 267 268
		return datastore.Key{}, false
	}
	name := file[:len(file)-len(extension)]
Jeromy's avatar
Jeromy committed
269
	return datastore.NewKey(name), true
270 271
}

272 273
func (fs *Datastore) makeDir(dir string) error {
	if err := fs.makeDirNoSync(dir); err != nil {
Jeromy's avatar
Jeromy committed
274
		return err
275 276 277 278 279 280
	}

	// 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
281 282 283 284
	if fs.sync {
		if err := syncDir(fs.path); err != nil {
			return err
		}
285 286 287 288
	}
	return nil
}

289
func (fs *Datastore) makeDirNoSync(dir string) error {
kpcyrd's avatar
kpcyrd committed
290
	if err := os.Mkdir(dir, 0755); err != nil {
Jeromy's avatar
Jeromy committed
291 292 293 294 295
		// EEXIST is safe to ignore here, that just means the prefix
		// directory already existed.
		if !os.IsExist(err) {
			return err
		}
296
		return nil
Jeromy's avatar
Jeromy committed
297
	}
298 299 300

	// Track DiskUsage of this NEW folder
	fs.updateDiskUsage(dir, true)
Jeromy's avatar
Jeromy committed
301 302 303
	return nil
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
// 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.
319
	err = os.Rename(tmpPath, path)
320 321 322 323
	fs.updateDiskUsage(path, true)
	return err
}

Jeromy's avatar
Jeromy committed
324
var putMaxRetries = 6
325

326 327 328 329 330 331 332 333 334 335
// 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.
Tommi Virtanen's avatar
Tommi Virtanen committed
336 337 338 339 340 341
func (fs *Datastore) Put(key datastore.Key, value interface{}) error {
	val, ok := value.([]byte)
	if !ok {
		return datastore.ErrInvalidType
	}

342
	var err error
Jeromy's avatar
Jeromy committed
343
	for i := 1; i <= putMaxRetries; i++ {
344 345 346 347 348
		err = fs.doWriteOp(&op{
			typ: opPut,
			key: key,
			v:   val,
		})
349
		if err == nil {
Jeromy's avatar
Jeromy committed
350
			break
351 352 353
		}

		if !strings.Contains(err.Error(), "too many open files") {
Jeromy's avatar
Jeromy committed
354
			break
355 356
		}

Or Rikon's avatar
Or Rikon committed
357
		log.Errorf("too many open files, retrying in %dms", 100*i)
Jeromy's avatar
Jeromy committed
358
		time.Sleep(time.Millisecond * 100 * time.Duration(i))
359 360 361 362
	}
	return err
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
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
}

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

Tommi Virtanen's avatar
Tommi Virtanen committed
402
	dir, path := fs.encode(key)
403
	if err := fs.makeDir(dir); err != nil {
404
		return err
Tommi Virtanen's avatar
Tommi Virtanen committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	}

	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
427
	if fs.sync {
428
		if err := syncFile(tmp); err != nil {
Jeromy's avatar
Jeromy committed
429 430
			return err
		}
431
	}
Tommi Virtanen's avatar
Tommi Virtanen committed
432 433 434 435 436
	if err := tmp.Close(); err != nil {
		return err
	}
	closed = true

437
	err = fs.renameAndUpdateDiskUsage(tmp.Name(), path)
Tommi Virtanen's avatar
Tommi Virtanen committed
438 439 440 441 442
	if err != nil {
		return err
	}
	removed = true

Jeromy's avatar
Jeromy committed
443 444 445 446
	if fs.sync {
		if err := syncDir(dir); err != nil {
			return err
		}
447
	}
Tommi Virtanen's avatar
Tommi Virtanen committed
448 449 450
	return nil
}

Jeromy's avatar
Jeromy committed
451 452
func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
	var dirsToSync []string
453
	files := make(map[*os.File]*op)
Jeromy's avatar
Jeromy committed
454 455 456 457 458 459 460

	for key, value := range data {
		val, ok := value.([]byte)
		if !ok {
			return datastore.ErrInvalidType
		}
		dir, path := fs.encode(key)
461
		if err := fs.makeDirNoSync(dir); err != nil {
Jeromy's avatar
Jeromy committed
462 463 464 465 466 467 468 469 470 471 472 473 474
			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
		}

475 476 477 478 479 480
		files[tmp] = &op{
			typ:  opRename,
			path: path,
			tmp:  tmp.Name(),
			key:  key,
		}
Jeromy's avatar
Jeromy committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	}

	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
501
		if fs.sync {
502
			if err := syncFile(fi); err != nil {
Jeromy's avatar
Jeromy committed
503 504
				return err
			}
Jeromy's avatar
Jeromy committed
505 506 507 508 509 510 511 512 513 514 515
		}

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

		// signify closed
		ops[fi] = 1
	}

	// move files to their proper places
516 517
	for fi, op := range files {
		fs.doWriteOp(op)
Jeromy's avatar
Jeromy committed
518 519 520 521 522
		// signify removed
		ops[fi] = 2
	}

	// now sync the dirs for those files
Jeromy's avatar
Jeromy committed
523 524 525 526 527
	if fs.sync {
		for _, dir := range dirsToSync {
			if err := syncDir(dir); err != nil {
				return err
			}
Jeromy's avatar
Jeromy committed
528 529
		}

Jeromy's avatar
Jeromy committed
530 531 532 533
		// sync top flatfs dir
		if err := syncDir(fs.path); err != nil {
			return err
		}
Jeromy's avatar
Jeromy committed
534 535 536 537 538
	}

	return nil
}

Tommi Virtanen's avatar
Tommi Virtanen committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552
func (fs *Datastore) Get(key datastore.Key) (value interface{}, err error) {
	_, 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
553 554 555 556 557 558 559 560 561
	_, 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
562 563
}

564 565 566
// 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
567
func (fs *Datastore) Delete(key datastore.Key) error {
568 569 570 571 572 573 574 575 576 577
	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
578
	_, path := fs.encode(key)
579 580 581

	fSize := fileSize(path)

Tommi Virtanen's avatar
Tommi Virtanen committed
582 583
	switch err := os.Remove(path); {
	case err == nil:
584 585
		newDu := atomic.AddInt64(&fs.diskUsage, -fSize)
		fs.checkpointDiskUsage(newDu)
Tommi Virtanen's avatar
Tommi Virtanen committed
586 587 588 589 590 591
		return nil
	case os.IsNotExist(err):
		return datastore.ErrNotFound
	default:
		return err
	}
Tommi Virtanen's avatar
Tommi Virtanen committed
592 593 594
}

func (fs *Datastore) Query(q query.Query) (query.Results, error) {
595 596 597 598 599 600 601 602 603 604 605
	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")
	}

606
	reschan := make(chan query.Result, query.KeysOnlyBufSize)
Jeromy's avatar
Jeromy committed
607 608
	go func() {
		defer close(reschan)
609
		err := fs.walkTopLevel(fs.path, reschan)
Jeromy's avatar
Jeromy committed
610
		if err != nil {
611
			reschan <- query.Result{Error: errors.New("walk failed: " + err.Error())}
612
		}
Jeromy's avatar
Jeromy committed
613 614
	}()
	return query.ResultsWithChan(q, reschan), nil
Tommi Virtanen's avatar
Tommi Virtanen committed
615 616
}

617 618 619 620 621
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
622
	defer dir.Close()
623 624 625 626 627
	names, err := dir.Readdirnames(-1)
	if err != nil {
		return err
	}
	for _, dir := range names {
Kevin Atkinson's avatar
Kevin Atkinson committed
628 629 630 631 632

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

633 634 635 636
		err = fs.walk(filepath.Join(path, dir), reschan)
		if err != nil {
			return err
		}
Kevin Atkinson's avatar
Kevin Atkinson committed
637

638 639 640 641
	}
	return nil
}

642 643 644
// 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.
645
func folderSize(path string, deadline time.Time) (int64, initAccuracy, error) {
646 647 648 649
	var du int64

	folder, err := os.Open(path)
	if err != nil {
650
		return 0, "", err
651 652 653 654 655
	}
	defer folder.Close()

	stat, err := folder.Stat()
	if err != nil {
656
		return 0, "", err
657 658 659 660
	}

	files, err := folder.Readdirnames(-1)
	if err != nil {
661
		return 0, "", err
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
	}

	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]
	}

679
	accuracy := exactA
680
	for {
681 682 683
		// Do not process any files after deadline is over
		if time.Now().After(deadline) {
			accuracy = timedoutA
684 685 686
			break
		}

687 688 689 690
		if i >= totalFiles || filesProcessed >= maxFiles {
			if filesProcessed >= maxFiles {
				accuracy = approxA
			}
691 692 693 694 695 696 697 698
			break
		}

		// Stat the file
		fname := files[i]
		subpath := filepath.Join(path, fname)
		st, err := os.Stat(subpath)
		if err != nil {
699
			return 0, "", err
700 701 702 703
		}

		// Find folder size recursively
		if st.IsDir() {
704
			du2, acc, err := folderSize(filepath.Join(subpath), deadline)
705
			if err != nil {
706
				return 0, "", err
707
			}
708
			accuracy = combineAccuracy(acc, accuracy)
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
			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)
731
	return du, accuracy, nil
732 733 734 735
}

// calculateDiskUsage tries to read the DiskUsageFile for a cached
// diskUsage value, otherwise walks the datastore files.
736
// it is only safe to call in Open()
737 738 739
func (fs *Datastore) calculateDiskUsage() error {
	// Try to obtain a previously stored value from disk
	if persDu := fs.readDiskUsageFile(); persDu > 0 {
740
		fs.diskUsage = persDu
741 742 743 744 745
		return nil
	}

	fmt.Printf("Calculating datastore size. This might take %s at most and will happen only once\n", DiskUsageCalcTimeout.String())
	deadline := time.Now().Add(DiskUsageCalcTimeout)
746
	du, accuracy, err := folderSize(fs.path, deadline)
747 748 749
	if err != nil {
		return err
	}
750
	if accuracy == timedoutA {
751 752 753 754 755 756 757
		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")
	}

758 759 760 761
	fs.storedValue.accuracy = accuracy
	fs.diskUsage = du
	fs.persistDiskUsageFile()

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
	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 {
		newDu := atomic.AddInt64(&fs.diskUsage, fsize)
		fs.checkpointDiskUsage(newDu)
	}
}

func (fs *Datastore) checkpointDiskUsage(newDuInt int64) {
	newDu := float64(newDuInt)
790
	lastCheckpointDu := float64(atomic.LoadInt64(&fs.storedValue.diskUsage))
791 792 793 794 795 796 797 798 799
	diff := math.Abs(newDu - lastCheckpointDu)

	// If the difference between the checkpointed disk usage and
	// current one is larger than than 1% of the checkpointed: store it.
	if (lastCheckpointDu * diskUsageCheckpointPercent / 100.0) < diff {
		fs.persistDiskUsageFile()
	}
}

800 801
// persistDiskUsageFile updates the diskusage file with the last known
// value
802
func (fs *Datastore) persistDiskUsageFile() {
803 804
	fs.updateLock.Lock()
	defer fs.updateLock.Unlock()
805

806 807 808 809 810 811 812
	du := atomic.LoadInt64(&fs.diskUsage)

	origVal := fs.storedValue.diskUsage
	// update the stored diskUsage value now to prevent unnecessary
	// calls to persistDiskUsageFile.  On error role back the value to
	// the original
	atomic.StoreInt64(&fs.storedValue.diskUsage, du)
813 814 815

	tmp, err := ioutil.TempFile(fs.path, "du-")
	if err != nil {
816
		atomic.StoreInt64(&fs.storedValue.diskUsage, origVal)
817 818
		return
	}
819 820 821 822

	encoder := json.NewEncoder(tmp)
	if err := encoder.Encode(&fs.storedValue); err != nil {
		atomic.StoreInt64(&fs.storedValue.diskUsage, origVal)
823 824
		return
	}
825

826
	if err := tmp.Close(); err != nil {
827
		atomic.StoreInt64(&fs.storedValue.diskUsage, origVal)
828 829 830
		return
	}

831 832 833
	if err := os.Rename(tmp.Name(), filepath.Join(fs.path, DiskUsageFile)); err != nil {
		atomic.StoreInt64(&fs.storedValue.diskUsage, origVal)
	}
834 835
}

836
// readDiskUsageFile is only safe to call in Open()
837 838 839 840 841 842
func (fs *Datastore) readDiskUsageFile() int64 {
	fpath := filepath.Join(fs.path, DiskUsageFile)
	duB, err := ioutil.ReadFile(fpath)
	if err != nil {
		return 0
	}
843
	err = json.Unmarshal(duB, &fs.storedValue)
844 845 846
	if err != nil {
		return 0
	}
847
	return fs.storedValue.diskUsage
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
}

// 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
}

868 869 870 871 872 873 874
// 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 {
	return string(fs.storedValue.accuracy)
}

875 876 877 878 879
func (fs *Datastore) walk(path string, reschan chan query.Result) error {
	dir, err := os.Open(path)
	if err != nil {
		return err
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
880
	defer dir.Close()
881 882 883 884 885 886 887 888 889 890

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

891 892 893 894 895 896 897 898 899 900 901 902
	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 {
903
			log.Warningf("failed to decode flatfs entry: %s", fn)
904 905 906 907 908 909 910 911 912 913 914 915
			continue
		}

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

Jeromy's avatar
Jeromy committed
916
func (fs *Datastore) Close() error {
917
	fs.persistDiskUsageFile()
918 919
	// FIXME: Should we check that the file was updated and if not
	// return an error
Jeromy's avatar
Jeromy committed
920 921 922
	return nil
}

Jeromy's avatar
Jeromy committed
923
type flatfsBatch struct {
Jeromy's avatar
Jeromy committed
924 925 926 927 928 929
	puts    map[datastore.Key]interface{}
	deletes map[datastore.Key]struct{}

	ds *Datastore
}

Jeromy's avatar
Jeromy committed
930
func (fs *Datastore) Batch() (datastore.Batch, error) {
Jeromy's avatar
Jeromy committed
931
	return &flatfsBatch{
Jeromy's avatar
Jeromy committed
932 933 934
		puts:    make(map[datastore.Key]interface{}),
		deletes: make(map[datastore.Key]struct{}),
		ds:      fs,
Jeromy's avatar
Jeromy committed
935
	}, nil
Jeromy's avatar
Jeromy committed
936 937
}

Jeromy's avatar
Jeromy committed
938
func (bt *flatfsBatch) Put(key datastore.Key, val interface{}) error {
Jeromy's avatar
Jeromy committed
939 940 941 942
	bt.puts[key] = val
	return nil
}

Jeromy's avatar
Jeromy committed
943
func (bt *flatfsBatch) Delete(key datastore.Key) error {
Jeromy's avatar
Jeromy committed
944 945 946 947
	bt.deletes[key] = struct{}{}
	return nil
}

Jeromy's avatar
Jeromy committed
948
func (bt *flatfsBatch) Commit() error {
Jeromy's avatar
Jeromy committed
949 950 951 952 953 954 955 956 957 958 959 960 961
	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
962 963 964
var _ datastore.ThreadSafeDatastore = (*Datastore)(nil)

func (*Datastore) IsThreadSafe() {}