提交 7472e165 编写于 作者: D Davies Liu

refactor

上级 35b1cd2a
......@@ -16,7 +16,6 @@
package main
import (
"context"
"fmt"
"net/http"
_ "net/http/pprof"
......@@ -172,7 +171,7 @@ func mount(c *cli.Context) error {
m.OnMsg(meta.CompactChunk, meta.MsgCallback(func(args ...interface{}) error {
slices := args[0].([]meta.Slice)
chunkid := args[1].(uint64)
return compact(chunkConf, store, slices, chunkid)
return vfs.Compact(chunkConf, store, slices, chunkid)
}))
conf := &vfs.Config{
......@@ -315,61 +314,3 @@ func mountFlags() *cli.Command {
},
}
}
func readSlice(store chunk.ChunkStore, s *meta.Slice, page *chunk.Page, off int) error {
buf := page.Data
read := 0
reader := store.NewReader(s.Chunkid, int(s.Size))
for read < len(buf) {
p := page.Slice(read, len(buf)-read)
n, err := reader.ReadAt(context.Background(), p, off+int(s.Off))
p.Release()
if n == 0 && err != nil {
return err
}
read += n
off += n
}
return nil
}
func compact(conf chunk.Config, store chunk.ChunkStore, slices []meta.Slice, chunkid uint64) error {
writer := store.NewWriter(chunkid)
defer writer.Abort()
var pos int
for i, s := range slices {
if s.Chunkid == 0 {
_, err := writer.WriteAt(make([]byte, int(s.Len)), int64(pos))
if err != nil {
return err
}
pos += int(s.Len)
continue
}
var read int
for read < int(s.Len) {
l := utils.Min(conf.BlockSize, int(s.Len)-read)
p := chunk.NewOffPage(l)
if err := readSlice(store, &s, p, read); err != nil {
logger.Infof("can't compact chunk %d, retry later, read %d: %s", chunkid, i, err)
p.Release()
return err
}
_, err := writer.WriteAt(p.Data, int64(pos+read))
p.Release()
if err != nil {
logger.Errorf("can't compact chunk %d, retry later, write: %s", chunkid, err)
return err
}
read += l
if pos+read >= conf.BlockSize {
if err = writer.FlushTo(pos + read); err != nil {
panic(err)
}
}
}
pos += int(s.Len)
}
return writer.Finish(pos)
}
/*
* JuiceFS, Copyright (C) 2020 Juicedata, Inc.
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package vfs
import (
"context"
"github.com/juicedata/juicefs/pkg/chunk"
"github.com/juicedata/juicefs/pkg/meta"
"github.com/juicedata/juicefs/pkg/utils"
)
func readSlice(store chunk.ChunkStore, s *meta.Slice, page *chunk.Page, off int) error {
buf := page.Data
read := 0
reader := store.NewReader(s.Chunkid, int(s.Size))
for read < len(buf) {
p := page.Slice(read, len(buf)-read)
n, err := reader.ReadAt(context.Background(), p, off+int(s.Off))
p.Release()
if n == 0 && err != nil {
return err
}
read += n
off += n
}
return nil
}
func Compact(conf chunk.Config, store chunk.ChunkStore, slices []meta.Slice, chunkid uint64) error {
writer := store.NewWriter(chunkid)
defer writer.Abort()
var pos int
for i, s := range slices {
if s.Chunkid == 0 {
_, err := writer.WriteAt(make([]byte, int(s.Len)), int64(pos))
if err != nil {
return err
}
pos += int(s.Len)
continue
}
var read int
for read < int(s.Len) {
l := utils.Min(conf.BlockSize, int(s.Len)-read)
p := chunk.NewOffPage(l)
if err := readSlice(store, &s, p, read); err != nil {
logger.Infof("can't compact chunk %d, retry later, read %d: %s", chunkid, i, err)
p.Release()
return err
}
_, err := writer.WriteAt(p.Data, int64(pos+read))
p.Release()
if err != nil {
logger.Errorf("can't compact chunk %d, retry later, write: %s", chunkid, err)
return err
}
read += l
if pos+read >= conf.BlockSize {
if err = writer.FlushTo(pos + read); err != nil {
panic(err)
}
}
}
pos += int(s.Len)
}
return writer.Finish(pos)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册