Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vfs: write control file asynchronously #1747

Merged
merged 5 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@ func info(ctx *cli.Context) error {
if err != nil {
logger.Fatalf("write message: %s", err)
}

data := make([]byte, 4)
n, err := f.Read(data)
if err != nil {
logger.Fatalf("read size: %d %s", n, err)
}
n := readControl(f, data)
if n == 1 && data[0] == byte(syscall.EINVAL&0xff) {
logger.Fatalf("info is not supported, please upgrade and mount again")
}
Expand Down
5 changes: 1 addition & 4 deletions cmd/rmr.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ func rmr(ctx *cli.Context) error {
logger.Fatalf("write message: %s", err)
}
var errs = make([]byte, 1)
n, err := f.Read(errs)
if err != nil || n != 1 {
logger.Fatalf("read message: %d %s", n, err)
}
_ = readControl(f, errs)
if errs[0] != 0 {
errno := syscall.Errno(errs[0])
if runtime.GOOS == "windows" {
Expand Down
22 changes: 18 additions & 4 deletions cmd/warmup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package main

import (
"bufio"
"io"
"os"
"path/filepath"
"strings"
"time"

"github.com/juicedata/juicefs/pkg/meta"
"github.com/juicedata/juicefs/pkg/utils"
Expand Down Expand Up @@ -71,6 +73,18 @@ $ juicefs warmup -f /tmp/filelist`,

const batchMax = 10240

func readControl(cf *os.File, resp []byte) int {
for {
if n, err := cf.Read(resp); err == nil {
return n
} else if err == io.EOF {
time.Sleep(time.Millisecond * 300)
} else {
logger.Fatalf("Read message: %d %s", n, err)
}
}
}

// send fill-cache command to controller file
func sendCommand(cf *os.File, batch []string, count int, threads uint, background bool) {
paths := strings.Join(batch[:count], "\n")
Expand All @@ -93,9 +107,7 @@ func sendCommand(cf *os.File, batch []string, count int, threads uint, backgroun
return
}
var errs = make([]byte, 1)
if n, err := cf.Read(errs); err != nil || n != 1 {
logger.Fatalf("Read message: %d %s", n, err)
}
_ = readControl(cf, errs) // 0 < n <= 1
if errs[0] != 0 {
logger.Fatalf("Warm up failed: %d", errs[0])
}
Expand Down Expand Up @@ -193,7 +205,9 @@ func warmup(ctx *cli.Context) error {
bar.IncrBy(index)
}
progress.Done()
logger.Infof("Successfully warmed up %d paths", bar.Current())
if !background {
logger.Infof("Successfully warmed up %d paths", bar.Current())
}

return nil
}
4 changes: 3 additions & 1 deletion pkg/vfs/vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,9 @@ func (v *VFS) Write(ctx Context, ino Ino, buf []byte, off, fh uint64) (err sysca
h.data = append(h.data, h.pending...)
h.pending = h.pending[:0]
if rb.Left() == size {
h.data = append(h.data, v.handleInternalMsg(ctx, cmd, rb)...)
go func() {
h.data = append(h.data, v.handleInternalMsg(ctx, cmd, rb)...)
}()
} else {
logger.Warnf("broken message: %d %d < %d", cmd, size, rb.Left())
h.data = append(h.data, uint8(syscall.EIO&0xff))
Expand Down
16 changes: 13 additions & 3 deletions pkg/vfs/vfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,16 @@ func TestInternalFile(t *testing.T) {
if e != 0 {
t.Fatalf("open .stats: %s", e)
}
readControl := func(resp []byte, off uint64) (int, syscall.Errno) {
for {
if n, errno := v.Read(ctx, fe.Inode, resp, off, fh); n == 0 {
time.Sleep(time.Millisecond * 300)
} else {
return n, errno
}
}
}

// rmr
buf = make([]byte, 4+4+8+1+4)
w := utils.FromBuffer(buf)
Expand All @@ -713,7 +723,7 @@ func TestInternalFile(t *testing.T) {
}
var off uint64 = uint64(len(buf))
resp := make([]byte, 1024*10)
if n, e := v.Read(ctx, fe.Inode, resp, off, fh); e != 0 || n != 1 {
if n, e := readControl(resp, off); e != 0 || n != 1 {
t.Fatalf("read result: %s %d", e, n)
} else if resp[0] != byte(syscall.ENOENT) {
t.Fatalf("rmr result: %s", string(buf[:n]))
Expand All @@ -731,7 +741,7 @@ func TestInternalFile(t *testing.T) {
}
off += uint64(len(buf))
buf = make([]byte, 1024*10)
if n, e := v.Read(ctx, fe.Inode, buf, off, fh); e != 0 || n == 0 {
if n, e = readControl(buf, off); e != 0 {
t.Fatalf("read result: %s", e)
} else if !strings.Contains(string(buf[:n]), "dirs:") {
t.Fatalf("info result: %s", string(buf[:n]))
Expand All @@ -756,7 +766,7 @@ func TestInternalFile(t *testing.T) {
}
off += uint64(len(buf))
resp = make([]byte, 1024*10)
if n, e = v.Read(ctx, fe.Inode, resp, off, fh); e != 0 || n != 1 {
if n, e = readControl(resp, off); e != 0 || n != 1 {
t.Fatalf("read result: %s", e)
} else if resp[0] != 0 {
t.Fatalf("fill result: %s", string(buf[:n]))
Expand Down