Skip to content

Commit

Permalink
Merge pull request #256 from SiaFoundation/nate/add-volume-read-write…
Browse files Browse the repository at this point in the history
…-alerts

Add volume read and write alerts
  • Loading branch information
n8maninger authored Dec 30, 2023
2 parents 8bc291e + 48f2fa1 commit 7be056e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ rhp3:
log:
level: info # global log level
stdout:
enabled: true # enable logging to stdout
enabled: true # enable logging to stdout
level: info # log level for console logger
format: human # log format (human, json)
enableANSI: true # enable ANSI color codes (disabled on Windows)
format: human # log format (human, json)
enableANSI: true # enable ANSI color codes (disabled on Windows)
file:
enabled: true # enable logging to file
level: info # log level for file logger
path: /var/log/hostd/hostd.log # the path of the log file
level: info # log level for file logger
path: /var/log/hostd/hostd.log # the path of the log file
format: json # log format (human, json)
```
Expand Down
28 changes: 28 additions & 0 deletions host/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,20 @@ func (vm *VolumeManager) Read(root types.Hash256) (*[rhp2.SectorSize]byte, error
vm.mu.Unlock()
sector, err := v.ReadSector(loc.Index)
if err != nil {
stats := v.Stats()
vm.a.Register(alerts.Alert{
ID: v.alertID("read"),
Severity: alerts.SeverityError,
Message: "Failed to read sector",
Data: map[string]interface{}{
"volume": v.Location(),
"failedReads": stats.FailedReads,
"failedWrites": stats.FailedWrites,
"sector": root,
"error": err.Error(),
},
Timestamp: time.Now(),
})
return nil, fmt.Errorf("failed to read sector data: %w", err)
}

Expand Down Expand Up @@ -910,6 +924,20 @@ func (vm *VolumeManager) Write(root types.Hash256, data *[rhp2.SectorSize]byte)

// write the sector to the volume
if err := vol.WriteSector(data, loc.Index); err != nil {
stats := vol.Stats()
vm.a.Register(alerts.Alert{
ID: vol.alertID("write"),
Severity: alerts.SeverityError,
Message: "Failed to write sector",
Data: map[string]interface{}{
"volume": vol.Location(),
"failedReads": stats.FailedReads,
"failedWrites": stats.FailedWrites,
"sector": root,
"error": err.Error(),
},
Timestamp: time.Now(),
})
return fmt.Errorf("failed to write sector data: %w", err)
}
vm.log.Debug("wrote sector", zap.String("root", root.String()), zap.Int64("volume", loc.Volume), zap.Uint64("index", loc.Index), zap.Duration("elapsed", time.Since(start)))
Expand Down
21 changes: 19 additions & 2 deletions host/storage/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"

rhp2 "go.sia.tech/core/rhp/v2"
"go.sia.tech/core/types"
"lukechampine.com/frand"
)

Expand All @@ -30,8 +31,9 @@ type (
// held.
mu sync.RWMutex

data volumeData // data is a flatfile that stores the volume's sector data
stats VolumeStats
location string // location is the path to the volume's file
data volumeData // data is a flatfile that stores the volume's sector data
stats VolumeStats
}

// VolumeStats contains statistics about a volume
Expand Down Expand Up @@ -93,6 +95,20 @@ func (v *volume) appendError(err error) {
}
}

// Location returns the location of the volume
func (v *volume) Location() string {
v.mu.RLock()
defer v.mu.RUnlock()
return v.location
}

// alertID returns a deterministic alert ID for a volume and context
func (v *volume) alertID(context string) types.Hash256 {
v.mu.RLock()
defer v.mu.RUnlock()
return types.HashBytes([]byte(v.location + context))
}

// OpenVolume opens the volume at localPath
func (v *volume) OpenVolume(localPath string, reload bool) error {
v.mu.Lock()
Expand All @@ -104,6 +120,7 @@ func (v *volume) OpenVolume(localPath string, reload bool) error {
if err != nil {
return err
}
v.location = localPath
v.data = f
return nil
}
Expand Down

0 comments on commit 7be056e

Please sign in to comment.