Skip to content

Commit

Permalink
♻️ Base connect_hub() off of edge function (#879)
Browse files Browse the repository at this point in the history
* migrate to edge function in connect

* install from lamindb PR

* ignore test_update_schema_in_hub for now

* check for FunctionsHttpError

* restore test_update_schema_in_hub

* restore check_whether_migrations_in_sync call

* check for lnid

* write api_url and schema_id

* Check that schema_id is UUID

* check correctness of lnid

* cross reference edge function tests
  • Loading branch information
Koncopd authored Oct 4, 2024
1 parent 9df1486 commit edb6fef
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 138 deletions.
8 changes: 6 additions & 2 deletions lamindb_setup/_connect_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ._close import close as close_instance
from ._init_instance import MESSAGE_NO_MULTIPLE_INSTANCE, load_from_isettings
from ._silence_loggers import silence_loggers
from .core._hub_core import connect_instance as connect_instance_from_hub
from .core._hub_core import connect_instance_hub
from .core._hub_utils import (
LaminDsn,
LaminDsnModel,
Expand Down Expand Up @@ -128,7 +128,7 @@ def _connect_instance(
# on the hub
# do not call hub if the user is anonymous
if owner != "anonymous":
hub_result = connect_instance_from_hub(
hub_result = connect_instance_hub(
owner=owner, name=name, access_token=access_token
)
else:
Expand Down Expand Up @@ -160,6 +160,10 @@ def _connect_instance(
git_repo=instance_result["git_repo"],
keep_artifacts_local=bool(instance_result["keep_artifacts_local"]),
is_on_hub=True,
api_url=instance_result["api_url"],
schema_id=None
if (schema_id := instance_result["schema_id"]) is None
else UUID(schema_id),
)
else:
if hub_result != "anonymous-user":
Expand Down
71 changes: 5 additions & 66 deletions lamindb_setup/core/_hub_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,69 +325,7 @@ def _init_instance(
logger.important(f"go to: https://lamin.ai/{isettings.owner}/{isettings.name}")


def connect_instance(
*,
owner: str, # account_handle
name: str, # instance_name
access_token: str | None = None,
) -> tuple[dict, dict] | str:
from ._settings import settings

if settings.user.handle != "anonymous" or access_token is not None:
return call_with_fallback_auth(
_connect_instance, owner=owner, name=name, access_token=access_token
)
else:
return call_with_fallback(_connect_instance, owner=owner, name=name)


def _connect_instance(
*,
owner: str, # account_handle
name: str, # instance_name
client: Client,
) -> tuple[dict, dict] | str:
instance_account_storage = select_instance_by_owner_name(owner, name, client)
if instance_account_storage is None:
# try the via single requests, will take more time
account = select_account_by_handle(owner, client)
if account is None:
return "account-not-exists"
instance = select_instance_by_name(account["id"], name, client)
if instance is None:
return "instance-not-found"
# get default storage
storage = select_default_storage_by_instance_id(instance["id"], client)
if storage is None:
return "default-storage-does-not-exist-on-hub"
else:
account = instance_account_storage.pop("account")
storage = instance_account_storage.pop("storage")
instance = instance_account_storage
# check if is postgres instance
# this used to be a check for `instance["db"] is not None` in earlier versions
# removed this on 2022-10-25 and can remove from the hub probably for lamindb 1.0
if instance["db_scheme"] is not None:
db_user = select_db_user_by_instance(instance["id"], client)
if db_user is None:
name, password = "none", "none"
else:
name, password = db_user["db_user_name"], db_user["db_user_password"]
# construct dsn from instance and db_account fields
db_dsn = LaminDsn.build(
scheme=instance["db_scheme"],
user=name,
password=password,
host=instance["db_host"],
port=instance["db_port"],
database=instance["db_database"],
)
instance["db"] = db_dsn
check_whether_migrations_in_sync(instance["lamindb_version"])
return instance, storage # type: ignore


def _connect_instance_new(
def _connect_instance_hub(
owner: str, # account_handle
name: str, # instance_name
client: Client,
Expand Down Expand Up @@ -439,10 +377,11 @@ def _connect_instance_new(
database=instance["db_database"],
)
instance["db"] = db_dsn
check_whether_migrations_in_sync(instance["lamindb_version"])
return instance, storage # type: ignore


def connect_instance_new(
def connect_instance_hub(
*,
owner: str, # account_handle
name: str, # instance_name
Expand All @@ -452,10 +391,10 @@ def connect_instance_new(

if settings.user.handle != "anonymous" or access_token is not None:
return call_with_fallback_auth(
_connect_instance_new, owner=owner, name=name, access_token=access_token
_connect_instance_hub, owner=owner, name=name, access_token=access_token
)
else:
return call_with_fallback(_connect_instance_new, owner=owner, name=name)
return call_with_fallback(_connect_instance_hub, owner=owner, name=name)


def access_aws(storage_root: str, access_token: str | None = None) -> dict[str, dict]:
Expand Down
5 changes: 5 additions & 0 deletions lamindb_setup/core/_settings_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def __init__(
schema: str | None = None, # comma-separated string of schema names
git_repo: str | None = None, # a git repo URL
is_on_hub: bool | None = None, # initialized from hub
api_url: str | None = None,
schema_id: UUID | None = None,
_locker_user: UserSettings | None = None, # user to lock for if cloud sqlite
):
from ._hub_utils import validate_db_arg
Expand All @@ -73,6 +75,9 @@ def __init__(
self._keep_artifacts_local = keep_artifacts_local
self._storage_local: StorageSettings | None = None
self._is_on_hub = is_on_hub
# private, needed for writing instance settings
self._api_url = api_url
self._schema_id = schema_id
# if None then settings.user is used
self._locker_user = _locker_user

Expand Down
22 changes: 15 additions & 7 deletions lamindb_setup/core/_settings_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def save_settings(
prefix: str,
):
with open(settings_file, "w") as f:
for store_key, type in type_hints.items():
if type == Optional[str]:
type = str
if type == Optional[bool]:
type = bool
for store_key, type_ in type_hints.items():
if type_ == Optional[str]:
type_ = str
if type_ == Optional[bool]:
type_ = bool
if "__" not in store_key:
if store_key == "model_config":
continue
Expand All @@ -52,7 +52,15 @@ def save_settings(
elif store_key == "storage_region":
value = settings.storage.region
else:
if store_key in {"db", "schema_str", "name_", "uuid", "id"}:
if store_key in {
"db",
"schema_str",
"name_",
"uuid",
"id",
"api_url",
"schema_id",
}:
settings_key = f"_{store_key.rstrip('_')}"
else:
settings_key = store_key
Expand All @@ -62,7 +70,7 @@ def save_settings(
elif isinstance(value, UUID):
value = value.hex
else:
value = type(value)
value = type_(value)
f.write(f"{prefix}{store_key}={value}\n")


Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def lint(session: nox.Session) -> None:
)
def install(session: nox.Session, group: str) -> None:
no_deps_packages = "git+https://github.com/laminlabs/lnschema-core git+https://github.com/laminlabs/wetlab git+https://github.com/laminlabs/lamin-cli"
schema_deps = f"""uv pip install --system git+https://github.com/laminlabs/bionty git+https://github.com/laminlabs/lamindb
schema_deps = f"""uv pip install --system git+https://github.com/laminlabs/bionty git+https://github.com/laminlabs/lamindb@migrate_edge_connect
uv pip install --system --no-deps {no_deps_packages}
"""
if group == "hub-cloud":
Expand Down
11 changes: 11 additions & 0 deletions tests/hub-cloud/test_edge_request.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import os
from uuid import UUID

import httpx
import lamindb_setup as ln_setup
from lamindb_setup.core.hashing import hash_and_encode_as_b62


# This shows how to make a request to execute get-instance-settings-v1 edge function
# This edge function is used in lamindb_setup.core._hub_core.connect_instance_hub
# connect_instance_hub is tested in hub-local tests, in test_all.py test_connect_instance_hub
def test_edge_request():
if os.environ["LAMIN_ENV"] == "prod":
# login to have access_token in ln_setup.settings.user
Expand Down Expand Up @@ -32,11 +37,17 @@ def test_edge_request():
instance = response.json()
# instance id
assert instance["id"] == "037ba1e0-8d80-4f91-a902-75a47735076a"
assert "lnid" in instance
# check correctness of lnid
assert instance["lnid"] == hash_and_encode_as_b62(UUID(instance["id"]).hex)[:12]
assert instance["owner"] == "laminlabs"
assert instance["name"] == "lamindata"
assert instance["api_url"] == "https://us-east-1.api.lamin.ai"
assert instance["schema_str"] == "bionty,wetlab"
assert "schema_id" in instance
# check that schema_id is well-formed UUID
# if not, throws ValueError
UUID(instance["schema_id"])
assert "git_repo" in instance
assert "keep_artifacts_local" in instance
assert "lamindb_version" in instance
Expand Down
4 changes: 2 additions & 2 deletions tests/hub-cloud/test_init_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest
from lamindb_setup._connect_instance import InstanceNotFoundError
from lamindb_setup.core._hub_client import connect_hub_with_auth
from lamindb_setup.core._hub_core import _connect_instance
from lamindb_setup.core._hub_core import _connect_instance_hub
from lamindb_setup.core._hub_crud import (
Client,
select_account_by_handle,
Expand Down Expand Up @@ -38,7 +38,7 @@ def test_init_instance_postgres_default_name(get_hub_client):
ln_setup.register(_test=True)
assert ln_setup.settings.instance.slug == "testuser2/pgtest"
# and check
instance, storage = _connect_instance(
instance, storage = _connect_instance_hub(
owner="testuser2", name=instance_name, client=hub
)
# hub checks
Expand Down
Loading

0 comments on commit edb6fef

Please sign in to comment.