Skip to content

Commit 9718855

Browse files
committed
Backend processes are started only for the requested nodes to run. The processes are started separately: VNode first, wait shorly, then QNodeOS
1 parent cea8d1b commit 9718855

3 files changed

Lines changed: 35 additions & 14 deletions

File tree

simulaqron/network.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def __init__(self, nodes: List[str], network_config_file: Path, network_name: st
7272
self._running = False
7373
self.name = network_name
7474

75-
self.processes: List[Process] = []
75+
self._virtual_node_processes: List[Process] = []
76+
self._qnodeos_processes: List[Process] = []
7677
self._logger = logging.getLogger(f"{self.__class__.__name__}({self.name})")
7778

7879
# Determine the nodes to start, using the in-memory network config
@@ -114,6 +115,10 @@ def running(self) -> bool:
114115
def __del__(self):
115116
self.stop()
116117

118+
@property
119+
def processes(self) -> List[Process]:
120+
return self._qnodeos_processes + self._virtual_node_processes
121+
117122
def _setup_processes(self):
118123
"""
119124
Setup the processes forming the network, however they are not started yet.
@@ -125,15 +130,16 @@ def _setup_processes(self):
125130
for node in self._nodes_to_start:
126131
process_virtual = Process(
127132
target=start_vnode,
128-
args=(node.name, self._network_config_file, self.name, simulaqron_settings.log_level),
133+
args=(node.name, self._network_config_file, self.name, [node.name for node in self._nodes_to_start]),
129134
name=f"VirtNode {node.name}"
130135
)
131136
process_qnodeos = Process(
132137
target=start_qnodeos,
133-
args=(node.name, self._network_config_file, self.name, simulaqron_settings.log_level),
138+
args=(node.name, self._network_config_file, self.name),
134139
name=f"QnodeOSNode {node.name}"
135140
)
136-
self.processes += [process_virtual, process_qnodeos]
141+
self._virtual_node_processes.append(process_virtual)
142+
self._qnodeos_processes.append(process_qnodeos)
137143

138144
def start(self, wait_until_running=False):
139145
"""
@@ -143,9 +149,17 @@ def start(self, wait_until_running=False):
143149
:param wait_until_running: bool
144150
"""
145151
self._logger.info("Starting network with name %s", self.name)
146-
for p in self.processes:
152+
for p in self._virtual_node_processes:
153+
if not p.is_alive():
154+
self._logger.debug("Starting Virtual Node process %s", p.name)
155+
p.daemon = True
156+
p.start()
157+
158+
time.sleep(1)
159+
160+
for p in self._qnodeos_processes:
147161
if not p.is_alive():
148-
self._logger.debug("Starting process %s", p.name)
162+
self._logger.debug("Starting QNodeOS process %s", p.name)
149163
p.daemon = True
150164
p.start()
151165

simulaqron/start/start_vnode.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import sys
3333
from functools import partial
3434
from pathlib import Path
35-
35+
from typing import List
3636

3737
from simulaqron.reactor import reactor
3838
from simulaqron.virtual_node.virtual import Backend
@@ -52,7 +52,7 @@ def _sigterm_handler(name, _signo, _stack_frame):
5252
reactor.stop()
5353

5454

55-
def start_vnode(name: str, network_config_file: Path, network_name: str = "default", log_level: str = "WARNING"):
55+
def start_vnode(name: str, network_config_file: Path, network_name: str, nodes_running: List[str]):
5656
"""
5757
Start the execution of a virtual simulaqron node. This node will simulate all quantum aspects
5858
of the node, and is then reachable via Twisted PB (Simulaqron Native Mode) or - when also starting QNPU -
@@ -64,8 +64,8 @@ def start_vnode(name: str, network_config_file: Path, network_name: str = "defau
6464
:type network_config_file: Path
6565
:param network_name: Name of the network (e.g., 'default').
6666
:type network_name: str
67-
:param log_level: Logging level (e.g., 'DEBUG', 'INFO', 'WARNING').
68-
:type log_level: str
67+
:param nodes_running: List of nodes running (e.g., ['Alice', 'Bob']).
68+
:type nodes_running: List[str]
6969
"""
7070

7171
# Let's ensure we have read the config file. This relies on the right one being passed from network.py

simulaqron/virtual_node/virtual.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
import logging
3535
from twisted.internet.defer import inlineCallbacks, DeferredLock, Deferred, DeferredList
36-
from twisted.internet.error import ConnectionRefusedError, CannotListenError
36+
from twisted.internet.error import CannotListenError
3737
from twisted.internet.task import deferLater
3838
from twisted.spread import pb
3939
from twisted.spread.pb import RemoteError, RemoteReference
@@ -102,22 +102,29 @@ def call_method(obj, method_name, *args, **kwargs):
102102
# forming the quantum network
103103
#
104104
class Backend:
105-
def __init__(self, name: str, network_name: str = "default"):
105+
def __init__(self, name: str, nodes_running: List[str], network_name: str):
106106
"""
107107
Create the Virtual Node backend. This will read the networks configuration and
108108
populate the name,hostname,port information with the information found in the
109109
configuration file for the given name.
110110
111111
:param name: Node name to start.
112112
:type name: str
113-
:network_name: Name of the network to start.
113+
:param nodes_running: List of nodes currently running.
114+
:type nodes_running: List[str]
115+
:param network_name: Name of the network to start.
114116
:type network_name: str
115117
"""
116118
self._logger = logging.getLogger(f"{self.__class__.__name__}({name})")
117119

118120
# Read the configuration file
119121
self.config = SocketsConfig(network_config, network_name=network_name, config_type="vnode")
122+
# We only want to start connections to the nodes that are running, not to all
123+
# of nodes that are defined in the network config
124+
# To this end, we filter the loaded network config, so it contains only the running nodes
125+
self.config.filter(nodes_running)
120126
self.myID: Host = self.config.hostDict[name]
127+
self._logger.debug("myID: (name: %s) %s", name, str(self.myID))
121128

122129
def start(
123130
self,
@@ -141,7 +148,7 @@ def start(
141148
self._logger.debug("Running reactor")
142149
reactor.run()
143150
except CannotListenError as exc:
144-
self._logger.debug("NetQASM server address (%d) is already in use.", self.myID.port, exc_info=exc)
151+
self._logger.debug("NetQASM server port (%d) is already in use.", self.myID.port, exc_info=exc)
145152
return
146153
except Exception as e:
147154
self._logger.debug("Critical error when starting local virtual node server", exc_info=e)

0 commit comments

Comments
 (0)