-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathclp_config.py
More file actions
1126 lines (896 loc) · 40.3 KB
/
clp_config.py
File metadata and controls
1126 lines (896 loc) · 40.3 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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import pathlib
from enum import auto
from types import MappingProxyType
from typing import Annotated, Any, ClassVar, Literal
from urllib.parse import urlencode
import yaml
from pydantic import (
BaseModel,
ConfigDict,
Field,
field_validator,
model_validator,
PlainSerializer,
PrivateAttr,
StringConstraints,
)
from strenum import KebabCaseStrEnum, LowercaseStrEnum
from clp_py_utils.clp_logging import LoggingLevel
from clp_py_utils.core import (
get_config_value,
make_config_path_absolute,
read_yaml_config_file,
resolve_host_path_in_container,
validate_path_could_be_dir,
)
from clp_py_utils.serialization_utils import serialize_path, serialize_str_enum
# Constants
# Component names
DB_COMPONENT_NAME = "database"
QUEUE_COMPONENT_NAME = "queue"
REDIS_COMPONENT_NAME = "redis"
SPIDER_SCHEDULER_COMPONENT_NAME = "spider_scheduler"
REDUCER_COMPONENT_NAME = "reducer"
RESULTS_CACHE_COMPONENT_NAME = "results_cache"
COMPRESSION_SCHEDULER_COMPONENT_NAME = "compression_scheduler"
QUERY_SCHEDULER_COMPONENT_NAME = "query_scheduler"
PRESTO_COORDINATOR_COMPONENT_NAME = "presto-coordinator"
COMPRESSION_WORKER_COMPONENT_NAME = "compression_worker"
QUERY_WORKER_COMPONENT_NAME = "query_worker"
API_SERVER_COMPONENT_NAME = "api_server"
LOG_INGESTOR_COMPONENT_NAME = "log_ingestor"
WEBUI_COMPONENT_NAME = "webui"
MCP_SERVER_COMPONENT_NAME = "mcp_server"
GARBAGE_COLLECTOR_COMPONENT_NAME = "garbage_collector"
# Action names
ARCHIVE_MANAGER_ACTION_NAME = "archive_manager"
QUERY_JOBS_TABLE_NAME = "query_jobs"
QUERY_TASKS_TABLE_NAME = "query_tasks"
COMPRESSION_JOBS_TABLE_NAME = "compression_jobs"
COMPRESSION_TASKS_TABLE_NAME = "compression_tasks"
# Paths
CONTAINER_CLP_HOME = pathlib.Path("/") / "opt" / "clp"
CONTAINER_AWS_CONFIG_DIRECTORY = CONTAINER_CLP_HOME / ".aws"
CONTAINER_INPUT_LOGS_ROOT_DIR = pathlib.Path("/") / "mnt" / "logs"
CLP_DEFAULT_CONFIG_FILE_RELATIVE_PATH = pathlib.Path("etc") / "clp-config.yaml"
CLP_DEFAULT_CREDENTIALS_FILE_PATH = pathlib.Path("etc") / "credentials.yaml"
CLP_DEFAULT_DATA_DIRECTORY_PATH = pathlib.Path("var") / "data"
CLP_DEFAULT_ARCHIVES_DIRECTORY_PATH = CLP_DEFAULT_DATA_DIRECTORY_PATH / "archives"
CLP_DEFAULT_ARCHIVES_STAGING_DIRECTORY_PATH = CLP_DEFAULT_DATA_DIRECTORY_PATH / "staged-archives"
CLP_DEFAULT_STREAMS_DIRECTORY_PATH = CLP_DEFAULT_DATA_DIRECTORY_PATH / "streams"
CLP_DEFAULT_STREAMS_STAGING_DIRECTORY_PATH = CLP_DEFAULT_DATA_DIRECTORY_PATH / "staged-streams"
CLP_DEFAULT_LOG_DIRECTORY_PATH = pathlib.Path("var") / "log"
CLP_DEFAULT_TMP_DIRECTORY_PATH = pathlib.Path("var") / "tmp"
CLP_DEFAULT_DATASET_NAME = "default"
CLP_METADATA_TABLE_PREFIX = "clp_"
CLP_PACKAGE_CONTAINER_IMAGE_ID_PATH = pathlib.Path("clp-package-image.id")
CLP_SHARED_CONFIG_FILENAME = ".clp-config.yaml"
CLP_VERSION_FILE_PATH = pathlib.Path("VERSION")
# Environment variable names
CLP_DB_ROOT_USER_ENV_VAR_NAME = "CLP_DB_ROOT_USER"
CLP_DB_ROOT_PASS_ENV_VAR_NAME = "CLP_DB_ROOT_PASS"
CLP_DB_USER_ENV_VAR_NAME = "CLP_DB_USER"
CLP_DB_PASS_ENV_VAR_NAME = "CLP_DB_PASS"
CLP_QUEUE_USER_ENV_VAR_NAME = "CLP_QUEUE_USER"
CLP_QUEUE_PASS_ENV_VAR_NAME = "CLP_QUEUE_PASS"
CLP_REDIS_PASS_ENV_VAR_NAME = "CLP_REDIS_PASS"
SPIDER_DB_USER_ENV_VAR_NAME = "SPIDER_DB_USER"
SPIDER_DB_PASS_ENV_VAR_NAME = "SPIDER_DB_PASS"
# Serializer
StrEnumSerializer = PlainSerializer(serialize_str_enum)
# Generic types
NonEmptyStr = Annotated[str, StringConstraints(min_length=1, strip_whitespace=True)]
NonNegativeInt = Annotated[int, Field(ge=0)]
PositiveFloat = Annotated[float, Field(gt=0)]
PositiveInt = Annotated[int, Field(gt=0)]
# Specific types
# TODO: Replace this with pydantic_extra_types.domain.DomainStr.
DomainStr = NonEmptyStr
Port = Annotated[int, Field(gt=0, lt=2**16)]
SerializablePath = Annotated[pathlib.Path, PlainSerializer(serialize_path)]
ZstdCompressionLevel = Annotated[int, Field(ge=1, le=19)]
LoggingLevelRust = Literal[
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE",
"OFF",
]
class DeploymentType(KebabCaseStrEnum):
BASE = auto()
FULL = auto()
SPIDER_BASE = auto()
SPIDER_FULL = auto()
class StorageEngine(KebabCaseStrEnum):
CLP = auto()
CLP_S = auto()
StorageEngineStr = Annotated[StorageEngine, StrEnumSerializer]
class BundledService(LowercaseStrEnum):
DATABASE = auto()
QUEUE = auto()
REDIS = auto()
RESULTS_CACHE = auto()
BundledServiceStr = Annotated[BundledService, StrEnumSerializer]
class DatabaseEngine(KebabCaseStrEnum):
MARIADB = auto()
MYSQL = auto()
DatabaseEngineStr = Annotated[DatabaseEngine, StrEnumSerializer]
class OrchestrationType(KebabCaseStrEnum):
CELERY = auto()
SPIDER = auto()
OrchestrationTypeStr = Annotated[OrchestrationType, StrEnumSerializer]
class QueryEngine(KebabCaseStrEnum):
CLP = auto()
CLP_S = auto()
PRESTO = auto()
QueryEngineStr = Annotated[QueryEngine, StrEnumSerializer]
class StorageType(LowercaseStrEnum):
FS = auto()
S3 = auto()
class AwsAuthType(LowercaseStrEnum):
credentials = auto()
profile = auto()
env_vars = auto()
ec2 = auto()
AwsAuthTypeStr = Annotated[AwsAuthType, StrEnumSerializer]
class Package(BaseModel):
storage_engine: StorageEngineStr = StorageEngine.CLP_S
query_engine: QueryEngineStr = QueryEngine.CLP_S
@model_validator(mode="after")
def validate_query_engine_package_compatibility(self):
query_engine = self.query_engine
storage_engine = self.storage_engine
if query_engine in [QueryEngine.CLP, QueryEngine.CLP_S]:
if query_engine != storage_engine:
raise ValueError(
f"query_engine '{query_engine}' is only compatible with "
f"storage_engine '{query_engine}'."
)
elif query_engine == QueryEngine.PRESTO:
if storage_engine != StorageEngine.CLP_S:
raise ValueError(
f"query_engine '{QueryEngine.PRESTO}' is only compatible with "
f"storage_engine '{StorageEngine.CLP_S}'."
)
else:
raise ValueError(f"Unsupported query_engine '{query_engine}'.")
return self
class ClpDbUserType(KebabCaseStrEnum):
"""Database user types used by CLP components."""
CLP = auto()
ROOT = auto()
SPIDER = auto()
class ClpDbNameType(KebabCaseStrEnum):
"""Database name types used by CLP components."""
CLP = auto()
SPIDER = auto()
_DB_USER_TYPE_TO_DB_NAME_TYPE: MappingProxyType[ClpDbUserType, ClpDbNameType] = MappingProxyType(
{
ClpDbUserType.CLP: ClpDbNameType.CLP,
ClpDbUserType.ROOT: ClpDbNameType.CLP,
ClpDbUserType.SPIDER: ClpDbNameType.SPIDER,
}
)
yaml.SafeDumper.add_multi_representer(
KebabCaseStrEnum,
yaml.representer.SafeRepresenter.represent_str,
)
class DbUserCredentials(BaseModel):
"""Credentials for a database user."""
username: NonEmptyStr
password: NonEmptyStr
class Database(BaseModel):
DEFAULT_PORT: ClassVar[int] = 3306
type: DatabaseEngineStr = DatabaseEngine.MARIADB
host: DomainStr = "localhost"
port: Port = DEFAULT_PORT
names: dict[ClpDbNameType, NonEmptyStr] = {
ClpDbNameType.CLP: "clp-db",
ClpDbNameType.SPIDER: "spider-db",
}
ssl_cert: NonEmptyStr | None = None
auto_commit: bool = False
compress: bool = True
credentials: dict[ClpDbUserType, DbUserCredentials] = {}
def ensure_credentials_loaded(self, user_type: ClpDbUserType) -> None:
"""
Ensures that credentials for the given `user_type` are loaded.
:param user_type:
:raise ValueError: If credentials for the given `user_type` are not loaded.
"""
if user_type not in self.credentials:
err_msg = f"Credentials for user type '{user_type}' are not loaded."
raise ValueError(err_msg)
def get_mysql_connection_params(
self,
disable_localhost_socket_connection: bool = False,
user_type: ClpDbUserType = ClpDbUserType.CLP,
) -> dict[str, Any]:
"""
Returns a dictionary of connection parameters to be used by mysql's or mariadb's `connect()`
method, ensuring only credentials for the given `user_type` are loaded.
:param disable_localhost_socket_connection: If true, force TCP connections.
:param user_type: User type whose credentials should be included.
:return: Dictionary of MySQL connection parameters.
"""
self.ensure_credentials_loaded(user_type)
host = self.host
if disable_localhost_socket_connection and "localhost" == self.host:
host = "127.0.0.1"
# Currently, mysql's connection parameters are the same as mariadb
connection_params = {
"host": host,
"port": self.port,
"user": self.credentials[user_type].username,
"password": self.credentials[user_type].password,
"database": self.names[_DB_USER_TYPE_TO_DB_NAME_TYPE[user_type]],
"compress": self.compress,
"autocommit": self.auto_commit,
}
if self.ssl_cert:
connection_params["ssl_cert"] = self.ssl_cert
return connection_params
def get_clp_connection_params_and_type(
self,
disable_localhost_socket_connection: bool = False,
user_type: ClpDbUserType = ClpDbUserType.CLP,
) -> dict[str, Any]:
"""
Returns a dictionary of connection parameters to be used by CLP components and ensures only
credentials for the given `user_type` are loaded.
:param disable_localhost_socket_connection: If true, force TCP connections.
:param user_type: User type whose credentials should be included.
:return: Dictionary of CLP connection parameters.
"""
self.ensure_credentials_loaded(user_type)
host = self.host
if disable_localhost_socket_connection and "localhost" == self.host:
host = "127.0.0.1"
d = self.dump_to_primitive_dict()
d["credentials"] = {user_type: self.credentials[user_type].model_dump()}
d["host"] = host
d["table_prefix"] = CLP_METADATA_TABLE_PREFIX
# NOTE: clp-core does not distinguish between mysql and mariadb
d["type"] = DatabaseEngine.MYSQL.value
return d
def get_container_url(self, user_type: ClpDbUserType = ClpDbUserType.CLP) -> str:
"""
Returns a JDBC URL for connecting to the database from within a container.
"""
self.ensure_credentials_loaded(user_type)
query = urlencode(
{
"user": self.credentials[user_type].username,
"password": self.credentials[user_type].password,
}
)
return (
f"jdbc:{self.type.value}://{self.host}:{self.port}/"
f"{self.names[_DB_USER_TYPE_TO_DB_NAME_TYPE[user_type]]}?{query}"
)
def dump_to_primitive_dict(self) -> dict[str, Any]:
""":return: A dictionary representation of this model, excluding credentials."""
return self.model_dump(exclude={"credentials"})
def load_credentials_from_file(self, credentials_file_path: pathlib.Path):
"""
Loads database credentials from a YAML file.
:param credentials_file_path:
:raise ValueError: If the file is empty or does not contain the expected keys.
"""
config = read_yaml_config_file(credentials_file_path)
if config is None:
raise ValueError(f"Credentials file '{credentials_file_path}' is empty.")
try:
self.credentials[ClpDbUserType.CLP] = DbUserCredentials(
username=get_config_value(config, f"{DB_COMPONENT_NAME}.username"),
password=get_config_value(config, f"{DB_COMPONENT_NAME}.password"),
)
self.credentials[ClpDbUserType.ROOT] = DbUserCredentials(
username=get_config_value(config, f"{DB_COMPONENT_NAME}.root_username"),
password=get_config_value(config, f"{DB_COMPONENT_NAME}.root_password"),
)
self.credentials[ClpDbUserType.SPIDER] = DbUserCredentials(
username=get_config_value(config, f"{DB_COMPONENT_NAME}.spider_username"),
password=get_config_value(config, f"{DB_COMPONENT_NAME}.spider_password"),
)
except KeyError as ex:
raise ValueError(
f"Credentials file '{credentials_file_path}' does not contain key '{ex}'."
)
def load_credentials_from_env(self, user_type: ClpDbUserType = ClpDbUserType.CLP):
"""
Loads database credentials from environment variables for the given user type.
:param user_type:
:raise ValueError: If the user type is not supported.
:raise ValueError: Propagates `_get_env_var`'s exceptions.
"""
if user_type == ClpDbUserType.CLP:
user_env_var = CLP_DB_USER_ENV_VAR_NAME
pass_env_var = CLP_DB_PASS_ENV_VAR_NAME
elif user_type == ClpDbUserType.ROOT:
user_env_var = CLP_DB_ROOT_USER_ENV_VAR_NAME
pass_env_var = CLP_DB_ROOT_PASS_ENV_VAR_NAME
elif user_type == ClpDbUserType.SPIDER:
user_env_var = SPIDER_DB_USER_ENV_VAR_NAME
pass_env_var = SPIDER_DB_PASS_ENV_VAR_NAME
else:
err_msg = f"Unsupported user type '{user_type}'."
raise ValueError(err_msg)
self.credentials[user_type] = DbUserCredentials(
username=_get_env_var(user_env_var),
password=_get_env_var(pass_env_var),
)
def transform_for_container(self, is_bundled: bool):
self.host = DB_COMPONENT_NAME
if is_bundled:
self.port = self.DEFAULT_PORT
class SpiderScheduler(BaseModel):
DEFAULT_PORT: ClassVar[int] = 6000
host: DomainStr = "localhost"
port: Port = DEFAULT_PORT
def transform_for_container(self):
self.host = SPIDER_SCHEDULER_COMPONENT_NAME
self.port = self.DEFAULT_PORT
class CompressionScheduler(BaseModel):
UNLIMITED_CONCURRENT_TASKS_PER_JOB: ClassVar[NonNegativeInt] = 0
jobs_poll_delay: PositiveFloat = 0.1 # seconds
max_concurrent_tasks_per_job: NonNegativeInt = UNLIMITED_CONCURRENT_TASKS_PER_JOB
logging_level: LoggingLevel = "INFO"
type: OrchestrationTypeStr = OrchestrationType.CELERY
class QueryScheduler(BaseModel):
DEFAULT_PORT: ClassVar[int] = 7000
host: DomainStr = "localhost"
port: Port = DEFAULT_PORT
jobs_poll_delay: PositiveFloat = 0.1 # seconds
max_datasets_per_query: PositiveInt | None = 10
num_archives_to_search_per_sub_job: PositiveInt = 16
logging_level: LoggingLevel = "INFO"
def transform_for_container(self):
self.host = QUERY_SCHEDULER_COMPONENT_NAME
self.port = self.DEFAULT_PORT
class CompressionWorker(BaseModel):
logging_level: LoggingLevel = "INFO"
class QueryWorker(BaseModel):
logging_level: LoggingLevel = "INFO"
class Redis(BaseModel):
DEFAULT_PORT: ClassVar[int] = 6379
host: DomainStr = "localhost"
port: Port = DEFAULT_PORT
query_backend_database: int = 0
compression_backend_database: int = 1
# redis can perform authentication without a username
password: str | None = None
def dump_to_primitive_dict(self):
return self.model_dump(exclude={"password"})
def load_credentials_from_file(self, credentials_file_path: pathlib.Path):
config = read_yaml_config_file(credentials_file_path)
if config is None:
raise ValueError(f"Credentials file '{credentials_file_path}' is empty.")
try:
self.password = get_config_value(config, f"{REDIS_COMPONENT_NAME}.password")
except KeyError as ex:
raise ValueError(
f"Credentials file '{credentials_file_path}' does not contain key '{ex}'."
)
def load_credentials_from_env(self):
"""
:raise ValueError: if any expected environment variable is not set.
"""
self.password = _get_env_var(CLP_REDIS_PASS_ENV_VAR_NAME)
def transform_for_container(self, is_bundled: bool):
self.host = REDIS_COMPONENT_NAME
if is_bundled:
self.port = self.DEFAULT_PORT
class Reducer(BaseModel):
DEFAULT_PORT: ClassVar[int] = 14009
host: DomainStr = "localhost"
base_port: Port = DEFAULT_PORT
logging_level: LoggingLevel = "INFO"
upsert_interval: PositiveInt = 100 # milliseconds
def transform_for_container(self):
self.host = REDUCER_COMPONENT_NAME
self.base_port = self.DEFAULT_PORT
class ResultsCache(BaseModel):
DEFAULT_PORT: ClassVar[int] = 27017
host: DomainStr = "localhost"
port: Port = DEFAULT_PORT
db_name: NonEmptyStr = "clp-query-results"
stream_collection_name: NonEmptyStr = "stream-files"
retention_period: PositiveInt | None = 60
def get_uri(self):
return f"mongodb://{self.host}:{self.port}/{self.db_name}"
def transform_for_container(self, is_bundled: bool):
self.host = RESULTS_CACHE_COMPONENT_NAME
if is_bundled:
self.port = self.DEFAULT_PORT
class Queue(BaseModel):
DEFAULT_PORT: ClassVar[int] = 5672
host: DomainStr = "localhost"
port: Port = DEFAULT_PORT
username: NonEmptyStr | None = None
password: str | None = None
def dump_to_primitive_dict(self):
return self.model_dump(exclude={"username", "password"})
def load_credentials_from_file(self, credentials_file_path: pathlib.Path):
config = read_yaml_config_file(credentials_file_path)
if config is None:
raise ValueError(f"Credentials file '{credentials_file_path}' is empty.")
try:
self.username = get_config_value(config, f"{QUEUE_COMPONENT_NAME}.username")
self.password = get_config_value(config, f"{QUEUE_COMPONENT_NAME}.password")
except KeyError as ex:
raise ValueError(
f"Credentials file '{credentials_file_path}' does not contain key '{ex}'."
)
def load_credentials_from_env(self):
"""
:raise ValueError: if any expected environment variable is not set.
"""
self.username = _get_env_var(CLP_QUEUE_USER_ENV_VAR_NAME)
self.password = _get_env_var(CLP_QUEUE_PASS_ENV_VAR_NAME)
def transform_for_container(self, is_bundled: bool):
self.host = QUEUE_COMPONENT_NAME
if is_bundled:
self.port = self.DEFAULT_PORT
class S3Credentials(BaseModel):
access_key_id: NonEmptyStr
secret_access_key: NonEmptyStr
session_token: NonEmptyStr | None = None
class AwsAuthentication(BaseModel):
type: AwsAuthTypeStr
profile: NonEmptyStr | None = None
credentials: S3Credentials | None = None
@model_validator(mode="before")
@classmethod
def validate_authentication(cls, data):
if not isinstance(data, dict):
raise ValueError(
"authentication config expects to be initialized from a dict, but received"
f" {type(data).__name__}."
)
auth_type = data.get("type")
profile = data.get("profile")
credentials = data.get("credentials")
try:
auth_enum = AwsAuthType(auth_type)
except ValueError:
raise ValueError(f"Unsupported authentication type '{auth_type}'.")
if profile and credentials:
raise ValueError("profile and credentials cannot be set simultaneously.")
if AwsAuthType.profile == auth_enum and not profile:
raise ValueError(f"profile must be set when type is '{auth_enum}.'")
if AwsAuthType.credentials == auth_enum and not credentials:
raise ValueError(f"credentials must be set when type is '{auth_enum}.'")
if auth_enum in [AwsAuthType.ec2, AwsAuthType.env_vars] and (profile or credentials):
raise ValueError(f"profile and credentials must not be set when type is '{auth_enum}.'")
return data
class S3Config(BaseModel):
endpoint_url: str | None = None
region_code: NonEmptyStr | None = None
bucket: NonEmptyStr
key_prefix: str
aws_authentication: AwsAuthentication
class S3IngestionConfig(BaseModel):
type: Literal[StorageType.S3.value] = StorageType.S3.value
aws_authentication: AwsAuthentication
def transform_for_container(self):
pass
class FsStorage(BaseModel):
type: Literal[StorageType.FS.value] = StorageType.FS.value
directory: SerializablePath
@field_validator("directory", mode="before")
@classmethod
def validate_directory(cls, value):
_validate_directory(value)
return value
def make_config_paths_absolute(self, clp_home: pathlib.Path):
self.directory = make_config_path_absolute(clp_home, self.directory)
class S3Storage(BaseModel):
type: Literal[StorageType.S3.value] = StorageType.S3.value
s3_config: S3Config
staging_directory: SerializablePath
@field_validator("staging_directory", mode="before")
@classmethod
def validate_staging_directory(cls, value):
_validate_directory(value)
return value
@field_validator("s3_config")
@classmethod
def validate_key_prefix(cls, value):
key_prefix = value.key_prefix
if "" == key_prefix:
raise ValueError("s3_config.key_prefix cannot be empty")
if not key_prefix.endswith("/"):
raise ValueError('s3_config.key_prefix must end with "/"')
return value
def make_config_paths_absolute(self, clp_home: pathlib.Path):
self.staging_directory = make_config_path_absolute(clp_home, self.staging_directory)
class FsIngestionConfig(FsStorage):
directory: SerializablePath = pathlib.Path("/")
def transform_for_container(self):
input_logs_dir = self.directory.resolve()
self.directory = CONTAINER_INPUT_LOGS_ROOT_DIR / input_logs_dir.relative_to(
input_logs_dir.anchor
)
class ArchiveFsStorage(FsStorage):
directory: SerializablePath = CLP_DEFAULT_ARCHIVES_DIRECTORY_PATH
def transform_for_container(self):
self.directory = pathlib.Path("/") / CLP_DEFAULT_ARCHIVES_DIRECTORY_PATH
class StreamFsStorage(FsStorage):
directory: SerializablePath = CLP_DEFAULT_STREAMS_DIRECTORY_PATH
def transform_for_container(self):
self.directory = pathlib.Path("/") / CLP_DEFAULT_STREAMS_DIRECTORY_PATH
class ArchiveS3Storage(S3Storage):
staging_directory: SerializablePath = CLP_DEFAULT_ARCHIVES_STAGING_DIRECTORY_PATH
def transform_for_container(self):
self.staging_directory = pathlib.Path("/") / CLP_DEFAULT_ARCHIVES_STAGING_DIRECTORY_PATH
class StreamS3Storage(S3Storage):
staging_directory: SerializablePath = CLP_DEFAULT_STREAMS_STAGING_DIRECTORY_PATH
def transform_for_container(self):
self.staging_directory = pathlib.Path("/") / CLP_DEFAULT_STREAMS_STAGING_DIRECTORY_PATH
def _get_directory_from_storage_config(
storage_config: FsStorage | S3Storage,
) -> pathlib.Path:
storage_type = storage_config.type
if StorageType.FS == storage_type:
return storage_config.directory
if StorageType.S3 == storage_type:
return storage_config.staging_directory
raise NotImplementedError(f"storage.type {storage_type} is not supported")
def _set_directory_for_storage_config(storage_config: FsStorage | S3Storage, directory) -> None:
storage_type = storage_config.type
if StorageType.FS == storage_type:
storage_config.directory = directory
elif StorageType.S3 == storage_type:
storage_config.staging_directory = directory
else:
raise NotImplementedError(f"storage.type {storage_type} is not supported")
class ArchiveOutput(BaseModel):
storage: ArchiveFsStorage | ArchiveS3Storage = ArchiveFsStorage()
target_archive_size: PositiveInt = 256 * 1024 * 1024 # 256 MiB
target_dictionaries_size: PositiveInt = 32 * 1024 * 1024 # 32 MiB
target_encoded_file_size: PositiveInt = 256 * 1024 * 1024 # 256 MiB
target_segment_size: PositiveInt = 256 * 1024 * 1024 # 256 MiB
compression_level: ZstdCompressionLevel = 3
retention_period: PositiveInt | None = None
def set_directory(self, directory: pathlib.Path):
_set_directory_for_storage_config(self.storage, directory)
def get_directory(self) -> pathlib.Path:
return _get_directory_from_storage_config(self.storage)
class StreamOutput(BaseModel):
storage: StreamFsStorage | StreamS3Storage = StreamFsStorage()
target_uncompressed_size: PositiveInt = 128 * 1024 * 1024
def set_directory(self, directory: pathlib.Path):
_set_directory_for_storage_config(self.storage, directory)
def get_directory(self) -> pathlib.Path:
return _get_directory_from_storage_config(self.storage)
class WebUi(BaseModel):
DEFAULT_PORT: ClassVar[int] = 4000
host: DomainStr = "localhost"
port: Port = DEFAULT_PORT
results_metadata_collection_name: NonEmptyStr = "results-metadata"
rate_limit: PositiveInt = 1000
class SweepInterval(BaseModel):
model_config = ConfigDict(extra="forbid")
archive: PositiveInt = 60
search_result: PositiveInt = 30
class McpServer(BaseModel):
DEFAULT_PORT: ClassVar[int] = 8000
host: DomainStr = "localhost"
port: Port = DEFAULT_PORT
logging_level: LoggingLevel = "INFO"
class GarbageCollector(BaseModel):
logging_level: LoggingLevel = "INFO"
sweep_interval: SweepInterval = SweepInterval()
class QueryJobPollingConfig(BaseModel):
initial_backoff_ms: int = Field(default=100, alias="initial_backoff")
max_backoff_ms: int = Field(default=5000, alias="max_backoff")
class ApiServer(BaseModel):
host: DomainStr = "localhost"
port: Port = 3001
query_job_polling: QueryJobPollingConfig = QueryJobPollingConfig()
default_max_num_query_results: int = 1000
class LogIngestor(BaseModel):
host: DomainStr = "localhost"
port: Port = 3002
buffer_flush_timeout: PositiveInt = 300 # seconds
buffer_flush_threshold: PositiveInt = 4096 * 1024 * 1024 # 4 GiB
channel_capacity: PositiveInt = 10
logging_level: LoggingLevelRust = "INFO"
class Presto(BaseModel):
DEFAULT_PORT: ClassVar[int] = 8080
host: DomainStr
port: Port
def transform_for_container(self):
self.host = PRESTO_COORDINATOR_COMPONENT_NAME
self.port = self.DEFAULT_PORT
def _get_env_var(name: str) -> str:
value = os.getenv(name)
if value is None:
raise ValueError(f"Missing environment variable: {name}")
return value
class Telemetry(BaseModel):
disable: bool = False
class ClpConfig(BaseModel):
container_image_ref: NonEmptyStr | None = None
logs_input: FsIngestionConfig | S3IngestionConfig = FsIngestionConfig()
bundled: list[BundledServiceStr] = [
BundledService.DATABASE,
BundledService.QUEUE,
BundledService.REDIS,
BundledService.RESULTS_CACHE,
]
package: Package = Package()
database: Database = Database()
# Default to use celery backend
queue: Queue | None = Queue()
redis: Redis | None = Redis()
reducer: Reducer = Reducer()
results_cache: ResultsCache = ResultsCache()
compression_scheduler: CompressionScheduler = CompressionScheduler()
spider_scheduler: SpiderScheduler | None = None
query_scheduler: QueryScheduler = QueryScheduler()
compression_worker: CompressionWorker = CompressionWorker()
query_worker: QueryWorker = QueryWorker()
webui: WebUi = WebUi()
garbage_collector: GarbageCollector = GarbageCollector()
api_server: ApiServer | None = ApiServer()
log_ingestor: LogIngestor | None = LogIngestor()
credentials_file_path: SerializablePath = CLP_DEFAULT_CREDENTIALS_FILE_PATH
mcp_server: McpServer | None = None
presto: Presto | None = None
archive_output: ArchiveOutput = ArchiveOutput()
stream_output: StreamOutput = StreamOutput()
data_directory: SerializablePath = CLP_DEFAULT_DATA_DIRECTORY_PATH
logs_directory: SerializablePath = CLP_DEFAULT_LOG_DIRECTORY_PATH
tmp_directory: SerializablePath = CLP_DEFAULT_TMP_DIRECTORY_PATH
aws_config_directory: SerializablePath | None = None
telemetry: Telemetry = Telemetry()
_container_image_id_path: SerializablePath = PrivateAttr(
default=CLP_PACKAGE_CONTAINER_IMAGE_ID_PATH
)
_version_file_path: SerializablePath = PrivateAttr(default=CLP_VERSION_FILE_PATH)
@field_validator("aws_config_directory")
@classmethod
def expand_profile_user_home(cls, value: SerializablePath | None) -> SerializablePath | None:
if value is not None:
value = value.expanduser()
return value
def make_config_paths_absolute(self, clp_home: pathlib.Path):
if StorageType.FS == self.logs_input.type:
self.logs_input.make_config_paths_absolute(clp_home)
self.credentials_file_path = make_config_path_absolute(clp_home, self.credentials_file_path)
self.archive_output.storage.make_config_paths_absolute(clp_home)
self.stream_output.storage.make_config_paths_absolute(clp_home)
self.data_directory = make_config_path_absolute(clp_home, self.data_directory)
self.logs_directory = make_config_path_absolute(clp_home, self.logs_directory)
self.tmp_directory = make_config_path_absolute(clp_home, self.tmp_directory)
self._container_image_id_path = make_config_path_absolute(
clp_home, self._container_image_id_path
)
self._version_file_path = make_config_path_absolute(clp_home, self._version_file_path)
def validate_logs_input_config(self, use_host_mount: bool = False):
logs_input_type = self.logs_input.type
if StorageType.FS == logs_input_type:
# NOTE: This can't be a pydantic validator since input_logs_dir might be a
# package-relative path that will only be resolved after pydantic validation
input_logs_dir = self.logs_input.directory
resolved_input_logs_dir = (
resolve_host_path_in_container(input_logs_dir) if use_host_mount else input_logs_dir
)
if not resolved_input_logs_dir.exists():
raise ValueError(f"logs_input.directory '{input_logs_dir}' doesn't exist.")
if not resolved_input_logs_dir.is_dir():
raise ValueError(f"logs_input.directory '{input_logs_dir}' is not a directory.")
if StorageType.S3 == logs_input_type and StorageEngine.CLP_S != self.package.storage_engine:
raise ValueError(
f"logs_input.type = 's3' is only supported with package.storage_engine"
f" = '{StorageEngine.CLP_S}'"
)
def validate_archive_output_config(self, use_host_mount: bool = False):
if (
StorageType.S3 == self.archive_output.storage.type
and StorageEngine.CLP_S != self.package.storage_engine
):
raise ValueError(
f"archive_output.storage.type = 's3' is only supported with package.storage_engine"
f" = '{StorageEngine.CLP_S}'"
)
archive_output_dir = self.archive_output.get_directory()
resolved_archive_output_dir = (
resolve_host_path_in_container(archive_output_dir)
if use_host_mount
else archive_output_dir
)
try:
validate_path_could_be_dir(resolved_archive_output_dir)
except ValueError as ex:
raise ValueError(f"archive_output.storage's directory is invalid: {ex}")
def validate_stream_output_config(self, use_host_mount: bool = False):
if (
StorageType.S3 == self.stream_output.storage.type
and StorageEngine.CLP_S != self.package.storage_engine
):
raise ValueError(
f"stream_output.storage.type = 's3' is only supported with package.storage_engine"
f" = '{StorageEngine.CLP_S}'"
)
stream_output_dir = self.stream_output.get_directory()
resolved_stream_output_dir = (
resolve_host_path_in_container(stream_output_dir)
if use_host_mount
else stream_output_dir
)
try:
validate_path_could_be_dir(resolved_stream_output_dir)
except ValueError as ex:
raise ValueError(f"stream_output.storage's directory is invalid: {ex}")
def validate_data_dir(self, use_host_mount: bool = False):
data_dir = self.data_directory
resolved_data_dir = resolve_host_path_in_container(data_dir) if use_host_mount else data_dir
try:
validate_path_could_be_dir(resolved_data_dir)
except ValueError as ex:
raise ValueError(f"data_directory is invalid: {ex}")
def validate_logs_dir(self, use_host_mount: bool = False):
logs_dir = self.logs_directory
resolved_logs_dir = resolve_host_path_in_container(logs_dir) if use_host_mount else logs_dir
try:
validate_path_could_be_dir(resolved_logs_dir)
except ValueError as ex:
raise ValueError(f"logs_directory is invalid: {ex}")
def validate_tmp_dir(self, use_host_mount: bool = False):
tmp_dir = self.tmp_directory
resolved_tmp_dir = resolve_host_path_in_container(tmp_dir) if use_host_mount else tmp_dir
try:
validate_path_could_be_dir(resolved_tmp_dir)
except ValueError as ex:
raise ValueError(f"tmp_directory is invalid: {ex}")
def validate_aws_config_dir(self, use_host_mount: bool = False):
profile_auth_used = False
auth_configs = []
if StorageType.S3 == self.logs_input.type:
auth_configs.append(self.logs_input.aws_authentication)
if StorageType.S3 == self.archive_output.storage.type:
auth_configs.append(self.archive_output.storage.s3_config.aws_authentication)
if StorageType.S3 == self.stream_output.storage.type:
auth_configs.append(self.stream_output.storage.s3_config.aws_authentication)
for auth in auth_configs:
if AwsAuthType.profile == auth.type:
profile_auth_used = True
break
if profile_auth_used:
if self.aws_config_directory is None:
raise ValueError(
"aws_config_directory must be set when using profile authentication"
)
resolved_aws_config_dir = (
resolve_host_path_in_container(self.aws_config_directory)
if use_host_mount
else self.aws_config_directory
)
if not resolved_aws_config_dir.exists():
raise ValueError(
f"aws_config_directory does not exist: '{self.aws_config_directory}'"
)
if not profile_auth_used and self.aws_config_directory is not None:
raise ValueError(
"aws_config_directory should not be set when profile authentication is not used"
)
def validate_api_server(self):
if StorageEngine.CLP == self.package.storage_engine and self.api_server is not None:
raise ValueError(
f"The API server is only compatible with storage engine `{StorageEngine.CLP_S}`."
)
def load_container_image_ref(self):
if self.container_image_ref is not None: