Repository navigation
build: drop psycopg-binary from extra_proxy and use the image libpq #42909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
8
commits into
main
Choose a base branch
from
litellm_psycopg_no_binary_wheel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1326c51
build: drop psycopg-binary from extra_proxy and link psycopg to the i…
yucheng-berri 62fc7ef
test(integration): spawn the partitioned proxy child with -P
yucheng-berri 7399cca
build: tighten the extra_proxy psycopg note
yucheng-berri 9ad7f13
build: describe the bundled OpenSSL accurately
yucheng-berri 3348c26
fix(proxy-extras): name the libpq load failure when the partition che…
yucheng-berri 7f33732
ci: install libpq5 on the database integration shard for the pure Pyt…
yucheng-berri 185a56e
Merge remote-tracking branch 'origin/main' into litellm_psycopg_no_bi…
yucheng-berri 36627a6
build: ship libpq in the backend, gateway and migrations images for p…
yucheng-berri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
tests/integration/database/test_spend_logs_partition_driver.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import os | ||
| import socket | ||
| import subprocess | ||
| import sys | ||
| import uuid | ||
| from collections.abc import Iterator | ||
| from contextlib import contextmanager | ||
| from pathlib import Path | ||
| from typing import Final | ||
| from urllib.parse import urlsplit, urlunsplit | ||
|
|
||
| import psycopg | ||
| from integration._support.client import Gateway | ||
| from integration._support.process import owned_proxy_process | ||
| from packaging.requirements import Requirement | ||
| from psycopg import sql | ||
|
|
||
| REPO_ROOT: Final = Path(__file__).resolve().parents[3] | ||
| PRISMA_DIR: Final = REPO_ROOT / "litellm-proxy-extras" / "litellm_proxy_extras" | ||
| PARTITION_SCRIPT: Final = REPO_ROOT / "db_scripts" / "partition_spend_logs.sql" | ||
| IMAGE_DOCKERFILES: Final = ( | ||
| REPO_ROOT / "Dockerfile", | ||
| REPO_ROOT / "docker" / "Dockerfile.database", | ||
| REPO_ROOT / "docker" / "Dockerfile.non_root", | ||
| REPO_ROOT / "backend" / "Dockerfile", | ||
| REPO_ROOT / "gateway" / "Dockerfile", | ||
| REPO_ROOT / "migrations" / "Dockerfile", | ||
| ) | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| PURE_PYTHON_DRIVER: Final = {"PSYCOPG_IMPL": "python"} | ||
|
|
||
|
|
||
| def locked_extra_proxy_packages() -> frozenset[str]: | ||
| export: Final = subprocess.run( | ||
| ["uv", "export", "--frozen", "--no-hashes", "--no-dev", "--no-emit-project", "--extra", "extra_proxy"], | ||
| check=True, | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=120, | ||
| cwd=REPO_ROOT, | ||
| ) | ||
| return frozenset( | ||
| Requirement(line.split(" ;")[0]).name.lower().replace("_", "-") | ||
| for line in export.stdout.splitlines() | ||
| if line and not line.startswith(("#", "-", " ")) | ||
| ) | ||
|
|
||
|
|
||
| def free_port() -> int: | ||
| with socket.socket() as reserve: | ||
| reserve.bind(("127.0.0.1", 0)) | ||
| return reserve.getsockname()[1] | ||
|
|
||
|
|
||
| def runtime_stage(dockerfile: Path) -> str: | ||
| return dockerfile.read_text().rsplit("FROM $LITELLM_RUNTIME_IMAGE", maxsplit=1)[1] | ||
|
|
||
|
|
||
| @contextmanager | ||
| def partitioned_database() -> Iterator[str]: | ||
| name: Final = f"integration_partitioned_{uuid.uuid4().hex}" | ||
| admin_url: Final = os.environ["DATABASE_URL"] | ||
| database_url: Final = urlunsplit(urlsplit(admin_url)._replace(path=f"/{name}")) | ||
| with psycopg.connect(admin_url, autocommit=True) as admin: | ||
| admin.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(name))) | ||
| try: | ||
| subprocess.run( | ||
| [ | ||
| sys.executable, | ||
| "-I", | ||
| "-m", | ||
| "prisma", | ||
| "migrate", | ||
| "deploy", | ||
| "--schema", | ||
| str(PRISMA_DIR / "schema.prisma"), | ||
| ], | ||
| check=True, | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=300, | ||
| env={**os.environ, "DATABASE_URL": database_url}, | ||
| ) | ||
| with psycopg.connect(database_url, autocommit=True) as connection: | ||
| connection.execute(PARTITION_SCRIPT.read_text()) | ||
| yield database_url | ||
| finally: | ||
| admin.execute(sql.SQL("DROP DATABASE {} WITH (FORCE)").format(sql.Identifier(name))) | ||
|
|
||
|
|
||
| def test_proxy_image_extra_ships_psycopg_without_the_binary_wheel_and_uses_the_image_libpq() -> None: | ||
| packages: Final = locked_extra_proxy_packages() | ||
| assert "psycopg" in packages, sorted(packages) | ||
| assert "psycopg-binary" not in packages, sorted(packages) | ||
| for dockerfile in IMAGE_DOCKERFILES: | ||
| assert "libpq" in runtime_stage(dockerfile), f"{dockerfile.name} runtime stage installs no libpq for psycopg" | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def test_pure_python_psycopg_detects_partitioned_spend_logs_and_refuses_db_push(gateway: Gateway) -> None: | ||
| with partitioned_database() as database_url: | ||
| proxy: Final = subprocess.run( | ||
| [ | ||
| sys.executable, | ||
| "-P", | ||
| "-m", | ||
| "integration._support.proxy", | ||
| "--config", | ||
| "tests/integration/proxy_config.yaml", | ||
| "--host", | ||
| "127.0.0.1", | ||
| "--port", | ||
| str(free_port()), | ||
| "--use_prisma_db_push", | ||
| ], | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=300, | ||
| cwd=REPO_ROOT, | ||
| env={ | ||
| **os.environ, | ||
| **PURE_PYTHON_DRIVER, | ||
| "DATABASE_URL": database_url, | ||
| "LITELLM_MASTER_KEY": gateway.key, | ||
| "LITELLM_SALT_KEY": os.environ.get("LITELLM_SALT_KEY", "sk-integration-salt"), | ||
| }, | ||
| ) | ||
| output: Final = proxy.stdout + proxy.stderr | ||
| assert proxy.returncode != 0, output | ||
| assert "LiteLLM_SpendLogs is a partitioned table" in output, output | ||
| assert "psycopg is not installed" not in output, output | ||
| with psycopg.connect(database_url) as connection: | ||
| partitioned: Final = connection.execute( | ||
| "SELECT 1 FROM pg_partitioned_table pt JOIN pg_class c ON c.oid = pt.partrelid " | ||
| "WHERE c.relname = 'LiteLLM_SpendLogs'" | ||
| ).fetchone() | ||
| assert partitioned is not None, "db push rewrote the partitioned LiteLLM_SpendLogs table" | ||
|
|
||
|
|
||
| def test_pure_python_psycopg_lets_an_unpartitioned_proxy_boot_and_serve(gateway: Gateway, tmp_path: Path) -> None: | ||
| with owned_proxy_process(gateway, tmp_path, PURE_PYTHON_DRIVER) as proxy: | ||
| health: Final = proxy.gateway.request("GET", "/health/readiness") | ||
| assert health.status_code == 200, health.text | ||
| assert health.json()["db"] == "connected", health.text | ||
| log: Final = proxy.log.read_text() | ||
| assert "psycopg is not installed" not in log, log | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.