Skip to content

Commit

Permalink
Merge pull request #378 from SiaFoundation/nate/default-pinned-threshold
Browse files Browse the repository at this point in the history
Default pinned threshold to 2%
  • Loading branch information
n8maninger authored May 3, 2024
2 parents 9f9ad89 + c6f1ae0 commit e8e7d15
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 59 deletions.
114 changes: 56 additions & 58 deletions host/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,6 @@ func (vm *VolumeManager) AddVolume(ctx context.Context, localPath string, maxSec
vm.vs.SetAvailable(volumeID, true)

go func() {
defer vol.SetStatus(VolumeStatusReady)

log := vm.log.Named("initialize").With(zap.Int64("volumeID", volumeID), zap.Uint64("maxSectors", maxSectors))
start := time.Now()

Expand All @@ -486,7 +484,7 @@ func (vm *VolumeManager) AddVolume(ctx context.Context, localPath string, maxSec
alert.Severity = alerts.SeverityInfo
}
vm.a.Register(alert)

vol.SetStatus(VolumeStatusReady)
select {
case result <- err:
default:
Expand Down Expand Up @@ -565,8 +563,6 @@ func (vm *VolumeManager) RemoveVolume(ctx context.Context, id int64, force bool,
}

go func() {
defer vol.SetStatus(oldStatus)

var migrated, failed int

updateRemovalAlert := func(message string, severity alerts.Severity, err error) {
Expand All @@ -580,53 +576,56 @@ func (vm *VolumeManager) RemoveVolume(ctx context.Context, id int64, force bool,
vm.a.Register(alert)
}

migrated, failed, err := vm.vs.MigrateSectors(ctx, id, 0, func(newLoc SectorLocation) error {
err := vm.migrateSector(newLoc)
doMigration := func() error {
migrated, failed, err = vm.vs.MigrateSectors(ctx, id, 0, func(newLoc SectorLocation) error {
err := vm.migrateSector(newLoc)
if err != nil {
failed++
} else {
migrated++
}
updateRemovalAlert("Removing volume", alerts.SeverityInfo, nil) // error is ignored during migration
return err
})
if err != nil {
failed++
} else {
migrated++
log.Error("failed to migrate sectors", zap.Error(err))
// update the alert
updateRemovalAlert("Failed to remove volume", alerts.SeverityError, err)
return err
} else if !force && failed > 0 {
updateRemovalAlert("Failed to remove volume", alerts.SeverityError, ErrMigrationFailed)
return ErrMigrationFailed
}
updateRemovalAlert("Removing volume", alerts.SeverityInfo, nil) // error is ignored during migration
return err
})
if err != nil {
log.Error("failed to migrate sectors", zap.Error(err))
// update the alert
updateRemovalAlert("Failed to remove volume", alerts.SeverityError, err)
result <- err
return
} else if !force && failed > 0 {
updateRemovalAlert("Failed to remove volume", alerts.SeverityError, ErrMigrationFailed)
result <- ErrMigrationFailed
return
}

// remove the volume from the volume store
if err := vm.vs.RemoveVolume(id, force); err != nil {
log.Error("failed to remove volume", zap.Error(err))
// update the alert
updateRemovalAlert("Failed to remove volume", alerts.SeverityError, err)
result <- err
return
// remove the volume from the volume store
if err := vm.vs.RemoveVolume(id, force); err != nil {
log.Error("failed to remove volume", zap.Error(err))
// update the alert
updateRemovalAlert("Failed to remove volume", alerts.SeverityError, err)
return err
}
delete(vm.volumes, id)

// close the volume file and remove it from disk
if err := vol.Close(); err != nil {
log.Error("failed to close volume", zap.Error(err))
updateRemovalAlert("Failed to close volume files", alerts.SeverityError, err)
return err
} else if err := os.Remove(stat.LocalPath); err != nil && !errors.Is(err, os.ErrNotExist) {
log.Error("failed to remove volume file", zap.Error(err))
updateRemovalAlert("Failed to delete volume file", alerts.SeverityError, err)
return err
}
updateRemovalAlert("Volume removed", alerts.SeverityInfo, nil)
return nil
}
delete(vm.volumes, id)

// close the volume file and remove it from disk
if err := vol.Close(); err != nil {
log.Error("failed to close volume", zap.Error(err))
updateRemovalAlert("Failed to close volume files", alerts.SeverityError, err)
result <- err
return
} else if err := os.Remove(stat.LocalPath); err != nil && !errors.Is(err, os.ErrNotExist) {
log.Error("failed to remove volume file", zap.Error(err))
updateRemovalAlert("Failed to delete volume file", alerts.SeverityError, err)
result <- err
return
err := doMigration()
vol.SetStatus(oldStatus)
select {
case result <- err:
default:
}

updateRemovalAlert("Volume removed", alerts.SeverityInfo, nil)
result <- nil
}()

return nil
Expand Down Expand Up @@ -670,20 +669,12 @@ func (vm *VolumeManager) ResizeVolume(ctx context.Context, id int64, maxSectors

go func() {
log := vm.log.Named("resize").With(zap.Int64("volumeID", id))

defer func() {
if resetReadOnly {
// reset the volume to read-write
if err := vm.vs.SetReadOnly(id, false); err != nil {
vm.log.Error("failed to set volume to read-write", zap.Error(err))
}
}
vol.SetStatus(VolumeStatusReady)
}()

ctx, cancel, err := vm.tg.AddContext(ctx)
if err != nil {
result <- err
select {
case result <- err:
default:
}
return
}
defer cancel()
Expand Down Expand Up @@ -722,6 +713,13 @@ func (vm *VolumeManager) ResizeVolume(ctx context.Context, id int64, maxSectors
alert.Severity = alerts.SeverityInfo
}
vm.a.Register(alert)
if resetReadOnly {
// reset the volume to read-write
if err := vm.vs.SetReadOnly(id, false); err != nil {
vm.log.Error("failed to set volume to read-write", zap.Error(err))
}
}
vol.SetStatus(VolumeStatusReady)
select {
case result <- err:
default:
Expand Down
5 changes: 4 additions & 1 deletion persist/sqlite/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ FROM host_pinned_settings;`

err = s.queryRow(query).Scan(&pinned.Currency, &pinned.Threshold, &pinned.Storage.Pinned, &pinned.Storage.Value, &pinned.Ingress.Pinned, &pinned.Ingress.Value, &pinned.Egress.Pinned, &pinned.Egress.Value, &pinned.MaxCollateral.Pinned, &pinned.MaxCollateral.Value)
if errors.Is(err, sql.ErrNoRows) {
return pin.PinnedSettings{}, nil
return pin.PinnedSettings{
Currency: "usd",
Threshold: 0.02,
}, nil
}
return
}
Expand Down

0 comments on commit e8e7d15

Please sign in to comment.