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

Serverless application infrastructure #225

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"timeout": 30,
"memory": 128,
"languages": ["python"],
"trigger": "queue"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import datetime, json, os

from . import misc
from . import queue

from . import nosql
nosql_client = nosql.nosql.get_instance()

nosql_table_name = 'booking_table'

# import boto3
# session = boto3.Session()
# dynamodb = session.resource('dynamodb')
# table = dynamodb.Table('booking_table')


def handler(event):
"""AWS Lambda Function entrypoint to cancel booking

Parameters
----------
event: dict, required
Step Functions State Machine event

chargeId: string
pre-authorization charge ID

context: object, required
Lambda Context runtime methods and attributes
Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

Returns
-------
boolean

Raises
------
BookingCancellationException
Booking Cancellation Exception including error message upon failure
"""
if ('booking_id' not in event):
raise ValueError('Invalid booking ID')

booking_id = event['booking_id']

print(f'Cancelling booking - {booking_id}')
update_begin = datetime.datetime.now()
# TODO: rewrite with generic nosql wrapper once it is merged
# ret = table.update_item(
# Key={'id': booking_id},
# ConditionExpression='id = :idVal',
# UpdateExpression='SET #STATUS = :cancelled',
# ExpressionAttributeNames={'#STATUS': 'status'},
# ExpressionAttributeValues={':idVal': booking_id, ':cancelled': 'CANCELLED'},
# )
update_end = datetime.datetime.now()

release_flight_input = {
'outbound_flight_id': event['outbound_flight_id'],
'parent_execution_id': event['request-id']
}

queue_begin = datetime.datetime.now()
queue_client = queue.queue(
misc.function_name(
fname='release_flight',
language='python',
version='3.9',
trigger='queue'
)
)
queue_client.send_message(json.dumps(release_flight_input))
queue_end = datetime.datetime.now()

update_time = (update_end - update_begin) / datetime.timedelta(microseconds=1)
queue_time = (queue_end - queue_begin) / datetime.timedelta(microseconds=1)
return {
'result': 0,
'fns_triggered': 1,
'measurement': {
'update_time': update_time,
'queue_time': queue_time
}
}


"""
Sample input:
{
"booking_id": "5347fc8e-46f2-434d-9d09-fa4d31f7f266",
"outbound_flight_id": "fae7c68d-2683-4968-87a2-dfe2a090c2d1"
}
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"timeout": 30,
"memory": 128,
"languages": ["python"],
"trigger": "queue"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import datetime, json, os

payment_endpoint = 'dummy'

from . import misc
from . import queue


def handler(event):
"""AWS Lambda Function entrypoint to collect payment

Parameters
----------
event: dict, required
Step Functions State Machine event

chargeId: string
pre-authorization charge ID

Returns
-------
dict
receiptUrl: string
receipt URL of charge collected

price: int
amount collected
"""
if ('charge_id' not in event):
raise ValueError('Invalid Charge ID')

pre_authorization_token = event['charge_id']
customer_id = event['customer_id']

print(f'Collecting payment from customer {customer_id} using {pre_authorization_token} token')
if (not payment_endpoint):
raise ValueError('Payment API URL is invalid -- Consider reviewing PAYMENT_API_URL env')

# This used to be an external API call:
#
# payment_payload = {'charge_id': charge_id}
# ret = requests.post(payment_endpoint, json=payment_payload)
# ret.raise_for_status()
# payment_response = ret.json()

if (payment_successful()):
confirm_booking_input = {
'customer_id': event['customer_id'],
'booking_id': event['booking_id'],
'parent_execution_id': event['request-id']
}

queue_begin = datetime.datetime.now()
queue_client = queue.queue(
misc.function_name(
fname='confirm_booking',
language='python',
version='3.9',
trigger='queue'
)
)
queue_client.send_message(json.dumps(confirm_booking_input))
queue_end = datetime.datetime.now()
else:
cancel_booking_input = {
'outbound_flight_id': event['outbound_flight_id'],
'booking_id': event['booking_id'],
'parent_execution_id': event['request-id']
}

queue_begin = datetime.datetime.now()
queue_client = queue.queue(
misc.function_name(
fname='cancel_booking',
language='python',
version='3.9',
trigger='queue'
)
)
queue_client.send_message(json.dumps(cancel_booking_input))
queue_end = datetime.datetime.now()

queue_time = (queue_end - queue_begin) / datetime.timedelta(microseconds=1)
return {
'result': 0,
'fns_triggered': 1,
'measurement': {
'queue_time': queue_time
}
}

def payment_successful():
return True # False


"""
Sample input:
{
"charge_id": "ch_1EeqlbF4aIiftV70qXHQewmn",
"customer_id": "d749f277-0950-4ad6-ab04-98988721e475",
"booking_id": "5347fc8e-46f2-434d-9d09-fa4d31f7f266",
"outbound_flight_id": "fae7c68d-2683-4968-87a2-dfe2a090c2d1"
}
"""
4 changes: 4 additions & 0 deletions benchmarks/100.webapps/130.airline-booking/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "app",
"resources": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"timeout": 30,
"memory": 128,
"languages": ["python", "nodejs"],
"trigger": "queue"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import datetime, json, os, secrets

from . import misc
from . import queue

from . import nosql
nosql_client = nosql.nosql.get_instance()

nosql_table_name = 'booking_table'

# import boto3
# session = boto3.Session()
# dynamodb = session.resource('dynamodb')
# table = dynamodb.Table('booking_table')


def handler(event):
"""AWS Lambda Function entrypoint to confirm booking

Parameters
----------
event: dict, required
Step Functions State Machine event

bookingId: string
Unique Booking ID of an unconfirmed booking

Returns
-------
string
bookingReference generated
"""
if ('booking_id' not in event):
raise ValueError('Invalid booking ID')

booking_id = event['booking_id']

print(f'Confirming booking - {booking_id}')
reference = secrets.token_urlsafe(4)
update_begin = datetime.datetime.now()
# TODO: rewrite with generic nosql wrapper once it is merged
# ret = table.update_item(
# Key={'id': booking_id},
# ConditionExpression='id = :idVal',
# UpdateExpression='SET bookingReference = :br, #STATUS = :confirmed',
# ExpressionAttributeNames={'#STATUS': 'status'},
# ExpressionAttributeValues={
# ':br': reference,
# ':idVal': booking_id,
# ':confirmed': 'CONFIRMED',
# },
# ReturnValues='UPDATED_NEW',
# )
update_end = datetime.datetime.now()

notify_booking_input = {
'customer_id': event['customer_id'],
'reference': reference,
'parent_execution_id': event['request-id']
}

queue_begin = datetime.datetime.now()
queue_client = queue.queue(
misc.function_name(
fname='notify_booking',
language='python',
version='3.9',
trigger='queue'
)
)
queue_client.send_message(json.dumps(notify_booking_input))
queue_end = datetime.datetime.now()

update_time = (update_end - update_begin) / datetime.timedelta(microseconds=1)
queue_time = (queue_end - queue_begin) / datetime.timedelta(microseconds=1)
return {
'result': 0,
'fns_triggered': 1,
'measurement': {
'update_time': update_time,
'queue_time': queue_time
}
}


"""
Sample input:
{
"customer_id": "d749f277-0950-4ad6-ab04-98988721e475",
"booking_id": "5347fc8e-46f2-434d-9d09-fa4d31f7f266"
}
"""
19 changes: 19 additions & 0 deletions benchmarks/100.webapps/130.airline-booking/input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import glob, os

def buckets_count():
return (1, 0)

def upload_files(data_root, data_dir, upload_func):
for root, dirs, files in os.walk(data_dir):
prefix = os.path.relpath(root, data_root)
for file in files:
file_name = prefix + '/' + file
filepath = os.path.join(root, file)
upload_func(0, file_name, filepath)

def generate_input(data_dir, size, benchmarks_bucket, input_paths, output_paths, upload_func):
input_config = {}
input_config['charge_id'] = 'ch_1EeqlbF4aIiftV70qXHQewmn'
input_config['customer_id'] = 'd749f277-0950-4ad6-ab04-98988721e475'
input_config['outbound_flight_id'] = 'fae7c68d-2683-4968-87a2-dfe2a090c2d1'
return input_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"timeout": 30,
"memory": 128,
"languages": ["python"],
"trigger": "queue"
}
Loading