1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00
alice-lg-alice-lg/backend/neighbours_store.go

195 lines
4.1 KiB
Go
Raw Normal View History

2017-06-23 16:27:09 +02:00
package main
import (
"log"
"regexp"
"strconv"
2017-07-14 14:18:47 +02:00
"sync"
2017-06-23 16:27:09 +02:00
"time"
2017-07-14 14:18:47 +02:00
2018-06-19 10:02:16 +02:00
"github.com/alice-lg/alice-lg/backend/api"
2017-06-23 16:27:09 +02:00
)
2017-06-23 17:40:19 +02:00
type NeighboursIndex map[string]api.Neighbour
2017-06-23 16:27:09 +02:00
type NeighboursStore struct {
2017-06-23 18:01:49 +02:00
neighboursMap map[int]NeighboursIndex
configMap map[int]SourceConfig
statusMap map[int]StoreStatus
2017-07-14 14:18:47 +02:00
2018-06-26 10:51:11 +02:00
sync.RWMutex
2017-06-23 17:40:19 +02:00
}
func NewNeighboursStore(config *Config) *NeighboursStore {
// Build source mapping
2017-06-23 18:01:49 +02:00
neighboursMap := make(map[int]NeighboursIndex)
configMap := make(map[int]SourceConfig)
statusMap := make(map[int]StoreStatus)
2017-06-23 17:40:19 +02:00
for _, source := range config.Sources {
2017-06-23 18:01:49 +02:00
sourceId := source.Id
configMap[sourceId] = source
statusMap[sourceId] = StoreStatus{
2017-06-23 17:40:19 +02:00
State: STATE_INIT,
}
2017-06-23 18:01:49 +02:00
neighboursMap[sourceId] = make(NeighboursIndex)
2017-06-23 17:40:19 +02:00
}
store := &NeighboursStore{
neighboursMap: neighboursMap,
statusMap: statusMap,
configMap: configMap,
}
return store
}
func (self *NeighboursStore) Start() {
log.Println("Starting local neighbours store")
go self.init()
}
func (self *NeighboursStore) init() {
// Perform initial update
self.update()
// Initial logging
self.Stats().Log()
// Periodically update store
for {
time.Sleep(5 * time.Minute)
self.update()
}
}
func (self *NeighboursStore) update() {
2017-06-23 18:01:49 +02:00
for sourceId, _ := range self.neighboursMap {
2017-06-23 17:40:19 +02:00
// Get current state
2017-06-23 18:01:49 +02:00
if self.statusMap[sourceId].State == STATE_UPDATING {
2017-06-23 17:40:19 +02:00
continue // nothing to do here. really.
}
// Start updating
2018-06-26 10:51:11 +02:00
self.Lock()
2017-06-23 18:01:49 +02:00
self.statusMap[sourceId] = StoreStatus{
2017-06-23 17:40:19 +02:00
State: STATE_UPDATING,
}
2018-06-26 10:51:11 +02:00
self.Unlock()
2017-06-23 17:40:19 +02:00
2017-06-23 18:01:49 +02:00
source := self.configMap[sourceId].getInstance()
2017-06-23 17:40:19 +02:00
neighboursRes, err := source.Neighbours()
neighbours := neighboursRes.Neighbours
if err != nil {
// That's sad.
2018-06-26 10:51:11 +02:00
self.Lock()
2017-06-23 18:01:49 +02:00
self.statusMap[sourceId] = StoreStatus{
2017-06-23 17:40:19 +02:00
State: STATE_ERROR,
LastError: err,
LastRefresh: time.Now(),
}
2018-06-26 10:51:11 +02:00
self.Unlock()
2017-06-23 17:40:19 +02:00
continue
}
// Update data
// Make neighbours index
index := make(NeighboursIndex)
for _, neighbour := range neighbours {
index[neighbour.Id] = neighbour
}
2018-06-26 10:51:11 +02:00
self.Lock()
2017-06-23 18:01:49 +02:00
self.neighboursMap[sourceId] = index
2017-06-23 17:40:19 +02:00
// Update state
2017-06-23 18:01:49 +02:00
self.statusMap[sourceId] = StoreStatus{
2017-06-23 17:40:19 +02:00
LastRefresh: time.Now(),
State: STATE_READY,
}
2018-06-26 10:51:11 +02:00
self.Unlock()
2017-06-23 17:40:19 +02:00
}
}
func (self *NeighboursStore) GetNeighbourAt(
2017-06-23 18:01:49 +02:00
sourceId int,
2017-06-23 17:40:19 +02:00
id string,
) api.Neighbour {
// Lookup neighbour on RS
2018-06-26 10:51:11 +02:00
self.RLock()
2017-06-23 18:01:49 +02:00
neighbours := self.neighboursMap[sourceId]
2018-06-26 10:51:11 +02:00
self.RUnlock()
2017-06-23 17:40:19 +02:00
return neighbours[id]
}
2017-06-29 14:24:08 +02:00
func (self *NeighboursStore) LookupNeighboursAt(
sourceId int,
query string,
) []api.Neighbour {
results := []api.Neighbour{}
2018-06-26 10:51:11 +02:00
self.RLock()
neighbours := self.neighboursMap[sourceId]
2018-06-26 10:51:11 +02:00
self.RUnlock()
asn := -1
if regex := regexp.MustCompile(`^AS(\d+)`); regex.MatchString(query) {
groups := regex.FindStringSubmatch(query)
if a, err := strconv.Atoi(groups[1]); err == nil {
asn = a
}
}
for _, neighbour := range neighbours {
if asn >= 0 && neighbour.Asn == asn { // only executed if valid AS query is detected
results = append(results, neighbour)
} else if ContainsCi(neighbour.Description, query) {
results = append(results, neighbour)
} else {
continue
}
}
return results
}
func (self *NeighboursStore) LookupNeighbours(
query string,
) api.NeighboursLookupResults {
// Create empty result set
results := make(api.NeighboursLookupResults)
for sourceId, _ := range self.neighboursMap {
results[sourceId] = self.LookupNeighboursAt(sourceId, query)
}
2017-06-29 14:24:08 +02:00
return results
}
2017-06-23 17:40:19 +02:00
// Build some stats for monitoring
func (self *NeighboursStore) Stats() NeighboursStoreStats {
totalNeighbours := 0
rsStats := []RouteServerNeighboursStats{}
2018-06-26 10:51:11 +02:00
self.RLock()
2017-06-23 18:01:49 +02:00
for sourceId, neighbours := range self.neighboursMap {
status := self.statusMap[sourceId]
2017-06-23 17:40:19 +02:00
totalNeighbours += len(neighbours)
serverStats := RouteServerNeighboursStats{
2017-06-23 18:01:49 +02:00
Name: self.configMap[sourceId].Name,
2017-06-23 17:40:19 +02:00
State: stateToString(status.State),
Neighbours: len(neighbours),
UpdatedAt: status.LastRefresh,
}
rsStats = append(rsStats, serverStats)
}
2018-06-26 10:51:11 +02:00
self.RUnlock()
2017-06-23 17:40:19 +02:00
storeStats := NeighboursStoreStats{
TotalNeighbours: totalNeighbours,
RouteServers: rsStats,
}
return storeStats
2017-06-23 16:27:09 +02:00
}