Commit dbcca10e authored by David Vorick's avatar David Vorick

compress scan histories, toss very offline hosts

parent 1cde4d32
......@@ -79,12 +79,21 @@ type FileInfo struct {
// aggregates the host's external settings and metrics with its public key.
type HostDBEntry struct {
HostExternalSettings
PublicKey types.SiaPublicKey `json:"publickey"`
// ScanHistory is the set of scans performed on the host. It should always
// be ordered according to the scan's Timestamp, oldest to newest.
ScanHistory HostDBScans `json:"scanhistory"`
// FirstSeen is the last block height at which this host was announced.
FirstSeen types.BlockHeight `json:"firstseen"`
// Measurements that have been taken on the host. The most recent
// measurements are kept in full detail, historic ones are compressed into
// the historic values.
HistoricDowntime time.Duration `json:"historicdowntime"`
HistoricUptime time.Duration `json:"historicuptime"`
ScanHistory HostDBScans `json:"scanhistory"`
// The public key of the host, stored separately to minimize risk of certain
// MitM based vulnerabilities.
PublicKey types.SiaPublicKey `json:"publickey"`
}
// HostDBScan represents a single scan event.
......
......@@ -11,10 +11,18 @@ const (
// cannot successfully get a random number.
defaultScanSleep = 1*time.Hour + 37*time.Minute
// maxHostDowntime specifies the maximum amount of time that a host is
// allowed to be offline while still being in the hostdb.
maxHostDowntime = 30 * 24 * time.Hour
// maxScanSleep is the maximum amount of time that the hostdb will sleep
// between performing scans of the hosts.
maxScanSleep = 4 * time.Hour
// minScans specifies the number of scans that a host should have before the
// scans start getting compressed.
minScans = 20
// minScanSleep is the minimum amount of time that the hostdb will sleep
// between performing scans of the hosts.
minScanSleep = 1*time.Hour + 20*time.Minute
......
......@@ -4,9 +4,6 @@
// set of hosts it has found and updates who is online.
package hostdb
// TODO: Scan history should be truncated. Further, if a host hasn't been around
// in long enough, they should just be removed (1 month).
import (
"errors"
"fmt"
......
......@@ -3,7 +3,6 @@ package hostdb
import (
"math"
"math/big"
"time"
"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/modules"
......@@ -256,8 +255,8 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 {
// Compute the total measured uptime and total measured downtime for this
// host.
var uptime time.Duration
var downtime time.Duration
downtime := entry.HistoricDowntime
uptime := entry.HistoricUptime
recentTime := entry.ScanHistory[0].Timestamp
recentSuccess := entry.ScanHistory[0].Success
for _, scan := range entry.ScanHistory[1:] {
......
......@@ -126,6 +126,38 @@ func (hdb *HostDB) updateEntry(entry modules.HostDBEntry, netErr error) {
newEntry.ScanHistory = append(newEntry.ScanHistory, modules.HostDBScan{Timestamp: time.Now(), Success: netErr == nil})
}
// If the host's earliest scan is more than a month old and there is no
// recent uptime, mark the host for deletion.
var recentUptime bool
for _, scan := range entry.ScanHistory {
if scan.Success {
recentUptime = true
}
}
// If the host has been offline for too long, delete the host from the
// hostdb.
if time.Now().Sub(newEntry.ScanHistory[0].Timestamp) > maxHostDowntime && !recentUptime && len(newEntry.ScanHistory) >= minScans {
err := hdb.hostTree.Remove(newEntry.PublicKey)
if err != nil {
hdb.log.Println("ERROR: unable to remove host newEntry which has had a ton of downtime:", err)
}
// The function should terminate here as no more interaction is needed
// with this host.
return
}
// Compress any old scans into the historic values.
for len(newEntry.ScanHistory) > minScans && time.Now().Sub(newEntry.ScanHistory[0].Timestamp) > maxHostDowntime {
timePassed := newEntry.ScanHistory[1].Timestamp.Sub(newEntry.ScanHistory[0].Timestamp)
if newEntry.ScanHistory[1].Success {
newEntry.HistoricUptime += timePassed
} else {
newEntry.HistoricDowntime += timePassed
}
}
// Add the updated entry
if !exists {
err := hdb.hostTree.Insert(newEntry)
......
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