Skip to content

Commit

Permalink
Merge pull request #361 from SpiNNakerManchester/with_database
Browse files Browse the repository at this point in the history
Use less transactions
  • Loading branch information
Christian-B authored Sep 25, 2023
2 parents e02ab1b + 7674f60 commit dadab25
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 39 deletions.
42 changes: 10 additions & 32 deletions spinnman/spalloc/spalloc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from packaging.version import Version
import queue
import requests
import sqlite3
import struct
import threading
from typing import Dict, List, Tuple
Expand Down Expand Up @@ -98,7 +97,8 @@ def __init__(
logger.info("established session to {} for {}", service_url, username)

@staticmethod
def open_job_from_database(conn: sqlite3.Cursor) -> SpallocJob:
def open_job_from_database(
service_url, job_url, cookies, headers) -> SpallocJob:
"""
Create a job from the description in the attached database. This is
intended to allow for access to the job's allocated resources from
Expand All @@ -110,35 +110,16 @@ def open_job_from_database(conn: sqlite3.Cursor) -> SpallocJob:
credentials may have expired; if so, the job will be unable to
regenerate them.
:param ~sqlite3.Cursor conn:
The database cursor to retrieve the job details from. Assumes
the presence of a ``proxy_configuration`` table with ``kind``,
``name`` and ``value`` columns.
:param str service_url:
:param str job_url:
:param dict(str, str) cookies:
:param dict(str, str) headers:
:return:
The job handle, or ``None`` if the records in the database are
absent or incomplete.
:rtype: SpallocJob
"""
service_url = None
job_url = None
cookies = {}
headers = {}
for row in conn.execute("""
SELECT kind, name, value FROM proxy_configuration
"""):
kind, name, value = row
if kind == "SPALLOC":
if name == "service uri":
service_url = value
elif name == "job uri":
job_url = value
elif kind == "COOKIE":
cookies[name] = value
elif kind == "HEADER":
headers[name] = value
if not service_url or not job_url or not cookies or not headers:
# Cannot possibly work without a session or job
return None
session = Session(service_url, session_credentials=(cookies, headers))
return _SpallocJob(session, job_url)

Expand Down Expand Up @@ -457,8 +438,8 @@ def __init__(self, session, job_handle):
self.__proxy_thread = None
self.__proxy_ping = None

@overrides(SpallocJob._write_session_credentials_to_db)
def _write_session_credentials_to_db(self, cur):
@overrides(SpallocJob.get_session_credentials_for_db)
def get_session_credentials_for_db(self):
config = {}
config["SPALLOC", "service uri"] = self._service_url
config["SPALLOC", "job uri"] = self._url
Expand All @@ -471,10 +452,7 @@ def _write_session_credentials_to_db(self, cur):
# We never write the auth headers themselves; we just extend the
# session
del headers["Authorization"]
cur.executemany("""
INSERT INTO proxy_configuration(kind, name, value)
VALUES(?, ?, ?)
""", [(k1, k2, v) for (k1, k2), v in config.items()])
return config

@overrides(SpallocJob.get_state)
def get_state(self):
Expand Down
9 changes: 2 additions & 7 deletions spinnman/spalloc/spalloc_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from sqlite3 import Cursor
from typing import Dict, Tuple
from spinn_utilities.abstract_base import AbstractBase, abstractmethod
from spinn_utilities.abstract_context_manager import AbstractContextManager
Expand Down Expand Up @@ -194,17 +193,13 @@ def where_is_machine(self, x: int, y: int) -> Tuple[int, int, int]:
"""

@abstractmethod
def _write_session_credentials_to_db(self, cur: Cursor):
def get_session_credentials_for_db(self):
"""
Write the session credentials for the job to the database accessed by
the given cursor.
Get the session credentials for the job to be written into a database
.. note::
May assume that there is a ``proxy_configuration`` table with
``kind``, ``name`` and ``value`` columns.
:param ~sqlite3.Cursor cur:
The open cursor to the database.
"""

def __enter__(self):
Expand Down

0 comments on commit dadab25

Please sign in to comment.