Commit b5a8757
authored
Optimize SONIC configuration generation performance (#1778)
Eliminates N+1 query problems and implements bulk fetching strategies
across NetBox API calls in SONIC configuration generation.
Changes in config_generator.py:
1. _get_transfer_role_ipv4_addresses():
- Use get_cached_device_interfaces() instead of direct interface query
- Bulk fetch all device IP addresses: utils.nb.ipam.ip_addresses.filter(device_id=device.id)
- Bulk fetch all transfer role prefixes: utils.nb.ipam.prefixes.filter(role="transfer")
- Build transfer_networks list with ipaddress.ip_network() for containment checks
- Create interface_map dictionary for O(1) lookups
- Process all IPs with in-memory containment checks instead of per-IP prefix queries
2. _add_tagged_vlans_to_ports() and _add_vlan_configuration():
- Build netbox_name_to_sonic reverse mapping dictionary once
- Use O(1) dictionary lookups instead of O(n) iterations through netbox_interfaces
- Remove backward compatibility fallback to convert_netbox_interface_to_sonic()
- Add explicit error handling with warning logs for missing interfaces
3. _get_metalbox_ip_for_device():
- Add module-level _metalbox_ip_cache dictionary
- Cache both successful and failed lookup results
- Check cache before executing lookup logic
- Add clear_metalbox_ip_cache() function
- Integrate cache clearing into clear_all_caches()
Changes in netbox.py:
1. get_device_vlans():
- Replace utils.nb.dcim.interfaces.filter() with get_cached_device_interfaces()
- Bulk fetch all IP addresses: utils.nb.ipam.ip_addresses.filter(device_id=device.id)
- Build interface_ips_map dictionary for O(1) IP lookups by interface ID
- Replace per-VLAN-interface IP queries with dictionary lookups
2. get_device_loopbacks():
- Replace utils.nb.dcim.interfaces.filter() with get_cached_device_interfaces()
- Bulk fetch all IP addresses: utils.nb.ipam.ip_addresses.filter(device_id=device.id)
- Build interface_ips_map dictionary for O(1) IP lookups by interface ID
- Replace per-loopback IP queries with dictionary lookups
3. get_device_interface_ips():
- Replace utils.nb.dcim.interfaces.filter() with get_cached_device_interfaces()
- Bulk fetch all IP addresses: utils.nb.ipam.ip_addresses.filter(device_id=device.id)
- Build interface_ips_map dictionary for O(1) IP lookups by interface ID
- Replace per-interface IP queries with dictionary lookups
All function signatures remain unchanged. Existing error handling and
logging patterns preserved.
AI-assisted: Claude Code
Signed-off-by: Christian Berendt <[email protected]>1 parent b15562b commit b5a8757
2 files changed
+158
-79
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
| 166 | + | |
| 167 | + | |
166 | 168 | | |
167 | 169 | | |
168 | 170 | | |
169 | 171 | | |
170 | 172 | | |
171 | | - | |
172 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
173 | 186 | | |
174 | 187 | | |
175 | 188 | | |
| |||
229 | 242 | | |
230 | 243 | | |
231 | 244 | | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
| 245 | + | |
| 246 | + | |
236 | 247 | | |
237 | 248 | | |
238 | 249 | | |
| |||
270 | 281 | | |
271 | 282 | | |
272 | 283 | | |
| 284 | + | |
| 285 | + | |
273 | 286 | | |
274 | 287 | | |
275 | 288 | | |
276 | | - | |
277 | | - | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
278 | 302 | | |
279 | 303 | | |
280 | 304 | | |
| |||
286 | 310 | | |
287 | 311 | | |
288 | 312 | | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
| 313 | + | |
| 314 | + | |
293 | 315 | | |
294 | 316 | | |
295 | 317 | | |
| |||
325 | 347 | | |
326 | 348 | | |
327 | 349 | | |
| 350 | + | |
| 351 | + | |
328 | 352 | | |
329 | 353 | | |
330 | 354 | | |
331 | | - | |
332 | | - | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
333 | 368 | | |
334 | 369 | | |
335 | 370 | | |
| |||
340 | 375 | | |
341 | 376 | | |
342 | 377 | | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
| 378 | + | |
| 379 | + | |
347 | 380 | | |
348 | 381 | | |
349 | 382 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
40 | 44 | | |
41 | 45 | | |
42 | 46 | | |
| |||
606 | 610 | | |
607 | 611 | | |
608 | 612 | | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
609 | 619 | | |
610 | 620 | | |
611 | 621 | | |
612 | 622 | | |
613 | | - | |
614 | | - | |
615 | | - | |
616 | | - | |
617 | | - | |
618 | | - | |
619 | | - | |
620 | | - | |
621 | | - | |
622 | | - | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
623 | 630 | | |
624 | 631 | | |
625 | 632 | | |
| |||
699 | 706 | | |
700 | 707 | | |
701 | 708 | | |
702 | | - | |
703 | | - | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
704 | 714 | | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
705 | 729 | | |
706 | 730 | | |
707 | 731 | | |
| |||
710 | 734 | | |
711 | 735 | | |
712 | 736 | | |
| 737 | + | |
713 | 738 | | |
714 | | - | |
715 | | - | |
716 | | - | |
717 | | - | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
718 | 747 | | |
719 | | - | |
720 | | - | |
721 | | - | |
722 | | - | |
723 | | - | |
724 | | - | |
725 | | - | |
726 | | - | |
727 | | - | |
728 | | - | |
| 748 | + | |
| 749 | + | |
729 | 750 | | |
730 | | - | |
731 | | - | |
732 | | - | |
733 | | - | |
734 | | - | |
735 | | - | |
736 | | - | |
737 | | - | |
738 | | - | |
739 | | - | |
740 | | - | |
741 | | - | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
742 | 769 | | |
743 | | - | |
744 | | - | |
745 | | - | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
746 | 773 | | |
747 | 774 | | |
748 | 775 | | |
| |||
1112 | 1139 | | |
1113 | 1140 | | |
1114 | 1141 | | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
1115 | 1147 | | |
1116 | 1148 | | |
1117 | 1149 | | |
| |||
1173 | 1205 | | |
1174 | 1206 | | |
1175 | 1207 | | |
| 1208 | + | |
| 1209 | + | |
1176 | 1210 | | |
1177 | 1211 | | |
1178 | 1212 | | |
1179 | 1213 | | |
1180 | 1214 | | |
1181 | 1215 | | |
| 1216 | + | |
| 1217 | + | |
1182 | 1218 | | |
1183 | 1219 | | |
1184 | 1220 | | |
1185 | 1221 | | |
| 1222 | + | |
| 1223 | + | |
1186 | 1224 | | |
1187 | 1225 | | |
1188 | 1226 | | |
| |||
1276 | 1314 | | |
1277 | 1315 | | |
1278 | 1316 | | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
1279 | 1324 | | |
1280 | 1325 | | |
1281 | 1326 | | |
| |||
1300 | 1345 | | |
1301 | 1346 | | |
1302 | 1347 | | |
| 1348 | + | |
1303 | 1349 | | |
1304 | 1350 | | |
1305 | 1351 | | |
1306 | 1352 | | |
1307 | 1353 | | |
1308 | 1354 | | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
1309 | 1361 | | |
1310 | 1362 | | |
1311 | 1363 | | |
| |||
1314 | 1366 | | |
1315 | 1367 | | |
1316 | 1368 | | |
1317 | | - | |
1318 | | - | |
1319 | | - | |
1320 | | - | |
1321 | | - | |
1322 | | - | |
1323 | | - | |
1324 | | - | |
1325 | | - | |
1326 | | - | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
1327 | 1376 | | |
1328 | 1377 | | |
1329 | 1378 | | |
| |||
1337 | 1386 | | |
1338 | 1387 | | |
1339 | 1388 | | |
1340 | | - | |
1341 | | - | |
1342 | | - | |
1343 | | - | |
1344 | | - | |
1345 | | - | |
1346 | | - | |
1347 | | - | |
1348 | | - | |
1349 | | - | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
| 1394 | + | |
| 1395 | + | |
1350 | 1396 | | |
1351 | 1397 | | |
1352 | 1398 | | |
| |||
0 commit comments