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

Draft: Feature/memory db #12

Open
wants to merge 2 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
5 changes: 3 additions & 2 deletions config/aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ resources:
BucketName: ${self:provider.environment.S3_BUCKET}-data

plugins:
- serverless-s3-remover
- serverless-python-requirements
# FIXME: ServerlessError: Serverless plugins not found.
# - serverless-s3-remover
# - serverless-python-requirements

custom:
remover:
Expand Down
54 changes: 54 additions & 0 deletions functions/aws/control/redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import base64
from typing import Union
from rediscluster import RedisCluster
from faaskeeper.node import Node
import boto3
from .storage import Storage

# FIXME
# acquire from AWS Redis ACL resource
# make it pass as config variable
redis_port = 6379
redis_host = "TBD"
redis_username = "TBD"
redis_password = "TBD"


class RedisStorage(Storage):
def __init__(self, cluster_name: str):
super().__init__(cluster_name)
self._redis = RedisCluster(startup_nodes=[{"host": redis_host, "port": redis_port}],
decode_responses=True, skip_full_coverage_check=True,
ssl=True, username=redis_username, password=redis_password)

def write(self, key: str, data: Union[bytes, str]):
"""Redis write"""

print(data)
return self._redis.hset(key, data)

def update(self, key: str, data: dict):
"""Redis update"""

# FIXME
# Same logic as in S3 update
pass

def read(self, key: str):
"""Redis read"""

for mask in self._redis.scan_iter(key):
return self._redis.hgetcall(mask)

return None

def delete(self, key: str):
"""Redis delete"""

self._redis.delete(key)

@property
def errorSupplier(self):
"""Redis exceptions"""

return None
39 changes: 38 additions & 1 deletion functions/aws/model/user_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from faaskeeper.node import Node
from functions.aws.control import DynamoStorage as DynamoDriver
from functions.aws.control import S3Storage as S3Driver
from functions.aws.control import RedisStorage as RedisDriver


class OpResult(Enum):
Expand Down Expand Up @@ -95,7 +96,8 @@ def _serialize(self, node: Node) -> bytes:
modified_system = node.modified.system.serialize()
modified_epoch: Set[int] = set()

counters = [created_system, created_epoch, modified_system, modified_epoch]
counters = [created_system, created_epoch,
modified_system, modified_epoch]
total_length = reduce(lambda a, b: a + b, map(len, counters))
return struct.pack(
f"{5+total_length}I",
Expand Down Expand Up @@ -124,3 +126,38 @@ def update(self, node: Node):
@property
def errorSupplier(self):
return self._storage.errorSupplier

class RedisStorage(Storage):
def _serialize(self, node: Node) -> bytes:

created_system = node.created.system.serialize()
created_epoch: Set[int] = set()
modified_system = node.modified.system.serialize()
modified_epoch: Set[int] = set()

counters = [created_system, created_epoch,
modified_system, modified_epoch]
total_length = reduce(lambda a, b: a + b, map(len, counters))
return struct.pack(
f"{5+total_length}I",
4 + total_length,
len(created_system),
*created_system,
len(created_epoch),
*created_epoch,
len(modified_system),
*modified_system,
len(modified_epoch),
*modified_epoch,
)

def __init__(self, cluster_name: str):
self._storage = RedisDriver(cluster_name)

def write(self, node: Node):
self._storage.write(node.path, self._serialize(node) + node.data)
return OpResult.SUCCESS

def update(self, node: Node):
self._storage.write(node.path, self._serialize(node) + node.data)
return OpResult.SUCCESS
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ boto3-stubs[lambda]
prompt_toolkit
isort
pytest

redis-py-cluster
nodeenv