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

Commit

Permalink
Will be squashed: Uptime 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkeljuhl committed Apr 3, 2018
1 parent 6a3d643 commit 36759d6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmd/siac/hostcmd.go
Expand Up @@ -9,9 +9,9 @@ import (
"text/tabwriter"

"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/node/api"
"github.com/NebulousLabs/Sia/types"

"github.com/NebulousLabs/Sia/node/api"
"github.com/spf13/cobra"
)

Expand Down
8 changes: 3 additions & 5 deletions cmd/siac/hostdbcmd.go
Expand Up @@ -2,14 +2,12 @@ package main

import (
"fmt"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/node/api"
"github.com/spf13/cobra"
"math/big"
"os"
"text/tabwriter"

"github.com/spf13/cobra"

"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/node/api"
)

const scanHistoryLen = 30
Expand Down
3 changes: 3 additions & 0 deletions modules/renter/hostdb/consts.go
Expand Up @@ -34,6 +34,9 @@ const (
// scans start getting compressed.
minScans = 12

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

// recentInteractionWeightLimit caps the number of recent interactions as a
// percentage of the historic interactions, to be certain that a large
// amount of activity in a short period of time does not overwhelm the
Expand Down
37 changes: 20 additions & 17 deletions modules/renter/hostdb/hostweight.go
Expand Up @@ -311,11 +311,9 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 {
// host.
downtime := entry.HistoricDowntime
uptime := entry.HistoricUptime
recentTime := entry.ScanHistory[0].Timestamp
recentBlockHeight := entry.ScanHistory[0].BlockHeight
recentSuccess := entry.ScanHistory[0].Success
recentScan := entry.ScanHistory[0]
for _, scan := range entry.ScanHistory[1:] {
if recentTime.After(scan.Timestamp) {
if recentScan.Timestamp.After(scan.Timestamp) {
if build.DEBUG {
hdb.log.Critical("Host entry scan history not sorted.")
} else {
Expand All @@ -325,19 +323,8 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 {
continue
}

blockDiff := scan.BlockHeight - recentBlockHeight
timePassed := float64(time.Duration(blockDiff) * 10 * time.Minute)
halftime := float64(1 * time.Hour * 24 * 30)
decay := math.Pow(0.5, timePassed/halftime)

if recentSuccess {
uptime += time.Duration(timePassed * decay)
} else {
downtime += time.Duration(timePassed * decay)
}
recentTime = scan.Timestamp
recentBlockHeight = scan.BlockHeight
recentSuccess = scan.Success
decayUptimeOrDowntime(&entry, scan, recentScan)
recentScan = scan
}
// Sanity check against 0 total time.
if uptime == 0 && downtime == 0 {
Expand Down Expand Up @@ -375,6 +362,22 @@ 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
16 changes: 1 addition & 15 deletions modules/renter/hostdb/scan.go
Expand Up @@ -5,7 +5,6 @@ package hostdb
// settings of the hosts.

import (
"math"
"net"
"time"

Expand Down Expand Up @@ -208,20 +207,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 {
oldBlockHeight := newEntry.ScanHistory[0].BlockHeight
newBlockHeight := newEntry.ScanHistory[1].BlockHeight

timePassed := float64(time.Duration(newBlockHeight-oldBlockHeight) * 10 * time.Minute)
halftime := float64(1 * time.Hour * 24 * 30)
decay := math.Pow(0.5, timePassed/halftime)

if newEntry.ScanHistory[0].Success {
newEntry.HistoricUptime *= time.Duration(decay)
newEntry.HistoricUptime += time.Duration(timePassed * decay)
} else {
newEntry.HistoricDowntime *= time.Duration(decay)
newEntry.HistoricDowntime += time.Duration(timePassed * decay)
}
decayUptimeOrDowntime(&newEntry, newEntry.ScanHistory[1], newEntry.ScanHistory[0])
newEntry.ScanHistory = newEntry.ScanHistory[1:]
}

Expand Down

0 comments on commit 36759d6

Please sign in to comment.