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 Aug 27, 2024
1 parent d4f90ad commit ff144ac
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 203 deletions.
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.22.2

toolchain go1.23.0

replace github.com/longhorn/types v0.0.0-20240725040629-473d671316c4 => github.com/chanyilin/types v0.0.0-20240827082245-b720128c87f4

require (
github.com/golang/protobuf v1.5.4
github.com/gorilla/mux v1.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8 h1:SjZ2GvvOononHOpK
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8/go.mod h1:uEyr4WpAH4hio6LFriaPkL938XnrvLpNPmQHBdrmbIE=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chanyilin/types v0.0.0-20240827082245-b720128c87f4 h1:owlhqpqY7n3UV4U+T0E0olMGKVxoVCL/Xuye9BTHKKA=
github.com/chanyilin/types v0.0.0-20240827082245-b720128c87f4/go.mod h1:KlJuZB8NfHchWshYxYgV9pPIxBKC04Vq05G2TfgMf7w=
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
Expand Down Expand Up @@ -82,8 +84,6 @@ github.com/longhorn/longhorn-engine v1.5.5 h1:44p9Iyp4boKSHgMAl8iD4nY/EQRnF/e7hP
github.com/longhorn/longhorn-engine v1.5.5/go.mod h1:/QEAmYu1ZNsKR22d8wdqssbz1ja7Yyj/GyPu8BYkTEk=
github.com/longhorn/sparse-tools v0.0.0-20240729132735-18b207e459ff h1:gmdQDbnaGJ/zmrK+QJzSys8mH679os6i7vW/pOpRn1U=
github.com/longhorn/sparse-tools v0.0.0-20240729132735-18b207e459ff/go.mod h1:iUJCZtOKG/9xv2rfrUAYZntFTzP5dZtvy4Kwe6dMcUc=
github.com/longhorn/types v0.0.0-20240725040629-473d671316c4 h1:L2g0sIJ2fXt4BSFRYNnF6ObtKryCUFm9qLcCXHWssCk=
github.com/longhorn/types v0.0.0-20240725040629-473d671316c4/go.mod h1:KlJuZB8NfHchWshYxYgV9pPIxBKC04Vq05G2TfgMf7w=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
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
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
14 changes: 14 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"syscall"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -151,6 +152,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 +315,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 * 512, nil
}

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

0 comments on commit ff144ac

Please sign in to comment.