From 4bb11ac72ade5c76c4b24c9a8a8636e9a52be003 Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Mon, 2 May 2016 09:20:21 -0700 Subject: Turned load reporting into a plugin --- src/core/ext/load_reporting/load_reporting.c | 57 +++++++++++++++++----- src/core/ext/load_reporting/load_reporting.h | 32 ++++++------ .../ext/load_reporting/load_reporting_filter.c | 27 +++++----- .../grpc_unsecure_plugin_registry.c | 4 ++ 4 files changed, 80 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/core/ext/load_reporting/load_reporting.c b/src/core/ext/load_reporting/load_reporting.c index 9081ffce15..fb32685dc9 100644 --- a/src/core/ext/load_reporting/load_reporting.c +++ b/src/core/ext/load_reporting/load_reporting.c @@ -42,27 +42,33 @@ #include "src/core/lib/channel/channel_stack_builder.h" #include "src/core/lib/surface/channel_init.h" -struct grpc_load_reporting_data { +struct grpc_load_reporting_config { grpc_load_reporting_fn fn; void *data; }; -grpc_load_reporting_data *grpc_load_reporting_create(grpc_load_reporting_fn fn, - void *data) { - grpc_load_reporting_data *lrd = gpr_malloc(sizeof(grpc_load_reporting_data)); - lrd->fn = fn; - lrd->data = data; - return lrd; +grpc_load_reporting_config *grpc_load_reporting_config_create( + grpc_load_reporting_fn fn, void *data) { + grpc_load_reporting_config *lrc = + gpr_malloc(sizeof(grpc_load_reporting_config)); + lrc->fn = fn; + lrc->data = data; + return lrc; } -void grpc_load_reporting_destroy(grpc_load_reporting_data *lrd) { - gpr_free(lrd); +grpc_load_reporting_config *grpc_load_reporting_config_copy( + grpc_load_reporting_config *src) { + return grpc_load_reporting_config_create(src->fn, src->data); } -void grpc_load_reporting_call(grpc_load_reporting_data *lrd, - const grpc_call_stats *stats) { - if (lrd->fn != NULL) { - lrd->fn(lrd->data, stats); +void grpc_load_reporting_config_destroy(grpc_load_reporting_config *lrc) { + gpr_free(lrc); +} + +void grpc_load_reporting_config_call(grpc_load_reporting_config *lrc, + const grpc_call_stats *stats) { + if (lrc->fn != NULL) { + lrc->fn(stats, lrc->data); } } @@ -87,6 +93,31 @@ static bool maybe_add_load_reporting_filter(grpc_channel_stack_builder *builder, return true; } +static void lrd_arg_destroy(void *p) { grpc_load_reporting_config_destroy(p); } + +static void *lrd_arg_copy(void *p) { + return grpc_load_reporting_config_copy(p); +} + +static int lrd_arg_cmp(void *a, void *b) { + grpc_load_reporting_config *lhs = a; + grpc_load_reporting_config *rhs = b; + return !(lhs->fn == rhs->fn && lhs->data == rhs->data); +} + +static const grpc_arg_pointer_vtable lrd_ptr_vtable = { + lrd_arg_copy, lrd_arg_destroy, lrd_arg_cmp}; + +grpc_arg grpc_load_reporting_config_create_arg( + grpc_load_reporting_config *lrc) { + grpc_arg arg; + arg.type = GRPC_ARG_POINTER; + arg.key = GRPC_ARG_ENABLE_LOAD_REPORTING; + arg.value.pointer.p = lrc; + arg.value.pointer.vtable = &lrd_ptr_vtable; + return arg; +} + /* Plugin registration */ void grpc_load_reporting_plugin_init(void) { diff --git a/src/core/ext/load_reporting/load_reporting.h b/src/core/ext/load_reporting/load_reporting.h index fcf555da85..806af2bda7 100644 --- a/src/core/ext/load_reporting/load_reporting.h +++ b/src/core/ext/load_reporting/load_reporting.h @@ -37,28 +37,28 @@ #include "src/core/lib/iomgr/closure.h" #include "src/core/lib/surface/call.h" -typedef struct grpc_load_reporting_data grpc_load_reporting_data; +typedef struct grpc_load_reporting_config grpc_load_reporting_config; -/** Custom function to be called by the load reporting filter. - * - * The \a data pointer is the same as the one passed to \a - * grpc_load_reporting_init. \a stats are the final per-call statistics gathered - * by the gRPC runtime. */ -typedef void (*grpc_load_reporting_fn)(void *data, - const grpc_call_stats *stats); +/** Custom function to be called by the load reporting filter. */ +typedef void (*grpc_load_reporting_fn)(const grpc_call_stats *stats, + void *data); /** Register \a fn as the function to be invoked by the load reporting filter, - * passing \a data as its namesake argument. To be called only from a plugin - * init function. */ -grpc_load_reporting_data *grpc_load_reporting_create(grpc_load_reporting_fn fn, - void *data); + * passing \a data alongisde the call stats */ +grpc_load_reporting_config *grpc_load_reporting_config_create( + grpc_load_reporting_fn fn, void *data); + +grpc_load_reporting_config *grpc_load_reporting_config_copy( + grpc_load_reporting_config *src); -// XXX -void grpc_load_reporting_destroy(grpc_load_reporting_data *lrd); +void grpc_load_reporting_config_destroy(grpc_load_reporting_config *lrc); /** Invoke the function registered by \a grpc_load_reporting_init, passing it \a * stats as one of the arguments (see \a load_reporting_fn). */ -void grpc_load_reporting_call(grpc_load_reporting_data *lrd, - const grpc_call_stats *stats); +void grpc_load_reporting_config_call(grpc_load_reporting_config *lrc, + const grpc_call_stats *stats); + +/** Return a \a grpc_arg enabling load reporting */ +grpc_arg grpc_load_reporting_config_create_arg(grpc_load_reporting_config *lrc); #endif /* GRPC_CORE_EXT_LOAD_REPORTING_LOAD_REPORTING_H */ diff --git a/src/core/ext/load_reporting/load_reporting_filter.c b/src/core/ext/load_reporting/load_reporting_filter.c index 81f4a0277c..6970f064b8 100644 --- a/src/core/ext/load_reporting/load_reporting_filter.c +++ b/src/core/ext/load_reporting/load_reporting_filter.c @@ -31,8 +31,8 @@ * */ -#include #include +#include #include "src/core/ext/load_reporting/load_reporting.h" #include "src/core/ext/load_reporting/load_reporting_filter.h" @@ -40,7 +40,7 @@ #include "src/core/lib/profiling/timers.h" typedef struct call_data { void *dummy; } call_data; -typedef struct channel_data { grpc_load_reporting_data *lrd; } channel_data; +typedef struct channel_data { grpc_load_reporting_config *lrc; } channel_data; /* Constructor for call_data */ static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, @@ -50,9 +50,11 @@ static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, const grpc_call_stats *stats, void *ignored) { channel_data *chand = elem->channel_data; - GPR_TIMER_BEGIN("load_reporting_filter", 0); - grpc_load_reporting_call(chand->lrd, stats); - GPR_TIMER_END("load_reporting_filter", 0); + if (chand->lrc != NULL) { + GPR_TIMER_BEGIN("load_reporting_filter", 0); + grpc_load_reporting_config_call(chand->lrc, stats); + GPR_TIMER_END("load_reporting_filter", 0); + } } /* Constructor for channel_data */ @@ -65,20 +67,23 @@ static void init_channel_elem(grpc_exec_ctx *exec_ctx, memset(chand, 0, sizeof(channel_data)); for (size_t i = 0; i < args->channel_args->num_args; i++) { - if (0 == strcmp(args->channel_args->args[i].key, GRPC_ARG_ENABLE_LOAD_REPORTING)) { - chand->lrd = args->channel_args->args[i].value.pointer.p; - GPR_ASSERT(chand->lrd != NULL); + if (0 == strcmp(args->channel_args->args[i].key, + GRPC_ARG_ENABLE_LOAD_REPORTING)) { + grpc_load_reporting_config *arg_lrc = + args->channel_args->args[i].value.pointer.p; + chand->lrc = grpc_load_reporting_config_copy(arg_lrc); + GPR_ASSERT(chand->lrc != NULL); + break; } } - GPR_ASSERT(chand->lrd != NULL); /* arg actually found */ - + GPR_ASSERT(chand->lrc != NULL); /* arg actually found */ } /* Destructor for channel data */ static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem) { channel_data *chand = elem->channel_data; - grpc_load_reporting_destroy(chand->lrd); + grpc_load_reporting_config_destroy(chand->lrc); } const grpc_channel_filter grpc_load_reporting_filter = { diff --git a/src/core/plugin_registry/grpc_unsecure_plugin_registry.c b/src/core/plugin_registry/grpc_unsecure_plugin_registry.c index a6108ae7a9..7995078725 100644 --- a/src/core/plugin_registry/grpc_unsecure_plugin_registry.c +++ b/src/core/plugin_registry/grpc_unsecure_plugin_registry.c @@ -41,6 +41,8 @@ extern void grpc_resolver_dns_native_init(void); extern void grpc_resolver_dns_native_shutdown(void); extern void grpc_resolver_sockaddr_init(void); extern void grpc_resolver_sockaddr_shutdown(void); +extern void grpc_load_reporting_plugin_init(void); +extern void grpc_load_reporting_plugin_shutdown(void); extern void grpc_lb_policy_pick_first_init(void); extern void grpc_lb_policy_pick_first_shutdown(void); extern void grpc_lb_policy_round_robin_init(void); @@ -57,6 +59,8 @@ void grpc_register_built_in_plugins(void) { grpc_resolver_dns_native_shutdown); grpc_register_plugin(grpc_resolver_sockaddr_init, grpc_resolver_sockaddr_shutdown); + grpc_register_plugin(grpc_load_reporting_plugin_init, + grpc_load_reporting_plugin_shutdown); grpc_register_plugin(grpc_lb_policy_pick_first_init, grpc_lb_policy_pick_first_shutdown); grpc_register_plugin(grpc_lb_policy_round_robin_init, -- cgit v1.2.3