gitlab.com/jokerrs1/Sia@v1.3.2/modules/renter/hostdb/persist.go (about)

     1  package hostdb
     2  
     3  import (
     4  	"path/filepath"
     5  	"time"
     6  
     7  	"github.com/NebulousLabs/Sia/modules"
     8  	"github.com/NebulousLabs/Sia/persist"
     9  	"github.com/NebulousLabs/Sia/types"
    10  )
    11  
    12  var (
    13  	// persistFilename defines the name of the file that holds the hostdb's
    14  	// persistence.
    15  	persistFilename = "hostdb.json"
    16  
    17  	// persistMetadata defines the metadata that tags along with the most recent
    18  	// version of the hostdb persistence file.
    19  	persistMetadata = persist.Metadata{
    20  		Header:  "HostDB Persistence",
    21  		Version: "0.5",
    22  	}
    23  )
    24  
    25  // hdbPersist defines what HostDB data persists across sessions.
    26  type hdbPersist struct {
    27  	AllHosts    []modules.HostDBEntry
    28  	BlockHeight types.BlockHeight
    29  	LastChange  modules.ConsensusChangeID
    30  }
    31  
    32  // persistData returns the data in the hostdb that will be saved to disk.
    33  func (hdb *HostDB) persistData() (data hdbPersist) {
    34  	data.AllHosts = hdb.hostTree.All()
    35  	data.BlockHeight = hdb.blockHeight
    36  	data.LastChange = hdb.lastChange
    37  	return data
    38  }
    39  
    40  // saveSync saves the hostdb persistence data to disk and then syncs to disk.
    41  func (hdb *HostDB) saveSync() error {
    42  	return hdb.deps.SaveFileSync(persistMetadata, hdb.persistData(), filepath.Join(hdb.persistDir, persistFilename))
    43  }
    44  
    45  // load loads the hostdb persistence data from disk.
    46  func (hdb *HostDB) load() error {
    47  	// Fetch the data from the file.
    48  	var data hdbPersist
    49  	err := hdb.deps.LoadFile(persistMetadata, &data, filepath.Join(hdb.persistDir, persistFilename))
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	// Set the hostdb internal values.
    55  	hdb.blockHeight = data.BlockHeight
    56  	hdb.lastChange = data.LastChange
    57  
    58  	// Load each of the hosts into the host tree.
    59  	for _, host := range data.AllHosts {
    60  		// COMPATv1.1.0
    61  		//
    62  		// The host did not always track its block height correctly, meaning
    63  		// that previously the FirstSeen values and the blockHeight values
    64  		// could get out of sync.
    65  		if hdb.blockHeight < host.FirstSeen {
    66  			host.FirstSeen = hdb.blockHeight
    67  		}
    68  
    69  		err := hdb.hostTree.Insert(host)
    70  		if err != nil {
    71  			hdb.log.Debugln("ERROR: could not insert host while loading:", host.NetAddress)
    72  		}
    73  
    74  		// Make sure that all hosts have gone through the initial scanning.
    75  		if len(host.ScanHistory) < 2 {
    76  			hdb.queueScan(host)
    77  		}
    78  	}
    79  	return nil
    80  }
    81  
    82  // threadedSaveLoop saves the hostdb to disk every 2 minutes, also saving when
    83  // given the shutdown signal.
    84  func (hdb *HostDB) threadedSaveLoop() {
    85  	for {
    86  		select {
    87  		case <-hdb.tg.StopChan():
    88  			return
    89  		case <-time.After(saveFrequency):
    90  			hdb.mu.Lock()
    91  			err := hdb.saveSync()
    92  			hdb.mu.Unlock()
    93  			if err != nil {
    94  				hdb.log.Println("Difficulties saving the hostdb:", err)
    95  			}
    96  		}
    97  	}
    98  }