From 3daaefec606d95926511f9f3bfcbca097957cc6b Mon Sep 17 00:00:00 2001 From: chrislu Date: Sun, 25 Dec 2022 12:12:22 -0800 Subject: [PATCH] add debug mode to compare data read and write --- weed/mount/filehandle.go | 17 ++++++++++++++++- weed/mount/weedfs_file_read.go | 19 +++++++++++++++++++ weed/mount/weedfs_file_sync.go | 4 ++++ weed/mount/weedfs_file_write.go | 5 +++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/weed/mount/filehandle.go b/weed/mount/filehandle.go index 1dd70e861..a08b66a00 100644 --- a/weed/mount/filehandle.go +++ b/weed/mount/filehandle.go @@ -5,14 +5,16 @@ import ( "github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/util" - "golang.org/x/exp/slices" "golang.org/x/sync/semaphore" "math" + "os" "sync" ) type FileHandleId uint64 +var IsDebug = true + type FileHandle struct { fh FileHandleId counter int64 @@ -31,6 +33,9 @@ type FileHandle struct { orderedMutex *semaphore.Weighted isDeleted bool + + // for debugging + mirrorFile *os.File } func newFileHandle(wfs *WFS, handleId FileHandleId, inode uint64, entry *filer_pb.Entry) *FileHandle { @@ -50,6 +55,14 @@ func newFileHandle(wfs *WFS, handleId FileHandleId, inode uint64, entry *filer_p Entry: entry, } + if IsDebug { + var err error + fh.mirrorFile, err = os.OpenFile("/tmp/sw/"+entry.Name, os.O_RDWR|os.O_CREATE, 0600) + if err != nil { + println("failed to create mirror:", err.Error()) + } + } + return fh } @@ -97,5 +110,7 @@ func (fh *FileHandle) Release() { fh.dirtyPages.Destroy() fh.CloseReader() + if IsDebug { + fh.mirrorFile.Close() } } diff --git a/weed/mount/weedfs_file_read.go b/weed/mount/weedfs_file_read.go index a5a17a3ba..d098b6883 100644 --- a/weed/mount/weedfs_file_read.go +++ b/weed/mount/weedfs_file_read.go @@ -1,7 +1,9 @@ package mount import ( + "bytes" "context" + "fmt" "io" "github.com/hanwen/go-fuse/v2/fuse" @@ -50,6 +52,23 @@ func (wfs *WFS) Read(cancel <-chan struct{}, in *fuse.ReadIn, buff []byte) (fuse return nil, fuse.EIO } + if IsDebug { + // print(".") + mirrorData := make([]byte, totalRead) + fh.mirrorFile.ReadAt(mirrorData, offset) + if bytes.Compare(mirrorData, buff[:totalRead]) != 0 { + + againBuff := make([]byte, len(buff)) + againRead, _ := readDataByFileHandle(buff, fh, offset) + againCorrect := bytes.Compare(mirrorData, againBuff[:againRead]) == 0 + againSame := bytes.Compare(buff[:totalRead], againBuff[:againRead]) == 0 + + fmt.Printf("\ncompare %v [%d,%d) size:%d againSame:%v againCorrect:%v\n", fh.mirrorFile.Name(), offset, offset+totalRead, totalRead, againSame, againCorrect) + //fmt.Printf("read mirrow data: %v\n", mirrorData) + //fmt.Printf("read actual data: %v\n", buff[:totalRead]) + } + } + return fuse.ReadResultData(buff[:totalRead]), fuse.OK } diff --git a/weed/mount/weedfs_file_sync.go b/weed/mount/weedfs_file_sync.go index 429924592..2022f88a6 100644 --- a/weed/mount/weedfs_file_sync.go +++ b/weed/mount/weedfs_file_sync.go @@ -181,5 +181,9 @@ func (wfs *WFS) doFlush(fh *FileHandle, uid, gid uint32) fuse.Status { return fuse.EIO } + if IsDebug { + fh.mirrorFile.Sync() + } + return fuse.OK } diff --git a/weed/mount/weedfs_file_write.go b/weed/mount/weedfs_file_write.go index 3eaefef94..5e2e14f25 100644 --- a/weed/mount/weedfs_file_write.go +++ b/weed/mount/weedfs_file_write.go @@ -73,5 +73,10 @@ func (wfs *WFS) Write(cancel <-chan struct{}, in *fuse.WriteIn, data []byte) (wr fh.dirtyMetadata = true + if IsDebug { + // print("+") + fh.mirrorFile.WriteAt(data, offset) + } + return written, fuse.OK }