Commit 2e218ac4 authored by David Vorick's avatar David Vorick

fix broken tests, fix broken uptime calcs

parent 13b243a8
Pipeline #57482369 passed with stages
in 32 minutes and 26 seconds
......@@ -3,6 +3,7 @@ package hostdb
import (
"fmt"
"math"
"time"
"gitlab.com/NebulousLabs/Sia/build"
"gitlab.com/NebulousLabs/Sia/modules"
......@@ -396,6 +397,11 @@ func (hdb *HostDB) lifetimeAdjustments(entry modules.HostDBEntry) float64 {
// new host to give the host some initial uptime or downtime. Modification of
// this function needs to be made paying attention to the structure of that
// function.
//
// TODO: This function doesn't correctly handle situations where the user's
// clock goes back in time. If the user adjusts their system clock to be in the
// past, we'll get timestamping that's out of order, and this will cause erratic
// / improper / untested behavior.
func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 {
// Special case: if we have scanned the host twice or fewer, don't perform
// uptime math.
......@@ -429,7 +435,7 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 {
if build.DEBUG {
hdb.log.Critical("Host entry scan history not sorted.")
} else {
hdb.log.Print("WARNING: Host entry scan history not sorted.")
hdb.log.Print("WARN: Host entry scan history not sorted.")
}
// Ignore the unsorted scan entry.
continue
......@@ -442,8 +448,19 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 {
recentTime = scan.Timestamp
recentSuccess = scan.Success
}
// One more check to incorporate the uptime or downtime of the most recent
// scan, we assume that if we scanned them right now, their uptime /
// downtime status would be equal to what it currently is.
if recentSuccess {
uptime += time.Now().Sub(recentTime)
} else {
downtime += time.Now().Sub(recentTime)
}
// Sanity check against 0 total time.
if uptime == 0 && downtime == 0 {
build.Critical("uptime and downtime are zero for this host, should have been caught in earlier logic")
return 0.001 // Shouldn't happen.
}
......
......@@ -174,8 +174,8 @@ func TestHostWeightWithNoCollateral(t *testing.T) {
}
}
// TestHostWeightMaxDuration checks that the host with more collateral has more
// weight.
// TestHostWeightMaxDuration checks that the host with an unacceptable duration
// has a lower score.
func TestHostWeightMaxDuration(t *testing.T) {
if testing.Short() {
t.SkipNow()
......@@ -208,7 +208,7 @@ func TestHostWeightCollateralDifferences(t *testing.T) {
w1 := hdb.weightFunc(entry).Score()
w2 := hdb.weightFunc(entry2).Score()
if w1.Cmp(w2) < 0 {
if w1.Cmp(w2) <= 0 {
t.Error("Larger collateral should have more weight")
}
}
......@@ -306,16 +306,17 @@ func TestHostWeightUptimeDifferences(t *testing.T) {
w1 := hdb.weightFunc(entry).Score()
w2 := hdb.weightFunc(entry2).Score()
if w1.Cmp(w2) < 0 {
if w1.Cmp(w2) <= 0 {
t.Log(w1)
t.Log(w2)
t.Error("Been around longer should have more weight")
t.Error("A host with recorded downtime should have a lower score")
}
}
// TestHostWeightUptimeDifferences2 checks that hosts with poorer uptimes have
// lower weights.
func TestHostWeightUptimeDifferences2(t *testing.T) {
t.Skip("Hostdb is not currently doing exponentiation on uptime")
if testing.Short() {
t.SkipNow()
}
......@@ -324,6 +325,11 @@ func TestHostWeightUptimeDifferences2(t *testing.T) {
entry := DefaultHostDBEntry
entry.ScanHistory = modules.HostDBScans{
{Timestamp: time.Now().Add(time.Hour * -200), Success: true},
{Timestamp: time.Now().Add(time.Hour * -180), Success: true},
{Timestamp: time.Now().Add(time.Hour * -160), Success: true},
{Timestamp: time.Now().Add(time.Hour * -140), Success: true},
{Timestamp: time.Now().Add(time.Hour * -120), Success: true},
{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
{Timestamp: time.Now().Add(time.Hour * -80), Success: false},
{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
......@@ -333,6 +339,11 @@ func TestHostWeightUptimeDifferences2(t *testing.T) {
entry2 := entry
entry2.ScanHistory = modules.HostDBScans{
{Timestamp: time.Now().Add(time.Hour * -200), Success: true},
{Timestamp: time.Now().Add(time.Hour * -180), Success: true},
{Timestamp: time.Now().Add(time.Hour * -160), Success: true},
{Timestamp: time.Now().Add(time.Hour * -140), Success: true},
{Timestamp: time.Now().Add(time.Hour * -120), Success: true},
{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
{Timestamp: time.Now().Add(time.Hour * -80), Success: true},
{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
......@@ -342,8 +353,10 @@ func TestHostWeightUptimeDifferences2(t *testing.T) {
w1 := hdb.weightFunc(entry).Score()
w2 := hdb.weightFunc(entry2).Score()
if w1.Cmp(w2) < 0 {
t.Errorf("Been around longer should have more weight\n\t%v\n\t%v", w1, w2)
if w1.Cmp(w2) <= 0 {
t.Log(w1)
t.Log(w2)
t.Errorf("Downtime that's further in the past should be penalized less")
}
}
......@@ -358,6 +371,11 @@ func TestHostWeightUptimeDifferences3(t *testing.T) {
entry := DefaultHostDBEntry
entry.ScanHistory = modules.HostDBScans{
{Timestamp: time.Now().Add(time.Hour * -200), Success: true},
{Timestamp: time.Now().Add(time.Hour * -180), Success: true},
{Timestamp: time.Now().Add(time.Hour * -160), Success: true},
{Timestamp: time.Now().Add(time.Hour * -140), Success: true},
{Timestamp: time.Now().Add(time.Hour * -120), Success: true},
{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
{Timestamp: time.Now().Add(time.Hour * -80), Success: false},
{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
......@@ -367,6 +385,11 @@ func TestHostWeightUptimeDifferences3(t *testing.T) {
entry2 := entry
entry2.ScanHistory = modules.HostDBScans{
{Timestamp: time.Now().Add(time.Hour * -200), Success: true},
{Timestamp: time.Now().Add(time.Hour * -180), Success: true},
{Timestamp: time.Now().Add(time.Hour * -160), Success: true},
{Timestamp: time.Now().Add(time.Hour * -140), Success: true},
{Timestamp: time.Now().Add(time.Hour * -120), Success: true},
{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
{Timestamp: time.Now().Add(time.Hour * -80), Success: false},
{Timestamp: time.Now().Add(time.Hour * -60), Success: false},
......@@ -376,8 +399,10 @@ func TestHostWeightUptimeDifferences3(t *testing.T) {
w1 := hdb.weightFunc(entry).Score()
w2 := hdb.weightFunc(entry2).Score()
if w1.Cmp(w2) < 0 {
t.Error("Been around longer should have more weight")
if w1.Cmp(w2) <= 0 {
t.Log(w1)
t.Log(w2)
t.Error("A host with longer downtime should have a lower score")
}
}
......@@ -392,6 +417,11 @@ func TestHostWeightUptimeDifferences4(t *testing.T) {
entry := DefaultHostDBEntry
entry.ScanHistory = modules.HostDBScans{
{Timestamp: time.Now().Add(time.Hour * -200), Success: true},
{Timestamp: time.Now().Add(time.Hour * -180), Success: true},
{Timestamp: time.Now().Add(time.Hour * -160), Success: true},
{Timestamp: time.Now().Add(time.Hour * -140), Success: true},
{Timestamp: time.Now().Add(time.Hour * -120), Success: true},
{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
{Timestamp: time.Now().Add(time.Hour * -80), Success: true},
{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
......@@ -401,6 +431,11 @@ func TestHostWeightUptimeDifferences4(t *testing.T) {
entry2 := entry
entry2.ScanHistory = modules.HostDBScans{
{Timestamp: time.Now().Add(time.Hour * -200), Success: true},
{Timestamp: time.Now().Add(time.Hour * -180), Success: true},
{Timestamp: time.Now().Add(time.Hour * -160), Success: true},
{Timestamp: time.Now().Add(time.Hour * -140), Success: true},
{Timestamp: time.Now().Add(time.Hour * -120), Success: true},
{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
{Timestamp: time.Now().Add(time.Hour * -80), Success: true},
{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
......@@ -410,8 +445,10 @@ func TestHostWeightUptimeDifferences4(t *testing.T) {
w1 := hdb.weightFunc(entry).Score()
w2 := hdb.weightFunc(entry2).Score()
if w1.Cmp(w2) < 0 {
t.Error("Been around longer should have more weight")
if w1.Cmp(w2) <= 0 {
t.Log(w1)
t.Log(w2)
t.Error("longer tail downtime should have a lower score")
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment