Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit 69d6ab0

Browse files
committed
fix sync logic
1 parent 57cf313 commit 69d6ab0

File tree

4 files changed

+51
-42
lines changed

4 files changed

+51
-42
lines changed

handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (cr *Cluster) getRecordMiddleWare() utils.MiddleWareFunc {
212212
case <-updateTicker.C:
213213
cr.stats.Lock()
214214

215-
log.Infof("Served %d requests, total responsed body = %s, total used CPU time = %.2fs",
215+
log.Infof("Served %d requests, total responsed body = %s, total IO waiting time = %.2fs",
216216
total, utils.BytesToUnit(totalBytes), totalUsed)
217217
for ua, v := range uas {
218218
if ua == "" {

lang/en/us.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,20 @@ var areaUS = map[string]string{
5858
"error.check.open.failed": "Cannot open %q: %v",
5959
"error.check.hash.failed": "Cannot calculate hash for %s: %v",
6060

61-
"info.sync.prepare": "Preparing to sync files, length of filelist is %d ...",
62-
"hint.sync.start": "Starting sync files, count: %d, bytes: %s",
63-
"hint.sync.done": "All files were synchronized, use time: %v, %s/s",
64-
"error.sync.failed": "File sync failed: %v",
65-
"info.sync.none": "All files were synchronized",
66-
"warn.sync.interrupted": "File sync interrupted",
67-
"info.sync.config": "Sync config: %#v",
68-
"hint.sync.total": "Total: ",
69-
"hint.sync.downloading": "> Downloading ",
70-
"hint.sync.downloading.handler": "Downloading %s from handler",
71-
"info.sync.downloaded": "Downloaded %s [%s] %.2f%%",
72-
"error.sync.download.failed": "Download error %s:\n\t%s",
73-
"error.sync.create.failed": "Cannot create %s/%s: %v",
61+
"info.sync.prepare": "Preparing to sync files, length of filelist is %d ...",
62+
"hint.sync.start": "Starting sync files, count: %d, bytes: %s",
63+
"hint.sync.done": "All files were synchronized, use time: %v, %s/s",
64+
"error.sync.failed": "File sync failed: %v",
65+
"info.sync.none": "All files were synchronized",
66+
"warn.sync.interrupted": "File sync interrupted",
67+
"info.sync.config": "Sync config: %#v",
68+
"hint.sync.total": "Total: ",
69+
"hint.sync.downloading": "> Downloading ",
70+
"hint.sync.downloading.handler": "Downloading %s from handler",
71+
"info.sync.downloaded": "Downloaded %s [%s] %.2f%%",
72+
"error.sync.download.failed": "Download error %s:\n\t%s",
73+
"error.sync.download.failed.retry": "Download error %s, retry after %v:\n\t%s",
74+
"error.sync.create.failed": "Cannot create %s/%s: %v",
7475

7576
"info.gc.start": "Starting garbage collector for %s",
7677
"info.gc.done": "Garbage collect finished for %s",

lang/zh/cn.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,20 @@ var areaCN = map[string]string{
5858
"error.check.open.failed": "无法打开 %q: %v",
5959
"error.check.hash.failed": "无法为 %s 计算哈希值: %v",
6060

61-
"info.sync.prepare": "准备同步中, 文件列表长度为 %d ...",
62-
"hint.sync.start": "开始同步, 总计: %d, 字节: %s",
63-
"hint.sync.done": "文件同步完成, 用时: %v, %s/s",
64-
"error.sync.failed": "文件同步失败: %v",
65-
"info.sync.none": "所有文件已同步",
66-
"warn.sync.interrupted": "同步已中断",
67-
"info.sync.config": "同步配置: %#v",
68-
"hint.sync.total": "总计: ",
69-
"hint.sync.downloading": "> 下载中 ",
70-
"hint.sync.downloading.handler": "Downloading %s from handler",
71-
"info.sync.downloaded": "已下载 %s [%s] %.2f%%",
72-
"error.sync.download.failed": "下载失败 %s:\n\t%s",
73-
"error.sync.create.failed": "无法创建 %s/%s: %v",
61+
"info.sync.prepare": "准备同步中, 文件列表长度为 %d ...",
62+
"hint.sync.start": "开始同步, 总计: %d, 字节: %s",
63+
"hint.sync.done": "文件同步完成, 用时: %v, %s/s",
64+
"error.sync.failed": "文件同步失败: %v",
65+
"info.sync.none": "所有文件已同步",
66+
"warn.sync.interrupted": "同步已中断",
67+
"info.sync.config": "同步配置: %#v",
68+
"hint.sync.total": "总计: ",
69+
"hint.sync.downloading": "> 下载中 ",
70+
"hint.sync.downloading.handler": "Downloading %s from handler",
71+
"info.sync.downloaded": "已下载 %s [%s] %.2f%%",
72+
"error.sync.download.failed": "下载失败 %s:\n\t%s",
73+
"error.sync.download.failed.retry": "下载失败 %s, %v 后重新尝试:\n\t%s",
74+
"error.sync.create.failed": "无法创建 %s/%s: %v",
7475

7576
"info.gc.start": "正在清理 %s",
7677
"info.gc.done": "已清理 %s",

sync.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (cr *Cluster) checkFileFor(
267267
mpb.AppendDecorators(
268268
decor.CountersNoUnit("%d / %d", decor.WCSyncSpaceR),
269269
decor.NewPercentage("%d", decor.WCSyncSpaceR),
270-
decor.EwmaETA(decor.ET_STYLE_GO, 30),
270+
decor.EwmaETA(decor.ET_STYLE_GO, 60),
271271
),
272272
mpb.BarExtender((mpb.BarFillerFunc)(func(w io.Writer, _ decor.Statistics) (err error) {
273273
if checkingHashMux.TryLock() {
@@ -489,7 +489,7 @@ func (cr *Cluster) syncFiles(ctx context.Context, files []FileInfo, heavyCheck b
489489
var stats syncStats
490490
stats.pg = pg
491491
stats.noOpen = syncCfg.Source == "center"
492-
stats.slots = limited.NewBufSlots(syncCfg.Concurrency)
492+
stats.slots = limited.NewBufSlots(syncCfg.Concurrency + 1)
493493
stats.totalFiles = totalFiles
494494
for _, f := range missing {
495495
stats.totalSize += f.Size
@@ -669,15 +669,15 @@ func (cr *Cluster) fetchFile(ctx context.Context, stats *syncStats, f FileInfo)
669669
defer close(pathRes)
670670

671671
var barUnit decor.SizeB1024
672-
var trycount atomic.Int32
673-
trycount.Store(1)
672+
var tried atomic.Int32
673+
tried.Store(1)
674674
bar := stats.pg.AddBar(f.Size,
675675
mpb.BarRemoveOnComplete(),
676676
mpb.BarPriority(slotId),
677677
mpb.PrependDecorators(
678678
decor.Name(Tr("hint.sync.downloading")),
679679
decor.Any(func(decor.Statistics) string {
680-
tc := trycount.Load()
680+
tc := tried.Load()
681681
if tc <= 1 {
682682
return ""
683683
}
@@ -688,22 +688,23 @@ func (cr *Cluster) fetchFile(ctx context.Context, stats *syncStats, f FileInfo)
688688
mpb.AppendDecorators(
689689
decor.NewPercentage("%d", decor.WCSyncSpace),
690690
decor.Counters(barUnit, "[%.1f / %.1f]", decor.WCSyncSpace),
691-
decor.EwmaSpeed(barUnit, "%.1f", 10, decor.WCSyncSpace),
691+
decor.EwmaSpeed(barUnit, "%.1f", 30, decor.WCSyncSpace),
692692
decor.OnComplete(
693-
decor.EwmaETA(decor.ET_STYLE_GO, 10, decor.WCSyncSpace), "done",
693+
decor.EwmaETA(decor.ET_STYLE_GO, 30, decor.WCSyncSpace), "done",
694694
),
695695
),
696696
)
697697
defer bar.Abort(true)
698698

699699
noOpen := stats.noOpen
700+
badOpen := false
700701
interval := time.Second
701702
for {
702703
bar.SetCurrent(0)
703704
hashMethod, err := getHashMethod(len(f.Hash))
704705
if err == nil {
705706
var path string
706-
if path, err = cr.fetchFileWithBuf(ctx, f, hashMethod, buf, noOpen, func(r io.Reader) io.Reader {
707+
if path, err = cr.fetchFileWithBuf(ctx, f, hashMethod, buf, noOpen, badOpen, func(r io.Reader) io.Reader {
707708
return ProxyReader(r, bar, stats.totalBar, &stats.lastInc)
708709
}); err == nil {
709710
pathRes <- path
@@ -716,14 +717,15 @@ func (cr *Cluster) fetchFile(ctx context.Context, stats *syncStats, f FileInfo)
716717
}
717718
bar.SetRefill(bar.Current())
718719

719-
log.Errorf(Tr("error.sync.download.failed"), f.Path, err)
720-
c := trycount.Add(1)
720+
c := tried.Add(1)
721721
if c > maxRetryCount {
722+
log.Errorf(Tr("error.sync.download.failed"), f.Path, err)
722723
break
723724
}
724725
if c > maxTryWithOpen {
725-
noOpen = true
726+
badOpen = true
726727
}
728+
log.Errorf(Tr("error.sync.download.failed.retry"), f.Path, interval, err)
727729
select {
728730
case <-time.After(interval):
729731
interval *= 2
@@ -739,20 +741,25 @@ func (cr *Cluster) fetchFile(ctx context.Context, stats *syncStats, f FileInfo)
739741
func (cr *Cluster) fetchFileWithBuf(
740742
ctx context.Context, f FileInfo,
741743
hashMethod crypto.Hash, buf []byte,
742-
noOpen bool,
744+
noOpen bool, badOpen bool,
743745
wrapper func(io.Reader) io.Reader,
744746
) (path string, err error) {
745747
var (
746748
reqPath = f.Path
749+
query url.Values
747750
req *http.Request
748751
res *http.Response
749752
fd *os.File
750753
r io.Reader
751754
)
752-
if noOpen {
755+
if badOpen {
753756
reqPath = "/openbmclapi/download/" + f.Hash
757+
} else if noOpen {
758+
query = url.Values{
759+
"noopen": {"1"},
760+
}
754761
}
755-
if req, err = cr.makeReqWithAuth(ctx, http.MethodGet, reqPath, nil); err != nil {
762+
if req, err = cr.makeReqWithAuth(ctx, http.MethodGet, reqPath, query); err != nil {
756763
return
757764
}
758765
req.Header.Set("Accept-Encoding", "gzip, deflate")
@@ -904,7 +911,7 @@ func (cr *Cluster) DownloadFile(ctx context.Context, hash string) (err error) {
904911
}
905912
defer free()
906913

907-
path, err := cr.fetchFileWithBuf(ctx, f, hashMethod, buf, true, nil)
914+
path, err := cr.fetchFileWithBuf(ctx, f, hashMethod, buf, true, true, nil)
908915
if err != nil {
909916
return
910917
}

0 commit comments

Comments
 (0)