Store a new row's earliest timestamp as first_seen (closes #112)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
Each handler took the latest time per key in a batch and inserted it as a new row's first_seen, so a key updated twice in one batch, seconds apart, was stored first seen at the later time, and the IP lookup could answer with a prefix first_seen after the route's last_seen. The prefix handler now keeps the earliest time of all of the batch's updates for each prefix. The AS, peer and peering handlers keep the earliest and the latest time per key in database.SeenTimes, and GetOrCreateASNBatch, UpdatePeerBatch and RecordPeering store a new row first seen at the earliest and last seen at the latest. A stored row still has only its last_seen set, to the latest. Model: opus-5-5
This commit was merged in pull request #137.
This commit is contained in:
@@ -37,8 +37,8 @@ const (
|
||||
|
||||
// PeerHandler tracks BGP peers from UPDATE messages using batched operations.
|
||||
// It maintains a queue of peer updates and periodically flushes them to the database
|
||||
// in batches to improve performance. The handler deduplicates peer updates within
|
||||
// each batch, keeping only the most recent update for each peer IP address.
|
||||
// in batches to improve performance. The handler writes one row per peer IP
|
||||
// address for each batch, with the earliest and the latest time of its updates.
|
||||
type PeerHandler struct {
|
||||
db database.Store
|
||||
logger *logger.Logger
|
||||
@@ -227,31 +227,28 @@ func (h *PeerHandler) flushBatch() {
|
||||
return
|
||||
}
|
||||
|
||||
// Deduplicate by peer IP, keeping the latest update for each peer
|
||||
peerMap := make(map[string]peerUpdate)
|
||||
// The earliest and the latest time the batch saw each peer, with the peer
|
||||
// AS and message type of its latest update. The batch holds several
|
||||
// updates for a peer, so a new peer is stored first seen at the earliest of
|
||||
// them and last seen at the latest.
|
||||
peers := make(map[string]database.PeerUpdate)
|
||||
for _, update := range batch {
|
||||
existing, ok := peerMap[update.peerIP]
|
||||
if !ok || update.timestamp.After(existing.timestamp) {
|
||||
peerMap[update.peerIP] = update
|
||||
peer, ok := peers[update.peerIP]
|
||||
if !ok || update.timestamp.After(peer.Seen.Last) {
|
||||
peer.PeerIP = update.peerIP
|
||||
peer.PeerASN = update.peerASN
|
||||
peer.MessageType = update.messageType
|
||||
}
|
||||
}
|
||||
|
||||
// Convert to database format
|
||||
dbPeerMap := make(map[string]database.PeerUpdate)
|
||||
for peerIP, update := range peerMap {
|
||||
dbPeerMap[peerIP] = database.PeerUpdate{
|
||||
PeerIP: update.peerIP,
|
||||
PeerASN: update.peerASN,
|
||||
MessageType: update.messageType,
|
||||
Timestamp: update.timestamp,
|
||||
}
|
||||
peer.Seen.Add(update.timestamp)
|
||||
peers[update.peerIP] = peer
|
||||
}
|
||||
|
||||
// Process all peers in a single batch transaction
|
||||
err := h.db.UpdatePeerBatch(dbPeerMap)
|
||||
err := h.db.UpdatePeerBatch(peers)
|
||||
if err != nil {
|
||||
h.logger.Error("Failed to process peer batch",
|
||||
"error", err, "count", len(dbPeerMap))
|
||||
"error", err, "count", len(peers))
|
||||
}
|
||||
|
||||
h.mu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user