-
Notifications
You must be signed in to change notification settings - Fork 988
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/compact: add new compact cmd, used for specific path (#4337)
Signed-off-by: jiefeng <[email protected]>
- Loading branch information
1 parent
0b79ca7
commit b1b6d29
Showing
11 changed files
with
404 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* JuiceFS, Copyright 2024 Juicedata, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func createTestFile(path string, size int, partCnt int) error { | ||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
content := []byte(strings.Repeat("a", size/partCnt)) | ||
for i := 0; i < partCnt; i++ { | ||
if _, err = file.Write(content); err != nil { | ||
return err | ||
} | ||
if err = file.Sync(); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type testDir struct { | ||
path string | ||
fileCnt int | ||
fileSize int | ||
filePart int | ||
} | ||
|
||
func initForCompactTest(mountDir string, dirs map[string]testDir) { | ||
for _, d := range dirs { | ||
dirPath := filepath.Join(mountDir, d.path) | ||
|
||
err := os.MkdirAll(dirPath, 0755) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for i := 0; i < d.fileCnt; i++ { | ||
if err := createTestFile(filepath.Join(dirPath, fmt.Sprintf("%d", i)), d.fileSize, d.filePart); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestCompact(t *testing.T) { | ||
logger.Level = logrus.DebugLevel | ||
var bucket string | ||
mountTemp(t, &bucket, []string{"--trash-days=0"}, nil) | ||
defer umountTemp(t) | ||
|
||
dirs := map[string]testDir{ | ||
"d1/d11": { | ||
path: "d1/d11", | ||
fileCnt: 10, | ||
fileSize: 10, | ||
filePart: 2, | ||
}, | ||
"d1": { | ||
path: "d1", | ||
fileCnt: 20, | ||
fileSize: 10, | ||
filePart: 5, | ||
}, | ||
"d2": { | ||
path: "d2", | ||
fileCnt: 5, | ||
fileSize: 20, | ||
filePart: 4, | ||
}, | ||
} | ||
initForCompactTest(testMountPoint, dirs) | ||
dataDir := filepath.Join(bucket, testVolume, "chunks") | ||
|
||
sumChunks := 0 | ||
for _, d := range dirs { | ||
sumChunks += d.fileCnt * d.filePart | ||
} | ||
|
||
chunkCnt := getFileCount(dataDir) | ||
assert.Equal(t, sumChunks, chunkCnt) | ||
|
||
for _, d := range dirs { | ||
err := Main([]string{"", "compact", filepath.Join(testMountPoint, d.path)}) | ||
assert.Nil(t, err) | ||
|
||
chunkCnt = getFileCount(dataDir) | ||
sumChunks -= d.fileCnt * (d.filePart - 1) | ||
assert.Equal(t, sumChunks, chunkCnt) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
/* | ||
* JuiceFS, Copyright 2024 Juicedata, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"path/filepath" | ||
"syscall" | ||
|
||
"github.com/juicedata/juicefs/pkg/meta" | ||
"github.com/juicedata/juicefs/pkg/utils" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func cmdCompact() *cli.Command { | ||
return &cli.Command{ | ||
Name: "compact", | ||
Action: compact, | ||
Category: "TOOL", | ||
Usage: "Trigger compaction of chunks", | ||
ArgsUsage: "PATH...", | ||
Description: ` | ||
Examples: | ||
# compact with path | ||
$ juicefs compact /mnt/jfs/foo | ||
`, | ||
Flags: []cli.Flag{ | ||
&cli.UintFlag{ | ||
Name: "threads", | ||
Aliases: []string{"p"}, | ||
Value: 10, | ||
Usage: "compact concurrency", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func compact(ctx *cli.Context) error { | ||
setup(ctx, 1) | ||
|
||
coCnt := ctx.Int("threads") | ||
if coCnt <= 0 { | ||
logger.Warn("threads should be > 0") | ||
coCnt = 1 | ||
} else if coCnt >= math.MaxUint16 { | ||
logger.Warn("threads should be < MaxUint16") | ||
coCnt = math.MaxUint16 | ||
} | ||
|
||
paths := ctx.Args().Slice() | ||
for i := 0; i < len(paths); i++ { | ||
path, err := filepath.Abs(paths[i]) | ||
if err != nil { | ||
logger.Fatalf("get absolute path of %s error: %v", paths[i], err) | ||
} | ||
|
||
inodeNo, err := utils.GetFileInode(path) | ||
if err != nil { | ||
logger.Errorf("lookup inode for %s error: %v", path, err) | ||
continue | ||
} | ||
inode := meta.Ino(inodeNo) | ||
|
||
if !inode.IsValid() { | ||
logger.Fatalf("inode numbe %d not valid", inode) | ||
} | ||
|
||
if err = doCompact(inode, path, uint16(coCnt)); err != nil { | ||
logger.Error(err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func doCompact(inode meta.Ino, path string, coCnt uint16) error { | ||
f, err := openController(path) | ||
if err != nil { | ||
return fmt.Errorf("open control file for [%d:%s]: %w", inode, path, err) | ||
} | ||
defer f.Close() | ||
|
||
headerLen, bodyLen := uint32(8), uint32(8+2) | ||
wb := utils.NewBuffer(headerLen + bodyLen) | ||
wb.Put32(meta.CompactPath) | ||
wb.Put32(bodyLen) | ||
wb.Put64(uint64(inode)) | ||
wb.Put16(coCnt) | ||
|
||
_, err = f.Write(wb.Bytes()) | ||
if err != nil { | ||
logger.Fatalf("write message: %s", err) | ||
} | ||
|
||
progress := utils.NewProgress(false) | ||
bar := progress.AddCountBar("Compacted chunks", 0) | ||
_, errno := readProgress(f, func(totalChunks, currChunks uint64) { | ||
bar.SetTotal(int64(totalChunks)) | ||
bar.SetCurrent(int64(currChunks)) | ||
}) | ||
|
||
bar.Done() | ||
progress.Done() | ||
|
||
if errno == syscall.EINVAL { | ||
logger.Fatalf("compact is not supported, please upgrade and mount again") | ||
} | ||
if errno != 0 { | ||
return fmt.Errorf("compact [%d:%s] error: %s", inode, path, errno) | ||
} | ||
|
||
logger.Infof("compact [%d:%s] success.", inode, path) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
/* | ||
* JuiceFS, Copyright 2024 Juicedata, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func cmdCompact() *cli.Command { | ||
return &cli.Command{ | ||
Name: "compact", | ||
Action: compact, | ||
Category: "TOOL", | ||
Usage: "Trigger compaction of chunks, not supported for Windows", | ||
} | ||
} | ||
|
||
func compact(ctx *cli.Context) error { | ||
logger.Warnf("not supported for Windows.") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,7 @@ func Main(args []string) error { | |
cmdDebug(), | ||
cmdClone(), | ||
cmdSummary(), | ||
cmdCompact(), | ||
}, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.