123123
124124if TYPE_CHECKING :
125125 from ai .backend .manager .models .domain import DomainRow
126- from ai .backend .manager .models .keypair import KeyPairRow
127126 from ai .backend .manager .models .scaling_group import ScalingGroupRow
128127 from ai .backend .manager .models .user import UserRow
129128
@@ -494,17 +493,17 @@ async def handle_session_exception(
494493
495494def _build_session_fetch_query (
496495 base_cond : Any ,
497- access_key : AccessKey | None = None ,
498496 * ,
497+ owner_id : UUID | None = None ,
499498 allow_stale : bool = True ,
500499 for_update : bool = False ,
501500 do_ordering : bool = False ,
502501 max_matches : int | None = None ,
503502 eager_loading_op : Sequence [_AbstractLoad ] | None = None ,
504503) -> sa .sql .Select [Any ]:
505504 cond = base_cond
506- if access_key :
507- cond = cond & (SessionRow .access_key == access_key )
505+ if owner_id is not None :
506+ cond = cond & (SessionRow .user_uuid == owner_id )
508507 if not allow_stale :
509508 cond = cond & (~ SessionRow .status .in_ (DEAD_SESSION_STATUSES ))
510509 query = (
@@ -528,8 +527,8 @@ def _build_session_fetch_query(
528527async def _match_sessions_by_id (
529528 db_session : SASession ,
530529 session_id_or_list : SessionId | list [SessionId ],
531- access_key : AccessKey | None = None ,
532530 * ,
531+ owner_id : UUID | None = None ,
533532 allow_prefix : bool = False ,
534533 allow_stale : bool = True ,
535534 for_update : bool = False ,
@@ -546,7 +545,7 @@ async def _match_sessions_by_id(
546545 cond = SessionRow .id == session_id_or_list
547546 query = _build_session_fetch_query (
548547 cond ,
549- access_key ,
548+ owner_id = owner_id ,
550549 max_matches = max_matches ,
551550 allow_stale = allow_stale ,
552551 for_update = for_update ,
@@ -560,8 +559,8 @@ async def _match_sessions_by_id(
560559async def _match_sessions_by_name (
561560 db_session : SASession ,
562561 session_name : str ,
563- access_key : AccessKey ,
564562 * ,
563+ owner_id : UUID | None = None ,
565564 allow_prefix : bool = False ,
566565 allow_stale : bool = True ,
567566 for_update : bool = False ,
@@ -575,7 +574,7 @@ async def _match_sessions_by_name(
575574 cond = SessionRow .name == session_name
576575 query = _build_session_fetch_query (
577576 cond ,
578- access_key ,
577+ owner_id = owner_id ,
579578 max_matches = max_matches ,
580579 allow_stale = allow_stale ,
581580 for_update = for_update ,
@@ -595,20 +594,6 @@ class ConcurrencyUsed:
595594 compute_session_ids : set [SessionId ] = field (default_factory = set )
596595 system_session_ids : set [SessionId ] = field (default_factory = set )
597596
598- @property
599- def compute_concurrency_used_key (self ) -> str :
600- return f"{ COMPUTE_CONCURRENCY_USED_KEY_PREFIX } { self .access_key } "
601-
602- @property
603- def system_concurrency_used_key (self ) -> str :
604- return f"{ SYSTEM_CONCURRENCY_USED_KEY_PREFIX } { self .access_key } "
605-
606- def to_cnt_map (self ) -> Mapping [str , int ]:
607- return {
608- self .compute_concurrency_used_key : len (self .compute_session_ids ),
609- self .system_concurrency_used_key : len (self .system_session_ids ),
610- }
611-
612597
613598class SessionOp (enum .StrEnum ):
614599 CREATE = "create_session"
@@ -637,13 +622,6 @@ class KernelLoadingStrategy(enum.StrEnum):
637622}
638623
639624
640- # Defined for avoiding circular import
641- def _get_keypair_row_join_condition () -> sa .sql .elements .ColumnElement [Any ]:
642- from ai .backend .manager .models .keypair import KeyPairRow
643-
644- return KeyPairRow .access_key == foreign (SessionRow .access_key )
645-
646-
647625def _get_user_row_join_condition () -> sa .sql .elements .ColumnElement [Any ]:
648626 from ai .backend .manager .models .user import UserRow
649627
@@ -731,14 +709,7 @@ class SessionRow(Base): # type: ignore[misc]
731709 back_populates = "sessions" ,
732710 foreign_keys = [user_uuid ],
733711 )
734-
735712 access_key : Mapped [str | None ] = mapped_column ("access_key" , sa .String (length = 20 ))
736- access_key_row : Mapped [KeyPairRow | None ] = relationship (
737- "KeyPairRow" ,
738- primaryjoin = _get_keypair_row_join_condition ,
739- back_populates = "sessions" ,
740- foreign_keys = [access_key ],
741- )
742713
743714 # `image` column is identical to kernels `image` column.
744715 images : Mapped [list [str ] | None ] = mapped_column ("images" , sa .ARRAY (sa .String ), nullable = True )
@@ -884,7 +855,7 @@ class SessionRow(Base): # type: ignore[misc]
884855 sa .Index ("ix_session_status_with_priority" , "status" , "priority" ),
885856 # Unique index for session names per user excluding terminal statuses
886857 sa .Index (
887- "ix_sessions_unique_name_per_user_nonterminal " ,
858+ "ix_sessions_unique_name_per_owner_nonterminal " ,
888859 "name" ,
889860 "user_uuid" ,
890861 unique = True ,
@@ -923,8 +894,7 @@ def from_dataclass(cls, session_data: SessionData) -> SessionRow:
923894 target_sgroup_names = session_data .target_sgroup_names ,
924895 domain_name = session_data .domain_name ,
925896 group_id = session_data .group_id ,
926- user_uuid = session_data .user_uuid ,
927- access_key = session_data .access_key ,
897+ user_uuid = session_data .owner_id ,
928898 images = session_data .images ,
929899 tag = session_data .tag ,
930900 occupying_slots = session_data .occupying_slots ,
@@ -968,8 +938,7 @@ def to_dataclass(self, owner: UserData | None = None) -> SessionData:
968938 target_sgroup_names = self .target_sgroup_names ,
969939 domain_name = self .domain_name ,
970940 group_id = self .group_id ,
971- user_uuid = self .user_uuid ,
972- access_key = AccessKey (self .access_key ) if self .access_key else None ,
941+ owner_id = self .user_uuid ,
973942 images = self .images ,
974943 tag = self .tag ,
975944 occupying_slots = self .occupying_slots ,
@@ -1017,8 +986,7 @@ def from_session_info(cls, info: SessionInfo) -> Self:
1017986 target_sgroup_names = info .resource .target_sgroup_names ,
1018987 domain_name = info .metadata .domain_name ,
1019988 group_id = info .metadata .group_id ,
1020- user_uuid = info .metadata .user_uuid ,
1021- access_key = info .metadata .access_key ,
989+ user_uuid = info .metadata .owner_id ,
1022990 images = info .image .images ,
1023991 tag = info .image .tag or info .metadata .tag ,
1024992 occupying_slots = info .resource .occupying_slots ,
@@ -1059,8 +1027,7 @@ def to_session_info(self) -> SessionInfo:
10591027 name = self .name or "" ,
10601028 domain_name = self .domain_name ,
10611029 group_id = self .group_id ,
1062- user_uuid = self .user_uuid ,
1063- access_key = self .access_key or "" ,
1030+ owner_id = self .user_uuid ,
10641031 session_type = self .session_type ,
10651032 priority = self .priority ,
10661033 created_at = self .created_at ,
@@ -1284,11 +1251,10 @@ def set_status(
12841251 if _status_info is not None :
12851252 self .status_info = _status_info
12861253
1287- def delegate_ownership (self , user_uuid : UUID , access_key : AccessKey ) -> None :
1288- self .user_uuid = user_uuid
1289- self .access_key = access_key
1254+ def delegate_ownership (self , owner_id : UUID ) -> None :
1255+ self .user_uuid = owner_id
12901256 for kernel_row in self .kernels :
1291- kernel_row .delegate_ownership (user_uuid , access_key )
1257+ kernel_row .delegate_ownership (owner_id )
12921258
12931259 @staticmethod
12941260 async def delete_by_user_id (user_uuid : UUID , * , db_session : SASession ) -> None :
@@ -1357,8 +1323,8 @@ async def match_sessions(
13571323 cls ,
13581324 db_session : SASession ,
13591325 session_reference : str | UUID | list [UUID ],
1360- access_key : AccessKey | None ,
13611326 * ,
1327+ owner_id : UUID | None = None ,
13621328 allow_prefix : bool = False ,
13631329 allow_stale : bool = True ,
13641330 for_update : bool = False ,
@@ -1412,7 +1378,7 @@ async def match_sessions(
14121378 for fetch_func in query_list :
14131379 rows = await fetch_func (
14141380 db_session ,
1415- access_key = access_key ,
1381+ owner_id = owner_id ,
14161382 allow_stale = allow_stale ,
14171383 for_update = for_update ,
14181384 max_matches = max_matches ,
@@ -1428,8 +1394,8 @@ async def get_session(
14281394 cls ,
14291395 db_session : SASession ,
14301396 session_name_or_id : str | UUID ,
1431- access_key : AccessKey | None = None ,
14321397 * ,
1398+ owner_id : UUID | None = None ,
14331399 allow_stale : bool = False ,
14341400 for_update : bool = False ,
14351401 kernel_loading_strategy : KernelLoadingStrategy = KernelLoadingStrategy .NONE ,
@@ -1474,7 +1440,7 @@ async def get_session(
14741440 session_list = await cls .match_sessions (
14751441 db_session ,
14761442 session_name_or_id ,
1477- access_key ,
1443+ owner_id = owner_id ,
14781444 allow_stale = allow_stale ,
14791445 for_update = for_update ,
14801446 eager_loading_op = _eager_loading_op ,
@@ -1499,8 +1465,8 @@ async def list_sessions(
14991465 cls ,
15001466 db_session : SASession ,
15011467 session_ids : list [UUID ],
1502- access_key : AccessKey | None = None ,
15031468 * ,
1469+ owner_id : UUID | None = None ,
15041470 allow_stale : bool = False ,
15051471 for_update : bool = False ,
15061472 kernel_loading_strategy : KernelLoadingStrategy = KernelLoadingStrategy .NONE ,
@@ -1531,7 +1497,7 @@ async def list_sessions(
15311497 session_list = await cls .match_sessions (
15321498 db_session ,
15331499 session_ids ,
1534- access_key ,
1500+ owner_id = owner_id ,
15351501 allow_stale = allow_stale ,
15361502 for_update = for_update ,
15371503 eager_loading_op = _eager_loading_op ,
@@ -1547,8 +1513,8 @@ async def get_session_by_id(
15471513 cls ,
15481514 db_session : SASession ,
15491515 session_id : SessionId ,
1550- access_key : AccessKey | None = None ,
15511516 * ,
1517+ owner_id : UUID | None = None ,
15521518 max_matches : int | None = None ,
15531519 allow_stale : bool = True ,
15541520 for_update : bool = False ,
@@ -1557,7 +1523,7 @@ async def get_session_by_id(
15571523 sessions = await _match_sessions_by_id (
15581524 db_session ,
15591525 session_id ,
1560- access_key ,
1526+ owner_id = owner_id ,
15611527 max_matches = max_matches ,
15621528 allow_stale = allow_stale ,
15631529 for_update = for_update ,
@@ -1586,7 +1552,6 @@ async def get_sgroup_managed_sessions(
15861552 noload ("*" ),
15871553 selectinload (SessionRow .group ).options (noload ("*" )),
15881554 selectinload (SessionRow .domain ).options (noload ("*" )),
1589- selectinload (SessionRow .access_key_row ).options (noload ("*" )),
15901555 selectinload (SessionRow .kernels ).options (noload ("*" )),
15911556 )
15921557 )
0 commit comments