Skip to content

Commit 8e53993

Browse files
committed
[aws] Add Redis wrappers for C++
1 parent de87302 commit 8e53993

File tree

5 files changed

+142
-2
lines changed

5 files changed

+142
-2
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
#include <string>
3+
4+
#include <aws/core/Aws.h>
5+
6+
#include "redis.hpp"
7+
#include "utils.hpp"
8+
9+
Redis::Redis(std::string redis_hostname, int redis_port)
10+
{
11+
_context = redisConnect(redis_hostname.c_str(), redis_port);
12+
if (_context == nullptr || _context->err) {
13+
if (_context) {
14+
std::cerr << "Redis Error: " << _context->errstr << '\n';
15+
} else {
16+
std::cerr << "Can't allocate redis context\n";
17+
}
18+
}
19+
}
20+
21+
bool Redis::is_initialized()
22+
{
23+
return _context != nullptr;
24+
}
25+
26+
Redis::~Redis()
27+
{
28+
redisFree(_context);
29+
}
30+
31+
uint64_t Redis::download_file(Aws::String const &key,
32+
int &required_retries, bool with_backoff)
33+
{
34+
std::string comm = "GET " + key;
35+
36+
auto bef = timeSinceEpochMillisec();
37+
int retries = 0;
38+
const int MAX_RETRIES = 1500;
39+
40+
while (retries < MAX_RETRIES) {
41+
42+
redisReply* reply = (redisReply*) redisCommand(_context, comm.c_str());
43+
44+
if (reply->type == REDIS_REPLY_NIL || reply->type == REDIS_REPLY_ERROR) {
45+
46+
retries += 1;
47+
if(with_backoff) {
48+
int sleep_time = retries;
49+
if (retries > 100) {
50+
sleep_time = retries * 2;
51+
}
52+
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time));
53+
}
54+
55+
} else {
56+
57+
uint64_t finishedTime = timeSinceEpochMillisec();
58+
required_retries = retries;
59+
60+
freeReplyObject(reply);
61+
return finishedTime - bef;
62+
63+
}
64+
freeReplyObject(reply);
65+
}
66+
return 0;
67+
}
68+
69+
uint64_t Redis::upload_file(Aws::String const &key,
70+
int size, char* pBuf)
71+
{
72+
std::string comm = "SET " + key + " %b";
73+
74+
75+
uint64_t bef = timeSinceEpochMillisec();
76+
redisReply* reply = (redisReply*) redisCommand(_context, comm.c_str(), pBuf, size);
77+
uint64_t finishedTime = timeSinceEpochMillisec();
78+
79+
if (reply->type == REDIS_REPLY_NIL || reply->type == REDIS_REPLY_ERROR) {
80+
std::cerr << "Failed to write in Redis!" << std::endl;
81+
abort();
82+
}
83+
freeReplyObject(reply);
84+
85+
return finishedTime - bef;
86+
}
87+
88+
uint64_t Redis::delete_file(std::string const &key)
89+
{
90+
std::string comm = "DEL " + key;
91+
92+
uint64_t bef = timeSinceEpochMillisec();
93+
redisReply* reply = (redisReply*) redisCommand(_context, comm.c_str());
94+
uint64_t finishedTime = timeSinceEpochMillisec();
95+
96+
if (reply->type == REDIS_REPLY_NIL || reply->type == REDIS_REPLY_ERROR) {
97+
std::cerr << "Couldn't delete the key!" << '\n';
98+
abort();
99+
}
100+
freeReplyObject(reply);
101+
102+
return finishedTime - bef;
103+
}
104+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#include <cstdint>
3+
#include <string>
4+
5+
#include <aws/core/utils/memory/stl/AWSString.h>
6+
7+
#include <hiredis/hiredis.h>
8+
9+
class Redis
10+
{
11+
redisContext* _context;
12+
public:
13+
14+
Redis(std::string redis_hostname, int redis_port);
15+
~Redis();
16+
17+
bool is_initialized();
18+
19+
uint64_t download_file(Aws::String const &key, int &required_retries, bool with_backoff);
20+
21+
uint64_t upload_file(Aws::String const &key, int size, char* pBuf);
22+
23+
uint64_t delete_file(std::string const &key);
24+
25+
};
26+

config/systems.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"versions": ["all"],
7474
"images": ["build"],
7575
"deployment": {
76-
"files": [ "handler.cpp", "key-value.cpp", "key-value.hpp", "storage.cpp", "storage.hpp", "utils.cpp", "utils.hpp"]
76+
"files": [ "handler.cpp", "key-value.cpp", "key-value.hpp", "storage.cpp", "storage.hpp", "redis.hpp", "redis.cpp", "utils.cpp", "utils.hpp"]
7777
}
7878
}
7979
}

docker/aws/cpp/Dockerfile.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ ARG BASE_REPOSITORY
33
ARG BASE_IMAGE
44
FROM ${BASE_REPOSITORY}:dependencies-sdk.aws.cpp.all as sdk
55
FROM ${BASE_REPOSITORY}:dependencies-boost.aws.cpp.all as boost
6+
FROM ${BASE_REPOSITORY}:dependencies-hiredis.aws.cpp.all as hiredis
67
FROM ${BASE_REPOSITORY}:dependencies-runtime.aws.cpp.all as runtime
78

89
FROM ${BASE_IMAGE} as builder
@@ -22,6 +23,7 @@ RUN chmod +x /sebs/installer.sh
2223
COPY --from=sdk /opt /opt
2324
COPY --from=boost /opt /opt
2425
COPY --from=runtime /opt /opt
26+
COPY --from=hiredis /opt /opt
2527

2628
# useradd and groupmod is installed in /usr/sbin which is not in PATH
2729
ENV PATH=/usr/sbin:$PATH

sebs/benchmark.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,14 @@ def add_deployment_package_cpp(self, output_dir):
319319

320320
# FIXME: Configure CMakeLists.txt dependencies
321321
# FIXME: Configure for AWS - this should be generic
322+
# FIXME: optional hiredis
322323
cmake_script = """
323324
cmake_minimum_required(VERSION 3.9)
324325
set(CMAKE_CXX_STANDARD 11)
325326
project(benchmark LANGUAGES CXX)
326327
add_executable(
327328
${PROJECT_NAME} "handler.cpp" "key-value.cpp"
328-
"storage.cpp" "utils.cpp" "main.cpp"
329+
"storage.cpp" "redis.cpp" "utils.cpp" "main.cpp"
329330
)
330331
target_include_directories(${PROJECT_NAME} PRIVATE ".")
331332
@@ -342,6 +343,13 @@ def add_deployment_package_cpp(self, output_dir):
342343
find_package(AWSSDK COMPONENTS s3 dynamodb core)
343344
target_link_libraries(${PROJECT_NAME} PUBLIC ${AWSSDK_LINK_LIBRARIES})
344345
346+
find_package(PkgConfig REQUIRED)
347+
set(ENV{PKG_CONFIG_PATH} "/opt/lib/pkgconfig")
348+
pkg_check_modules(HIREDIS REQUIRED IMPORTED_TARGET hiredis)
349+
350+
target_include_directories(${PROJECT_NAME} PUBLIC PkgConfig::HIREDIS)
351+
target_link_libraries(${PROJECT_NAME} PUBLIC PkgConfig::HIREDIS)
352+
345353
# this line creates a target that packages your binary and zips it up
346354
aws_lambda_package_target(${PROJECT_NAME})
347355
"""

0 commit comments

Comments
 (0)