Skip to content

Commit edd5c7e

Browse files
main changes
1 parent 3c6ad26 commit edd5c7e

4 files changed

Lines changed: 331 additions & 229 deletions

File tree

components/job-orchestration/job_orchestration/executor/query/extract_stream_task.py

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
s3_put,
2222
)
2323
from clp_py_utils.sql_adapter import SqlAdapter
24+
from structlog.contextvars import bound_contextvars
2425

2526
from job_orchestration.executor.query.celery import app
2627
from job_orchestration.executor.query.utils import (
@@ -35,6 +36,25 @@
3536
logger = get_task_logger(__name__)
3637

3738

39+
def _get_extract_stream_task_log_context(
40+
job_id: str,
41+
task_id: int,
42+
query_job_type: str,
43+
archive_id: str,
44+
dataset: str | None,
45+
) -> dict[str, Any]:
46+
context: dict[str, Any] = {
47+
"job_id": job_id,
48+
"task_id": task_id,
49+
"query_job_type": query_job_type,
50+
"archive_id": archive_id,
51+
}
52+
if dataset is not None:
53+
context["dataset"] = dataset
54+
55+
return context
56+
57+
3858
def _make_clp_command_and_env_vars(
3959
clp_home: Path,
4060
worker_config: WorkerConfig,
@@ -198,7 +218,7 @@ def extract_stream_entry_point(
198218
clp_logging_level = os.getenv("CLP_LOGGING_LEVEL")
199219
set_logging_level(logger, clp_logging_level)
200220

201-
logger.info(f"Started {task_name} task for job {job_id}")
221+
logger.info(f"Started {task_name} task")
202222

203223
start_time = datetime.datetime.now()
204224
task_status: QueryTaskStatus
@@ -303,24 +323,31 @@ def extract_stream(
303323
self: Task,
304324
job_id: str,
305325
task_id: int,
326+
query_job_type: str,
306327
job_config: dict,
307328
archive_id: str,
308329
clp_metadata_db_conn_params: dict,
309330
results_cache_uri: str,
310331
dataset: str | None = None,
311332
) -> dict[str, Any]:
312-
try:
313-
return extract_stream_entry_point(
314-
job_id,
315-
task_id,
316-
job_config,
317-
archive_id,
318-
clp_metadata_db_conn_params,
319-
results_cache_uri,
320-
dataset,
333+
with bound_contextvars(
334+
**_get_extract_stream_task_log_context(
335+
job_id, task_id, query_job_type, archive_id, dataset
321336
)
322-
except SoftTimeLimitExceeded:
323-
logger.exception(
324-
f"Stream extraction task job_id={job_id} task_id={task_id} exceeded soft time limit."
325-
)
326-
raise
337+
):
338+
try:
339+
return extract_stream_entry_point(
340+
job_id,
341+
task_id,
342+
job_config,
343+
archive_id,
344+
clp_metadata_db_conn_params,
345+
results_cache_uri,
346+
dataset,
347+
)
348+
except SoftTimeLimitExceeded:
349+
logger.exception("Stream extraction task exceeded soft time limit.")
350+
raise
351+
except Exception:
352+
logger.exception("Stream extraction task failed with an unexpected exception.")
353+
raise

components/job-orchestration/job_orchestration/executor/query/fs_search_task.py

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,45 @@
2222
)
2323
from clp_py_utils.sql_adapter import SqlAdapter
2424
from clp_py_utils.telemetry_config import is_telemetry_disabled_by_env
25+
from structlog.contextvars import bound_contextvars
2526

2627
from job_orchestration.executor.query.celery import app
2728
from job_orchestration.executor.query.utils import (
29+
get_query_hash,
2830
report_task_failure,
2931
run_query_task,
3032
)
3133
from job_orchestration.executor.utils import load_worker_config
34+
from job_orchestration.scheduler.constants import QueryJobType
3235
from job_orchestration.scheduler.job_config import SearchJobConfig
3336
from job_orchestration.scheduler.scheduler_data import QueryTaskResult, QueryTaskStatus
3437

3538
# Setup logging
3639
logger = get_task_logger(__name__)
3740

3841

42+
def _get_search_task_log_context(
43+
job_id: str,
44+
task_id: int,
45+
job_config_blob: bytes,
46+
archive_id: str,
47+
dataset: str | None,
48+
) -> dict[str, Any]:
49+
context: dict[str, Any] = {
50+
"job_id": job_id,
51+
"task_id": task_id,
52+
"query_job_type": QueryJobType.SEARCH_OR_AGGREGATION.to_str(),
53+
"archive_id": archive_id,
54+
}
55+
if dataset is not None:
56+
context["dataset"] = dataset
57+
58+
search_config = SearchJobConfig.model_validate(msgpack.unpackb(job_config_blob))
59+
context["query"] = search_config.query_string
60+
context["query_hash"] = get_query_hash(search_config.query_string)
61+
return context
62+
63+
3964
def _make_core_clp_command_and_env_vars(
4065
clp_home: Path,
4166
worker_config: WorkerConfig,
@@ -238,7 +263,7 @@ def search_entry_point(
238263
clp_logging_level = os.getenv("CLP_LOGGING_LEVEL")
239264
set_logging_level(logger, clp_logging_level)
240265

241-
logger.info(f"Started {task_name} task for job {job_id}")
266+
logger.info(f"Started {task_name} task")
242267

243268
start_time = datetime.datetime.now()
244269
sql_adapter = SqlAdapter(Database.model_validate(clp_metadata_db_conn_params))
@@ -312,16 +337,24 @@ def search(
312337
results_cache_uri: str,
313338
dataset: str | None = None,
314339
) -> dict[str, Any]:
315-
try:
316-
return search_entry_point(
317-
job_id,
318-
task_id,
319-
job_config_blob,
320-
archive_id,
321-
clp_metadata_db_conn_params,
322-
results_cache_uri,
323-
dataset,
340+
with bound_contextvars(
341+
**_get_search_task_log_context(
342+
job_id, task_id, job_config_blob, archive_id, dataset
324343
)
325-
except SoftTimeLimitExceeded:
326-
logger.exception(f"Search task job_id={job_id} task_id={task_id} exceeded soft time limit.")
327-
raise
344+
):
345+
try:
346+
return search_entry_point(
347+
job_id,
348+
task_id,
349+
job_config_blob,
350+
archive_id,
351+
clp_metadata_db_conn_params,
352+
results_cache_uri,
353+
dataset,
354+
)
355+
except SoftTimeLimitExceeded:
356+
logger.exception("Search task exceeded soft time limit.")
357+
raise
358+
except Exception:
359+
logger.exception("Search task failed with an unexpected exception.")
360+
raise

components/job-orchestration/job_orchestration/executor/query/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import hashlib
23
import logging
34
import os
45
import signal
@@ -15,6 +16,10 @@
1516
from job_orchestration.scheduler.scheduler_data import QueryTaskResult, QueryTaskStatus
1617

1718

19+
def get_query_hash(query_string: str) -> str:
20+
return hashlib.sha256(query_string.encode("utf-8")).hexdigest()
21+
22+
1823
def get_task_log_file_path(clp_logs_dir: Path, job_id: str, task_id: int) -> Path:
1924
worker_logs_dir = clp_logs_dir / job_id
2025
worker_logs_dir.mkdir(exist_ok=True, parents=True)
@@ -91,12 +96,10 @@ def sigterm_handler(_signo, _stack_frame):
9196
return_code = task_proc.returncode
9297
if 0 != return_code:
9398
task_status = QueryTaskStatus.FAILED
94-
logger.error(
95-
f"{task_name} task {task_id} failed for job {job_id} - return_code={return_code}"
96-
)
99+
logger.error(f"{task_name} task failed - return_code={return_code}")
97100
else:
98101
task_status = QueryTaskStatus.SUCCEEDED
99-
logger.info(f"{task_name} task {task_id} completed for job {job_id}")
102+
logger.info(f"{task_name} task completed")
100103

101104
clo_log_file.close()
102105
if 0 != return_code:

0 commit comments

Comments
 (0)