Skip to content

Commit 472aa68

Browse files
authored
Merge pull request #181 from SiaFoundation/christopher/fix-v2-storage-total
Add v2 numbers to total and remaining storage in metrics
2 parents 086609d + 7181a21 commit 472aa68

File tree

6 files changed

+459
-178
lines changed

6 files changed

+459
-178
lines changed

api/api_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ func newExplorer(t *testing.T, network *consensus.Network, genesisBlock types.Bl
4949
cm := chain.NewManager(store, genesisState)
5050

5151
e, err := explorer.NewExplorer(cm, db, 1000, config.Scanner{
52-
Threads: 10,
52+
BatchSize: 100,
5353
Timeout: 30 * time.Second,
54+
CheckAgainDelay: 100 * time.Millisecond,
5455
MaxLastScan: 3 * time.Hour,
5556
MinLastAnnouncement: 90 * 24 * time.Hour,
5657
}, log)

cmd/explored/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ var cfg = config.Config{
4545
EnableUPNP: false,
4646
},
4747
Scanner: config.Scanner{
48-
Threads: 10,
48+
BatchSize: 100,
4949
Timeout: 1 * time.Minute,
50+
CheckAgainDelay: 15 * time.Second,
5051
MaxLastScan: 1 * time.Hour,
5152
MinLastAnnouncement: 365 * 24 * time.Hour,
5253
},

config/config.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,24 @@ type (
1818

1919
// Scanner contains the configuration for the host scanner.
2020
Scanner struct {
21-
Threads int `yaml:"threads,omitempty"`
22-
Timeout time.Duration `yaml:"timeout,omitempty"`
23-
MaxLastScan time.Duration `yaml:"maxLastScan,omitempty"`
21+
// BatchSize represents the maximum number of hosts we will
22+
// simultaneously scan.
23+
BatchSize uint64 `yaml:"batchSize,omitempty"`
24+
// Timeout represents the maximum amount of time we will spend scanning
25+
// a single host.
26+
Timeout time.Duration `yaml:"timeout,omitempty"`
27+
// CheckAgainDelay represents the amount of time we will wait before
28+
// calling HostsForScanning again if the previous call returned zero
29+
// hosts to scan.
30+
CheckAgainDelay time.Duration `yaml:"checkAgainDelay,omitempty"`
31+
// MaxLastScan represents how frequently hosts will be scanned. If a
32+
// scan is successful, the hosts next scan time will be set to
33+
// the current time plus MaxLastScan. If it fails, the next scan time
34+
// is set to the current time plus MaxLastScan * pow(2, # of
35+
// consecutive failed scans).
36+
MaxLastScan time.Duration `yaml:"maxLastScan,omitempty"`
37+
// MinLastAnnouncement represents how far back we will search for
38+
// announcements to find hosts to scan.
2439
MinLastAnnouncement time.Duration `yaml:"minLastAnnouncement,omitempty"`
2540
}
2641

explorer/scan.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ import (
1818
"go.uber.org/zap"
1919
)
2020

21-
const (
22-
scanBatchSize = 100
23-
)
24-
2521
func isSynced(b Block) bool {
2622
return time.Since(b.Timestamp) <= 3*time.Hour
2723
}
@@ -226,7 +222,7 @@ func (e *Explorer) scanHosts() {
226222
now := types.CurrentTimestamp()
227223
lastAnnouncementCutoff := now.Add(-e.scanCfg.MinLastAnnouncement)
228224

229-
batch, err := e.s.HostsForScanning(lastAnnouncementCutoff, scanBatchSize)
225+
batch, err := e.s.HostsForScanning(lastAnnouncementCutoff, e.scanCfg.BatchSize)
230226
if err != nil {
231227
e.log.Info("failed to get hosts for scanning:", zap.Error(err))
232228
return
@@ -235,7 +231,8 @@ func (e *Explorer) scanHosts() {
235231
case <-e.ctx.Done():
236232
e.log.Info("shutdown:", zap.Error(e.ctx.Err()))
237233
return
238-
case <-time.After(15 * time.Second):
234+
// wait until we call HostsForScanning again
235+
case <-time.After(e.scanCfg.CheckAgainDelay):
239236
continue // check again
240237
}
241238
}

persist/sqlite/metrics.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"slices"
66
"time"
77

8+
proto4 "go.sia.tech/core/rhp/v4"
9+
810
"go.sia.tech/core/types"
911
"go.sia.tech/explored/explorer"
1012
)
@@ -65,10 +67,27 @@ func (s *Store) HostMetrics() (result explorer.HostMetrics, err error) {
6567
return fmt.Errorf("failed to scan host: %w", err)
6668
}
6769

68-
result.TotalStorage += host.Settings.TotalStorage
69-
result.RemainingStorage += host.Settings.RemainingStorage
70+
if host.V2 {
71+
result.TotalStorage += proto4.SectorSize * host.V2Settings.TotalStorage
72+
result.RemainingStorage += proto4.SectorSize * host.V2Settings.RemainingStorage
73+
74+
v2MaxCollateral = append(v2MaxCollateral, host.V2Settings.MaxCollateral)
75+
v2MaxContractDuration = append(v2MaxContractDuration, host.V2Settings.MaxContractDuration)
76+
v2RemainingStorage = append(v2RemainingStorage, host.V2Settings.RemainingStorage)
77+
v2TotalStorage = append(v2TotalStorage, host.V2Settings.TotalStorage)
78+
79+
v2PricesContractPrice = append(v2PricesContractPrice, host.V2Settings.Prices.ContractPrice)
80+
v2PricesCollateral = append(v2PricesCollateral, host.V2Settings.Prices.Collateral)
81+
v2PricesStoragePrice = append(v2PricesStoragePrice, host.V2Settings.Prices.StoragePrice)
82+
v2PricesIngressPrice = append(v2PricesIngressPrice, host.V2Settings.Prices.IngressPrice)
83+
v2PricesEgressPrice = append(v2PricesEgressPrice, host.V2Settings.Prices.EgressPrice)
84+
v2PricesFreeSectorPrice = append(v2PricesFreeSectorPrice, host.V2Settings.Prices.FreeSectorPrice)
85+
v2PricesTipHeight = append(v2PricesTipHeight, host.V2Settings.Prices.TipHeight)
86+
v2PricesValidUntil = append(v2PricesValidUntil, uint64(host.V2Settings.Prices.ValidUntil.Unix()))
87+
} else {
88+
result.TotalStorage += host.Settings.TotalStorage
89+
result.RemainingStorage += host.Settings.RemainingStorage
7090

71-
if !host.V2 {
7291
settingsMaxDownloadBatchSize = append(settingsMaxDownloadBatchSize, host.Settings.MaxDownloadBatchSize)
7392
settingsMaxDuration = append(settingsMaxDuration, host.Settings.MaxDuration)
7493
settingsMaxReviseBatchSize = append(settingsMaxReviseBatchSize, host.Settings.MaxReviseBatchSize)
@@ -120,20 +139,6 @@ func (s *Store) HostMetrics() (result explorer.HostMetrics, err error) {
120139
priceTableWindowSize = append(priceTableWindowSize, host.PriceTable.WindowSize)
121140
priceTableRegistryEntriesLeft = append(priceTableRegistryEntriesLeft, host.PriceTable.RegistryEntriesLeft)
122141
priceTableRegistryEntriesTotal = append(priceTableRegistryEntriesTotal, host.PriceTable.RegistryEntriesTotal)
123-
} else {
124-
v2MaxCollateral = append(v2MaxCollateral, host.V2Settings.MaxCollateral)
125-
v2MaxContractDuration = append(v2MaxContractDuration, host.V2Settings.MaxContractDuration)
126-
v2RemainingStorage = append(v2RemainingStorage, host.V2Settings.RemainingStorage)
127-
v2TotalStorage = append(v2TotalStorage, host.V2Settings.TotalStorage)
128-
129-
v2PricesContractPrice = append(v2PricesContractPrice, host.V2Settings.Prices.ContractPrice)
130-
v2PricesCollateral = append(v2PricesCollateral, host.V2Settings.Prices.Collateral)
131-
v2PricesStoragePrice = append(v2PricesStoragePrice, host.V2Settings.Prices.StoragePrice)
132-
v2PricesIngressPrice = append(v2PricesIngressPrice, host.V2Settings.Prices.IngressPrice)
133-
v2PricesEgressPrice = append(v2PricesEgressPrice, host.V2Settings.Prices.EgressPrice)
134-
v2PricesFreeSectorPrice = append(v2PricesFreeSectorPrice, host.V2Settings.Prices.FreeSectorPrice)
135-
v2PricesTipHeight = append(v2PricesTipHeight, host.V2Settings.Prices.TipHeight)
136-
v2PricesValidUntil = append(v2PricesValidUntil, uint64(host.V2Settings.Prices.ValidUntil.Unix()))
137142
}
138143

139144
count++

0 commit comments

Comments
 (0)