fix(metanode): fsync the crc file after wirte

Signed-off-by: gukaifeng <gukaifeng@xiaomi.com>
Signed-off-by: slasher <shenjie1@oppo.com>
This commit is contained in:
gukaifeng 2024-09-09 21:27:27 +08:00 committed by Victor1319
parent 02b037cdc1
commit 2e24163a51
4 changed files with 70 additions and 2 deletions

View File

@ -39,6 +39,7 @@ import (
"github.com/cubefs/cubefs/util"
"github.com/cubefs/cubefs/util/atomicutil"
"github.com/cubefs/cubefs/util/errors"
"github.com/cubefs/cubefs/util/fileutil"
"github.com/cubefs/cubefs/util/log"
"github.com/cubefs/cubefs/util/timeutil"
)
@ -1367,7 +1368,7 @@ func (mp *metaPartition) store(sm *storeMsg) (err error) {
}
// write crc to file
if err = os.WriteFile(path.Join(tmpDir, SnapshotSign), crcBuffer.Bytes(), 0o775); err != nil {
if err = fileutil.WriteFileWithSync(path.Join(tmpDir, SnapshotSign), crcBuffer.Bytes(), 0o775); err != nil {
return
}
snapshotDir := path.Join(mp.config.RootDir, snapshotDir)

View File

@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/cubefs/cubefs/proto"
"github.com/cubefs/cubefs/util/fileutil"
)
func TestMetaPartition_LoadSnapshot(t *testing.T) {
@ -132,7 +133,7 @@ func TestMetaPartition_LoadSnapshot(t *testing.T) {
require.True(t, len(crcData) != 0)
crcData[0] = '0'
crcData[1] = '1'
err = os.WriteFile(path.Join(snapshotPath, SnapshotSign), crcData, 0o644)
err = fileutil.WriteFileWithSync(path.Join(snapshotPath, SnapshotSign), crcData, 0o644)
require.Nil(t, err)
err = partition.LoadSnapshot(snapshotPath)
require.Equal(t, ErrSnapshotCrcMismatch, err)

35
util/fileutil/write.go Normal file
View File

@ -0,0 +1,35 @@
// Copyright 2024 The CubeFS Authors.
//
// 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 fileutil
import (
"io/fs"
"os"
)
// WriteFileWithSync implements os.WriteFile() with fsync.
func WriteFileWithSync(filename string, data []byte, perm fs.FileMode) error {
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
}
if _, err = file.Write(data); err == nil {
err = file.Sync()
}
if err1 := file.Close(); err1 != nil && err == nil {
err = err1
}
return err
}

View File

@ -0,0 +1,31 @@
// Copyright 2024 The CubeFS Authors.
//
// 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 fileutil_test
import (
"os"
"path/filepath"
"testing"
"github.com/cubefs/cubefs/util/fileutil"
"github.com/stretchr/testify/require"
)
func TestWriteFileWithSync(t *testing.T) {
require.Error(t, fileutil.WriteFileWithSync(os.TempDir(), nil, 0o644))
filename := filepath.Join(os.TempDir(), "TestWriteFileWithSync.file")
defer os.RemoveAll(filename)
require.NoError(t, fileutil.WriteFileWithSync(filename, make([]byte, 32), 0o644))
}