Skip to content
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

Feature: Control of balance for instances #462

Merged
merged 29 commits into from
Aug 22, 2023
Merged

Feature: Control of balance for instances #462

merged 29 commits into from
Aug 22, 2023

Conversation

1yam
Copy link
Collaborator

@1yam 1yam commented Aug 3, 2023

Added functionality to check the current balance and compute costs for instances.

odesenfans and others added 2 commits July 27, 2023 23:38
Added functionality to check the current balance and compute costs for instances.
Copy link
Contributor

@odesenfans odesenfans left a comment

Choose a reason for hiding this comment

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

Good job, a few things to change here and there to keep you busy but the logic seems sound. Needs more unit tests though!

@@ -129,7 +129,7 @@ def recreate_cost_views():
free_disk.included_disk_space,
0) AS additional_disk_space) additional_disk,
LATERAL ( SELECT CASE
WHEN vms.persistent THEN 2000
WHEN COALESCE(vms.persistent, true) THEN 2000
ELSE 200
END AS base_compute_unit_price) bcp,
LATERAL ( SELECT 1 + vms.environment_internet::integer AS compute_unit_price_multiplier) m,
Copy link
Contributor

Choose a reason for hiding this comment

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

The changes to this migration script will have to be put in a separate migration script that recreates the VM cost view (views must be recreated entirely, you cannot just add a column). Create a new empty migration script with alembic revision -m "fix VM cost view" and then take the pieces that you need from this migration script to drop the vm cost view and recreate it.

Copy link
Contributor

Choose a reason for hiding this comment

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

The changes to this file must be reverted now.

src/aleph/handlers/content/content_handler.py Outdated Show resolved Hide resolved
@@ -298,6 +354,24 @@ class VmMessageHandler(ContentHandler):

"""

async def check_balance(self, session: DbSession, message: MessageDb):
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing return value type (-> None).

@@ -289,6 +301,50 @@ class HasParent(Protocol):
)


def get_extra_storage(content: InstanceContent, session: DbSession) -> int:
volumes: int = 0
for i in content.volumes:
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename i to volume.

@@ -289,6 +301,50 @@ class HasParent(Protocol):
)


def get_extra_storage(content: InstanceContent, session: DbSession) -> int:
volumes: int = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename to total_volume_size. It would be better if this function returned the size in bytes, and then let the caller do whatever it wants (convert to MB, MiB, Watts, I don't care).

return volumes


def get_additional_storage_price(content, session: DbSession) -> float:
Copy link
Contributor

Choose a reason for hiding this comment

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

Always use decimals when manipulating prices.

@@ -316,6 +315,7 @@ async def process(
session=session, pending_message=pending_message
)
content_handler = self.get_content_handler(message.type)
await content_handler.check_balance(session=session, message=message)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should definitely be called after check_dependencies (otherwise we might look for volumes that don't exist) and probably after check_permissions (we should reduce the amount of computation done before checking permissions).

src/aleph/types/message_status.py Show resolved Hide resolved
@@ -2,6 +2,7 @@
import itertools
Copy link
Contributor

Choose a reason for hiding this comment

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

I 'd like to see more tests:

  • unit tests for the code that computes the cost of a single InstanceContent object.
  • a test here that tries to process an instance without having enough tokens.

Copy link
Contributor

Choose a reason for hiding this comment

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

Where are the additional tests?

@@ -113,3 +113,14 @@ def refresh_vm_version(session: DbSession, vm_hash: str) -> None:
)
session.execute(delete(VmVersionDb).where(VmVersionDb.vm_hash == vm_hash))
session.execute(upsert_stmt)


def get_total_cost_for_address(session: DbSession, address: str) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

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

The return value should be a Decimal (costs can be fractional).

Copy link
Contributor

@odesenfans odesenfans left a comment

Choose a reason for hiding this comment

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

A lot of my comments from the first review still need to be addressed.

@@ -129,7 +129,7 @@ def recreate_cost_views():
free_disk.included_disk_space,
0) AS additional_disk_space) additional_disk,
LATERAL ( SELECT CASE
WHEN vms.persistent THEN 2000
WHEN COALESCE(vms.persistent, true) THEN 2000
ELSE 200
END AS base_compute_unit_price) bcp,
LATERAL ( SELECT 1 + vms.environment_internet::integer AS compute_unit_price_multiplier) m,
Copy link
Contributor

Choose a reason for hiding this comment

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

The changes to this file must be reverted now.

def downgrade() -> None:
op.execute("drop view costs_view")
op.execute("drop view vm_costs_view")
op.execute("drop view vm_volumes_files_view")
Copy link
Contributor

Choose a reason for hiding this comment

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

That's not really what the downgrade should do. A downgrade should bring the view back to its previous version. I'll fix it myself, just FYI.

src/aleph/handlers/content/content_handler.py Outdated Show resolved Hide resolved
@@ -243,7 +255,7 @@ def add_ref_to_check(_volume):


def check_parent_volumes_size_requirements(
session: DbSession, content: ExecutableContent
session: DbSession, content: ExecutableContent
Copy link
Contributor

Choose a reason for hiding this comment

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

I see a few changes in spacing in the PR, I don't understand why. Are you using the PyCharm formatter instead of black?

total_volume_size: int = 0
for volume in content.volumes:
if hasattr(volume, "ref") and volume.ref:
pin_file = get_message_file_pin(session=session, item_hash=volume.ref)
Copy link
Contributor

Choose a reason for hiding this comment

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

As discussed, this should be get_file_tag.

src/aleph/types/message_status.py Show resolved Hide resolved
@@ -2,6 +2,7 @@
import itertools
Copy link
Contributor

Choose a reason for hiding this comment

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

Where are the additional tests?

@1yam 1yam requested a review from odesenfans August 8, 2023 21:06
@odesenfans odesenfans changed the title Feature: Control of balance for Instances Feature: Control of balance for instances Aug 22, 2023
@odesenfans odesenfans merged commit 4c50268 into aleph-im:master Aug 22, 2023
2 checks passed
@odesenfans odesenfans deleted the 1yam-vm-balance-check branch August 22, 2023 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants