Skip to content

Commit bf01ed0

Browse files
Add in-source lambda config + improve fn names
1 parent e1b85d9 commit bf01ed0

File tree

10 files changed

+540
-26
lines changed

10 files changed

+540
-26
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ CMakeLists.txt.user
99
CMakeUserPresets.json
1010
.DS_Store
1111
*.out
12-
conan
12+
conan
13+
**/*.pyc

benchmarks/bots/nqueens/dispatcher.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
#include "./common.hpp"
1414

1515
using dispatcher = cppless::dispatcher::aws_lambda_dispatcher<>;
16+
namespace lambda = cppless::dispatcher::aws;
17+
constexpr unsigned int memory_limit = 2048;
18+
constexpr unsigned int ephemeral_storage = 64;
19+
using cpu_intensive_task =
20+
dispatcher::task<lambda::with_memory<memory_limit>,
21+
lambda::with_ephemeral_storage<ephemeral_storage>>;
1622

1723
auto nqueens(dispatcher_args args) -> unsigned int
1824
{
@@ -40,7 +46,7 @@ auto nqueens(dispatcher_args args) -> unsigned int
4046
for (unsigned int i = 0; i < prefixes.size(); i += prefix_length) {
4147
std::vector<unsigned int> prefix(prefixes.begin() + i,
4248
prefixes.begin() + i + prefix_length);
43-
dispatcher::task::sendable task = [prefix, size]
49+
cpu_intensive_task::sendable task = [prefix, size]
4450
{
4551
auto scratchpad = std::vector<unsigned char>(size);
4652
std::copy(prefix.begin(), prefix.end(), scratchpad.begin());

cmake/aws.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function(aws_lambda_package_target target)
1515
"--sysroot" ${SYSROOT}
1616
"--project" ${CMAKE_BINARY_DIR}
1717
"--image" ${DOCKER_IMAGE}
18+
"--target-name" ${target}
1819
"--strip"
1920
"--deploy"
2021
"--function-role-arn" ${AWS_LAMBDA_FUNCTION_ROLE_ARN}
@@ -46,6 +47,8 @@ function(aws_lambda_target NAME)
4647

4748
find_package(CURL REQUIRED)
4849
target_link_libraries("${NAME}" PRIVATE CURL::libcurl)
50+
51+
target_compile_definitions("${NAME}" PRIVATE "TARGET_NAME=\"${NAME}\"")
4952
endfunction()
5053

5154

examples/config.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
#include <string_view>
3+
4+
struct default_aws_lambda_task_configuration
5+
{
6+
constexpr static std::string_view description = "";
7+
constexpr static unsigned int memory = 1024; // MB
8+
constexpr static unsigned int ephemeral_storage = 512; // MB
9+
constexpr static unsigned int timeout = 60 * 5; // seconds
10+
};
11+
12+
template<class A, A Description>
13+
struct with_description
14+
{
15+
template<class Base>
16+
struct apply : public Base
17+
{
18+
constexpr static A description = Description;
19+
};
20+
};
21+
22+
template<unsigned int Memory>
23+
struct with_memory
24+
{
25+
template<class Base>
26+
struct apply : public Base
27+
{
28+
constexpr static unsigned int memory = Memory;
29+
};
30+
};
31+
32+
template<unsigned int EphemeralStorage>
33+
struct with_ephemeral_storage
34+
{
35+
template<class Base>
36+
struct apply : public Base
37+
{
38+
constexpr static unsigned int ephemeral_storage = EphemeralStorage;
39+
};
40+
};
41+
42+
template<unsigned int Timeout>
43+
class with_timeout
44+
{
45+
template<class Base>
46+
class apply : public Base
47+
{
48+
constexpr static unsigned int timeout = Timeout;
49+
};
50+
};
51+
52+
template<class... Modifiers>
53+
class aws_lambda_config;
54+
55+
template<class Modifier, class... Modifiers>
56+
class aws_lambda_config<Modifier, Modifiers...>
57+
: public Modifier::template apply<aws_lambda_config<Modifiers...>>
58+
{
59+
};
60+
61+
template<>
62+
class aws_lambda_config<> : public default_aws_lambda_task_configuration
63+
{
64+
};
65+
66+
using namespace std;
67+
int main(int argc, char* argv[])
68+
{
69+
constexpr auto memory = 2048;
70+
constexpr auto ephemeral_storage = 128;
71+
using config = aws_lambda_config<with_memory<memory>,
72+
with_ephemeral_storage<ephemeral_storage>>;
73+
74+
std::cout << "description: " << config::description << std::endl;
75+
std::cout << "memory: " << config::memory << std::endl;
76+
std::cout << "ephemeral_storage: " << config::ephemeral_storage << std::endl;
77+
std::cout << "timeout: " << config::timeout << std::endl;
78+
}

include/cppless/dispatcher/aws-lambda.hpp

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,94 @@
1616
#include <cppless/provider/aws/auth.hpp>
1717
#include <cppless/provider/aws/lambda.hpp>
1818
#include <cppless/utils/crypto/wrappers.hpp>
19+
#include <cppless/utils/fixed_string.hpp>
20+
#include <cppless/utils/fixed_string_serialization.hpp>
1921
#include <cppless/utils/uninitialized.hpp>
2022

23+
#ifndef TARGET_NAME
24+
# define TARGET_NAME "cppless" // NOLINT
25+
#endif
26+
2127
namespace cppless::dispatcher
2228
{
2329

30+
namespace aws
31+
{
32+
33+
struct default_config
34+
{
35+
constexpr static std::string_view description;
36+
constexpr static unsigned int memory = 1024; // MB
37+
constexpr static unsigned int ephemeral_storage = 512; // MB
38+
constexpr static unsigned int timeout = 60 * 5; // seconds
39+
};
40+
41+
template<class A, A Description>
42+
struct with_description
43+
{
44+
template<class Base>
45+
struct apply : public Base
46+
{
47+
constexpr static A description = Description;
48+
};
49+
};
50+
51+
template<unsigned int Memory>
52+
struct with_memory
53+
{
54+
template<class Base>
55+
struct apply : public Base
56+
{
57+
constexpr static unsigned int memory = Memory;
58+
};
59+
};
60+
61+
template<unsigned int EphemeralStorage>
62+
struct with_ephemeral_storage
63+
{
64+
template<class Base>
65+
struct apply : public Base
66+
{
67+
constexpr static unsigned int ephemeral_storage = EphemeralStorage;
68+
};
69+
};
70+
71+
template<unsigned int Timeout>
72+
class with_timeout
73+
{
74+
template<class Base>
75+
class apply : public Base
76+
{
77+
constexpr static unsigned int timeout = Timeout;
78+
};
79+
};
80+
81+
template<class... Modifiers>
82+
class config;
83+
84+
template<class Modifier, class... Modifiers>
85+
class config<Modifier, Modifiers...>
86+
: public Modifier::template apply<config<Modifiers...>>
87+
{
88+
};
89+
90+
template<>
91+
class config<> : public default_config
92+
{
93+
};
94+
95+
} // namespace aws
96+
2497
template<class Archive = json_binary_archive>
2598
class aws_lambda_dispatcher
2699
{
27100
public:
101+
using default_config = aws::config<>;
28102
using input_archive = typename Archive::input_archive;
29103
using output_archive = typename Archive::output_archive;
30104

31-
using task = cppless::task<aws_lambda_dispatcher>;
32-
template<class T>
33-
using sendable_task = typename task::template sendable<T>;
105+
template<class... Modifiers>
106+
using task = cppless::task<aws_lambda_dispatcher, aws::config<Modifiers...>>;
34107

35108
explicit aws_lambda_dispatcher(std::string prefix,
36109
cppless::aws::lambda::client client,
@@ -41,6 +114,29 @@ class aws_lambda_dispatcher
41114
{
42115
}
43116

117+
template<class Config>
118+
struct meta_serializer
119+
{
120+
template<unsigned int N>
121+
constexpr static auto serialize(basic_fixed_string<char, N> identifier)
122+
{
123+
using namespace cppless; // NOLINT
124+
return cppless::encode_base64(cppless::serialize(
125+
map(kv("ephemeral_storage", Config::ephemeral_storage),
126+
kv("memory", Config::memory),
127+
kv("timeout", Config::timeout),
128+
kv("identifier", identifier))));
129+
}
130+
131+
static auto identifier(const std::string& identifier) -> std::string
132+
{
133+
std::stringstream ss;
134+
ss << identifier << "#" << Config::ephemeral_storage << "#"
135+
<< Config::memory << "#" << Config::timeout;
136+
return ss.str();
137+
}
138+
};
139+
44140
template<class Lambda, class Res, class... Args>
45141
static auto main(int /*argc*/, char* /*argv*/[]) -> int
46142
{
@@ -139,25 +235,30 @@ class aws_lambda_dispatcher
139235
return *this;
140236
}
141237

142-
template<class Res, class... Args>
143-
auto dispatch(sendable_task<Res(Args...)>& t,
238+
template<class TaskType, class Res, class... Args>
239+
auto dispatch(TaskType& t,
144240
cppless::shared_future<Res> result_future,
145241
std::tuple<Args...> args) -> int
146242
{
147-
using specialized_task_data =
148-
task_data<sendable_task<Res(Args...)>, Args...>;
243+
using specialized_task_data = task_data<TaskType, Args...>;
149244

150245
int id = m_next_id++;
151246

152247
auto raw_function_name = t.identifier();
153-
std::string function_name;
248+
249+
std::string function_name = TARGET_NAME;
250+
function_name += "-";
154251

155252
evp_md_ctx ctx;
156253
ctx.update(raw_function_name);
157254
auto binary_digest = ctx.final();
158255

256+
std::string function_hex;
159257
boost::algorithm::hex_lower(binary_digest,
160-
std::back_inserter(function_name));
258+
std::back_inserter(function_hex));
259+
260+
// First 8 chars
261+
function_name += function_hex.substr(0, 8);
161262

162263
specialized_task_data data {t, args};
163264
auto string_payload = Archive::serialize(data);

include/cppless/dispatcher/sendable.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class receivable_lambda
5050
Lambda m_lambda;
5151
};
5252

53-
template<class Dispatcher>
53+
template<class Dispatcher, class Config = typename Dispatcher::default_config>
5454
struct task
5555
{
5656
using input_archive = typename Dispatcher::input_archive;
@@ -84,11 +84,14 @@ struct task
8484

8585
auto identifier() -> std::string override
8686
{
87-
return function_identifier<Lambda, Args...>().str();
87+
return Dispatcher::template meta_serializer<Config>::identifier(
88+
function_identifier<Lambda, Args...>().str());
8889
}
8990

90-
__attribute((entry))
91-
__attribute((meta(function_identifier<Lambda, Args...>()))) static auto
91+
__attribute((entry)) __attribute((
92+
meta(Dispatcher::template meta_serializer<Config>::template serialize<
93+
function_identifier<Lambda, Args...>().size() + 1>(
94+
function_identifier<Lambda, Args...>())))) static auto
9295
main(int argc, char* argv[]) -> int
9396
{
9497
return Dispatcher::template main<Lambda, Res, Args...>(argc, argv);

include/cppless/graph/host_controller_executor.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ class host_controller_executor
1919
using executor_type = host_controller_executor<Dispatcher>;
2020

2121
public:
22-
using task = typename Dispatcher::task;
22+
using task = typename Dispatcher::template task<>;
23+
24+
template<class Config>
25+
using custom_task = typename Dispatcher::template task<Config>;
2326

2427
class node_core : public graph::basic_node_core<executor_type>
2528
{

0 commit comments

Comments
 (0)