From 8dcd2351a5fde42711bf873af79c914e4576eff3 Mon Sep 17 00:00:00 2001 From: Simon Fishel Date: Tue, 25 Feb 2025 12:44:58 -0800 Subject: [PATCH] feat(EWT-2214): adds session type support and removes `reuse_session` since this is now the default behavior --- README.md | 12 +++++++++--- pyproject.toml | 2 +- wherobots/db/constants.py | 3 ++- wherobots/db/driver.py | 12 +++++++----- wherobots/db/session_type.py | 7 +++++++ 5 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 wherobots/db/session_type.py diff --git a/README.md b/README.md index 4915f76..494c4f6 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,12 @@ users may find useful: client application. The default is EWKT (string) and the most convenient for human inspection while still being usable by libraries like Shapely. -* `reuse_session`: controls whether an existing runtime of the same type - and in the same region that is available should be reused for this - connection. This is the default behavior. +* `session_type`: `"single"` or `"multi"`; if set to `"single"`, then each call + to `Connection.connect()` establishes an exclusive connection to a + Wherobots runtime; if set to "multi", then multiple `Connection.connect()` + calls with the same arguments and credentials will connect to the same + shared Wherobots runtime; `"single"` is the default. + + Consider multi-session for potential cost savings, but be mindful of + performance impacts from shared resources. You might need to adjust + cluster size if slowdowns occur, which could affect overall cost. diff --git a/pyproject.toml b/pyproject.toml index ed65183..36e478f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ name = "wherobots-python-dbapi" [tool.poetry] name = "wherobots-python-dbapi" -version = "0.11.1" +version = "0.12.0" description = "Python DB-API driver for Wherobots DB" authors = ["Maxime Petazzoni "] license = "Apache 2.0" diff --git a/wherobots/db/constants.py b/wherobots/db/constants.py index 808b27a..53c5b4b 100644 --- a/wherobots/db/constants.py +++ b/wherobots/db/constants.py @@ -4,6 +4,7 @@ from .region import Region from .runtime import Runtime +from .session_type import SessionType DEFAULT_ENDPOINT: str = "api.cloud.wherobots.com" # "api.cloud.wherobots.com" @@ -11,9 +12,9 @@ DEFAULT_RUNTIME: Runtime = Runtime.TINY DEFAULT_REGION: Region = Region.AWS_US_WEST_2 +DEFAULT_SESSION_TYPE: SessionType = SessionType.SINGLE DEFAULT_READ_TIMEOUT_SECONDS: float = 0.25 DEFAULT_SESSION_WAIT_TIMEOUT_SECONDS: float = 900 -DEFAULT_REUSE_SESSION: bool = True MAX_MESSAGE_SIZE: int = 100 * 2**20 # 100MiB PROTOCOL_VERSION: Version = Version("1.0.0") diff --git a/wherobots/db/driver.py b/wherobots/db/driver.py index f8a770d..2a5facd 100644 --- a/wherobots/db/driver.py +++ b/wherobots/db/driver.py @@ -2,6 +2,7 @@ A PEP-0249 compatible driver for interfacing with Wherobots DB. """ + import ssl from importlib import metadata from importlib.metadata import PackageNotFoundError @@ -19,9 +20,9 @@ from .constants import ( DEFAULT_ENDPOINT, DEFAULT_REGION, - DEFAULT_REUSE_SESSION, DEFAULT_RUNTIME, DEFAULT_READ_TIMEOUT_SECONDS, + DEFAULT_SESSION_TYPE, DEFAULT_SESSION_WAIT_TIMEOUT_SECONDS, MAX_MESSAGE_SIZE, PROTOCOL_VERSION, @@ -29,6 +30,7 @@ DataCompression, GeometryRepresentation, ResultsFormat, + SessionType, ) from .errors import ( InterfaceError, @@ -62,7 +64,7 @@ def connect( region: Region = None, wait_timeout: float = DEFAULT_SESSION_WAIT_TIMEOUT_SECONDS, read_timeout: float = DEFAULT_READ_TIMEOUT_SECONDS, - reuse_session: bool = DEFAULT_REUSE_SESSION, + session_type: Union[SessionType, None] = None, shutdown_after_inactive_seconds: Union[int, None] = None, results_format: Union[ResultsFormat, None] = None, data_compression: Union[DataCompression, None] = None, @@ -82,10 +84,10 @@ def connect( host = host or DEFAULT_ENDPOINT runtime = runtime or DEFAULT_RUNTIME region = region or DEFAULT_REGION + session_type = session_type or DEFAULT_SESSION_TYPE logging.info( - "%s %s runtime in %s from %s ...", - "Recycling" if reuse_session else "Requesting", + "Requesting %s runtime in %s from %s ...", runtime.value, region.value, host, @@ -98,7 +100,7 @@ def connect( try: resp = requests.post( url=f"{host}/sql/session", - params={"region": region.value, "reuse_session": reuse_session}, + params={"region": region.value, "sessionType": session_type.value}, json={ "runtimeId": runtime.value, "shutdownAfterInactiveSeconds": shutdown_after_inactive_seconds, diff --git a/wherobots/db/session_type.py b/wherobots/db/session_type.py new file mode 100644 index 0000000..1f41442 --- /dev/null +++ b/wherobots/db/session_type.py @@ -0,0 +1,7 @@ +from enum import auto +from strenum import LowercaseStrEnum + + +class SessionType(LowercaseStrEnum): + SINGLE = auto() + MULTI = auto()