Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Move updateUptime to scan.go; don't apply decay when calculating host…
Browse files Browse the repository at this point in the history
… weight
  • Loading branch information
mikkeljuhl committed Apr 27, 2018
1 parent f2a7e4d commit 23024af
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions modules/renter/hostdb/consts.go
Expand Up @@ -34,8 +34,8 @@ const (
// scans start getting compressed.
minScans = 12

// halftimeUpDownTime is the halftime used to decay the host up and downtime
halftimeUpDownTime = 30 * 24 * time.Hour
// uptimeHalflife is the halftime used to decay the host up and downtime
uptimeHalflife = 30 * 24 * time.Hour

// recentInteractionWeightLimit caps the number of recent interactions as a
// percentage of the historic interactions, to be certain that a large
Expand Down
32 changes: 11 additions & 21 deletions modules/renter/hostdb/hostweight.go
@@ -1,13 +1,12 @@
package hostdb

import (
"math"
"math/big"
"time"

"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/types"
"math"
"math/big"
"time"
)

var (
Expand Down Expand Up @@ -323,7 +322,14 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 {
continue
}

decayUptimeOrDowntime(&entry, scan, recentScan)
blocksPassed := scan.BlockHeight - recentScan.BlockHeight
timePassed := time.Duration(blocksPassed) * 10 * time.Minute

if recentScan.Success {
uptime += timePassed
} else {
downtime += timePassed
}
recentScan = scan
}
// Sanity check against 0 total time.
Expand Down Expand Up @@ -362,22 +368,6 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 {
return math.Pow(uptimeRatio, exp)
}

// decayHostUpOrDowntime decays a host's historic uptime or historic downtime.
// It also adds the new block height to the historic uptime or historic downtime.
func decayUptimeOrDowntime(entry *modules.HostDBEntry, scan modules.HostDBScan, recentScan modules.HostDBScan) {
blocksPassed := scan.BlockHeight - recentScan.BlockHeight
timePassed := time.Duration(blocksPassed) * 10 * time.Minute
decay := time.Duration(math.Pow(0.5, float64(timePassed)/float64(halftimeUpDownTime)))

if recentScan.Success {
entry.HistoricUptime *= decay
entry.HistoricUptime += timePassed * decay
} else {
entry.HistoricDowntime *= decay
entry.HistoricDowntime += timePassed * decay
}
}

// calculateHostWeight returns the weight of a host according to the settings of
// the host database entry.
func (hdb *HostDB) calculateHostWeight(entry modules.HostDBEntry) types.Currency {
Expand Down
19 changes: 18 additions & 1 deletion modules/renter/hostdb/scan.go
Expand Up @@ -13,8 +13,25 @@ import (
"github.com/NebulousLabs/Sia/encoding"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/fastrand"
"math"
)

// updateUptime decays a host's historic uptime or historic downtime.
// It also adds the new block height to the historic uptime or historic downtime.
func updateUptime(entry *modules.HostDBEntry, scan modules.HostDBScan, recentScan modules.HostDBScan) {
blocksPassed := scan.BlockHeight - recentScan.BlockHeight
timePassed := time.Duration(blocksPassed) * 10 * time.Minute
decay := time.Duration(math.Pow(0.5, float64(timePassed)/float64(uptimeHalflife)))
entry.HistoricUptime *= decay
entry.HistoricDowntime *= decay

if recentScan.Success {
entry.HistoricUptime += timePassed * decay
} else {
entry.HistoricDowntime += timePassed * decay
}
}

// queueScan will add a host to the queue to be scanned.
func (hdb *HostDB) queueScan(entry modules.HostDBEntry) {
// If this entry is already in the scan pool, can return immediately.
Expand Down Expand Up @@ -207,7 +224,7 @@ func (hdb *HostDB) updateEntry(entry modules.HostDBEntry, netErr error) {

// Compress any old scans into the historic values.
for len(newEntry.ScanHistory) > minScans && time.Now().Sub(newEntry.ScanHistory[0].Timestamp) > maxHostDowntime {
decayUptimeOrDowntime(&newEntry, newEntry.ScanHistory[1], newEntry.ScanHistory[0])
updateUptime(&newEntry, newEntry.ScanHistory[1], newEntry.ScanHistory[0])
newEntry.ScanHistory = newEntry.ScanHistory[1:]
}

Expand Down

0 comments on commit 23024af

Please sign in to comment.