-
Notifications
You must be signed in to change notification settings - Fork 84
Interface for Queues #220
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
oanarosca
wants to merge
7
commits into
master
Choose a base branch
from
oanarosca/queues
base: master
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
Interface for Queues #220
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
from typing import Optional | ||
from sebs.faas.queue import Queue, QueueType | ||
|
||
import boto3 | ||
|
||
|
||
class SQS(Queue): | ||
@staticmethod | ||
def typename() -> str: | ||
return "AWS.SQS" | ||
|
||
@staticmethod | ||
def deployment_name(): | ||
return "aws" | ||
|
||
@property | ||
def queue_url(self): | ||
return self._queue_url | ||
|
||
@property | ||
def queue_arn(self): | ||
return self._queue_arn | ||
|
||
def __init__( | ||
self, | ||
benchmark: str, | ||
queue_type: QueueType, | ||
region: str, | ||
queue_url: Optional[str] = None, | ||
queue_arn: Optional[str] = None | ||
): | ||
super().__init__( | ||
benchmark, | ||
queue_type, | ||
region | ||
) | ||
self._queue_url = queue_url | ||
self._queue_arn = queue_arn | ||
|
||
self.client = boto3.session.Session().client( | ||
'sqs', | ||
region_name=self.region | ||
) | ||
|
||
def create_queue(self) -> str: | ||
self.logging.debug(f"Creating queue {self.name}") | ||
|
||
if (self._queue_url and self._queue_arn): | ||
self.logging.debug("Queue already exists, reusing...") | ||
return | ||
|
||
self._queue_url = self.client.create_queue(QueueName=self.name)["QueueUrl"] | ||
self._queue_arn = self.client.get_queue_attributes( | ||
QueueUrl=self.queue_url, | ||
AttributeNames=["QueueArn"], | ||
)["Attributes"]["QueueArn"] | ||
|
||
self.logging.debug("Created queue") | ||
|
||
def remove_queue(self): | ||
self.logging.info(f"Deleting queue {self.name}") | ||
|
||
self.client.delete_queue(QueueUrl=self.queue_url) | ||
|
||
self.logging.info("Deleted queue") | ||
|
||
def send_message(self, serialized_message: str): | ||
self.client.send_message( | ||
QueueUrl=self.queue_url, | ||
MessageBody=serialized_message, | ||
) | ||
self.logging.info(f"Sent message to queue {self.name}") | ||
|
||
def receive_message(self) -> str: | ||
self.logging.info(f"Pulling a message from {self.name}") | ||
|
||
response = self.client.receive_message( | ||
QueueUrl=self.queue_url, | ||
AttributeNames=["SentTimestamp"], | ||
MaxNumberOfMessages=1, | ||
MessageAttributeNames=["All"], | ||
WaitTimeSeconds=5, | ||
) | ||
|
||
if ("Messages" not in response): | ||
self.logging.info("No messages to be received") | ||
return "" | ||
|
||
self.logging.info(f"Received a message from {self.name}") | ||
|
||
# Delete the message from the queue - serves as an acknowledgement | ||
# that it was received. | ||
self.client.delete_message( | ||
QueueUrl=self.queue_url, | ||
ReceiptHandle=response["Messages"][0]["ReceiptHandle"], | ||
) | ||
|
||
return response["Messages"][0]["Body"] | ||
|
||
def serialize(self) -> dict: | ||
return { | ||
"name": self.name, | ||
"type": self.queue_type, | ||
"region": self.region, | ||
"queue_url": self.queue_url, | ||
"queue_arn": self.queue_arn, | ||
} | ||
|
||
@staticmethod | ||
def deserialize(obj: dict) -> "SQS": | ||
return SQS( | ||
obj["name"], | ||
obj["type"], | ||
obj["region"], | ||
obj["queue_url"], | ||
obj["queue_arn"] | ||
) |
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,95 @@ | ||
from sebs.faas.queue import Queue, QueueType | ||
|
||
from azure.core.exceptions import ResourceExistsError | ||
from azure.identity import DefaultAzureCredential | ||
from azure.storage.queue import QueueClient | ||
|
||
|
||
class AzureQueue(Queue): | ||
@staticmethod | ||
def typename() -> str: | ||
return "Azure.Queue" | ||
|
||
@staticmethod | ||
def deployment_name(): | ||
return "azure" | ||
|
||
@property | ||
def storage_account(self) -> str: | ||
assert self._storage_account | ||
return self._storage_account | ||
|
||
@property | ||
def account_url(self) -> str: | ||
return f"https://{self.storage_account}.queue.core.windows.net" | ||
|
||
def __init__( | ||
self, | ||
benchmark: str, | ||
queue_type: QueueType, | ||
storage_account: str, | ||
region: str | ||
): | ||
default_credential = DefaultAzureCredential() | ||
|
||
super().__init__( | ||
benchmark, | ||
queue_type, | ||
region | ||
) | ||
self._storage_account = storage_account | ||
self.client = QueueClient(self.account_url, | ||
queue_name=self.name, | ||
credential=default_credential) | ||
|
||
def create_queue(self): | ||
self.logging.info(f"Creating queue {self.name}") | ||
|
||
try: | ||
self.client.create_queue() | ||
self.logging.info("Created queue") | ||
except ResourceExistsError: | ||
self.logging.info("Queue already exists, reusing...") | ||
|
||
def remove_queue(self): | ||
self.logging.info(f"Deleting queue {self.name}") | ||
|
||
self.client.delete_queue() | ||
|
||
self.logging.info("Deleted queue") | ||
|
||
def send_message(self, serialized_message: str): | ||
self.client.send_message(serialized_message) | ||
self.logging.info(f"Sent message to queue {self.name}") | ||
|
||
def receive_message(self) -> str: | ||
self.logging.info(f"Pulling a message from {self.name}") | ||
|
||
response = self.client.receive_messages( | ||
max_messages=1, | ||
timeout=5, | ||
) | ||
|
||
for msg in response: | ||
self.client.delete_message(msg) | ||
return msg.content | ||
|
||
self.logging.info("No messages to be received") | ||
return "" | ||
|
||
def serialize(self) -> dict: | ||
return { | ||
"name": self.name, | ||
"type": self.queue_type, | ||
"storage_account": self.storage_account, | ||
"region": self.region | ||
} | ||
|
||
@staticmethod | ||
def deserialize(obj: dict) -> "AzureQueue": | ||
return AzureQueue( | ||
obj["name"], | ||
obj["type"], | ||
obj["storage_account"], | ||
obj["region"] | ||
) | ||
oanarosca marked this conversation as resolved.
Show resolved
Hide resolved
|
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,63 @@ | ||
from abc import ABC | ||
from abc import abstractmethod | ||
from enum import Enum | ||
|
||
from sebs.utils import LoggingBase | ||
|
||
|
||
class QueueType(str, Enum): | ||
TRIGGER = "trigger" | ||
RESULT = "result" | ||
|
||
|
||
class Queue(ABC, LoggingBase): | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def deployment_name() -> str: | ||
pass | ||
|
||
@property | ||
def region(self): | ||
return self._region | ||
|
||
@property | ||
def queue_type(self): | ||
return self._queue_type | ||
|
||
@property | ||
def name(self): | ||
return self._name | ||
|
||
def __init__( | ||
self, | ||
benchmark: str, | ||
queue_type: QueueType, | ||
region: str | ||
): | ||
super().__init__() | ||
self._name = benchmark | ||
|
||
# Convention: the trigger queue carries the name of the function. The | ||
# result queue carries the name of the function + "-result". | ||
if (queue_type == QueueType.RESULT and not benchmark.endswith("-result")): | ||
self._name = "{}-{}".format(benchmark, queue_type) | ||
|
||
self._queue_type = queue_type | ||
self._region = region | ||
|
||
@abstractmethod | ||
def create_queue(self): | ||
pass | ||
|
||
@abstractmethod | ||
def remove_queue(self): | ||
pass | ||
|
||
@abstractmethod | ||
def send_message(self, serialized_message: str): | ||
pass | ||
|
||
@abstractmethod | ||
def receive_message(self) -> str: | ||
pass | ||
oanarosca marked this conversation as resolved.
Show resolved
Hide resolved
|
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.