Skip to content

Commit dcc9ac7

Browse files
authored
Merge pull request #740 from FlorentinD/catch-session-already-exists
Allow session name to be unique per user
2 parents f17e876 + 99a6ace commit dcc9ac7

File tree

10 files changed

+234
-42
lines changed

10 files changed

+234
-42
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Add Neo4j python driver rust extension as a new optional dependency.
1010
* Support creating GDS Sessions for self-managed Neo4j DBMS.
1111
* `GdsSessions.get_or_create` requires a new parameter `cloud_location` to specify where the session will be created.
12+
* Return the id of a session and allow deletion by id or name.
1213
* Add `ttl` parameter to `GdsSessions.get_or_create` to control if and when an unused session will be automatically deleted.
1314
* Add concurrency control for remote write-back procedures using the `concurrency` parameter.
1415

doc/modules/ROOT/pages/gds-session.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ sessions.list()
133133

134134
Use the `delete()` method to delete a GDS Session.
135135
This will terminate the session and stop any running costs from accumulating further.
136-
Deleting a session will not affect the configured AuraDB data source.
137-
However, any data not written back to the AuraDB instance will be lost.
136+
Deleting a session will not affect the configured Neo4j data source.
137+
However, any data not written back to the Neo4j instance will be lost.
138138

139139
.Deleting a GDS Session:
140140
[source, python, role=no-test]
141141
----
142-
sessions.delete("my-new-session")
142+
sessions.delete(session_name="my-new-session")
143143
----
144144

145145

doc/modules/ROOT/pages/tutorials/gds-sessions-self-managed.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ what that looks like
113113

114114
[source, python, role=no-test]
115115
----
116-
sessions.list()
116+
gds_sessions = sessions.list()
117+
118+
# Display the sessions
119+
from pandas import DataFrame
120+
DataFrame(gds_sessions)
117121
----
118122

119123
== Adding a dataset
@@ -297,7 +301,7 @@ stop incurring costs.
297301
----
298302
gds.delete()
299303
300-
# or sessions.delete("people-and-fruits")
304+
# or sessions.delete(session_name="people-and-fruits")
301305
----
302306

303307
[source, python, role=no-test]

doc/modules/ROOT/pages/tutorials/gds-sessions.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ GDS sessions feature enabled for your tenant. Contact your account
3030
manager to get the features enabled.
3131

3232
We also need to have the `+graphdatascience+` Python library installed,
33-
version `+1.11+` or later.
33+
version `+1.12a1+` or later.
3434

3535
[source, python, role=no-test]
3636
----
37-
%pip install "graphdatascience>=1.11"
37+
%pip install "graphdatascience>=1.12a1"
3838
----
3939

4040
== Aura API credentials
@@ -111,6 +111,10 @@ what that looks like
111111
[source, python, role=no-test]
112112
----
113113
sessions.list()
114+
115+
# Display the sessions
116+
from pandas import DataFrame
117+
DataFrame(sessions)
114118
----
115119

116120
== Adding a dataset

examples/gds-sessions-self-managed.ipynb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@
159159
"metadata": {},
160160
"outputs": [],
161161
"source": [
162-
"sessions.list()"
162+
"gds_sessions = sessions.list()\n",
163+
"\n",
164+
"# Display the sessions\n",
165+
"from pandas import DataFrame\n",
166+
"DataFrame(gds_sessions)"
163167
]
164168
},
165169
{
@@ -401,7 +405,7 @@
401405
"source": [
402406
"gds.delete()\n",
403407
"\n",
404-
"# or sessions.delete(\"people-and-fruits\")"
408+
"# or sessions.delete(session_name=\"people-and-fruits\")"
405409
]
406410
},
407411
{

examples/gds-sessions.ipynb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"This notebook requires having an AuraDB instance available and have the GDS sessions feature enabled for your tenant. \n",
4646
"Contact your account manager to get the features enabled. \n",
4747
"\n",
48-
"We also need to have the `graphdatascience` Python library installed, version `1.11` or later."
48+
"We also need to have the `graphdatascience` Python library installed, version `1.12a1` or later."
4949
]
5050
},
5151
{
@@ -58,7 +58,7 @@
5858
},
5959
"outputs": [],
6060
"source": [
61-
"%pip install \"graphdatascience>=1.11\""
61+
"%pip install \"graphdatascience>=1.12a1\""
6262
]
6363
},
6464
{
@@ -155,7 +155,11 @@
155155
"metadata": {},
156156
"outputs": [],
157157
"source": [
158-
"sessions.list()"
158+
"sessions.list()\n",
159+
"\n",
160+
"# Display the sessions\n",
161+
"from pandas import DataFrame\n",
162+
"DataFrame(sessions)"
159163
]
160164
},
161165
{

graphdatascience/session/dedicated_sessions.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,17 @@ def get_or_create(
8181
session_id=session_id, session_connection=session_connection, db_connection=db_connection
8282
)
8383

84-
def delete(self, session_name: str) -> bool:
85-
candidate = self._find_existing_session(session_name)
84+
def delete(self, *, session_name: Optional[str] = None, session_id: Optional[str] = None) -> bool:
85+
if not session_name and not session_id:
86+
raise ValueError("Either session_name or session_id must be provided.")
8687

87-
if candidate:
88-
return self._aura_api.delete_session(candidate.id) is not None
88+
if session_id:
89+
return self._aura_api.delete_session(session_id) is not None
90+
91+
if session_name:
92+
candidate = self._find_existing_session(session_name)
93+
if candidate:
94+
return self._aura_api.delete_session(candidate.id) is not None
8995

9096
return False
9197

@@ -96,12 +102,19 @@ def list(self, dbid: Optional[str] = None) -> List[SessionInfo]:
96102

97103
def _find_existing_session(self, session_name: str) -> Optional[SessionDetails]:
98104
matched_sessions: List[SessionDetails] = []
99-
# TODO pass dbid to list sessions (fail if for different dbid)
100105
matched_sessions = [s for s in self._aura_api.list_sessions() if s.name == session_name]
101106

102107
if len(matched_sessions) == 0:
103108
return None
104109

110+
# this will only occur for admins as we cannot resolve Aura-API client_id -> console_user_id we fail for now
111+
if len(matched_sessions) > 1:
112+
users = [s.user_id for s in matched_sessions]
113+
raise RuntimeError(
114+
"Admin users need to chose a unique session name for now."
115+
f" Multiple sessions with the name `{session_name}` across multiple users exist. Sessions found for users: {users}"
116+
)
117+
105118
return matched_sessions[0]
106119

107120
@staticmethod

graphdatascience/session/gds_sessions.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,17 @@ def get_or_create(
9494
"""
9595
return self._impl.get_or_create(session_name, memory, db_connection, ttl=ttl, cloud_location=cloud_location)
9696

97-
def delete(self, session_name: str) -> bool:
97+
def delete(self, *, session_name: Optional[str] = None, session_id: Optional[str] = None) -> bool:
9898
"""
99-
Delete a GDS session.
99+
Delete a GDS session either by name or id.
100100
Args:
101101
session_name: the name of the session to delete
102+
session_id: the id of the session to delete
102103
103104
Returns:
104105
True iff a session was deleted as a result of this call.
105106
"""
106-
return self._impl.delete(session_name)
107+
return self._impl.delete(session_name=session_name, session_id=session_id)
107108

108109
def list(self) -> List[SessionInfo]:
109110
"""

graphdatascience/session/session_info.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class SessionInfo:
1515
Represents information about a session.
1616
1717
Attributes:
18+
id (str): The ID of the session.
1819
name (str): The name of the session.
1920
memory (str): The size of the session.
2021
instance_id (Optional[str]): The ID of the AuraDB instance the session is attached to.
@@ -26,6 +27,7 @@ class SessionInfo:
2627
ttl (Optional[timedelta]): The time until the session is deleted if unused. The TTL gets renewed on every activity.
2728
"""
2829

30+
id: str
2931
name: str
3032
memory: SessionMemoryValue
3133
instance_id: Optional[str]
@@ -39,6 +41,7 @@ class SessionInfo:
3941
@classmethod
4042
def from_session_details(cls, details: SessionDetails) -> SessionInfo:
4143
return SessionInfo(
44+
id=details.id,
4245
name=details.name,
4346
memory=details.memory,
4447
instance_id=details.instance_id,

0 commit comments

Comments
 (0)