Skip to content

Commit

Permalink
Merge pull request grpc#7391 from dgquintas/lr_july_16
Browse files Browse the repository at this point in the history
Load Reporting Filter
  • Loading branch information
nicolasnoble authored Aug 1, 2016
2 parents 2507fef + 54fe31a commit a29ca4e
Show file tree
Hide file tree
Showing 41 changed files with 1,458 additions and 483 deletions.
44 changes: 23 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ h2_fd_test: $(BINDIR)/$(CONFIG)/h2_fd_test
h2_full_test: $(BINDIR)/$(CONFIG)/h2_full_test
h2_full+pipe_test: $(BINDIR)/$(CONFIG)/h2_full+pipe_test
h2_full+trace_test: $(BINDIR)/$(CONFIG)/h2_full+trace_test
h2_loadreporting_test: $(BINDIR)/$(CONFIG)/h2_loadreporting_test
h2_load_reporting_test: $(BINDIR)/$(CONFIG)/h2_load_reporting_test
h2_oauth2_test: $(BINDIR)/$(CONFIG)/h2_oauth2_test
h2_proxy_test: $(BINDIR)/$(CONFIG)/h2_proxy_test
h2_sockpair_test: $(BINDIR)/$(CONFIG)/h2_sockpair_test
Expand All @@ -1148,7 +1148,7 @@ h2_fd_nosec_test: $(BINDIR)/$(CONFIG)/h2_fd_nosec_test
h2_full_nosec_test: $(BINDIR)/$(CONFIG)/h2_full_nosec_test
h2_full+pipe_nosec_test: $(BINDIR)/$(CONFIG)/h2_full+pipe_nosec_test
h2_full+trace_nosec_test: $(BINDIR)/$(CONFIG)/h2_full+trace_nosec_test
h2_loadreporting_nosec_test: $(BINDIR)/$(CONFIG)/h2_loadreporting_nosec_test
h2_load_reporting_nosec_test: $(BINDIR)/$(CONFIG)/h2_load_reporting_nosec_test
h2_proxy_nosec_test: $(BINDIR)/$(CONFIG)/h2_proxy_nosec_test
h2_sockpair_nosec_test: $(BINDIR)/$(CONFIG)/h2_sockpair_nosec_test
h2_sockpair+trace_nosec_test: $(BINDIR)/$(CONFIG)/h2_sockpair+trace_nosec_test
Expand Down Expand Up @@ -1348,7 +1348,7 @@ buildtests_c: privatelibs_c \
$(BINDIR)/$(CONFIG)/h2_full_test \
$(BINDIR)/$(CONFIG)/h2_full+pipe_test \
$(BINDIR)/$(CONFIG)/h2_full+trace_test \
$(BINDIR)/$(CONFIG)/h2_loadreporting_test \
$(BINDIR)/$(CONFIG)/h2_load_reporting_test \
$(BINDIR)/$(CONFIG)/h2_oauth2_test \
$(BINDIR)/$(CONFIG)/h2_proxy_test \
$(BINDIR)/$(CONFIG)/h2_sockpair_test \
Expand All @@ -1364,7 +1364,7 @@ buildtests_c: privatelibs_c \
$(BINDIR)/$(CONFIG)/h2_full_nosec_test \
$(BINDIR)/$(CONFIG)/h2_full+pipe_nosec_test \
$(BINDIR)/$(CONFIG)/h2_full+trace_nosec_test \
$(BINDIR)/$(CONFIG)/h2_loadreporting_nosec_test \
$(BINDIR)/$(CONFIG)/h2_load_reporting_nosec_test \
$(BINDIR)/$(CONFIG)/h2_proxy_nosec_test \
$(BINDIR)/$(CONFIG)/h2_sockpair_nosec_test \
$(BINDIR)/$(CONFIG)/h2_sockpair+trace_nosec_test \
Expand Down Expand Up @@ -6403,6 +6403,7 @@ LIBEND2END_TESTS_SRC = \
test/core/end2end/tests/idempotent_request.c \
test/core/end2end/tests/invoke_large_request.c \
test/core/end2end/tests/large_metadata.c \
test/core/end2end/tests/load_reporting_hook.c \
test/core/end2end/tests/max_concurrent_streams.c \
test/core/end2end/tests/max_message_length.c \
test/core/end2end/tests/negative_deadline.c \
Expand Down Expand Up @@ -6481,6 +6482,7 @@ LIBEND2END_NOSEC_TESTS_SRC = \
test/core/end2end/tests/idempotent_request.c \
test/core/end2end/tests/invoke_large_request.c \
test/core/end2end/tests/large_metadata.c \
test/core/end2end/tests/load_reporting_hook.c \
test/core/end2end/tests/max_concurrent_streams.c \
test/core/end2end/tests/max_message_length.c \
test/core/end2end/tests/negative_deadline.c \
Expand Down Expand Up @@ -13992,34 +13994,34 @@ endif
endif


H2_LOADREPORTING_TEST_SRC = \
test/core/end2end/fixtures/h2_loadreporting.c \
H2_LOAD_REPORTING_TEST_SRC = \
test/core/end2end/fixtures/h2_load_reporting.c \

H2_LOADREPORTING_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(H2_LOADREPORTING_TEST_SRC))))
H2_LOAD_REPORTING_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(H2_LOAD_REPORTING_TEST_SRC))))
ifeq ($(NO_SECURE),true)

# You can't build secure targets if you don't have OpenSSL.

$(BINDIR)/$(CONFIG)/h2_loadreporting_test: openssl_dep_error
$(BINDIR)/$(CONFIG)/h2_load_reporting_test: openssl_dep_error

else



$(BINDIR)/$(CONFIG)/h2_loadreporting_test: $(H2_LOADREPORTING_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libend2end_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a
$(BINDIR)/$(CONFIG)/h2_load_reporting_test: $(H2_LOAD_REPORTING_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libend2end_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a
$(E) "[LD] Linking $@"
$(Q) mkdir -p `dirname $@`
$(Q) $(LD) $(LDFLAGS) $(H2_LOADREPORTING_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libend2end_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBS) $(LDLIBS_SECURE) -o $(BINDIR)/$(CONFIG)/h2_loadreporting_test
$(Q) $(LD) $(LDFLAGS) $(H2_LOAD_REPORTING_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libend2end_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBS) $(LDLIBS_SECURE) -o $(BINDIR)/$(CONFIG)/h2_load_reporting_test

endif

$(OBJDIR)/$(CONFIG)/test/core/end2end/fixtures/h2_loadreporting.o: $(LIBDIR)/$(CONFIG)/libend2end_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a
$(OBJDIR)/$(CONFIG)/test/core/end2end/fixtures/h2_load_reporting.o: $(LIBDIR)/$(CONFIG)/libend2end_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a

deps_h2_loadreporting_test: $(H2_LOADREPORTING_TEST_OBJS:.o=.dep)
deps_h2_load_reporting_test: $(H2_LOAD_REPORTING_TEST_OBJS:.o=.dep)

ifneq ($(NO_SECURE),true)
ifneq ($(NO_DEPS),true)
-include $(H2_LOADREPORTING_TEST_OBJS:.o=.dep)
-include $(H2_LOAD_REPORTING_TEST_OBJS:.o=.dep)
endif
endif

Expand Down Expand Up @@ -14432,23 +14434,23 @@ ifneq ($(NO_DEPS),true)
endif


H2_LOADREPORTING_NOSEC_TEST_SRC = \
test/core/end2end/fixtures/h2_loadreporting.c \
H2_LOAD_REPORTING_NOSEC_TEST_SRC = \
test/core/end2end/fixtures/h2_load_reporting.c \

H2_LOADREPORTING_NOSEC_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(H2_LOADREPORTING_NOSEC_TEST_SRC))))
H2_LOAD_REPORTING_NOSEC_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(H2_LOAD_REPORTING_NOSEC_TEST_SRC))))


$(BINDIR)/$(CONFIG)/h2_loadreporting_nosec_test: $(H2_LOADREPORTING_NOSEC_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libend2end_nosec_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util_unsecure.a $(LIBDIR)/$(CONFIG)/libgrpc_unsecure.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a
$(BINDIR)/$(CONFIG)/h2_load_reporting_nosec_test: $(H2_LOAD_REPORTING_NOSEC_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libend2end_nosec_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util_unsecure.a $(LIBDIR)/$(CONFIG)/libgrpc_unsecure.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a
$(E) "[LD] Linking $@"
$(Q) mkdir -p `dirname $@`
$(Q) $(LD) $(LDFLAGS) $(H2_LOADREPORTING_NOSEC_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libend2end_nosec_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util_unsecure.a $(LIBDIR)/$(CONFIG)/libgrpc_unsecure.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBS) -o $(BINDIR)/$(CONFIG)/h2_loadreporting_nosec_test
$(Q) $(LD) $(LDFLAGS) $(H2_LOAD_REPORTING_NOSEC_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libend2end_nosec_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util_unsecure.a $(LIBDIR)/$(CONFIG)/libgrpc_unsecure.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBS) -o $(BINDIR)/$(CONFIG)/h2_load_reporting_nosec_test

$(OBJDIR)/$(CONFIG)/test/core/end2end/fixtures/h2_loadreporting.o: $(LIBDIR)/$(CONFIG)/libend2end_nosec_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util_unsecure.a $(LIBDIR)/$(CONFIG)/libgrpc_unsecure.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a
$(OBJDIR)/$(CONFIG)/test/core/end2end/fixtures/h2_load_reporting.o: $(LIBDIR)/$(CONFIG)/libend2end_nosec_tests.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util_unsecure.a $(LIBDIR)/$(CONFIG)/libgrpc_unsecure.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a

deps_h2_loadreporting_nosec_test: $(H2_LOADREPORTING_NOSEC_TEST_OBJS:.o=.dep)
deps_h2_load_reporting_nosec_test: $(H2_LOAD_REPORTING_NOSEC_TEST_OBJS:.o=.dep)

ifneq ($(NO_DEPS),true)
-include $(H2_LOADREPORTING_NOSEC_TEST_OBJS:.o=.dep)
-include $(H2_LOAD_REPORTING_NOSEC_TEST_OBJS:.o=.dep)
endif


Expand Down
4 changes: 2 additions & 2 deletions src/core/ext/census/grpc_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static void client_init_call_elem(grpc_exec_ctx *exec_ctx,

static void client_destroy_call_elem(grpc_exec_ctx *exec_ctx,
grpc_call_element *elem,
const grpc_call_stats *stats,
const grpc_call_final_info *final_info,
void *ignored) {
call_data *d = elem->call_data;
GPR_ASSERT(d != NULL);
Expand All @@ -158,7 +158,7 @@ static void server_init_call_elem(grpc_exec_ctx *exec_ctx,

static void server_destroy_call_elem(grpc_exec_ctx *exec_ctx,
grpc_call_element *elem,
const grpc_call_stats *stats,
const grpc_call_final_info *final_info,
void *ignored) {
call_data *d = elem->call_data;
GPR_ASSERT(d != NULL);
Expand Down
2 changes: 1 addition & 1 deletion src/core/ext/client_config/client_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,

/* Destructor for call_data */
static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
const grpc_call_stats *stats,
const grpc_call_final_info *final_info,
void *and_free_memory) {
grpc_subchannel_call_holder_destroy(exec_ctx, elem->call_data);
gpr_free(and_free_memory);
Expand Down
60 changes: 5 additions & 55 deletions src/core/ext/load_reporting/load_reporting.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,12 @@
#include "src/core/lib/channel/channel_stack_builder.h"
#include "src/core/lib/surface/channel_init.h"

struct grpc_load_reporting_config {
grpc_load_reporting_fn fn;
void *user_data;
};

grpc_load_reporting_config *grpc_load_reporting_config_create(
grpc_load_reporting_fn fn, void *user_data) {
GPR_ASSERT(fn != NULL);
grpc_load_reporting_config *lrc =
gpr_malloc(sizeof(grpc_load_reporting_config));
lrc->fn = fn;
lrc->user_data = user_data;
return lrc;
}

grpc_load_reporting_config *grpc_load_reporting_config_copy(
grpc_load_reporting_config *src) {
return grpc_load_reporting_config_create(src->fn, src->user_data);
}

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_load_reporting_call_data *call_data) {
lrc->fn(call_data, lrc->user_data);
}

static bool is_load_reporting_enabled(const grpc_channel_args *a) {
if (a == NULL) return false;
for (size_t i = 0; i < a->num_args; i++) {
if (0 == strcmp(a->args[i].key, GRPC_ARG_ENABLE_LOAD_REPORTING)) {
return a->args[i].type == GRPC_ARG_POINTER &&
a->args[i].value.pointer.p != NULL;
return a->args[i].type == GRPC_ARG_INTEGER &&
a->args[i].value.integer != 0;
}
}
return false;
Expand All @@ -94,37 +64,17 @@ 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->user_data == rhs->user_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 grpc_load_reporting_enable_arg() {
grpc_arg arg;
arg.type = GRPC_ARG_POINTER;
arg.type = GRPC_ARG_INTEGER;
arg.key = GRPC_ARG_ENABLE_LOAD_REPORTING;
arg.value.pointer.p = lrc;
arg.value.pointer.vtable = &lrd_ptr_vtable;
arg.value.integer = 1;
return arg;
}

/* Plugin registration */

void grpc_load_reporting_plugin_init(void) {
grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX,
maybe_add_load_reporting_filter,
(void *)&grpc_load_reporting_filter);
grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX,
maybe_add_load_reporting_filter,
(void *)&grpc_load_reporting_filter);
Expand Down
61 changes: 33 additions & 28 deletions src/core/ext/load_reporting/load_reporting.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,47 @@
#ifndef GRPC_CORE_EXT_LOAD_REPORTING_LOAD_REPORTING_H
#define GRPC_CORE_EXT_LOAD_REPORTING_LOAD_REPORTING_H

#include "src/core/lib/iomgr/closure.h"
#include "src/core/lib/surface/call.h"
#include <grpc/impl/codegen/grpc_types.h>
#include "src/core/lib/channel/channel_stack.h"

typedef struct grpc_load_reporting_config grpc_load_reporting_config;
/** Metadata key for initial metadata coming from clients */
/* TODO(dgq): change to the final value TBD */
#define GRPC_LOAD_REPORTING_INITIAL_MD_KEY "load-reporting-initial"

/** Call information to be passed to the provided load reporting function upon
* completion of the call */
typedef struct grpc_load_reporting_call_data {
const grpc_call_stats *stats; /**< Stats for the call */
const char *trailing_md_string; /**< LR trailing metadata info */
} grpc_load_reporting_call_data;
/** Metadata key for trailing metadata from servers */
/* TODO(dgq): change to the final value TBD */
#define GRPC_LOAD_REPORTING_TRAILING_MD_KEY "load-reporting-trailing"

/** Custom function to be called by the load reporting filter. */
typedef void (*grpc_load_reporting_fn)(
const grpc_load_reporting_call_data *call_data, void *user_data);
/** Identifiers for the invocation point of the users LR callback */
typedef enum grpc_load_reporting_source {
GRPC_LR_POINT_UNKNOWN = 0,
GRPC_LR_POINT_CHANNEL_CREATION,
GRPC_LR_POINT_CHANNEL_DESTRUCTION,
GRPC_LR_POINT_CALL_CREATION,
GRPC_LR_POINT_CALL_DESTRUCTION
} grpc_load_reporting_source;

/** Register \a fn as the function to be invoked by the load reporting filter.
* \a fn will be invoked at the beginning and at the end of the call.
*
* For the first invocation, \a fn's first argument
* (grpc_load_reporting_call_data*) will be NULL. \a user_data is always passed
* as-is. */
grpc_load_reporting_config *grpc_load_reporting_config_create(
grpc_load_reporting_fn fn, void *user_data);
/** Call information to be passed to the provided LR callback. */
typedef struct grpc_load_reporting_call_data {
const grpc_load_reporting_source source; /**< point of last data update. */

/** Unique identifier for the channel associated with the data */
intptr_t channel_id;

grpc_load_reporting_config *grpc_load_reporting_config_copy(
grpc_load_reporting_config *src);
/** Unique identifier for the call associated with the data. If the call
* hasn't been created yet, it'll have a value of zero. */
intptr_t call_id;

void grpc_load_reporting_config_destroy(grpc_load_reporting_config *lrc);
/** Only valid when \a source is \a GRPC_LR_POINT_CALL_DESTRUCTION, that is,
* once the call has completed */
const grpc_call_final_info *final_info;

/** Invoke the function registered by \a grpc_load_reporting_init. */
void grpc_load_reporting_config_call(
grpc_load_reporting_config *lrc,
const grpc_load_reporting_call_data *call_data);
const char *initial_md_string; /**< value string for LR's initial md key */
const char *trailing_md_string; /**< value string for LR's trailing md key */
const char *method_name; /**< Corresponds to :path header */
} grpc_load_reporting_call_data;

/** Return a \a grpc_arg enabling load reporting */
grpc_arg grpc_load_reporting_config_create_arg(grpc_load_reporting_config *lrc);
grpc_arg grpc_load_reporting_enable_arg();

#endif /* GRPC_CORE_EXT_LOAD_REPORTING_LOAD_REPORTING_H */
Loading

0 comments on commit a29ca4e

Please sign in to comment.