Skip to content

Commit

Permalink
storage: count migrations as volume writes
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed May 20, 2024
1 parent 8860607 commit 828ff61
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
41 changes: 20 additions & 21 deletions host/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ type (
}
)

// initVolume adds a volume to the volume manager. If the volume is already
// added, it is returned. The volume mutex must be held before calling this
// function.
func (vm *VolumeManager) initVolume(id int64, status string, d volumeData) *volume {
if v, ok := vm.volumes[id]; ok {
return v
}
v := &volume{
recorder: vm.recorder,
stats: VolumeStats{
Status: status,
},
data: d,
}
vm.volumes[id] = v
return v
}

// loadVolumes opens all volumes. Volumes that are already loaded are skipped.
func (vm *VolumeManager) loadVolumes() error {
done, err := vm.tg.Add()
Expand All @@ -110,16 +128,7 @@ func (vm *VolumeManager) loadVolumes() error {
// load the volumes into memory
for _, vol := range volumes {
// if the volume has not been loaded yet, create a new volume
v := vm.volumes[vol.ID]
if v == nil {
v = &volume{
stats: VolumeStats{
Status: VolumeStatusUnavailable,
},
}
vm.volumes[vol.ID] = v
}

v := vm.initVolume(vol.ID, VolumeStatusUnavailable, nil)
if err := v.OpenVolume(vol.LocalPath, false); err != nil {
v.appendError(fmt.Errorf("failed to open volume: %w", err))
vm.log.Error("unable to open volume", zap.Error(err), zap.Int64("id", vol.ID), zap.String("path", vol.LocalPath))
Expand Down Expand Up @@ -449,13 +458,7 @@ func (vm *VolumeManager) AddVolume(ctx context.Context, localPath string, maxSec

// add the new volume to the volume map
vm.mu.Lock()
vol := &volume{
data: f,
stats: VolumeStats{
Status: VolumeStatusCreating,
},
}
vm.volumes[volumeID] = vol
vol := vm.initVolume(volumeID, VolumeStatusCreating, f)
vm.mu.Unlock()

vm.vs.SetAvailable(volumeID, true)
Expand Down Expand Up @@ -840,7 +843,6 @@ func (vm *VolumeManager) Read(root types.Hash256) (*[rhp2.SectorSize]byte, error
vm.cache.Add(root, sector)
vm.recorder.AddCacheMiss()
atomic.AddUint64(&vm.cacheMisses, 1)
vm.recorder.AddRead()
return sector, nil
}

Expand Down Expand Up @@ -926,9 +928,6 @@ func (vm *VolumeManager) Write(root types.Hash256, data *[rhp2.SectorSize]byte)
vm.mu.Unlock()
return nil
})
if err == nil {
vm.recorder.AddWrite()
}
return release, err
}

Expand Down
5 changes: 4 additions & 1 deletion host/storage/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type (

// A volume stores and retrieves sector data from a local file
volume struct {
recorder *sectorAccessRecorder

// when reading or writing to the volume, a read lock should be held.
// When resizing or updating the volume's state, a write lock should be
// held.
Expand Down Expand Up @@ -72,6 +74,7 @@ func (v *volume) incrementReadStats(err error) {
v.stats.FailedReads++
v.appendError(err)
} else {
v.recorder.AddRead()
v.stats.SuccessfulReads++
}
}
Expand All @@ -83,6 +86,7 @@ func (v *volume) incrementWriteStats(err error) {
v.stats.FailedWrites++
v.appendError(err)
} else {
v.recorder.AddWrite()
v.stats.SuccessfulWrites++
}
}
Expand Down Expand Up @@ -169,7 +173,6 @@ func (v *volume) ReadSector(index uint64) (*[rhp2.SectorSize]byte, error) {

var sector [rhp2.SectorSize]byte
_, err := v.data.ReadAt(sector[:], int64(index*rhp2.SectorSize))

if err != nil {
err = fmt.Errorf("failed to read sector at index %v: %w", index, err)
}
Expand Down

0 comments on commit 828ff61

Please sign in to comment.