Skip to content

Commit

Permalink
feat: add real size to backing image
Browse files Browse the repository at this point in the history
ref: longhorn/longhorn 8757

Signed-off-by: Jack Lin <jack.lin@suse.com>
  • Loading branch information
ChanYiLin committed Sep 20, 2024
1 parent 411cf2f commit a01e9f2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type BackingImage struct {
UUID string `json:"uuid"`
Size int64 `json:"size"`
VirtualSize int64 `json:"virtualSize"`
RealSize int64 `json:"realSize"`
ExpectedChecksum string `json:"expectedChecksum"`

Status BackingImageStatus `json:"status"`
Expand All @@ -33,6 +34,7 @@ func RPCToBackingImage(obj *rpc.BackingImageResponse) *BackingImage {
UUID: obj.Spec.Uuid,
Size: obj.Spec.Size,
VirtualSize: obj.Spec.VirtualSize,
RealSize: obj.Spec.RealSize,
ExpectedChecksum: obj.Spec.Checksum,

Status: BackingImageStatus{
Expand Down Expand Up @@ -114,6 +116,7 @@ type FileInfo struct {
UUID string `json:"uuid"`
Size int64 `json:"size"`
VirtualSize int64 `json:"virtualSize"`
RealSize int64 `json:"realSize"`
State string `json:"state"`
Progress int `json:"progress"`
ProcessedSize int64 `json:"processedSize"`
Expand Down
1 change: 1 addition & 0 deletions pkg/manager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ func backingImageResponse(fInfo *api.FileInfo) *rpc.BackingImageResponse {
Uuid: fInfo.UUID,
Size: fInfo.Size,
VirtualSize: fInfo.VirtualSize,
RealSize: fInfo.RealSize,
Checksum: fInfo.ExpectedChecksum,
},
Status: &rpc.BackingImageStatus{
Expand Down
1 change: 1 addition & 0 deletions pkg/sync/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ func getAndWaitFileState(cli *client.SyncClient, curPath, desireState string, wa
UUID: fInfo.UUID,
Size: fInfo.Size,
VirtualSize: fInfo.VirtualSize,
RealSize: fInfo.RealSize,
ExpectedChecksum: fInfo.ExpectedChecksum,
CurrentChecksum: fInfo.CurrentChecksum,
ModificationTime: fInfo.ModificationTime,
Expand Down
18 changes: 18 additions & 0 deletions pkg/sync/sync_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type SyncingFile struct {
diskUUID string
size int64
virtualSize int64
realSize int64
state types.State
progress int
processedSize int64
Expand Down Expand Up @@ -258,6 +259,7 @@ func (sf *SyncingFile) checkAndReuseFile() (err error) {
sf.modificationTime = info.ModTime().UTC().String()
sf.updateSyncReadyNoLock()
sf.updateVirtualSizeNoLock(sf.filePath)
sf.updateRealSizeNoLock(sf.filePath)
sf.writeConfigNoLock()
sf.lock.Unlock()

Expand Down Expand Up @@ -359,6 +361,7 @@ func (sf *SyncingFile) getNoLock() api.FileInfo {
UUID: sf.uuid,
Size: sf.size,
VirtualSize: sf.virtualSize,
RealSize: sf.realSize,
State: string(sf.state),
Progress: sf.progress,
ProcessedSize: sf.processedSize,
Expand Down Expand Up @@ -917,6 +920,7 @@ func (sf *SyncingFile) finishProcessing(err error) (finalErr error) {
sf.currentChecksum = config.CurrentChecksum
sf.updateSyncReadyNoLock()
sf.updateVirtualSizeNoLock(sf.tmpFilePath)
sf.updateRealSizeNoLock(sf.tmpFilePath)
sf.writeConfigNoLock()

// Renaming won't change the file modification time.
Expand Down Expand Up @@ -966,6 +970,7 @@ func (sf *SyncingFile) postProcessSyncFile() {
}
sf.updateSyncReadyNoLock()
sf.updateVirtualSizeNoLock(sf.tmpFilePath)
sf.updateRealSizeNoLock(sf.tmpFilePath)
sf.writeConfigNoLock()

// Renaming won't change the file modification time.
Expand Down Expand Up @@ -1000,6 +1005,18 @@ func (sf *SyncingFile) updateVirtualSizeNoLock(filePath string) {
sf.virtualSize = imgInfo.VirtualSize
}

func (sf *SyncingFile) updateRealSizeNoLock(filePath string) {
realSize, err := util.GetFileRealSize(filePath)
if err != nil {
sf.log.Warnf("SyncingFile: failed to get backing image virtual size: %v", err)
}

// This will be zero when there is an error, which allows components
// further up the stack to know that the real size somehow isn't
// available yet.
sf.realSize = realSize
}

func (sf *SyncingFile) handleFailureNoLock(err error) {
if err == nil {
return
Expand Down Expand Up @@ -1029,6 +1046,7 @@ func (sf *SyncingFile) writeConfigNoLock() {
UUID: sf.uuid,
Size: sf.size,
VirtualSize: sf.virtualSize,
RealSize: sf.realSize,
ExpectedChecksum: sf.expectedChecksum,
CurrentChecksum: sf.currentChecksum,
ModificationTime: sf.modificationTime,
Expand Down
3 changes: 2 additions & 1 deletion pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const (
DiskPathInContainer = "/data/"
DataSourceDirectoryName = "/tmp/"

DefaultSectorSize = 512
DefaultSectorSize = 512
DefaultLinuxBlcokSize = 512

DefaultManagerPort = 8000
DefaultDataSourceServerPort = 8000
Expand Down
15 changes: 15 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (
"os/exec"
"path/filepath"
"regexp"
"syscall"
"time"

"github.com/longhorn/backing-image-manager/pkg/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
Expand Down Expand Up @@ -151,6 +153,7 @@ type SyncingFileConfig struct {
UUID string `json:"uuid"`
Size int64 `json:"size"`
VirtualSize int64 `json:"virtualSize"`
RealSize int64 `json:"realSize"`
ExpectedChecksum string `json:"expectedChecksum"`
CurrentChecksum string `json:"currentChecksum"`
ModificationTime string `json:"modificationTime"`
Expand Down Expand Up @@ -313,6 +316,18 @@ func ConvertFromQcow2ToRaw(sourcePath, targetPath string) error {
return nil
}

func GetFileRealSize(filePath string) (int64, error) {
var stat syscall.Stat_t
err := syscall.Stat(filePath, &stat)
if err != nil {
return 0, err
}
fmt.Printf("stat.Blksize: %v\n", stat.Blksize)

// 512 is defined in the Linux kernel and remains consistent across all distributions.
return stat.Blocks * types.DefaultLinuxBlcokSize, nil
}

func FileModificationTime(filePath string) string {
fi, err := os.Stat(filePath)
if err != nil {
Expand Down

0 comments on commit a01e9f2

Please sign in to comment.