From 8a30491657eec10a17e9de3a2a9cc5743bcabee2 Mon Sep 17 00:00:00 2001 From: Tanvi Jagtap <139093547+tanvi-jagtap@users.noreply.github.com> Date: Wed, 12 Jun 2024 22:48:45 -0700 Subject: [PATCH] [grpc][Gpr_To_Absl_Logging] Migrating from gpr to absl logging GPR_ASSERT (#36817) [grpc][Gpr_To_Absl_Logging] Migrating from gpr to absl logging GPR_ASSERT Replacing GPR_ASSERT with c assert. These changes have been made using string replacement and regex. Closes #36817 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36817 from tanvi-jagtap:gpr_assert_removal_in_php 2222f9541314679e5a73f301bfafd5a70f280471 PiperOrigin-RevId: 642855768 --- src/ruby/ext/grpc/rb_call.c | 4 ++-- src/ruby/ext/grpc/rb_channel.c | 28 +++++++++++----------- src/ruby/ext/grpc/rb_channel_args.c | 2 +- src/ruby/ext/grpc/rb_compression_options.c | 6 ++--- src/ruby/ext/grpc/rb_event_thread.c | 4 ++-- src/ruby/ext/grpc/rb_grpc.c | 8 +++---- src/ruby/ext/grpc/rb_grpc.h | 8 +++++++ 7 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/ruby/ext/grpc/rb_call.c b/src/ruby/ext/grpc/rb_call.c index dde2901e6b2..9f8ede4defe 100644 --- a/src/ruby/ext/grpc/rb_call.c +++ b/src/ruby/ext/grpc/rb_call.c @@ -437,7 +437,7 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { tmp_str); return ST_STOP; } - GPR_ASSERT(md_ary->count < md_ary->capacity); + GRPC_RUBY_ASSERT(md_ary->count < md_ary->capacity); md_ary->metadata[md_ary->count].key = key_slice; md_ary->metadata[md_ary->count].value = value_slice; md_ary->count += 1; @@ -453,7 +453,7 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { tmp_str); return ST_STOP; } - GPR_ASSERT(md_ary->count < md_ary->capacity); + GRPC_RUBY_ASSERT(md_ary->count < md_ary->capacity); md_ary->metadata[md_ary->count].key = key_slice; md_ary->metadata[md_ary->count].value = value_slice; md_ary->count += 1; diff --git a/src/ruby/ext/grpc/rb_channel.c b/src/ruby/ext/grpc/rb_channel.c index 48099c258b6..580ef9ebd25 100644 --- a/src/ruby/ext/grpc/rb_channel.c +++ b/src/ruby/ext/grpc/rb_channel.c @@ -123,7 +123,7 @@ static void* run_poll_channels_loop_unblocking_func_wrapper(void* arg); // Needs to be called under global_connection_polling_mu static void grpc_rb_channel_watch_connection_state_op_complete( watch_state_op* op, int success) { - GPR_ASSERT(!op->op.api_callback_args.called_back); + GRPC_RUBY_ASSERT(!op->op.api_callback_args.called_back); op->op.api_callback_args.called_back = 1; op->op.api_callback_args.success = success; // wake up the watch API call that's waiting on this op @@ -133,7 +133,7 @@ static void grpc_rb_channel_watch_connection_state_op_complete( /* Avoids destroying a channel twice. */ static void grpc_rb_channel_safe_destroy(bg_watched_channel* bg) { gpr_mu_lock(&global_connection_polling_mu); - GPR_ASSERT(bg_watched_channel_list_lookup(bg)); + GRPC_RUBY_ASSERT(bg_watched_channel_list_lookup(bg)); if (!bg->channel_destroyed) { grpc_channel_destroy(bg->channel); bg->channel_destroyed = 1; @@ -253,7 +253,7 @@ static VALUE grpc_rb_channel_init(int argc, VALUE* argv, VALUE self) { ch = grpc_channel_create(target_chars, creds, &wrapper->args); } - GPR_ASSERT(ch); + GRPC_RUBY_ASSERT(ch); stack.channel = ch; stack.wrapper = wrapper; rb_thread_call_without_gvl( @@ -416,7 +416,7 @@ static void grpc_rb_channel_maybe_recreate_channel_after_fork( if (bg->channel_destroyed) { // There must be one ref at this point, held by the ruby-level channel // object, drop this one here. - GPR_ASSERT(bg->refcount == 1); + GRPC_RUBY_ASSERT(bg->refcount == 1); rb_thread_call_without_gvl(channel_safe_destroy_without_gil, bg, NULL, NULL); // re-create C-core channel @@ -581,8 +581,8 @@ static void bg_watched_channel_list_free_and_remove( bg_watched_channel* target) { bg_watched_channel* bg = NULL; - GPR_ASSERT(bg_watched_channel_list_lookup(target)); - GPR_ASSERT(target->channel_destroyed && target->refcount == 0); + GRPC_RUBY_ASSERT(bg_watched_channel_list_lookup(target)); + GRPC_RUBY_ASSERT(target->channel_destroyed && target->refcount == 0); if (bg_watched_channel_list_head == target) { bg_watched_channel_list_head = target->next; gpr_free(target); @@ -597,7 +597,7 @@ static void bg_watched_channel_list_free_and_remove( } bg = bg->next; } - GPR_ASSERT(0); + GRPC_RUBY_ASSERT(0); } /* Initialize a grpc_rb_channel's "protected grpc_channel" and try to push @@ -621,11 +621,11 @@ static void grpc_rb_channel_try_register_connection_polling( grpc_connectivity_state conn_state; watch_state_op* op = NULL; if (bg->refcount == 0) { - GPR_ASSERT(bg->channel_destroyed); + GRPC_RUBY_ASSERT(bg->channel_destroyed); bg_watched_channel_list_free_and_remove(bg); return; } - GPR_ASSERT(bg->refcount == 1); + GRPC_RUBY_ASSERT(bg->refcount == 1); if (bg->channel_destroyed || g_abort_channel_polling) { return; } @@ -633,7 +633,7 @@ static void grpc_rb_channel_try_register_connection_polling( if (conn_state == GRPC_CHANNEL_SHUTDOWN) { return; } - GPR_ASSERT(bg_watched_channel_list_lookup(bg)); + GRPC_RUBY_ASSERT(bg_watched_channel_list_lookup(bg)); // prevent bg from being free'd by GC while background thread is watching it bg->refcount++; op = gpr_zalloc(sizeof(watch_state_op)); @@ -679,7 +679,7 @@ static void* run_poll_channels_loop_no_gil(void* arg) { grpc_rb_channel_watch_connection_state_op_complete( (watch_state_op*)event.tag, event.success); } else { - GPR_ASSERT(0); + GRPC_RUBY_ASSERT(0); } } gpr_mu_unlock(&global_connection_polling_mu); @@ -769,9 +769,9 @@ static void do_basic_init() { */ void grpc_rb_channel_polling_thread_start() { gpr_once_init(&g_once_init, do_basic_init); - GPR_ASSERT(!RTEST(g_channel_polling_thread)); - GPR_ASSERT(!g_abort_channel_polling); - GPR_ASSERT(g_channel_polling_cq == NULL); + GRPC_RUBY_ASSERT(!RTEST(g_channel_polling_thread)); + GRPC_RUBY_ASSERT(!g_abort_channel_polling); + GRPC_RUBY_ASSERT(g_channel_polling_cq == NULL); g_channel_polling_cq = grpc_completion_queue_create_for_next(NULL); g_channel_polling_thread = rb_thread_create(run_poll_channels_loop, NULL); diff --git a/src/ruby/ext/grpc/rb_channel_args.c b/src/ruby/ext/grpc/rb_channel_args.c index 0dd43bcec96..4c252397e2a 100644 --- a/src/ruby/ext/grpc/rb_channel_args.c +++ b/src/ruby/ext/grpc/rb_channel_args.c @@ -160,7 +160,7 @@ void grpc_rb_hash_convert_to_channel_args(VALUE src_hash, } void grpc_rb_channel_args_destroy(grpc_channel_args* args) { - GPR_ASSERT(args != NULL); + GRPC_RUBY_ASSERT(args != NULL); if (args->args == NULL) return; for (int i = 0; i < args->num_args; i++) { // the key was created with gpr_strdup diff --git a/src/ruby/ext/grpc/rb_compression_options.c b/src/ruby/ext/grpc/rb_compression_options.c index 155c5dafe37..7bdc33004e8 100644 --- a/src/ruby/ext/grpc/rb_compression_options.c +++ b/src/ruby/ext/grpc/rb_compression_options.c @@ -181,9 +181,9 @@ void grpc_rb_compression_options_algorithm_name_to_value_internal( char* name_slice_str = grpc_slice_to_c_string(name_slice); char* error_message_str = NULL; VALUE error_message_ruby_str = Qnil; - GPR_ASSERT(gpr_asprintf(&error_message_str, - "Invalid compression algorithm name: %s", - name_slice_str) != -1); + GRPC_RUBY_ASSERT(gpr_asprintf(&error_message_str, + "Invalid compression algorithm name: %s", + name_slice_str) != -1); gpr_free(name_slice_str); error_message_ruby_str = rb_str_new(error_message_str, strlen(error_message_str)); diff --git a/src/ruby/ext/grpc/rb_event_thread.c b/src/ruby/ext/grpc/rb_event_thread.c index b22ca4c7f5d..eaa77965de4 100644 --- a/src/ruby/ext/grpc/rb_event_thread.c +++ b/src/ruby/ext/grpc/rb_event_thread.c @@ -149,12 +149,12 @@ void grpc_rb_event_queue_thread_start() { event_queue.head = event_queue.tail = NULL; } event_queue.abort = false; - GPR_ASSERT(!RTEST(g_event_thread)); + GRPC_RUBY_ASSERT(!RTEST(g_event_thread)); g_event_thread = rb_thread_create(grpc_rb_event_thread, NULL); } void grpc_rb_event_queue_thread_stop() { - GPR_ASSERT(g_one_time_init_done); + GRPC_RUBY_ASSERT(g_one_time_init_done); if (!RTEST(g_event_thread)) { gpr_log(GPR_ERROR, "GRPC_RUBY: call credentials thread stop: thread not running"); diff --git a/src/ruby/ext/grpc/rb_grpc.c b/src/ruby/ext/grpc/rb_grpc.c index 01c4777d800..0f73dc1d4b6 100644 --- a/src/ruby/ext/grpc/rb_grpc.c +++ b/src/ruby/ext/grpc/rb_grpc.c @@ -248,12 +248,12 @@ static pid_t g_init_pid; static long g_init_tid; static bool grpc_ruby_initial_pid(void) { - GPR_ASSERT(g_init_pid != 0); + GRPC_RUBY_ASSERT(g_init_pid != 0); return g_init_pid == getpid(); } static bool grpc_ruby_initial_thread(void) { - GPR_ASSERT(g_init_tid != 0); + GRPC_RUBY_ASSERT(g_init_tid != 0); return sys_gettid() == g_init_tid; } @@ -263,8 +263,8 @@ static void grpc_ruby_reset_init_state(void) { } static void grpc_ruby_basic_init(void) { - GPR_ASSERT(g_init_pid == 0); - GPR_ASSERT(g_init_tid == 0); + GRPC_RUBY_ASSERT(g_init_pid == 0); + GRPC_RUBY_ASSERT(g_init_tid == 0); grpc_ruby_reset_init_state(); // TODO(apolcyn): ideally, we should share logic with C-core // for determining whether or not fork support is enabled, rather diff --git a/src/ruby/ext/grpc/rb_grpc.h b/src/ruby/ext/grpc/rb_grpc.h index 1e7d391935f..7518b79e390 100644 --- a/src/ruby/ext/grpc/rb_grpc.h +++ b/src/ruby/ext/grpc/rb_grpc.h @@ -21,6 +21,8 @@ #include +#include + #include /* grpc_rb_mGrpcCore is the module containing the ruby wrapper GRPC classes. */ @@ -78,4 +80,10 @@ void grpc_rb_fork_unsafe_end(); void grpc_ruby_init(); +#define GRPC_RUBY_ASSERT(x) \ + if (!(x)) { \ + fprintf(stderr, "%s:%d assert failed\n", __FILE__, __LINE__); \ + abort(); \ + } + #endif /* GRPC_RB_H_ */