-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathmodels.py
More file actions
751 lines (588 loc) · 30.8 KB
/
models.py
File metadata and controls
751 lines (588 loc) · 30.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
import enum
import uuid
from datetime import datetime, timezone
from typing import Callable, List, Optional, Union
from sqlalchemy import (
BigInteger,
Boolean,
DateTime,
Enum,
Float,
ForeignKey,
Index,
Integer,
LargeBinary,
MetaData,
String,
Text,
TypeDecorator,
UniqueConstraint,
)
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from sqlalchemy.sql import false
from sqlalchemy_utils import UUIDType
from dstack._internal.core.errors import DstackError
from dstack._internal.core.models.backends.base import BackendType
from dstack._internal.core.models.common import CoreModel
from dstack._internal.core.models.fleets import FleetStatus
from dstack._internal.core.models.gateways import GatewayStatus
from dstack._internal.core.models.instances import InstanceStatus
from dstack._internal.core.models.profiles import (
DEFAULT_FLEET_TERMINATION_IDLE_TIME,
TerminationPolicy,
)
from dstack._internal.core.models.repos.base import RepoType
from dstack._internal.core.models.runs import (
JobStatus,
JobTerminationReason,
RunStatus,
RunTerminationReason,
)
from dstack._internal.core.models.users import GlobalRole, ProjectRole
from dstack._internal.core.models.volumes import VolumeStatus
from dstack._internal.server import settings
from dstack._internal.utils.common import get_current_datetime
from dstack._internal.utils.logging import get_logger
logger = get_logger(__name__)
class NaiveDateTime(TypeDecorator):
"""
A custom type decorator that ensures datetime objects are offset-naive when stored in the database
and offset-aware with UTC timezone when loaded from the database.
This is because we use datetimes in UTC everywhere, and
some databases (e.g. Postgres) throw an error if the timezone is set.
"""
impl = DateTime
cache_ok = True
def process_bind_param(self, value, dialect):
if value is not None and value.tzinfo is not None:
return value.replace(tzinfo=None)
return value
def process_result_value(self, value, dialect):
if value is None:
return None
return value.replace(tzinfo=timezone.utc)
class DecryptedString(CoreModel):
"""
A type for representing plaintext strings encrypted with `EncryptedString`.
Besides the string, stores information if the decryption was successful.
This is useful so that application code can have custom handling of failed decrypts (e.g. ignoring).
"""
# Do not read plaintext directly to avoid ignoring errors accidentally.
# Unpack with get_plaintext_or_error().
plaintext: Optional[str]
decrypted: bool = True
exc: Optional[Exception] = None
class Config:
arbitrary_types_allowed = True
def get_plaintext_or_error(self) -> str:
if self.decrypted and self.plaintext is not None:
return self.plaintext
exc = DstackError("Failed to access plaintext")
if self.exc is not None:
raise exc from self.exc
raise exc
class EncryptedString(TypeDecorator):
"""
A custom type decorator that encrypts and decrypts strings for storing in the db.
"""
impl = String
cache_ok = True
_encrypt_func: Callable[[str], str]
_decrypt_func: Callable[[str], str]
@classmethod
def set_encrypt_decrypt(
cls,
encrypt_func: Callable[[str], str],
decrypt_func: Callable[[str], str],
):
cls._encrypt_func = encrypt_func
cls._decrypt_func = decrypt_func
def process_bind_param(
self, value: Optional[Union[DecryptedString, str]], dialect
) -> Optional[str]:
if value is None:
return None
if isinstance(value, str):
# Passing string allows binding an encrypted value directly
# e.g. for comparisons
return value
return EncryptedString._encrypt_func(value.get_plaintext_or_error())
def process_result_value(self, value: Optional[str], dialect) -> Optional[DecryptedString]:
if value is None:
return value
try:
plaintext = EncryptedString._decrypt_func(value)
return DecryptedString(plaintext=plaintext, decrypted=True)
except Exception as e:
logger.debug("Failed to decrypt encrypted string: %s", repr(e))
return DecryptedString(plaintext=None, decrypted=False, exc=e)
class EnumAsString(TypeDecorator):
"""
A custom type decorator that stores enums as strings in the DB.
"""
impl = String
cache_ok = True
def __init__(self, enum_class: type[enum.Enum], *args, **kwargs):
self.enum_class = enum_class
super().__init__(*args, **kwargs)
def process_bind_param(self, value: Optional[enum.Enum], dialect) -> Optional[str]:
if value is None:
return None
return value.name
def process_result_value(self, value: Optional[str], dialect) -> Optional[enum.Enum]:
if value is None:
return None
return self.enum_class[value]
constraint_naming_convention = {
"ix": "ix_%(column_0_label)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s",
}
class BaseModel(DeclarativeBase):
metadata = MetaData(naming_convention=constraint_naming_convention)
class UserModel(BaseModel):
__tablename__ = "users"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
name: Mapped[str] = mapped_column(String(50), unique=True)
created_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
token: Mapped[DecryptedString] = mapped_column(EncryptedString(200), unique=True)
# token_hash is needed for fast search by token when stored token is encrypted
token_hash: Mapped[str] = mapped_column(String(2000), unique=True)
global_role: Mapped[GlobalRole] = mapped_column(Enum(GlobalRole))
# deactivated users cannot access API
active: Mapped[bool] = mapped_column(Boolean, default=True)
email: Mapped[Optional[str]] = mapped_column(String(200), nullable=True)
projects_quota: Mapped[int] = mapped_column(
Integer, default=settings.USER_PROJECT_DEFAULT_QUOTA
)
class ProjectModel(BaseModel):
__tablename__ = "projects"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
name: Mapped[str] = mapped_column(String(50), unique=True)
created_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
deleted: Mapped[bool] = mapped_column(Boolean, default=False)
is_public: Mapped[bool] = mapped_column(Boolean, default=False)
owner_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
owner: Mapped[UserModel] = relationship(lazy="joined")
members: Mapped[List["MemberModel"]] = relationship(
back_populates="project", order_by="MemberModel.member_num"
)
ssh_private_key: Mapped[str] = mapped_column(Text)
ssh_public_key: Mapped[str] = mapped_column(Text)
backends: Mapped[List["BackendModel"]] = relationship(back_populates="project")
default_gateway_id: Mapped[Optional[uuid.UUID]] = mapped_column(
ForeignKey("gateways.id", use_alter=True, ondelete="SET NULL"), nullable=True
)
default_gateway: Mapped[Optional["GatewayModel"]] = relationship(
foreign_keys=[default_gateway_id]
)
# TODO: Drop after the release without pools
# Note that multi-replica deployments can break if
# upgrading from an old version that uses pools to the version that drops pools from the DB.
default_pool_id: Mapped[Optional[UUIDType]] = mapped_column(
ForeignKey("pools.id", use_alter=True, ondelete="SET NULL"),
nullable=True,
deferred=True, # Not loaded so it can be deleted in the next releases
)
default_pool: Mapped[Optional["PoolModel"]] = relationship(foreign_keys=[default_pool_id])
class MemberModel(BaseModel):
__tablename__ = "members"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship()
user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
user: Mapped[UserModel] = relationship(lazy="joined")
project_role: Mapped[ProjectRole] = mapped_column(Enum(ProjectRole))
# member_num defines members ordering
member_num: Mapped[Optional[int]] = mapped_column(Integer)
class BackendModel(BaseModel):
__tablename__ = "backends"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship()
type: Mapped[BackendType] = mapped_column(EnumAsString(BackendType, 100))
config: Mapped[str] = mapped_column(String(20000))
auth: Mapped[DecryptedString] = mapped_column(EncryptedString(20000))
gateways: Mapped[List["GatewayModel"]] = relationship(back_populates="backend")
class RepoModel(BaseModel):
__tablename__ = "repos"
__table_args__ = (UniqueConstraint("project_id", "name", name="uq_repos_project_id_name"),)
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship()
# RepoModel.name stores repo_id
name: Mapped[str] = mapped_column(String(100))
type: Mapped[RepoType] = mapped_column(Enum(RepoType))
info: Mapped[str] = mapped_column(Text)
# `creds` is deprecated, for newly initialized repos per-user `RepoCredsModel` should be used
# instead. As of 0.18.25, there is no plan to remove this field, it's used as a fallback when
# `RepoCredsModel` associated with the user is not found.
creds: Mapped[Optional[str]] = mapped_column(String(5000))
class RepoCredsModel(BaseModel):
__tablename__ = "repo_creds"
__table_args__ = (
UniqueConstraint("repo_id", "user_id", name="uq_repo_creds_repo_id_user_id"),
)
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
repo_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("repos.id", ondelete="CASCADE"))
repo: Mapped["RepoModel"] = relationship()
user_id: Mapped["UserModel"] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
user: Mapped["UserModel"] = relationship()
creds: Mapped[DecryptedString] = mapped_column(EncryptedString(10000))
class CodeModel(BaseModel):
__tablename__ = "codes"
__table_args__ = (UniqueConstraint("repo_id", "blob_hash", name="uq_codes_repo_id_blob_hash"),)
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
repo_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("repos.id", ondelete="CASCADE"))
repo: Mapped["RepoModel"] = relationship()
blob_hash: Mapped[str] = mapped_column(String(4000))
blob: Mapped[Optional[bytes]] = mapped_column(LargeBinary) # None means blob is stored on s3
class FileArchiveModel(BaseModel):
__tablename__ = "file_archives"
__table_args__ = (
UniqueConstraint("user_id", "blob_hash", name="uq_file_archives_user_id_blob_hash"),
)
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
user_id: Mapped["UserModel"] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
user: Mapped["UserModel"] = relationship()
blob_hash: Mapped[str] = mapped_column(Text)
blob: Mapped[Optional[bytes]] = mapped_column(LargeBinary) # None means blob is stored on s3
class RunModel(BaseModel):
__tablename__ = "runs"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
deleted: Mapped[bool] = mapped_column(Boolean, default=False)
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship()
user_id: Mapped["UserModel"] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
user: Mapped["UserModel"] = relationship()
repo_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("repos.id", ondelete="CASCADE"))
repo: Mapped["RepoModel"] = relationship()
# Runs reference fleets so that fleets cannot be deleted while they are used.
# A fleet can have no busy instances but still be used by a run (e.g. a service with 0 replicas).
fleet_id: Mapped[Optional[uuid.UUID]] = mapped_column(ForeignKey("fleets.id"))
fleet: Mapped[Optional["FleetModel"]] = relationship(back_populates="runs")
run_name: Mapped[str] = mapped_column(String(100))
submitted_at: Mapped[datetime] = mapped_column(NaiveDateTime)
last_processed_at: Mapped[datetime] = mapped_column(NaiveDateTime)
next_triggered_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
status: Mapped[RunStatus] = mapped_column(Enum(RunStatus))
termination_reason: Mapped[Optional[RunTerminationReason]] = mapped_column(
Enum(RunTerminationReason)
)
# resubmission_attempt counts consecutive transitions to pending without provisioning.
# Can be used to choose retry delay depending on the attempt number.
resubmission_attempt: Mapped[int] = mapped_column(Integer, default=0)
run_spec: Mapped[str] = mapped_column(Text)
service_spec: Mapped[Optional[str]] = mapped_column(Text)
priority: Mapped[int] = mapped_column(Integer, default=0)
deployment_num: Mapped[int] = mapped_column(Integer)
desired_replica_count: Mapped[int] = mapped_column(Integer)
jobs: Mapped[List["JobModel"]] = relationship(
back_populates="run", lazy="selectin", order_by="[JobModel.replica_num, JobModel.job_num]"
)
gateway_id: Mapped[Optional[uuid.UUID]] = mapped_column(
ForeignKey("gateways.id", ondelete="SET NULL")
)
gateway: Mapped[Optional["GatewayModel"]] = relationship()
__table_args__ = (Index("ix_submitted_at_id", submitted_at.desc(), id),)
class JobModel(BaseModel):
__tablename__ = "jobs"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship()
run_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("runs.id", ondelete="CASCADE"))
run: Mapped["RunModel"] = relationship()
run_name: Mapped[str] = mapped_column(String(100))
job_num: Mapped[int] = mapped_column(Integer)
job_name: Mapped[str] = mapped_column(String(100))
submission_num: Mapped[int] = mapped_column(Integer)
submitted_at: Mapped[datetime] = mapped_column(NaiveDateTime)
last_processed_at: Mapped[datetime] = mapped_column(NaiveDateTime)
status: Mapped[JobStatus] = mapped_column(Enum(JobStatus))
termination_reason: Mapped[Optional[JobTerminationReason]] = mapped_column(
Enum(JobTerminationReason)
)
termination_reason_message: Mapped[Optional[str]] = mapped_column(Text)
# `disconnected_at` stores the first time of connectivity issues with the instance.
# Resets every time connectivity is restored.
disconnected_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
exit_status: Mapped[Optional[int]] = mapped_column(Integer)
job_spec_data: Mapped[str] = mapped_column(Text)
job_provisioning_data: Mapped[Optional[str]] = mapped_column(Text)
runner_timestamp: Mapped[Optional[int]] = mapped_column(BigInteger)
inactivity_secs: Mapped[Optional[int]] = mapped_column(Integer) # 0 - active, None - N/A
# `removed` is used to ensure that the instance is killed after the job is finished
remove_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
volumes_detached_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
# `instance_assigned` means instance assignment was done.
# if `instance_assigned` is True and `instance` is None, no instance was assigned.
instance_assigned: Mapped[bool] = mapped_column(Boolean, default=False)
instance_id: Mapped[Optional[uuid.UUID]] = mapped_column(
ForeignKey("instances.id", ondelete="CASCADE")
)
instance: Mapped[Optional["InstanceModel"]] = relationship(back_populates="jobs")
used_instance_id: Mapped[Optional[uuid.UUID]] = mapped_column(UUIDType(binary=False))
replica_num: Mapped[int] = mapped_column(Integer)
deployment_num: Mapped[int] = mapped_column(Integer)
job_runtime_data: Mapped[Optional[str]] = mapped_column(Text)
class GatewayModel(BaseModel):
__tablename__ = "gateways"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
name: Mapped[str] = mapped_column(String(100))
region: Mapped[str] = mapped_column(String(100))
wildcard_domain: Mapped[Optional[str]] = mapped_column(String(100))
# `configuration` is optional for compatibility with pre-0.18.2 gateways.
# Use `get_gateway_configuration` to construct `configuration` for old gateways.
configuration: Mapped[Optional[str]] = mapped_column(Text)
created_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
status: Mapped[GatewayStatus] = mapped_column(Enum(GatewayStatus))
status_message: Mapped[Optional[str]] = mapped_column(Text)
last_processed_at: Mapped[datetime] = mapped_column(NaiveDateTime)
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship(foreign_keys=[project_id])
backend_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("backends.id", ondelete="CASCADE"))
backend: Mapped["BackendModel"] = relationship(lazy="selectin")
gateway_compute_id: Mapped[Optional[uuid.UUID]] = mapped_column(
ForeignKey("gateway_computes.id", ondelete="CASCADE")
)
gateway_compute: Mapped[Optional["GatewayComputeModel"]] = relationship(lazy="joined")
runs: Mapped[List["RunModel"]] = relationship(back_populates="gateway")
__table_args__ = (UniqueConstraint("project_id", "name", name="uq_gateways_project_id_name"),)
class GatewayComputeModel(BaseModel):
__tablename__ = "gateway_computes"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
created_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
instance_id: Mapped[str] = mapped_column(String(100))
ip_address: Mapped[str] = mapped_column(String(100))
hostname: Mapped[Optional[str]] = mapped_column(String(100))
# `configuration` is optional for compatibility with pre-0.18.2 gateways.
# Use `get_gateway_compute_configuration` to construct `configuration` for old gateways.
configuration: Mapped[Optional[str]] = mapped_column(Text)
backend_data: Mapped[Optional[str]] = mapped_column(Text)
region: Mapped[str] = mapped_column(String(100))
backend_id: Mapped[Optional[uuid.UUID]] = mapped_column(
ForeignKey("backends.id", ondelete="CASCADE")
)
backend: Mapped[Optional["BackendModel"]] = relationship()
# The key to authorize the server with the gateway
ssh_private_key: Mapped[str] = mapped_column(Text)
ssh_public_key: Mapped[str] = mapped_column(Text)
# active means the server should maintain connection to gateway.
active: Mapped[bool] = mapped_column(Boolean, default=True)
deleted: Mapped[bool] = mapped_column(Boolean, server_default=false())
app_updated_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
# TODO: Drop after the release without pools
class PoolModel(BaseModel):
__tablename__ = "pools"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
name: Mapped[str] = mapped_column(String(50))
created_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
deleted: Mapped[bool] = mapped_column(Boolean, default=False)
deleted_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship(foreign_keys=[project_id])
instances: Mapped[List["InstanceModel"]] = relationship(back_populates="pool", lazy="selectin")
class FleetModel(BaseModel):
__tablename__ = "fleets"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
name: Mapped[str] = mapped_column(String(100))
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship(foreign_keys=[project_id])
created_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
last_processed_at: Mapped[datetime] = mapped_column(
NaiveDateTime, default=get_current_datetime
)
deleted: Mapped[bool] = mapped_column(Boolean, default=False)
deleted_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
status: Mapped[FleetStatus] = mapped_column(Enum(FleetStatus))
status_message: Mapped[Optional[str]] = mapped_column(Text)
spec: Mapped[str] = mapped_column(Text)
runs: Mapped[List["RunModel"]] = relationship(back_populates="fleet")
instances: Mapped[List["InstanceModel"]] = relationship(back_populates="fleet")
class InstanceModel(BaseModel):
__tablename__ = "instances"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
name: Mapped[str] = mapped_column(String(50))
instance_num: Mapped[int] = mapped_column(Integer, default=0)
# instance
created_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
last_processed_at: Mapped[datetime] = mapped_column(
NaiveDateTime, default=get_current_datetime
)
deleted: Mapped[bool] = mapped_column(Boolean, default=False)
deleted_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship(foreign_keys=[project_id])
# TODO: Drop after the release without pools
pool_id: Mapped[Optional[uuid.UUID]] = mapped_column(
ForeignKey("pools.id"),
deferred=True, # Not loaded so it can be deleted in the next releases
)
pool: Mapped[Optional["PoolModel"]] = relationship(back_populates="instances")
fleet_id: Mapped[Optional[uuid.UUID]] = mapped_column(ForeignKey("fleets.id"))
fleet: Mapped[Optional["FleetModel"]] = relationship(back_populates="instances")
status: Mapped[InstanceStatus] = mapped_column(Enum(InstanceStatus))
unreachable: Mapped[bool] = mapped_column(Boolean)
# VM
started_at: Mapped[Optional[datetime]] = mapped_column(
NaiveDateTime, default=get_current_datetime
)
finished_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
# create instance
# TODO: Introduce a field that would store all resolved instance profile parameters, etc, (similar to job_spec).
# Currently, profile parameters are parsed every time they are accessed (e.g. see profile.retry).
profile: Mapped[Optional[str]] = mapped_column(Text)
requirements: Mapped[Optional[str]] = mapped_column(Text)
instance_configuration: Mapped[Optional[str]] = mapped_column(Text)
# temination policy
termination_policy: Mapped[Optional[TerminationPolicy]] = mapped_column(String(100))
# TODO: Suggestion: do not assign DEFAULT_FLEET_TERMINATION_IDLE_TIME as the default here
# (make Optional instead; also instead of -1)
termination_idle_time: Mapped[int] = mapped_column(
Integer, default=DEFAULT_FLEET_TERMINATION_IDLE_TIME
)
# retry policy
last_retry_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
# instance termination handling
termination_deadline: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
termination_reason: Mapped[Optional[str]] = mapped_column(String(4000))
health_status: Mapped[Optional[str]] = mapped_column(String(4000))
first_termination_retry_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
last_termination_retry_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
# backend
backend: Mapped[Optional[BackendType]] = mapped_column(EnumAsString(BackendType, 100))
backend_data: Mapped[Optional[str]] = mapped_column(Text)
# offer
offer: Mapped[Optional[str]] = mapped_column(Text)
region: Mapped[Optional[str]] = mapped_column(String(2000))
price: Mapped[Optional[float]] = mapped_column(Float)
job_provisioning_data: Mapped[Optional[str]] = mapped_column(Text)
remote_connection_info: Mapped[Optional[str]] = mapped_column(Text)
# NULL means `auto` (only during provisioning, when ready it's not NULL)
total_blocks: Mapped[Optional[int]] = mapped_column(Integer)
busy_blocks: Mapped[int] = mapped_column(Integer, default=0)
jobs: Mapped[list["JobModel"]] = relationship(back_populates="instance", lazy="joined")
last_job_processed_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
volume_attachments: Mapped[List["VolumeAttachmentModel"]] = relationship(
back_populates="instance",
# Add delete-orphan option so that removing entries from volume_attachments
# automatically marks them for deletion.
# SQLalchemy requires delete when using delete-orphan.
cascade="save-update, merge, delete-orphan, delete",
)
class VolumeModel(BaseModel):
__tablename__ = "volumes"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
name: Mapped[str] = mapped_column(String(100))
user_id: Mapped["UserModel"] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
user: Mapped["UserModel"] = relationship()
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship(foreign_keys=[project_id])
created_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
last_processed_at: Mapped[datetime] = mapped_column(
NaiveDateTime, default=get_current_datetime
)
last_job_processed_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
deleted: Mapped[bool] = mapped_column(Boolean, default=False)
deleted_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
status: Mapped[VolumeStatus] = mapped_column(Enum(VolumeStatus))
status_message: Mapped[Optional[str]] = mapped_column(Text)
configuration: Mapped[str] = mapped_column(Text)
volume_provisioning_data: Mapped[Optional[str]] = mapped_column(Text)
attachments: Mapped[List["VolumeAttachmentModel"]] = relationship(back_populates="volume")
# Deprecated in favor of VolumeAttachmentModel.attachment_data
volume_attachment_data: Mapped[Optional[str]] = mapped_column(Text)
class VolumeAttachmentModel(BaseModel):
__tablename__ = "volumes_attachments"
volume_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("volumes.id"), primary_key=True)
volume: Mapped[VolumeModel] = relationship(back_populates="attachments")
instance_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("instances.id"), primary_key=True)
instance: Mapped[InstanceModel] = relationship(back_populates="volume_attachments")
attachment_data: Mapped[Optional[str]] = mapped_column(Text)
class PlacementGroupModel(BaseModel):
__tablename__ = "placement_groups"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
name: Mapped[str] = mapped_column(String(100))
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship(foreign_keys=[project_id])
fleet_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("fleets.id"))
fleet: Mapped["FleetModel"] = relationship(foreign_keys=[fleet_id])
# TODO: rename `fleet_deleted` -> `to_be_deleted`
fleet_deleted: Mapped[bool] = mapped_column(Boolean, default=False)
created_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
last_processed_at: Mapped[datetime] = mapped_column(
NaiveDateTime, default=get_current_datetime
)
deleted: Mapped[bool] = mapped_column(Boolean, default=False)
deleted_at: Mapped[Optional[datetime]] = mapped_column(NaiveDateTime)
configuration: Mapped[str] = mapped_column(Text)
provisioning_data: Mapped[Optional[str]] = mapped_column(Text)
class JobMetricsPoint(BaseModel):
__tablename__ = "job_metrics_points"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
job_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("jobs.id"))
job: Mapped["JobModel"] = relationship()
timestamp_micro: Mapped[int] = mapped_column(BigInteger)
cpu_usage_micro: Mapped[int] = mapped_column(BigInteger)
memory_usage_bytes: Mapped[int] = mapped_column(BigInteger)
memory_working_set_bytes: Mapped[int] = mapped_column(BigInteger)
# json-encoded lists of metric values of len(gpus) length
gpus_memory_usage_bytes: Mapped[str] = mapped_column(Text)
gpus_util_percent: Mapped[str] = mapped_column(Text)
class JobPrometheusMetrics(BaseModel):
__tablename__ = "job_prometheus_metrics"
job_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("jobs.id"), primary_key=True)
job: Mapped["JobModel"] = relationship()
collected_at: Mapped[datetime] = mapped_column(NaiveDateTime)
# Raw Prometheus text response
text: Mapped[str] = mapped_column(Text)
class SecretModel(BaseModel):
__tablename__ = "secrets"
__table_args__ = (UniqueConstraint("project_id", "name", name="uq_secrets_project_id_name"),)
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), primary_key=True, default=uuid.uuid4
)
project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project: Mapped["ProjectModel"] = relationship()
created_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
updated_at: Mapped[datetime] = mapped_column(NaiveDateTime, default=get_current_datetime)
name: Mapped[str] = mapped_column(String(200))
value: Mapped[DecryptedString] = mapped_column(EncryptedString())