func (d *DiskStore) flushKey(key string, data []byte) error { var err error bgTime := stat.BeginStat() defer func() { stat.EndStat("Cache:Write:FlushData", err, bgTime, 1) }() cachePath := d.buildCachePath(key, d.dir) log.LogDebugf("TRACE BCacheService flushKey Enter. key(%v) cachePath(%v)",
(vol, key string, data []byte)
| 592 | //} |
| 593 | |
| 594 | func (d *DiskStore) flushKey(vol, key string, data []byte) error { |
| 595 | var err error |
| 596 | bgTime := stat.BeginStat() |
| 597 | metric := exporter.NewTPCnt("cacheToDisk") |
| 598 | defer func() { |
| 599 | stat.EndStat("Cache:Write:FlushData", err, bgTime, 1) |
| 600 | metric.SetWithLabels(err, map[string]string{exporter.Vol: vol}) |
| 601 | }() |
| 602 | cachePath := d.buildCachePath(key, d.dir) |
| 603 | info, err := os.Stat(cachePath) |
| 604 | // if already cached |
| 605 | if err == nil && info.Size() > 0 { |
| 606 | return nil |
| 607 | } |
| 608 | log.LogDebugf("TRACE BCacheService flushKey Enter. key(%v) cachePath(%v)", key, cachePath) |
| 609 | d.checkBuildCacheDir(filepath.Dir(cachePath)) |
| 610 | tmp := cachePath + ".tmp" |
| 611 | f, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(d.mode)) |
| 612 | defer os.Remove(tmp) |
| 613 | if err != nil { |
| 614 | log.LogWarnf("Create block tmp file:%s err:%s!", tmp, err) |
| 615 | return err |
| 616 | } |
| 617 | // encrypt |
| 618 | encryptXOR(data) |
| 619 | _, err = f.Write(data) |
| 620 | if err != nil { |
| 621 | f.Close() |
| 622 | log.LogErrorf("Write tmp failed: file %s err %s!", tmp, err) |
| 623 | return err |
| 624 | } |
| 625 | err = f.Close() |
| 626 | if err != nil { |
| 627 | log.LogErrorf("Close tmp failed: file:%s err:%s!", tmp, err) |
| 628 | return err |
| 629 | } |
| 630 | //info, err := os.Stat(cachePath) |
| 631 | ////if already cached |
| 632 | //if !os.IsNotExist(err) { |
| 633 | // atomic.AddInt64(&d.usedSize, -(info.Size())) |
| 634 | // atomic.AddInt64(&d.usedCount, -1) |
| 635 | // os.Remove(cachePath) |
| 636 | //} |
| 637 | err = os.Rename(tmp, cachePath) |
| 638 | if err != nil { |
| 639 | log.LogErrorf("Rename block tmp file:%s err:%s!", tmp, err) |
| 640 | return err |
| 641 | } |
| 642 | atomic.AddInt64(&d.usedSize, int64(len(data))) |
| 643 | atomic.AddInt64(&d.usedCount, 1) |
| 644 | log.LogDebugf("TRACE BCacheService flushKey Exit. key(%v) cachePath(%v)", key, cachePath) |
| 645 | return nil |
| 646 | } |
| 647 | |
| 648 | func (d *DiskStore) load(key string) (ReadCloser, error) { |
| 649 | cachePath := d.buildCachePath(key, d.dir) |
no test coverage detected