From 08757a9e55baf25a9900a176704db04f68d44a4f Mon Sep 17 00:00:00 2001 From: Michal Szkarlat Date: Mon, 6 Sep 2021 18:35:15 +0200 Subject: [PATCH 1/2] plugins issue --- config/aws.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config/aws.yml b/config/aws.yml index 91d9715..2e2b205 100644 --- a/config/aws.yml +++ b/config/aws.yml @@ -130,8 +130,9 @@ resources: BucketName: ${self:provider.environment.S3_BUCKET}-data plugins: - - serverless-s3-remover - - serverless-python-requirements + # FIXME: ServerlessError: Serverless plugin not found. + # - serverless-s3-remover + # - serverless-python-requirements custom: remover: From 2748b962b8c9caf73073b9585d62006c1c951e38 Mon Sep 17 00:00:00 2001 From: Michal Szkarlat Date: Mon, 6 Sep 2021 20:14:12 +0200 Subject: [PATCH 2/2] first iteration for redis --- config/aws.yml | 2 +- functions/aws/control/redis.py | 54 +++++++++++++++++++++++++++++ functions/aws/model/user_storage.py | 39 ++++++++++++++++++++- requirements.txt | 2 +- 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 functions/aws/control/redis.py diff --git a/config/aws.yml b/config/aws.yml index 2e2b205..88d70cc 100644 --- a/config/aws.yml +++ b/config/aws.yml @@ -130,7 +130,7 @@ resources: BucketName: ${self:provider.environment.S3_BUCKET}-data plugins: - # FIXME: ServerlessError: Serverless plugin not found. + # FIXME: ServerlessError: Serverless plugins not found. # - serverless-s3-remover # - serverless-python-requirements diff --git a/functions/aws/control/redis.py b/functions/aws/control/redis.py new file mode 100644 index 0000000..75b8afe --- /dev/null +++ b/functions/aws/control/redis.py @@ -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 diff --git a/functions/aws/model/user_storage.py b/functions/aws/model/user_storage.py index 823cd65..bf3ab06 100644 --- a/functions/aws/model/user_storage.py +++ b/functions/aws/model/user_storage.py @@ -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): @@ -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", @@ -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 diff --git a/requirements.txt b/requirements.txt index 65c53ca..a3b108f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,5 +10,5 @@ boto3-stubs[lambda] prompt_toolkit isort pytest - +redis-py-cluster nodeenv