routing: add mission control import functionality

This commit is contained in:
carla 2021-03-18 10:46:45 +02:00
parent 6046c5cf86
commit 22491ad567
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91
2 changed files with 71 additions and 0 deletions

@ -342,6 +342,27 @@ func (m *MissionControl) GetHistorySnapshot() *MissionControlSnapshot {
return m.state.getSnapshot()
}
// ImportHistory imports the set of mission control results provided to our
// in-memory state. These results are not persisted, so will not survive
// restarts.
func (m *MissionControl) ImportHistory(history *MissionControlSnapshot) error {
if history == nil {
return errors.New("cannot import nil history")
}
m.Lock()
defer m.Unlock()
log.Infof("Importing history snapshot with %v pairs to mission control",
len(history.Pairs))
imported := m.state.importSnapshot(history)
log.Infof("Imported %v results to mission control", imported)
return nil
}
// GetPairHistorySnapshot returns the stored history for a given node pair.
func (m *MissionControl) GetPairHistorySnapshot(
fromNode, toNode route.Vertex) TimedPairResult {

@ -209,3 +209,53 @@ func (m *missionControlState) getSnapshot() *MissionControlSnapshot {
return &snapshot
}
// importSnapshot takes an existing snapshot and merges it with our current
// state if the result provided are fresher than our current results. It returns
// the number of pairs that were used.
func (m *missionControlState) importSnapshot(snapshot *MissionControlSnapshot) int {
var imported int
for _, pair := range snapshot.Pairs {
fromNode := pair.Pair.From
toNode := pair.Pair.To
results, found := m.getLastPairResult(fromNode)
if !found {
results = make(map[route.Vertex]TimedPairResult)
}
lastResult := results[toNode]
failResult := failPairResult(pair.FailAmt)
imported += m.importResult(
lastResult.FailTime, pair.FailTime, failResult,
fromNode, toNode,
)
successResult := successPairResult(pair.SuccessAmt)
imported += m.importResult(
lastResult.SuccessTime, pair.SuccessTime, successResult,
fromNode, toNode,
)
}
return imported
}
func (m *missionControlState) importResult(currentTs, importedTs time.Time,
importedResult pairResult, fromNode, toNode route.Vertex) int {
if currentTs.After(importedTs) {
log.Debugf("Not setting pair result for %v->%v (%v) "+
"success=%v, timestamp %v older than last result %v",
fromNode, toNode, importedResult.amt,
importedResult.success, importedTs, currentTs)
return 0
}
m.setLastPairResult(fromNode, toNode, importedTs, &importedResult)
return 1
}