-
Notifications
You must be signed in to change notification settings - Fork 19
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
Conversation
Added functionality to check the current balance and compute costs for instances.
There was a problem hiding this 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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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/vm.py
Outdated
@@ -298,6 +354,24 @@ class VmMessageHandler(ContentHandler): | |||
|
|||
""" | |||
|
|||
async def check_balance(self, session: DbSession, message: MessageDb): |
There was a problem hiding this comment.
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).
src/aleph/handlers/content/vm.py
Outdated
@@ -289,6 +301,50 @@ class HasParent(Protocol): | |||
) | |||
|
|||
|
|||
def get_extra_storage(content: InstanceContent, session: DbSession) -> int: | |||
volumes: int = 0 | |||
for i in content.volumes: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename i
to volume
.
src/aleph/handlers/content/vm.py
Outdated
@@ -289,6 +301,50 @@ class HasParent(Protocol): | |||
) | |||
|
|||
|
|||
def get_extra_storage(content: InstanceContent, session: DbSession) -> int: | |||
volumes: int = 0 |
There was a problem hiding this comment.
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).
src/aleph/handlers/content/vm.py
Outdated
return volumes | ||
|
||
|
||
def get_additional_storage_price(content, session: DbSession) -> float: |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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).
@@ -2,6 +2,7 @@ | |||
import itertools |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
src/aleph/db/accessors/vms.py
Outdated
@@ -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: |
There was a problem hiding this comment.
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).
There was a problem hiding this 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, |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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/vm.py
Outdated
@@ -243,7 +255,7 @@ def add_ref_to_check(_volume): | |||
|
|||
|
|||
def check_parent_volumes_size_requirements( | |||
session: DbSession, content: ExecutableContent | |||
session: DbSession, content: ExecutableContent |
There was a problem hiding this comment.
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?
src/aleph/handlers/content/vm.py
Outdated
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) |
There was a problem hiding this comment.
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
.
@@ -2,6 +2,7 @@ | |||
import itertools |
There was a problem hiding this comment.
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?
Co-authored-by: Olivier Desenfans <desenfans.olivier@gmail.com>
…1yam-vm-balance-check
Added functionality to check the current balance and compute costs for instances.