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

Check message status before checking the payment #679

Merged
merged 4 commits into from
Aug 23, 2024
Merged
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
20 changes: 19 additions & 1 deletion src/aleph/vm/orchestrator/messages.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import asyncio
import copy

from aiohttp import ClientConnectorError, ClientResponseError
from aiohttp import ClientConnectorError, ClientResponseError, ClientSession
from aiohttp.web_exceptions import HTTPNotFound, HTTPServiceUnavailable
from aleph_message.models import ExecutableMessage, ItemHash, MessageType
from aleph_message.status import MessageStatus

from aleph.vm.conf import settings
from aleph.vm.storage import get_latest_amend, get_message


Expand Down Expand Up @@ -69,3 +71,19 @@
message = copy.deepcopy(original_message)
await update_message(message)
return message, original_message


async def get_message_status(item_hash: ItemHash) -> MessageStatus:
"""
Fetch the status of an execution from the reference API server.
We use a normal API call to the CCN instead to use the connector because we want to get the updated status of the
message and bypass the messages cache.
"""
async with ClientSession() as session:
url = f"{settings.API_SERVER}/api/v0/messages/{item_hash}"
resp = await session.get(url)

Check warning on line 84 in src/aleph/vm/orchestrator/messages.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/orchestrator/messages.py#L83-L84

Added lines #L83 - L84 were not covered by tests
# Raise an error if the request failed
resp.raise_for_status()

Check warning on line 86 in src/aleph/vm/orchestrator/messages.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/orchestrator/messages.py#L86

Added line #L86 was not covered by tests

resp_data = await resp.json()
return resp_data["status"]

Check warning on line 89 in src/aleph/vm/orchestrator/messages.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/orchestrator/messages.py#L88-L89

Added lines #L88 - L89 were not covered by tests
12 changes: 11 additions & 1 deletion src/aleph/vm/orchestrator/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
ProgramMessage,
parse_message,
)
from aleph_message.status import MessageStatus
from yarl import URL

from aleph.vm.conf import settings
from aleph.vm.pool import VmPool
from aleph.vm.utils import create_task_log_exceptions

from .messages import load_updated_message
from .messages import get_message_status, load_updated_message
from .payment import (
compute_required_balance,
compute_required_flow,
Expand Down Expand Up @@ -148,6 +149,14 @@
while True:
await asyncio.sleep(settings.PAYMENT_MONITOR_INTERVAL)

# Check if the executions continues existing or are forgotten before checking the payment
for vm_hash in pool.executions.keys():
message_status = await get_message_status(vm_hash)

Check warning on line 154 in src/aleph/vm/orchestrator/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/orchestrator/tasks.py#L154

Added line #L154 was not covered by tests
if message_status != MessageStatus.PROCESSED:
logger.debug(f"Stopping {vm_hash} execution due to {message_status} message status")
await pool.stop_vm(vm_hash)
pool.forget_vm(vm_hash)

Check warning on line 158 in src/aleph/vm/orchestrator/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/orchestrator/tasks.py#L156-L158

Added lines #L156 - L158 were not covered by tests

# Check if the balance held in the wallet is sufficient holder tier resources (Not do it yet)
for sender, chains in pool.get_executions_by_sender(payment_type=PaymentType.hold).items():
for chain, executions in chains.items():
Expand All @@ -171,6 +180,7 @@
logger.debug(
f"Get stream flow from Sender {sender} to Receiver {settings.PAYMENT_RECEIVER_ADDRESS} of {stream}"
)

required_stream = await compute_required_flow(executions)
logger.debug(f"Required stream for Sender {sender} executions: {required_stream}")
# Stop executions until the required stream is reached
Expand Down
15 changes: 0 additions & 15 deletions tests/supervisor/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@
from aleph.vm.orchestrator.status import check_internet


@pytest.mark.asyncio
async def test_check_internet_no_server_header():
vm_id = ItemHash("cafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe")

mock_session = Mock()
mock_session.get = MagicMock()
mock_session.get.__aenter__.return_value.json = AsyncMock(return_value={"result": 200})

# A "Server" header is always expected in the result from the VM.
# If it is not present, the diagnostic VM is not working correctly
# and an error must be raised.
with pytest.raises(ValueError):
await check_internet(mock_session, vm_id)


@pytest.mark.asyncio
async def test_check_internet_wrong_result_code():
vm_id = ItemHash("cafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe")
Expand Down