Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 72 additions & 8 deletions tools/deployment/presto-clp/scripts/init.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import argparse
import logging
import math
import os
import sys
from pathlib import Path
from typing import Any, Dict, Optional

import psutil
import yaml
from dotenv import dotenv_values

Expand Down Expand Up @@ -226,21 +229,82 @@ def _add_worker_env_vars(coordinator_common_env_file_path: Path, env_vars: Dict[
config = dotenv_values(coordinator_common_env_file_path)

try:
env_vars["PRESTO_COORDINATOR_CONFIGPROPERTIES_DISCOVERY_URI"] = (
f'http://{config["PRESTO_COORDINATOR_SERVICENAME"]}'
f':{config["PRESTO_COORDINATOR_HTTPPORT"]}'
coordinator_service_name = _get_required_env_var(
config, "PRESTO_COORDINATOR_SERVICENAME", coordinator_common_env_file_path
)
except KeyError as e:
logger.error(
"Missing required key %s in '%s'",
e,
coordinator_common_env_file_path,
coordinator_http_port = _get_required_env_var(
config, "PRESTO_COORDINATOR_HTTPPORT", coordinator_common_env_file_path
)
except KeyError:
return False

env_vars["PRESTO_COORDINATOR_CONFIGPROPERTIES_DISCOVERY_URI"] = (
f"http://{coordinator_service_name}:{coordinator_http_port}"
)

if not _configure_worker_memory_env_vars(env_vars):
return False

return True


def _configure_worker_memory_env_vars(env_vars: Dict[str, str]) -> bool:
"""Adds memory-related environment variables for the worker."""

total_memory_bytes = psutil.virtual_memory().total
total_memory_gb = total_memory_bytes / (1024**3)
total_memory_gb_floor = max(1, math.floor(total_memory_gb))

system_memory_gb = min(
total_memory_gb_floor,
max(1, math.floor(total_memory_gb * 0.90)),
)
query_memory_gb = min(
system_memory_gb,
max(1, math.floor(system_memory_gb * (2 / 3))),
)
system_mem_limit_gb = min(
total_memory_gb_floor,
max(system_memory_gb, max(1, math.floor(total_memory_gb * 0.94))),
)

env_vars["PRESTO_WORKER_CONFIGPROPERTIES_QUERY_MEMORY_GB"] = str(query_memory_gb)
env_vars["PRESTO_WORKER_CONFIGPROPERTIES_SYSTEM_MEMORY_GB"] = str(system_memory_gb)
env_vars["PRESTO_WORKER_CONFIGPROPERTIES_SYSTEM_MEM_LIMIT_GB"] = str(system_mem_limit_gb)

logger.info(
(
"Detected %.2f GB of system memory; configured Presto worker memory limits "
"(query-memory-gb=%s, system-memory-gb=%s, system-mem-limit-gb=%s)."
),
total_memory_gb,
env_vars["PRESTO_WORKER_CONFIGPROPERTIES_QUERY_MEMORY_GB"],
env_vars["PRESTO_WORKER_CONFIGPROPERTIES_SYSTEM_MEMORY_GB"],
env_vars["PRESTO_WORKER_CONFIGPROPERTIES_SYSTEM_MEM_LIMIT_GB"],
)

return True


def _get_required_env_var(config: Dict[str, Optional[str]], key: str, env_file_path: Path) -> str:
"""
Fetches a required entry from an environment variables dictionary.

:param config:
:param key: The key that must exist in `config` with a non-empty value.
:param env_file_path:
:return: The string value associated with `key`.
:raises KeyError: If `key` is missing.
"""

value = config.get(key)
if value is None:
logger.error("Missing required key %s in '%s'", key, env_file_path)
raise KeyError(key)

return value

Comment on lines +289 to +306
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Consider validating empty strings to match the docstring.

The docstring states the function requires a "non-empty value," but the implementation only checks for None. Empty strings would pass validation but might be invalid for coordinator settings.

If empty strings should be rejected, apply this diff:

 def _get_required_env_var(config: Dict[str, Optional[str]], key: str, env_file_path: Path) -> str:
     """
     Fetches a required entry from an environment variables dictionary.
 
     :param config:
     :param key: The key that must exist in `config` with a non-empty value.
     :param env_file_path:
     :return: The string value associated with `key`.
     :raises KeyError: If `key` is missing.
     """
 
     value = config.get(key)
-    if value is None:
+    if not value:
         logger.error("Missing required key %s in '%s'", key, env_file_path)
         raise KeyError(key)
 
     return value

Alternatively, update the docstring to clarify that only None is checked:

-    :param key: The key that must exist in `config` with a non-empty value.
+    :param key: The key that must exist in `config` with a non-None value.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def _get_required_env_var(config: Dict[str, Optional[str]], key: str, env_file_path: Path) -> str:
"""
Fetches a required entry from an environment variables dictionary.
:param config:
:param key: The key that must exist in `config` with a non-empty value.
:param env_file_path:
:return: The string value associated with `key`.
:raises KeyError: If `key` is missing.
"""
value = config.get(key)
if value is None:
logger.error("Missing required key %s in '%s'", key, env_file_path)
raise KeyError(key)
return value
def _get_required_env_var(config: Dict[str, Optional[str]], key: str, env_file_path: Path) -> str:
"""
Fetches a required entry from an environment variables dictionary.
:param config:
:param key: The key that must exist in `config` with a non-empty value.
:param env_file_path:
:return: The string value associated with `key`.
:raises KeyError: If `key` is missing.
"""
value = config.get(key)
if not value:
logger.error("Missing required key %s in '%s'", key, env_file_path)
raise KeyError(key)
return value
🤖 Prompt for AI Agents
In tools/deployment/presto-clp/scripts/init.py around lines 289 to 306, the
function claims to require a "non-empty value" but only checks for None; change
the guard to reject empty or whitespace-only strings (e.g., treat value is None
or value.strip() == "" as missing), keep the same logger.error and raise
KeyError(key) behavior, and return the value otherwise; if you instead prefer
only to disallow None, update the docstring to remove "non-empty" wording.


def _generate_worker_clp_properties(
worker_config_template_path: Path, env_vars: Dict[str, str]
) -> bool:
Expand Down
1 change: 1 addition & 0 deletions tools/deployment/presto-clp/scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
psutil
python-dotenv
PyYAML
5 changes: 5 additions & 0 deletions tools/deployment/presto-clp/worker.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
PRESTO_WORKER_HTTP_PORT=8080

# config.properties
PRESTO_WORKER_CONFIGPROPERTIES_QUERY_MEMORY_GB=38
PRESTO_WORKER_CONFIGPROPERTIES_SYSTEM_MEMORY_GB=57
PRESTO_WORKER_CONFIGPROPERTIES_SYSTEM_MEM_LIMIT_GB=60

# node.properties
PRESTO_WORKER_NODEPROPERTIES_LOCATION=worker-location
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ http-server.http.port=${PRESTO_WORKER_HTTP_PORT}
shutdown-onset-sec=1
register-test-functions=false
runtime-metrics-collection-enabled=false
system-memory-gb=${PRESTO_WORKER_CONFIGPROPERTIES_SYSTEM_MEMORY_GB}
query-memory-gb=${PRESTO_WORKER_CONFIGPROPERTIES_QUERY_MEMORY_GB}
system-mem-limit-gb=${PRESTO_WORKER_CONFIGPROPERTIES_SYSTEM_MEM_LIMIT_GB}
Loading