Skip to content

Commit c22a26f

Browse files
authored
Use a single lock for the Azure graph builder (#2108)
1 parent f10ad0b commit c22a26f

File tree

1 file changed

+10
-13
lines changed
  • plugins/azure/fix_plugin_azure/resource

1 file changed

+10
-13
lines changed

plugins/azure/fix_plugin_azure/resource/base.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,7 @@ def __init__(
539539
config: AzureConfig,
540540
location_lookup: Optional[Dict[str, AzureLocation]] = None,
541541
location: Optional[AzureLocation] = None,
542-
graph_nodes_access: Optional[RWLock] = None,
543-
graph_edges_access: Optional[RWLock] = None,
542+
graph_access_lock: Optional[RWLock] = None,
544543
last_run_started_at: Optional[datetime] = None,
545544
) -> None:
546545
self.graph = graph
@@ -551,8 +550,7 @@ def __init__(
551550
self.core_feedback = core_feedback
552551
self.location_lookup = location_lookup or {}
553552
self.location = location
554-
self.graph_nodes_access = graph_nodes_access or RWLock()
555-
self.graph_edges_access = graph_edges_access or RWLock()
553+
self.graph_access_lock = graph_access_lock or RWLock()
556554
self.name = f"Azure:{subscription.name}"
557555
self.config = config
558556
self.last_run_started_at = last_run_started_at
@@ -602,7 +600,7 @@ def node(
602600
"""
603601
if isinstance(nd := node.get("node"), AzureResource):
604602
return nd # type: ignore
605-
with self.graph_nodes_access.read_access:
603+
with self.graph_access_lock.read_access:
606604
for n in self.graph:
607605
if clazz and not isinstance(n, clazz):
608606
continue
@@ -623,7 +621,7 @@ def nodes(
623621
result: List[AzureResourceType] = []
624622
if isinstance(nd := node.get("node"), AzureResource):
625623
result.append(nd) # type: ignore
626-
with self.graph_nodes_access.read_access:
624+
with self.graph_access_lock.read_access:
627625
for n in self.graph:
628626
if clazz and not isinstance(n, clazz):
629627
continue
@@ -661,7 +659,7 @@ def add_node(self, node: AzureResourceType, source: Optional[Json] = None) -> Op
661659
node._metadata["provider_link"] = f"https://portal.azure.com/#@/resource{node.id}/overview"
662660

663661
if last_edge_key is not None:
664-
with self.graph_nodes_access.write_access:
662+
with self.graph_access_lock.write_access:
665663
self.graph.add_node(node, source=source or {})
666664
return node
667665
else:
@@ -683,7 +681,7 @@ def add_edge(
683681
if isinstance(from_node, AzureResource) and isinstance(to_n, AzureResource):
684682
start, end = (to_n, from_node) if reverse else (from_node, to_n)
685683
log.debug(f"{self.name}: add edge: {start} -> {end} [{edge_type}]")
686-
with self.graph_edges_access.write_access:
684+
with self.graph_access_lock.write_access:
687685
return self.graph.add_edge(start, end, edge_type=edge_type)
688686
return None
689687

@@ -709,21 +707,21 @@ def dependant_node(
709707
if isinstance(from_node, AzureResource) and isinstance(to_n, AzureResource):
710708
start, end = (to_n, from_node) if reverse else (from_node, to_n)
711709
log.debug(f"{self.name}: add edge: {start} -> {end} [default]")
712-
with self.graph_edges_access.write_access:
710+
with self.graph_access_lock.write_access:
713711
self.graph.add_edge(start, end, edge_type=EdgeType.default)
714712
if delete_same_as_default:
715713
start, end = end, start
716714
log.debug(f"{self.name}: add edge: {end} -> {start} [delete]")
717715
self.graph.add_edge(end, start, edge_type=EdgeType.delete)
718716

719717
def resources_of(self, resource_type: Type[AzureResourceType]) -> List[AzureResourceType]:
720-
with self.graph_nodes_access.read_access:
718+
with self.graph_access_lock.read_access:
721719
return [n for n in self.graph.nodes if isinstance(n, resource_type)]
722720

723721
def edges_of(
724722
self, from_type: Type[AzureResource], to_type: Type[AzureResource], edge_type: EdgeType = EdgeType.default
725723
) -> List[EdgeKey]:
726-
with self.graph_edges_access.read_access:
724+
with self.graph_access_lock.read_access:
727725
return [
728726
key
729727
for (from_node, to_node, key) in self.graph.edges
@@ -745,8 +743,7 @@ def with_location(self, location: AzureLocation) -> GraphBuilder:
745743
core_feedback=self.core_feedback,
746744
location_lookup=self.location_lookup,
747745
location=location,
748-
graph_nodes_access=self.graph_nodes_access,
749-
graph_edges_access=self.graph_edges_access,
746+
graph_access_lock=self.graph_access_lock,
750747
config=self.config,
751748
)
752749

0 commit comments

Comments
 (0)