Repository navigation
feat(service-disco): routing table manager #2274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
20bf048
150e172
e7c255f
14e449f
5df8d19
dd39963
a00e3ed
309c2e9
2d6dd67
423c403
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| # Copyright (c) Status Research & Development GmbH | ||
|
|
||
| import std/[tables, sequtils] | ||
| import chronos, chronicles, results | ||
| import ../kademlia | ||
| import ../kademlia/[types, routing_table] | ||
| import ./types | ||
| import ./service_discovery_metrics | ||
|
|
||
| logScope: | ||
| topics = "service-disco service-routing-tables" | ||
|
|
||
| type | ||
| ServiceStatus* = enum | ||
| Interest = 0 | ||
| Provided = 1 | ||
| Both = 2 | ||
|
|
||
| ServiceRoutingTableManager* = ref object | ||
| tables*: Table[ServiceId, RoutingTable] | ||
| serviceStatus*: Table[ServiceId, ServiceStatus] | ||
|
|
||
| proc updateServiceTablesMetrics(manager: ServiceRoutingTableManager) {.raises: [].} = | ||
| cd_service_tables_count.set(manager.tables.len.float64) | ||
| var totalPeers = 0 | ||
| for table in manager.tables.values: | ||
| for bucket in table.buckets: | ||
| totalPeers += bucket.peers.len | ||
| cd_service_table_peers.set(totalPeers.float64) | ||
|
|
||
| proc new*(T: typedesc[ServiceRoutingTableManager]): T = | ||
| T( | ||
| tables: initTable[ServiceId, RoutingTable](), | ||
| serviceStatus: initTable[ServiceId, ServiceStatus](), | ||
| ) | ||
|
|
||
| proc addService*( | ||
| manager: ServiceRoutingTableManager, | ||
| serviceId: ServiceId, | ||
| mainRoutingTable: RoutingTable, | ||
| replication: int, | ||
| bucketsCount: int, | ||
| status: ServiceStatus, | ||
| ): bool = | ||
| # Fast path: service already exists | ||
| manager.serviceStatus.withValue(serviceId, currentStatus): | ||
| # No change needed | ||
| if currentStatus[] == status or currentStatus[] == Both: | ||
| return false | ||
|
|
||
| # Merge states | ||
| manager.serviceStatus[serviceId] = Both | ||
| manager.updateServiceTablesMetrics() | ||
| return true | ||
|
|
||
| # Create new routing table | ||
| var rtable = RoutingTable.new( | ||
| serviceId, | ||
| config = | ||
| RoutingTableConfig.new(replication = replication, maxBuckets = bucketsCount), | ||
| ) | ||
|
|
||
| # Seed from main table | ||
| for bucket in mainRoutingTable.buckets: | ||
| for peer in bucket.peers: | ||
| discard rtable.insert(peer.nodeId) | ||
|
|
||
|
Comment on lines
+65
to
+68
|
||
| manager.tables[serviceId] = rtable | ||
| manager.serviceStatus[serviceId] = status | ||
|
|
||
| manager.updateServiceTablesMetrics() | ||
| return true | ||
|
|
||
| proc removeService*( | ||
| manager: ServiceRoutingTableManager, serviceId: ServiceId, status: ServiceStatus | ||
| ) = | ||
| manager.serviceStatus.withValue(serviceId, currentStatus): | ||
| if currentStatus[] == status: | ||
| manager.tables.del(serviceId) | ||
| manager.serviceStatus.del(serviceId) | ||
| manager.updateServiceTablesMetrics() | ||
| return | ||
|
|
||
| if (currentStatus[], status) == (Both, Interest): | ||
| currentStatus[] = Provided | ||
| elif (currentStatus[], status) == (Both, Provided): | ||
| currentStatus[] = Interest | ||
|
|
||
| proc getTable*( | ||
| manager: ServiceRoutingTableManager, serviceId: ServiceId | ||
| ): Opt[RoutingTable] = | ||
| let res = catch: | ||
| manager.tables[serviceId] | ||
| let table = res.valueOr: | ||
| return Opt.none(RoutingTable) | ||
|
|
||
| return Opt.some(table) | ||
|
|
||
| proc insertPeer*( | ||
| manager: ServiceRoutingTableManager, serviceId: ServiceId, peerKey: Key | ||
| ) = | ||
| let res = catch: | ||
| manager.tables[serviceId] | ||
| var table = res.valueOr: | ||
| return | ||
|
|
||
| let inserted = table.insert(peerKey) | ||
| if inserted: | ||
| cd_service_table_insertions.inc() | ||
| manager.updateServiceTablesMetrics() | ||
|
|
||
| proc hasService*( | ||
| manager: ServiceRoutingTableManager, serviceId: ServiceId | ||
| ): bool {.inline.} = | ||
| ## Check if routing table exists for a service | ||
| serviceId in manager.tables | ||
|
|
||
| proc refreshAllTables*( | ||
| manager: ServiceRoutingTableManager, kad: KadDHT | ||
| ) {.async: (raises: [CancelledError]).} = | ||
| let tables = manager.tables.values.toSeq() | ||
|
|
||
| for rtable in tables: | ||
| await kad.refreshTable(rtable) | ||
|
|
||
| proc count*(manager: ServiceRoutingTableManager): int {.inline.} = | ||
| return manager.tables.len | ||
|
|
||
| proc serviceIds*(manager: ServiceRoutingTableManager): seq[ServiceId] {.inline.} = | ||
| return manager.tables.keys.toSeq() | ||
|
|
||
| proc clear*(manager: ServiceRoutingTableManager) {.inline.} = | ||
| manager.tables.clear() | ||
|
gmelodie marked this conversation as resolved.
Resolved
|
||
| manager.serviceStatus.clear() | ||
| manager.updateServiceTablesMetrics() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| # Copyright (c) Status Research & Development GmbH | ||
|
|
||
| {.push raises: [].} | ||
|
|
||
| import metrics | ||
|
|
||
| export metrics | ||
|
|
||
| # Message metrics | ||
| declarePublicCounter cd_messages_sent, "service discovery messages sent", ["type"] | ||
| declarePublicCounter cd_messages_received, | ||
| "service discovery messages received", ["type"] | ||
| declarePublicCounter cd_message_bytes_sent, | ||
| "service discovery message bytes sent", ["type"] | ||
| declarePublicCounter cd_message_bytes_received, | ||
| "service discovery message bytes received", ["type"] | ||
| declarePublicHistogram cd_message_duration_ms, | ||
| "service discovery message round trip duration in milliseconds", | ||
| ["type"], | ||
| buckets = [10.0, 25.0, 50.0, 100.0, 250.0, 500.0, 1000.0, 2500.0, 5000.0, 10000.0] | ||
|
|
||
| # Registration metrics | ||
| declarePublicCounter cd_register_responses, | ||
| "registration responses received", ["status"] | ||
| declarePublicCounter cd_register_requests, "registration requests handled", ["status"] | ||
|
|
||
| # Lookup metrics | ||
| declarePublicCounter cd_lookup_requests, "service lookup requests initiated" | ||
| declarePublicCounter cd_lookup_peers_found, "peers found during service lookup" | ||
|
|
||
| # Registrar cache metrics | ||
| declarePublicGauge cd_registrar_cache_ads, "total advertisements in registrar cache" | ||
| declarePublicGauge cd_registrar_cache_services, "number of services in registrar cache" | ||
| declarePublicCounter cd_registrar_ads_expired, | ||
| "advertisements expired from registrar cache" | ||
|
|
||
| # Service routing tables metrics | ||
| declarePublicGauge cd_service_tables_count, "number of service routing tables" | ||
| declarePublicGauge cd_service_table_peers, "total peers across all service tables" | ||
| declarePublicCounter cd_service_table_insertions, | ||
| "peer insertions into service routing tables" | ||
|
|
||
| # IpTree metrics | ||
| declarePublicGauge cd_iptree_unique_ips, "unique IPs tracked in the IP tree" | ||
|
|
||
| # Advertiser metrics | ||
| declarePublicGauge cd_advertiser_pending_actions, | ||
| "pending registration actions in queue" | ||
| declarePublicCounter cd_advertiser_actions_executed, "registration actions executed" | ||
| declarePublicCounter cd_advertiser_services_added, "services added for advertisement" | ||
| declarePublicCounter cd_advertiser_services_removed, | ||
| "services removed from advertisement" |
Uh oh!
There was an error while loading. Please reload this page.