diff options
Diffstat (limited to 'src')
38 files changed, 407 insertions, 311 deletions
diff --git a/src/core/ext/filters/client_channel/client_channel.cc b/src/core/ext/filters/client_channel/client_channel.cc index 3813190794..fa0c280f80 100644 --- a/src/core/ext/filters/client_channel/client_channel.cc +++ b/src/core/ext/filters/client_channel/client_channel.cc @@ -3242,7 +3242,7 @@ static void on_external_watch_complete_locked(void* arg, grpc_error* error) { "external_connectivity_watcher"); external_connectivity_watcher_list_remove(w->chand, w); gpr_free(w); - GRPC_CLOSURE_RUN(follow_up, GRPC_ERROR_REF(error)); + GRPC_CLOSURE_SCHED(follow_up, GRPC_ERROR_REF(error)); } static void watch_connectivity_state_locked(void* arg, diff --git a/src/core/ext/filters/client_channel/subchannel.cc b/src/core/ext/filters/client_channel/subchannel.cc index 140441da10..ad6b6dd192 100644 --- a/src/core/ext/filters/client_channel/subchannel.cc +++ b/src/core/ext/filters/client_channel/subchannel.cc @@ -408,7 +408,7 @@ static void on_external_state_watcher_done(void* arg, grpc_error* error) { gpr_mu_unlock(&w->subchannel->mu); GRPC_SUBCHANNEL_WEAK_UNREF(w->subchannel, "external_state_watcher"); gpr_free(w); - GRPC_CLOSURE_RUN(follow_up, GRPC_ERROR_REF(error)); + GRPC_CLOSURE_SCHED(follow_up, GRPC_ERROR_REF(error)); } static void on_alarm(void* arg, grpc_error* error) { diff --git a/src/core/ext/transport/chttp2/transport/chttp2_transport.cc b/src/core/ext/transport/chttp2/transport/chttp2_transport.cc index 7ff7cabfbd..cc4a823798 100644 --- a/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +++ b/src/core/ext/transport/chttp2/transport/chttp2_transport.cc @@ -1684,16 +1684,16 @@ static void send_ping_locked(grpc_chttp2_transport* t, */ static void send_keepalive_ping_locked(grpc_chttp2_transport* t) { if (t->closed_with_error != GRPC_ERROR_NONE) { - GRPC_CLOSURE_SCHED(&t->start_keepalive_ping_locked, - GRPC_ERROR_REF(t->closed_with_error)); - GRPC_CLOSURE_SCHED(&t->finish_keepalive_ping_locked, - GRPC_ERROR_REF(t->closed_with_error)); + GRPC_CLOSURE_RUN(&t->start_keepalive_ping_locked, + GRPC_ERROR_REF(t->closed_with_error)); + GRPC_CLOSURE_RUN(&t->finish_keepalive_ping_locked, + GRPC_ERROR_REF(t->closed_with_error)); return; } grpc_chttp2_ping_queue* pq = &t->ping_queue; if (!grpc_closure_list_empty(pq->lists[GRPC_CHTTP2_PCL_INFLIGHT])) { /* There is a ping in flight. Add yourself to the inflight closure list. */ - GRPC_CLOSURE_SCHED(&t->start_keepalive_ping_locked, GRPC_ERROR_NONE); + GRPC_CLOSURE_RUN(&t->start_keepalive_ping_locked, GRPC_ERROR_NONE); grpc_closure_list_append(&pq->lists[GRPC_CHTTP2_PCL_INFLIGHT], &t->finish_keepalive_ping_locked, GRPC_ERROR_NONE); return; diff --git a/src/core/lib/channel/channel_trace.cc b/src/core/lib/channel/channel_trace.cc index 654300cd32..eb7214b355 100644 --- a/src/core/lib/channel/channel_trace.cc +++ b/src/core/lib/channel/channel_trace.cc @@ -28,7 +28,7 @@ #include <stdlib.h> #include <string.h> -#include "src/core/lib/channel/channel_trace_registry.h" +#include "src/core/lib/channel/channelz_registry.h" #include "src/core/lib/channel/status_util.h" #include "src/core/lib/gpr/string.h" #include "src/core/lib/gpr/useful.h" @@ -70,7 +70,7 @@ ChannelTrace::ChannelTrace(size_t max_events) tail_trace_(nullptr) { if (max_list_size_ == 0) return; // tracing is disabled if max_events == 0 gpr_mu_init(&tracer_mu_); - channel_uuid_ = grpc_channel_trace_registry_register_channel_trace(this); + channel_uuid_ = ChannelzRegistry::Register(this); time_created_ = grpc_millis_to_timespec(grpc_core::ExecCtx::Get()->Now(), GPR_CLOCK_REALTIME); } @@ -83,7 +83,7 @@ ChannelTrace::~ChannelTrace() { it = it->next(); Delete<TraceEvent>(to_free); } - grpc_channel_trace_registry_unregister_channel_trace(channel_uuid_); + ChannelzRegistry::Unregister(channel_uuid_); gpr_mu_destroy(&tracer_mu_); } diff --git a/src/core/lib/channel/channel_trace_registry.cc b/src/core/lib/channel/channel_trace_registry.cc deleted file mode 100644 index 6c82431467..0000000000 --- a/src/core/lib/channel/channel_trace_registry.cc +++ /dev/null @@ -1,80 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include <grpc/impl/codegen/port_platform.h> - -#include "src/core/lib/avl/avl.h" -#include "src/core/lib/channel/channel_trace.h" -#include "src/core/lib/channel/channel_trace_registry.h" -#include "src/core/lib/gpr/useful.h" - -#include <grpc/support/alloc.h> -#include <grpc/support/log.h> - -// file global lock and avl. -static gpr_mu g_mu; -static grpc_avl g_avl; -static gpr_atm g_uuid = 0; - -// avl vtable for uuid (intptr_t) -> ChannelTrace -// this table is only looking, it does not own anything. -static void destroy_intptr(void* not_used, void* user_data) {} -static void* copy_intptr(void* key, void* user_data) { return key; } -static long compare_intptr(void* key1, void* key2, void* user_data) { - return GPR_ICMP(key1, key2); -} - -static void destroy_channel_trace(void* trace, void* user_data) {} -static void* copy_channel_trace(void* trace, void* user_data) { return trace; } -static const grpc_avl_vtable avl_vtable = { - destroy_intptr, copy_intptr, compare_intptr, destroy_channel_trace, - copy_channel_trace}; - -void grpc_channel_trace_registry_init() { - gpr_mu_init(&g_mu); - g_avl = grpc_avl_create(&avl_vtable); -} - -void grpc_channel_trace_registry_shutdown() { - grpc_avl_unref(g_avl, nullptr); - gpr_mu_destroy(&g_mu); -} - -intptr_t grpc_channel_trace_registry_register_channel_trace( - grpc_core::ChannelTrace* channel_trace) { - intptr_t prior = gpr_atm_no_barrier_fetch_add(&g_uuid, 1); - gpr_mu_lock(&g_mu); - g_avl = grpc_avl_add(g_avl, (void*)prior, channel_trace, nullptr); - gpr_mu_unlock(&g_mu); - return prior; -} - -void grpc_channel_trace_registry_unregister_channel_trace(intptr_t uuid) { - gpr_mu_lock(&g_mu); - g_avl = grpc_avl_remove(g_avl, (void*)uuid, nullptr); - gpr_mu_unlock(&g_mu); -} - -grpc_core::ChannelTrace* grpc_channel_trace_registry_get_channel_trace( - intptr_t uuid) { - gpr_mu_lock(&g_mu); - grpc_core::ChannelTrace* ret = static_cast<grpc_core::ChannelTrace*>( - grpc_avl_get(g_avl, (void*)uuid, nullptr)); - gpr_mu_unlock(&g_mu); - return ret; -} diff --git a/src/core/lib/channel/channel_trace_registry.h b/src/core/lib/channel/channel_trace_registry.h deleted file mode 100644 index 391ecba7de..0000000000 --- a/src/core/lib/channel/channel_trace_registry.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_REGISTRY_H -#define GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_REGISTRY_H - -#include <grpc/impl/codegen/port_platform.h> - -#include "src/core/lib/channel/channel_trace.h" - -#include <stdint.h> - -// TODO(ncteisen): convert this file to C++ - -void grpc_channel_trace_registry_init(); -void grpc_channel_trace_registry_shutdown(); - -// globally registers a ChannelTrace. Returns its unique uuid -intptr_t grpc_channel_trace_registry_register_channel_trace( - grpc_core::ChannelTrace* channel_trace); -// globally unregisters the ChannelTrace that is associated to uuid. -void grpc_channel_trace_registry_unregister_channel_trace(intptr_t uuid); -// if object with uuid has previously been registered, returns the ChannelTrace -// associated with that uuid. Else returns nullptr. -grpc_core::ChannelTrace* grpc_channel_trace_registry_get_channel_trace( - intptr_t uuid); - -#endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_REGISTRY_H */ diff --git a/src/core/lib/channel/channelz_registry.cc b/src/core/lib/channel/channelz_registry.cc new file mode 100644 index 0000000000..31d66e847a --- /dev/null +++ b/src/core/lib/channel/channelz_registry.cc @@ -0,0 +1,77 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include <grpc/impl/codegen/port_platform.h> + +#include "src/core/lib/channel/channel_trace.h" +#include "src/core/lib/channel/channelz_registry.h" +#include "src/core/lib/gpr/useful.h" +#include "src/core/lib/gprpp/memory.h" + +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> + +namespace grpc_core { +namespace { + +// singleton instance of the registry. +ChannelzRegistry* g_channelz_registry = nullptr; + +// avl vtable for uuid (intptr_t) -> channelz_obj (void*) +// this table is only looking, it does not own anything. +void destroy_intptr(void* not_used, void* user_data) {} +void* copy_intptr(void* key, void* user_data) { return key; } +long compare_intptr(void* key1, void* key2, void* user_data) { + return GPR_ICMP(key1, key2); +} + +void destroy_channelz_obj(void* channelz_obj, void* user_data) {} +void* copy_channelz_obj(void* channelz_obj, void* user_data) { + return channelz_obj; +} +const grpc_avl_vtable avl_vtable = {destroy_intptr, copy_intptr, compare_intptr, + destroy_channelz_obj, copy_channelz_obj}; + +} // anonymous namespace + +void ChannelzRegistry::Init() { g_channelz_registry = New<ChannelzRegistry>(); } + +void ChannelzRegistry::Shutdown() { Delete(g_channelz_registry); } + +ChannelzRegistry* ChannelzRegistry::Default() { + GPR_DEBUG_ASSERT(g_channelz_registry != nullptr); + return g_channelz_registry; +} + +ChannelzRegistry::ChannelzRegistry() : uuid_(1) { + gpr_mu_init(&mu_); + avl_ = grpc_avl_create(&avl_vtable); +} + +ChannelzRegistry::~ChannelzRegistry() { + grpc_avl_unref(avl_, nullptr); + gpr_mu_destroy(&mu_); +} + +void ChannelzRegistry::InternalUnregister(intptr_t uuid) { + gpr_mu_lock(&mu_); + avl_ = grpc_avl_remove(avl_, (void*)uuid, nullptr); + gpr_mu_unlock(&mu_); +} + +} // namespace grpc_core diff --git a/src/core/lib/channel/channelz_registry.h b/src/core/lib/channel/channelz_registry.h new file mode 100644 index 0000000000..4de7d478c5 --- /dev/null +++ b/src/core/lib/channel/channelz_registry.h @@ -0,0 +1,99 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H +#define GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H + +#include <grpc/impl/codegen/port_platform.h> + +#include "src/core/lib/avl/avl.h" +#include "src/core/lib/channel/channel_trace.h" + +#include <stdint.h> + +namespace grpc_core { + +// singleton registry object to track all objects that are needed to support +// channelz bookkeeping. All objects share globally distributed uuids. +class ChannelzRegistry { + public: + // To be called in grpc_init() + static void Init(); + + // To be callen in grpc_shutdown(); + static void Shutdown(); + + // globally registers a channelz Object. Returns its unique uuid + template <typename Object> + static intptr_t Register(Object* object) { + return Default()->InternalRegister(object); + } + + // globally unregisters the object that is associated to uuid. + static void Unregister(intptr_t uuid) { Default()->InternalUnregister(uuid); } + + // if object with uuid has previously been registered, returns the + // Object associated with that uuid. Else returns nullptr. + template <typename Object> + static Object* Get(intptr_t uuid) { + return Default()->InternalGet<Object>(uuid); + } + + private: + GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW + GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE + + ChannelzRegistry(); + ~ChannelzRegistry(); + + // Returned the singleton instance of ChannelzRegistry; + static ChannelzRegistry* Default(); + + // globally registers a channelz Object. Returns its unique uuid + template <typename Object> + intptr_t InternalRegister(Object* object) { + intptr_t prior = gpr_atm_no_barrier_fetch_add(&uuid_, 1); + gpr_mu_lock(&mu_); + avl_ = grpc_avl_add(avl_, (void*)prior, object, nullptr); + gpr_mu_unlock(&mu_); + return prior; + } + + // globally unregisters the object that is associated to uuid. + void InternalUnregister(intptr_t uuid); + + // if object with uuid has previously been registered, returns the + // Object associated with that uuid. Else returns nullptr. + template <typename Object> + Object* InternalGet(intptr_t uuid) { + gpr_mu_lock(&mu_); + Object* ret = + static_cast<Object*>(grpc_avl_get(avl_, (void*)uuid, nullptr)); + gpr_mu_unlock(&mu_); + return ret; + } + + // private members + gpr_mu mu_; + grpc_avl avl_; + gpr_atm uuid_; +}; + +} // namespace grpc_core + +#endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H */ diff --git a/src/core/lib/channel/handshaker.cc b/src/core/lib/channel/handshaker.cc index 2faeb64cb6..86f8699e04 100644 --- a/src/core/lib/channel/handshaker.cc +++ b/src/core/lib/channel/handshaker.cc @@ -28,6 +28,7 @@ #include "src/core/lib/channel/handshaker.h" #include "src/core/lib/debug/trace.h" #include "src/core/lib/iomgr/timer.h" +#include "src/core/lib/slice/slice_internal.h" grpc_core::TraceFlag grpc_handshaker_trace(false, "handshaker"); @@ -220,8 +221,26 @@ static bool call_next_handshaker_locked(grpc_handshake_manager* mgr, // callback. Otherwise, call the next handshaker. if (error != GRPC_ERROR_NONE || mgr->shutdown || mgr->args.exit_early || mgr->index == mgr->count) { + if (error == GRPC_ERROR_NONE && mgr->shutdown) { + error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("handshaker shutdown"); + // TODO(roth): It is currently necessary to shutdown endpoints + // before destroying then, even when we know that there are no + // pending read/write callbacks. This should be fixed, at which + // point this can be removed. + grpc_endpoint_shutdown(mgr->args.endpoint, GRPC_ERROR_REF(error)); + grpc_endpoint_destroy(mgr->args.endpoint); + mgr->args.endpoint = nullptr; + grpc_channel_args_destroy(mgr->args.args); + mgr->args.args = nullptr; + grpc_slice_buffer_destroy_internal(mgr->args.read_buffer); + gpr_free(mgr->args.read_buffer); + mgr->args.read_buffer = nullptr; + } if (grpc_handshaker_trace.enabled()) { - gpr_log(GPR_INFO, "handshake_manager %p: handshaking complete", mgr); + gpr_log(GPR_INFO, + "handshake_manager %p: handshaking complete -- scheduling " + "on_handshake_done with error=%s", + mgr, grpc_error_string(error)); } // Cancel deadline timer, since we're invoking the on_handshake_done // callback now. diff --git a/src/core/lib/iomgr/combiner.cc b/src/core/lib/iomgr/combiner.cc index 9429842eb8..6789e4d12d 100644 --- a/src/core/lib/iomgr/combiner.cc +++ b/src/core/lib/iomgr/combiner.cc @@ -63,11 +63,12 @@ struct grpc_combiner { gpr_refcount refs; }; +static void combiner_run(grpc_closure* closure, grpc_error* error); static void combiner_exec(grpc_closure* closure, grpc_error* error); static void combiner_finally_exec(grpc_closure* closure, grpc_error* error); static const grpc_closure_scheduler_vtable scheduler = { - combiner_exec, combiner_exec, "combiner:immediately"}; + combiner_run, combiner_exec, "combiner:immediately"}; static const grpc_closure_scheduler_vtable finally_scheduler = { combiner_finally_exec, combiner_finally_exec, "combiner:finally"}; @@ -343,6 +344,22 @@ static void combiner_finally_exec(grpc_closure* closure, grpc_error* error) { grpc_closure_list_append(&lock->final_list, closure, error); } +static void combiner_run(grpc_closure* closure, grpc_error* error) { + grpc_combiner* lock = COMBINER_FROM_CLOSURE_SCHEDULER(closure, scheduler); +#ifndef NDEBUG + closure->scheduled = false; + GRPC_COMBINER_TRACE(gpr_log( + GPR_DEBUG, + "Combiner:%p grpc_combiner_run closure:%p created [%s:%d] run [%s:%d]", + lock, closure, closure->file_created, closure->line_created, + closure->file_initiated, closure->line_initiated)); +#endif + GPR_ASSERT(grpc_core::ExecCtx::Get()->combiner_data()->active_combiner == + lock); + closure->cb(closure->cb_arg, error); + GRPC_ERROR_UNREF(error); +} + static void enqueue_finally(void* closure, grpc_error* error) { combiner_finally_exec(static_cast<grpc_closure*>(closure), GRPC_ERROR_REF(error)); diff --git a/src/core/lib/iomgr/resource_quota.cc b/src/core/lib/iomgr/resource_quota.cc index 8cf4fe9928..539bc120ce 100644 --- a/src/core/lib/iomgr/resource_quota.cc +++ b/src/core/lib/iomgr/resource_quota.cc @@ -386,7 +386,7 @@ static bool rq_reclaim(grpc_resource_quota* resource_quota, bool destructive) { resource_quota->debug_only_last_reclaimer_resource_user = resource_user; resource_quota->debug_only_last_initiated_reclaimer = c; resource_user->reclaimers[destructive] = nullptr; - GRPC_CLOSURE_RUN(c, GRPC_ERROR_NONE); + GRPC_CLOSURE_SCHED(c, GRPC_ERROR_NONE); return true; } diff --git a/src/core/lib/iomgr/tcp_custom.cc b/src/core/lib/iomgr/tcp_custom.cc index b3b2934014..990e8d632b 100644 --- a/src/core/lib/iomgr/tcp_custom.cc +++ b/src/core/lib/iomgr/tcp_custom.cc @@ -141,7 +141,7 @@ static void call_read_cb(custom_tcp_endpoint* tcp, grpc_error* error) { TCP_UNREF(tcp, "read"); tcp->read_slices = nullptr; tcp->read_cb = nullptr; - GRPC_CLOSURE_RUN(cb, error); + GRPC_CLOSURE_SCHED(cb, error); } static void custom_read_callback(grpc_custom_socket* socket, size_t nread, diff --git a/src/core/lib/iomgr/tcp_posix.cc b/src/core/lib/iomgr/tcp_posix.cc index 153be05e83..b79ffe20f1 100644 --- a/src/core/lib/iomgr/tcp_posix.cc +++ b/src/core/lib/iomgr/tcp_posix.cc @@ -366,7 +366,7 @@ static void call_read_cb(grpc_tcp* tcp, grpc_error* error) { tcp->read_cb = nullptr; tcp->incoming_buffer = nullptr; - GRPC_CLOSURE_RUN(cb, error); + GRPC_CLOSURE_SCHED(cb, error); } #define MAX_READ_IOVEC 4 @@ -629,7 +629,7 @@ static void tcp_handle_write(void* arg /* grpc_tcp */, grpc_error* error) { gpr_log(GPR_INFO, "write: %s", str); } - GRPC_CLOSURE_RUN(cb, error); + GRPC_CLOSURE_SCHED(cb, error); TCP_UNREF(tcp, "write"); } } diff --git a/src/core/lib/surface/call.cc b/src/core/lib/surface/call.cc index da488034ca..7ed1696f80 100644 --- a/src/core/lib/surface/call.cc +++ b/src/core/lib/surface/call.cc @@ -1259,8 +1259,12 @@ static void post_batch_completion(batch_control* bctl) { if (bctl->completion_data.notify_tag.is_closure) { /* unrefs bctl->error */ bctl->call = nullptr; - GRPC_CLOSURE_RUN((grpc_closure*)bctl->completion_data.notify_tag.tag, - error); + /* This closure may be meant to be run within some combiner. Since we aren't + * running in any combiner here, we need to use GRPC_CLOSURE_SCHED instead + * of GRPC_CLOSURE_RUN. + */ + GRPC_CLOSURE_SCHED((grpc_closure*)bctl->completion_data.notify_tag.tag, + error); GRPC_CALL_INTERNAL_UNREF(call, "completion"); } else { /* unrefs bctl->error */ diff --git a/src/core/lib/surface/init.cc b/src/core/lib/surface/init.cc index a14f77e346..16be81e9c2 100644 --- a/src/core/lib/surface/init.cc +++ b/src/core/lib/surface/init.cc @@ -27,7 +27,7 @@ #include <grpc/support/log.h> #include <grpc/support/time.h> #include "src/core/lib/channel/channel_stack.h" -#include "src/core/lib/channel/channel_trace_registry.h" +#include "src/core/lib/channel/channelz_registry.h" #include "src/core/lib/channel/connected_channel.h" #include "src/core/lib/channel/handshaker_registry.h" #include "src/core/lib/debug/stats.h" @@ -127,7 +127,7 @@ void grpc_init(void) { grpc_slice_intern_init(); grpc_mdctx_global_init(); grpc_channel_init_init(); - grpc_channel_trace_registry_init(); + grpc_core::ChannelzRegistry::Init(); grpc_security_pre_init(); grpc_core::ExecCtx::GlobalInit(); grpc_iomgr_init(); @@ -176,7 +176,7 @@ void grpc_shutdown(void) { grpc_mdctx_global_shutdown(); grpc_handshaker_factory_registry_shutdown(); grpc_slice_intern_shutdown(); - grpc_channel_trace_registry_shutdown(); + grpc_core::ChannelzRegistry::Shutdown(); grpc_stats_shutdown(); grpc_core::Fork::GlobalShutdown(); } diff --git a/src/php/ext/grpc/call.c b/src/php/ext/grpc/call.c index 9a1dcb640b..a794226298 100644 --- a/src/php/ext/grpc/call.c +++ b/src/php/ext/grpc/call.c @@ -30,9 +30,7 @@ #include "byte_buffer.h" zend_class_entry *grpc_ce_call; -#if PHP_MAJOR_VERSION >= 7 -static zend_object_handlers call_ce_handlers; -#endif +PHP_GRPC_DECLARE_OBJECT_HANDLER(call_ce_handlers) /* Frees and destroys an instance of wrapped_grpc_call */ PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_call) @@ -182,7 +180,8 @@ zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) { zval *call_object; PHP_GRPC_MAKE_STD_ZVAL(call_object); object_init_ex(call_object, grpc_ce_call); - wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(call_object); + wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call, + call_object); call->wrapped = wrapped; call->owned = owned; return call_object; @@ -203,7 +202,8 @@ PHP_METHOD(Call, __construct) { zval *deadline_obj; char *host_override = NULL; php_grpc_int host_override_len = 0; - wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); + wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call, + getThis()); /* "OsO|s" == 1 Object, 1 string, 1 Object, 1 optional string */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OsO|s", &channel_obj, @@ -215,7 +215,8 @@ PHP_METHOD(Call, __construct) { "an optional String", 1 TSRMLS_CC); return; } - wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(channel_obj); + wrapped_grpc_channel *channel = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, channel_obj); gpr_mu_lock(&channel->wrapper->mu); if (channel->wrapper == NULL || channel->wrapper->wrapped == NULL) { zend_throw_exception(spl_ce_InvalidArgumentException, @@ -225,7 +226,8 @@ PHP_METHOD(Call, __construct) { return; } add_property_zval(getThis(), "channel", channel_obj); - wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj); + wrapped_grpc_timeval *deadline = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, deadline_obj); grpc_slice method_slice = grpc_slice_from_copied_string(method); grpc_slice host_slice = host_override != NULL ? grpc_slice_from_copied_string(host_override) : grpc_empty_slice(); @@ -257,7 +259,8 @@ PHP_METHOD(Call, startBatch) { zval *inner_value; zval *message_value; zval *message_flags; - wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); + wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call, + getThis()); if (call->channel) { // startBatch in gRPC PHP server doesn't have channel in it. if (call->channel->wrapper == NULL || @@ -539,7 +542,8 @@ cleanup: * @return string The URI of the endpoint */ PHP_METHOD(Call, getPeer) { - wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); + wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call, + getThis()); char *peer = grpc_call_get_peer(call->wrapped); PHP_GRPC_RETVAL_STRING(peer, 1); gpr_free(peer); @@ -551,7 +555,8 @@ PHP_METHOD(Call, getPeer) { * @return void */ PHP_METHOD(Call, cancel) { - wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); + wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call, + getThis()); grpc_call_cancel(call->wrapped, NULL); } @@ -573,8 +578,9 @@ PHP_METHOD(Call, setCredentials) { } wrapped_grpc_call_credentials *creds = - Z_WRAPPED_GRPC_CALL_CREDS_P(creds_obj); - wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call_credentials, creds_obj); + wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call, + getThis()); grpc_call_error error = GRPC_CALL_ERROR; error = grpc_call_set_credentials(call->wrapped, creds->wrapped); diff --git a/src/php/ext/grpc/call.h b/src/php/ext/grpc/call.h index c07fb9ba69..7abc8fa0e8 100644 --- a/src/php/ext/grpc/call.h +++ b/src/php/ext/grpc/call.h @@ -33,23 +33,12 @@ PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_call) wrapped_grpc_channel* channel; PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_call) -#if PHP_MAJOR_VERSION < 7 - -#define Z_WRAPPED_GRPC_CALL_P(zv) \ - (wrapped_grpc_call *)zend_object_store_get_object(zv TSRMLS_CC) - -#else - static inline wrapped_grpc_call *wrapped_grpc_call_from_obj(zend_object *obj) { return (wrapped_grpc_call*)((char*)(obj) - XtOffsetOf(wrapped_grpc_call, std)); } -#define Z_WRAPPED_GRPC_CALL_P(zv) wrapped_grpc_call_from_obj(Z_OBJ_P((zv))) - -#endif /* PHP_MAJOR_VERSION */ - /* Creates and returns a PHP associative array of metadata from a C array of * call metadata */ zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC); diff --git a/src/php/ext/grpc/call_credentials.c b/src/php/ext/grpc/call_credentials.c index 12dcd46b21..fabb12a751 100644 --- a/src/php/ext/grpc/call_credentials.c +++ b/src/php/ext/grpc/call_credentials.c @@ -27,9 +27,7 @@ #include "call.h" zend_class_entry *grpc_ce_call_credentials; -#if PHP_MAJOR_VERSION >= 7 -static zend_object_handlers call_credentials_ce_handlers; -#endif +PHP_GRPC_DECLARE_OBJECT_HANDLER(call_credentials_ce_handlers) /* Frees and destroys an instance of wrapped_grpc_call_credentials */ PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_call_credentials) @@ -55,7 +53,8 @@ zval *grpc_php_wrap_call_credentials(grpc_call_credentials PHP_GRPC_MAKE_STD_ZVAL(credentials_object); object_init_ex(credentials_object, grpc_ce_call_credentials); wrapped_grpc_call_credentials *credentials = - Z_WRAPPED_GRPC_CALL_CREDS_P(credentials_object); + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call_credentials, + credentials_object); credentials->wrapped = wrapped; return credentials_object; } @@ -80,12 +79,12 @@ PHP_METHOD(CallCredentials, createComposite) { return; } wrapped_grpc_call_credentials *cred1 = - Z_WRAPPED_GRPC_CALL_CREDS_P(cred1_obj); + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call_credentials, cred1_obj); wrapped_grpc_call_credentials *cred2 = - Z_WRAPPED_GRPC_CALL_CREDS_P(cred2_obj); + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call_credentials, cred2_obj); grpc_call_credentials *creds = - grpc_composite_call_credentials_create(cred1->wrapped, cred2->wrapped, - NULL); + grpc_composite_call_credentials_create(cred1->wrapped, cred2->wrapped, + NULL); zval *creds_object = grpc_php_wrap_call_credentials(creds TSRMLS_CC); RETURN_DESTROY_ZVAL(creds_object); } diff --git a/src/php/ext/grpc/call_credentials.h b/src/php/ext/grpc/call_credentials.h index 1db90b96b2..e0cc863853 100644 --- a/src/php/ext/grpc/call_credentials.h +++ b/src/php/ext/grpc/call_credentials.h @@ -32,24 +32,12 @@ PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_call_credentials) grpc_call_credentials *wrapped; PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_call_credentials) -#if PHP_MAJOR_VERSION < 7 - -#define Z_WRAPPED_GRPC_CALL_CREDS_P(zv) \ - (wrapped_grpc_call_credentials *)zend_object_store_get_object(zv TSRMLS_CC) - -#else - static inline wrapped_grpc_call_credentials *wrapped_grpc_call_credentials_from_obj(zend_object *obj) { return (wrapped_grpc_call_credentials*)( (char*)(obj) - XtOffsetOf(wrapped_grpc_call_credentials, std)); } -#define Z_WRAPPED_GRPC_CALL_CREDS_P(zv) \ - wrapped_grpc_call_credentials_from_obj(Z_OBJ_P((zv))) - -#endif /* PHP_MAJOR_VERSION */ - /* Struct to hold callback function for plugin creds API */ typedef struct plugin_state { zend_fcall_info *fci; diff --git a/src/php/ext/grpc/channel.c b/src/php/ext/grpc/channel.c index dc17e05310..b17f3d9a61 100644 --- a/src/php/ext/grpc/channel.c +++ b/src/php/ext/grpc/channel.c @@ -36,9 +36,7 @@ #include "timeval.h" zend_class_entry *grpc_ce_channel; -#if PHP_MAJOR_VERSION >= 7 -static zend_object_handlers channel_ce_handlers; -#endif +PHP_GRPC_DECLARE_OBJECT_HANDLER(channel_ce_handlers) static gpr_mu global_persistent_list_mu; int le_plink; int le_bound; @@ -297,7 +295,8 @@ void create_and_add_channel_to_persistent_list( * @param array $args_array The arguments to pass to the Channel */ PHP_METHOD(Channel, __construct) { - wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); + wrapped_grpc_channel *channel = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis()); zval *creds_obj = NULL; char *target; php_grpc_int target_length; @@ -330,7 +329,8 @@ PHP_METHOD(Channel, __construct) { 1 TSRMLS_CC); return; } else { - creds = Z_WRAPPED_GRPC_CHANNEL_CREDS_P(creds_obj); + creds = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel_credentials, + creds_obj); php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials")); } } @@ -440,7 +440,8 @@ PHP_METHOD(Channel, __construct) { * @return string The URI of the endpoint */ PHP_METHOD(Channel, getTarget) { - wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); + wrapped_grpc_channel *channel = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis()); if (channel->wrapper == NULL) { zend_throw_exception(spl_ce_RuntimeException, "getTarget error." @@ -460,7 +461,8 @@ PHP_METHOD(Channel, getTarget) { * @return long The grpc connectivity state */ PHP_METHOD(Channel, getConnectivityState) { - wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); + wrapped_grpc_channel *channel = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis()); if (channel->wrapper == NULL) { zend_throw_exception(spl_ce_RuntimeException, "getConnectivityState error." @@ -491,7 +493,8 @@ PHP_METHOD(Channel, getConnectivityState) { * before deadline */ PHP_METHOD(Channel, watchConnectivityState) { - wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); + wrapped_grpc_channel *channel = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis()); if (channel->wrapper == NULL) { zend_throw_exception(spl_ce_RuntimeException, "watchConnectivityState error" @@ -513,7 +516,8 @@ PHP_METHOD(Channel, watchConnectivityState) { return; } - wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj); + wrapped_grpc_timeval *deadline = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, deadline_obj); grpc_channel_watch_connectivity_state(channel->wrapper->wrapped, (grpc_connectivity_state)last_state, deadline->wrapped, completion_queue, @@ -530,7 +534,8 @@ PHP_METHOD(Channel, watchConnectivityState) { * @return void */ PHP_METHOD(Channel, close) { - wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); + wrapped_grpc_channel *channel = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis()); if (channel->wrapper != NULL) { php_grpc_channel_unref(channel->wrapper); channel->wrapper = NULL; @@ -608,7 +613,8 @@ char *grpc_connectivity_state_name(grpc_connectivity_state state) { * @return array */ PHP_METHOD(Channel, getChannelInfo) { - wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); + wrapped_grpc_channel *channel = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis()); array_init(return_value); // Info about the target PHP_GRPC_ADD_STRING_TO_ARRAY(return_value, "target", diff --git a/src/php/ext/grpc/channel.h b/src/php/ext/grpc/channel.h index c0bd8c7daa..27752c9a3f 100644 --- a/src/php/ext/grpc/channel.h +++ b/src/php/ext/grpc/channel.h @@ -39,24 +39,12 @@ PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_channel) grpc_channel_wrapper *wrapper; PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_channel) -#if PHP_MAJOR_VERSION < 7 - -#define Z_WRAPPED_GRPC_CHANNEL_P(zv) \ - (wrapped_grpc_channel *)zend_object_store_get_object(zv TSRMLS_CC) - -#else - static inline wrapped_grpc_channel *wrapped_grpc_channel_from_obj(zend_object *obj) { return (wrapped_grpc_channel*)((char*)(obj) - XtOffsetOf(wrapped_grpc_channel, std)); } -#define Z_WRAPPED_GRPC_CHANNEL_P(zv) \ - wrapped_grpc_channel_from_obj(Z_OBJ_P((zv))) - -#endif /* PHP_MAJOR_VERSION */ - /* Initializes the Channel class */ GRPC_STARTUP_FUNCTION(channel); diff --git a/src/php/ext/grpc/channel_credentials.c b/src/php/ext/grpc/channel_credentials.c index 8bda64cf41..10d7380ca1 100644 --- a/src/php/ext/grpc/channel_credentials.c +++ b/src/php/ext/grpc/channel_credentials.c @@ -29,9 +29,7 @@ #include "channel.h" zend_class_entry *grpc_ce_channel_credentials; -#if PHP_MAJOR_VERSION >= 7 -static zend_object_handlers channel_credentials_ce_handlers; -#endif +PHP_GRPC_DECLARE_OBJECT_HANDLER(channel_credentials_ce_handlers) static char *default_pem_root_certs = NULL; static grpc_ssl_roots_override_result get_ssl_roots_override( @@ -74,7 +72,8 @@ zval *grpc_php_wrap_channel_credentials(grpc_channel_credentials *wrapped, PHP_GRPC_MAKE_STD_ZVAL(credentials_object); object_init_ex(credentials_object, grpc_ce_channel_credentials); wrapped_grpc_channel_credentials *credentials = - Z_WRAPPED_GRPC_CHANNEL_CREDS_P(credentials_object); + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel_credentials, + credentials_object); credentials->wrapped = wrapped; credentials->hashstr = hashstr; credentials->has_call_creds = has_call_creds; @@ -187,20 +186,19 @@ PHP_METHOD(ChannelCredentials, createComposite) { return; } wrapped_grpc_channel_credentials *cred1 = - Z_WRAPPED_GRPC_CHANNEL_CREDS_P(cred1_obj); + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel_credentials, cred1_obj); wrapped_grpc_call_credentials *cred2 = - Z_WRAPPED_GRPC_CALL_CREDS_P(cred2_obj); + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call_credentials, cred2_obj); grpc_channel_credentials *creds = - grpc_composite_channel_credentials_create(cred1->wrapped, cred2->wrapped, - NULL); + grpc_composite_channel_credentials_create(cred1->wrapped, cred2->wrapped, + NULL); // wrapped_grpc_channel_credentials object should keeps it's own // allocation. Otherwise it conflicts free hashstr with call.c. php_grpc_int cred1_len = strlen(cred1->hashstr); char *cred1_hashstr = malloc(cred1_len+1); strcpy(cred1_hashstr, cred1->hashstr); zval *creds_object = - grpc_php_wrap_channel_credentials(creds, cred1_hashstr, true - TSRMLS_CC); + grpc_php_wrap_channel_credentials(creds, cred1_hashstr, true TSRMLS_CC); RETURN_DESTROY_ZVAL(creds_object); } diff --git a/src/php/ext/grpc/channel_credentials.h b/src/php/ext/grpc/channel_credentials.h index ef312be13f..7c6cf30367 100644 --- a/src/php/ext/grpc/channel_credentials.h +++ b/src/php/ext/grpc/channel_credentials.h @@ -34,24 +34,12 @@ PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_channel_credentials) zend_bool has_call_creds; PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_channel_credentials) -#if PHP_MAJOR_VERSION < 7 - -#define Z_WRAPPED_GRPC_CHANNEL_CREDS_P(zv) \ - (wrapped_grpc_channel_credentials *)zend_object_store_get_object(zv TSRMLS_CC) - -#else - static inline wrapped_grpc_channel_credentials *wrapped_grpc_channel_credentials_from_obj(zend_object *obj) { return (wrapped_grpc_channel_credentials *)( (char*)(obj) - XtOffsetOf(wrapped_grpc_channel_credentials, std)); } -#define Z_WRAPPED_GRPC_CHANNEL_CREDS_P(zv) \ - wrapped_grpc_channel_credentials_from_obj(Z_OBJ_P((zv))) - -#endif /* PHP_MAJOR_VERSION */ - /* Initializes the ChannelCredentials PHP class */ void grpc_init_channel_credentials(TSRMLS_D); diff --git a/src/php/ext/grpc/php7_wrapper.h b/src/php/ext/grpc/php7_wrapper.h index 3b95fbe451..5afe656062 100644 --- a/src/php/ext/grpc/php7_wrapper.h +++ b/src/php/ext/grpc/php7_wrapper.h @@ -148,6 +148,11 @@ static inline int php_grpc_zend_hash_find(HashTable *ht, char *key, int len, #define PHP_GRPC_INIT_HANDLER(class_object, handler_name) +#define PHP_GRPC_DECLARE_OBJECT_HANDLER(handler_name) + +#define PHP_GRPC_GET_WRAPPED_OBJECT(class_object, zv) \ + (class_object *)zend_object_store_get_object(zv TSRMLS_CC) + #else #define php_grpc_int size_t @@ -272,6 +277,12 @@ static inline int php_grpc_zend_hash_del(HashTable *ht, char *key, int len) { handler_name.offset = XtOffsetOf(class_object, std); \ handler_name.free_obj = free_##class_object +#define PHP_GRPC_DECLARE_OBJECT_HANDLER(handler_name) \ + static zend_object_handlers handler_name; + +#define PHP_GRPC_GET_WRAPPED_OBJECT(class_object, zv) \ + class_object##_from_obj(Z_OBJ_P((zv))) + #endif /* PHP_MAJOR_VERSION */ #endif /* PHP7_WRAPPER_GRPC_H */ diff --git a/src/php/ext/grpc/server.c b/src/php/ext/grpc/server.c index ece7b0048a..cb7b188b0e 100644 --- a/src/php/ext/grpc/server.c +++ b/src/php/ext/grpc/server.c @@ -32,9 +32,7 @@ #include "timeval.h" zend_class_entry *grpc_ce_server; -#if PHP_MAJOR_VERSION >= 7 -static zend_object_handlers server_ce_handlers; -#endif +PHP_GRPC_DECLARE_OBJECT_HANDLER(server_ce_handlers) /* Frees and destroys an instance of wrapped_grpc_server */ PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_server) @@ -62,7 +60,8 @@ php_grpc_zend_object create_wrapped_grpc_server(zend_class_entry *class_type * @param array $args_array The arguments to pass to the server (optional) */ PHP_METHOD(Server, __construct) { - wrapped_grpc_server *server = Z_WRAPPED_GRPC_SERVER_P(getThis()); + wrapped_grpc_server *server = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_server, getThis()); zval *args_array = NULL; grpc_channel_args args; @@ -70,8 +69,7 @@ PHP_METHOD(Server, __construct) { if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a", &args_array) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, - "Server expects an array", - 1 TSRMLS_CC); + "Server expects an array", 1 TSRMLS_CC); return; } if (args_array == NULL) { @@ -96,7 +94,8 @@ PHP_METHOD(Server, requestCall) { grpc_metadata_array metadata; grpc_event event; - wrapped_grpc_server *server = Z_WRAPPED_GRPC_SERVER_P(getThis()); + wrapped_grpc_server *server = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_server, getThis()); zval *result; PHP_GRPC_MAKE_STD_ZVAL(result); object_init(result); @@ -147,7 +146,8 @@ PHP_METHOD(Server, requestCall) { PHP_METHOD(Server, addHttp2Port) { const char *addr; php_grpc_int addr_len; - wrapped_grpc_server *server = Z_WRAPPED_GRPC_SERVER_P(getThis()); + wrapped_grpc_server *server = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_server, getThis()); /* "s" == 1 string */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &addr, &addr_len) @@ -169,7 +169,8 @@ PHP_METHOD(Server, addSecureHttp2Port) { const char *addr; php_grpc_int addr_len; zval *creds_obj; - wrapped_grpc_server *server = Z_WRAPPED_GRPC_SERVER_P(getThis()); + wrapped_grpc_server *server = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_server, getThis()); /* "sO" == 1 string, 1 object */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sO", &addr, &addr_len, @@ -181,7 +182,7 @@ PHP_METHOD(Server, addSecureHttp2Port) { return; } wrapped_grpc_server_credentials *creds = - Z_WRAPPED_GRPC_SERVER_CREDS_P(creds_obj); + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_server_credentials, creds_obj); RETURN_LONG(grpc_server_add_secure_http2_port(server->wrapped, addr, creds->wrapped)); } @@ -191,7 +192,8 @@ PHP_METHOD(Server, addSecureHttp2Port) { * @return void */ PHP_METHOD(Server, start) { - wrapped_grpc_server *server = Z_WRAPPED_GRPC_SERVER_P(getThis()); + wrapped_grpc_server *server = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_server, getThis()); grpc_server_start(server->wrapped); } diff --git a/src/php/ext/grpc/server.h b/src/php/ext/grpc/server.h index ca70589cfa..fb1030ba32 100644 --- a/src/php/ext/grpc/server.h +++ b/src/php/ext/grpc/server.h @@ -29,23 +29,12 @@ PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_server) grpc_server *wrapped; PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_server) -#if PHP_MAJOR_VERSION < 7 - -#define Z_WRAPPED_GRPC_SERVER_P(zv) \ - (wrapped_grpc_server *)zend_object_store_get_object(zv TSRMLS_CC) - -#else - static inline wrapped_grpc_server *wrapped_grpc_server_from_obj(zend_object *obj) { return (wrapped_grpc_server*)((char*)(obj) - XtOffsetOf(wrapped_grpc_server, std)); } -#define Z_WRAPPED_GRPC_SERVER_P(zv) wrapped_grpc_server_from_obj(Z_OBJ_P((zv))) - -#endif /* PHP_MAJOR_VERSION */ - /* Initializes the Server class */ void grpc_init_server(TSRMLS_D); diff --git a/src/php/ext/grpc/server_credentials.c b/src/php/ext/grpc/server_credentials.c index d143217f04..72ce50fff7 100644 --- a/src/php/ext/grpc/server_credentials.c +++ b/src/php/ext/grpc/server_credentials.c @@ -22,9 +22,7 @@ #include <zend_exceptions.h> zend_class_entry *grpc_ce_server_credentials; -#if PHP_MAJOR_VERSION >= 7 -static zend_object_handlers server_credentials_ce_handlers; -#endif +PHP_GRPC_DECLARE_OBJECT_HANDLER(server_credentials_ce_handlers) /* Frees and destroys an instace of wrapped_grpc_server_credentials */ PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_server_credentials) @@ -50,7 +48,8 @@ zval *grpc_php_wrap_server_credentials(grpc_server_credentials PHP_GRPC_MAKE_STD_ZVAL(server_credentials_object); object_init_ex(server_credentials_object, grpc_ce_server_credentials); wrapped_grpc_server_credentials *server_credentials = - Z_WRAPPED_GRPC_SERVER_CREDS_P(server_credentials_object); + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_server_credentials, + server_credentials_object); server_credentials->wrapped = wrapped; return server_credentials_object; } diff --git a/src/php/ext/grpc/server_credentials.h b/src/php/ext/grpc/server_credentials.h index 68e505ca1e..24ff88a05e 100644 --- a/src/php/ext/grpc/server_credentials.h +++ b/src/php/ext/grpc/server_credentials.h @@ -40,24 +40,12 @@ PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_server_credentials) grpc_server_credentials *wrapped; PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_server_credentials) -#if PHP_MAJOR_VERSION < 7 - -#define Z_WRAPPED_GRPC_SERVER_CREDS_P(zv) \ - (wrapped_grpc_server_credentials *)zend_object_store_get_object(zv TSRMLS_CC) - -#else - static inline wrapped_grpc_server_credentials *wrapped_grpc_server_credentials_from_obj(zend_object *obj) { return (wrapped_grpc_server_credentials*)( (char*)(obj) - XtOffsetOf(wrapped_grpc_server_credentials, std)); } -#define Z_WRAPPED_GRPC_SERVER_CREDS_P(zv) \ - wrapped_grpc_server_credentials_from_obj(Z_OBJ_P((zv))) - -#endif /* PHP_MAJOR_VERSION */ - /* Initializes the Server_Credentials PHP class */ void grpc_init_server_credentials(TSRMLS_D); diff --git a/src/php/ext/grpc/timeval.c b/src/php/ext/grpc/timeval.c index 77a5ffa256..8f0048def6 100644 --- a/src/php/ext/grpc/timeval.c +++ b/src/php/ext/grpc/timeval.c @@ -22,9 +22,7 @@ #include <zend_exceptions.h> zend_class_entry *grpc_ce_timeval; -#if PHP_MAJOR_VERSION >= 7 -static zend_object_handlers timeval_ce_handlers; -#endif +PHP_GRPC_DECLARE_OBJECT_HANDLER(timeval_ce_handlers) /* Frees and destroys an instance of wrapped_grpc_call */ PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_timeval) @@ -44,7 +42,8 @@ zval *grpc_php_wrap_timeval(gpr_timespec wrapped TSRMLS_DC) { zval *timeval_object; PHP_GRPC_MAKE_STD_ZVAL(timeval_object); object_init_ex(timeval_object, grpc_ce_timeval); - wrapped_grpc_timeval *timeval = Z_WRAPPED_GRPC_TIMEVAL_P(timeval_object); + wrapped_grpc_timeval *timeval = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, timeval_object); memcpy(&timeval->wrapped, &wrapped, sizeof(gpr_timespec)); return timeval_object; } @@ -54,7 +53,8 @@ zval *grpc_php_wrap_timeval(gpr_timespec wrapped TSRMLS_DC) { * @param long $microseconds The number of microseconds in the interval */ PHP_METHOD(Timeval, __construct) { - wrapped_grpc_timeval *timeval = Z_WRAPPED_GRPC_TIMEVAL_P(getThis()); + wrapped_grpc_timeval *timeval = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, getThis()); php_grpc_long microseconds; /* "l" == 1 long */ @@ -84,8 +84,10 @@ PHP_METHOD(Timeval, add) { "add expects a Timeval", 1 TSRMLS_CC); return; } - wrapped_grpc_timeval *self = Z_WRAPPED_GRPC_TIMEVAL_P(getThis()); - wrapped_grpc_timeval *other = Z_WRAPPED_GRPC_TIMEVAL_P(other_obj); + wrapped_grpc_timeval *self = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, getThis()); + wrapped_grpc_timeval *other = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, other_obj); zval *sum = grpc_php_wrap_timeval(gpr_time_add(self->wrapped, other->wrapped) TSRMLS_CC); @@ -108,8 +110,10 @@ PHP_METHOD(Timeval, subtract) { "subtract expects a Timeval", 1 TSRMLS_CC); return; } - wrapped_grpc_timeval *self = Z_WRAPPED_GRPC_TIMEVAL_P(getThis()); - wrapped_grpc_timeval *other = Z_WRAPPED_GRPC_TIMEVAL_P(other_obj); + wrapped_grpc_timeval *self = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, getThis()); + wrapped_grpc_timeval *other = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, other_obj); zval *diff = grpc_php_wrap_timeval(gpr_time_sub(self->wrapped, other->wrapped) TSRMLS_CC); @@ -135,8 +139,10 @@ PHP_METHOD(Timeval, compare) { "compare expects two Timevals", 1 TSRMLS_CC); return; } - wrapped_grpc_timeval *a = Z_WRAPPED_GRPC_TIMEVAL_P(a_obj); - wrapped_grpc_timeval *b = Z_WRAPPED_GRPC_TIMEVAL_P(b_obj); + wrapped_grpc_timeval *a = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, a_obj); + wrapped_grpc_timeval *b = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, b_obj); long result = gpr_time_cmp(a->wrapped, b->wrapped); RETURN_LONG(result); } @@ -161,9 +167,12 @@ PHP_METHOD(Timeval, similar) { "compare expects three Timevals", 1 TSRMLS_CC); return; } - wrapped_grpc_timeval *a = Z_WRAPPED_GRPC_TIMEVAL_P(a_obj); - wrapped_grpc_timeval *b = Z_WRAPPED_GRPC_TIMEVAL_P(b_obj); - wrapped_grpc_timeval *thresh = Z_WRAPPED_GRPC_TIMEVAL_P(thresh_obj); + wrapped_grpc_timeval *a = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, a_obj); + wrapped_grpc_timeval *b = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, b_obj); + wrapped_grpc_timeval *thresh = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, thresh_obj); int result = gpr_time_similar(a->wrapped, b->wrapped, thresh->wrapped); RETURN_BOOL(result); } @@ -212,7 +221,8 @@ PHP_METHOD(Timeval, infPast) { * @return void */ PHP_METHOD(Timeval, sleepUntil) { - wrapped_grpc_timeval *this = Z_WRAPPED_GRPC_TIMEVAL_P(getThis()); + wrapped_grpc_timeval *this = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, getThis()); gpr_sleep_until(this->wrapped); } diff --git a/src/php/ext/grpc/timeval.h b/src/php/ext/grpc/timeval.h index 23f5c1e971..6f8cc62264 100644 --- a/src/php/ext/grpc/timeval.h +++ b/src/php/ext/grpc/timeval.h @@ -29,24 +29,12 @@ PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_timeval) gpr_timespec wrapped; PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_timeval) -#if PHP_MAJOR_VERSION < 7 - -#define Z_WRAPPED_GRPC_TIMEVAL_P(zv) \ - (wrapped_grpc_timeval *)zend_object_store_get_object(zv TSRMLS_CC) - -#else - static inline wrapped_grpc_timeval *wrapped_grpc_timeval_from_obj(zend_object *obj) { return (wrapped_grpc_timeval*)((char*)(obj) - XtOffsetOf(wrapped_grpc_timeval, std)); } -#define Z_WRAPPED_GRPC_TIMEVAL_P(zv) \ - wrapped_grpc_timeval_from_obj(Z_OBJ_P((zv))) - -#endif /* PHP_MAJOR_VERSION */ - /* Initialize the Timeval PHP class */ void grpc_init_timeval(TSRMLS_D); diff --git a/src/proto/grpc/lb/v1/load_balancer.proto b/src/proto/grpc/lb/v1/load_balancer.proto index 75c916defa..3e0a4b2c97 100644 --- a/src/proto/grpc/lb/v1/load_balancer.proto +++ b/src/proto/grpc/lb/v1/load_balancer.proto @@ -62,8 +62,10 @@ message LoadBalanceRequest { } message InitialLoadBalanceRequest { - // Name of load balanced service (IE, balancer.service.com) - // length should be less than 256 bytes. + // The name of the load balanced service (e.g., balancer.service.com). The max + // length of the name is 256 bytes. + // The name might include a port number. How to handle the port number is up + // to the balancer. string name = 1; } diff --git a/src/python/grpcio/grpc/_channel.py b/src/python/grpcio/grpc/_channel.py index 8cc0e981ef..2017d47130 100644 --- a/src/python/grpcio/grpc/_channel.py +++ b/src/python/grpcio/grpc/_channel.py @@ -58,6 +58,17 @@ _STREAM_STREAM_INITIAL_DUE = ( _CHANNEL_SUBSCRIPTION_CALLBACK_ERROR_LOG_MESSAGE = ( 'Exception calling channel subscription callback!') +_OK_RENDEZVOUS_REPR_FORMAT = ('<_Rendezvous of RPC that terminated with:\n' + '\tstatus = {}\n' + '\tdetails = "{}"\n' + '>') + +_NON_OK_RENDEZVOUS_REPR_FORMAT = ('<_Rendezvous of RPC that terminated with:\n' + '\tstatus = {}\n' + '\tdetails = "{}"\n' + '\tdebug_error_string = "{}"\n' + '>') + def _deadline(timeout): return None if timeout is None else time.time() + timeout @@ -91,6 +102,7 @@ class _RPCState(object): self.trailing_metadata = trailing_metadata self.code = code self.details = details + self.debug_error_string = None # The semantics of grpc.Future.cancel and grpc.Future.cancelled are # slightly wonky, so they have to be tracked separately from the rest of the # result of the RPC. This field tracks whether cancellation was requested @@ -137,6 +149,7 @@ def _handle_event(event, state, response_deserializer): else: state.code = code state.details = batch_operation.details() + state.debug_error_string = batch_operation.error_string() callbacks.extend(state.callbacks) state.callbacks = None return callbacks @@ -374,13 +387,23 @@ class _Rendezvous(grpc.RpcError, grpc.Future, grpc.Call): self._state.condition.wait() return _common.decode(self._state.details) + def debug_error_string(self): + with self._state.condition: + while self._state.debug_error_string is None: + self._state.condition.wait() + return _common.decode(self._state.debug_error_string) + def _repr(self): with self._state.condition: if self._state.code is None: return '<_Rendezvous object of in-flight RPC>' + elif self._state.code is grpc.StatusCode.OK: + return _OK_RENDEZVOUS_REPR_FORMAT.format( + self._state.code, self._state.details) else: - return '<_Rendezvous of RPC that terminated with ({}, {})>'.format( - self._state.code, _common.decode(self._state.details)) + return _NON_OK_RENDEZVOUS_REPR_FORMAT.format( + self._state.code, self._state.details, + self._state.debug_error_string) def __repr__(self): return self._repr() diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi index a4c0319553..2d6c900c54 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi @@ -291,6 +291,7 @@ cdef extern from "grpc/grpc.h": grpc_metadata_array *trailing_metadata grpc_status_code *status grpc_slice *status_details + char** error_string ctypedef struct grpc_op_data_recv_close_on_server: int *cancelled diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/operation.pxd.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/operation.pxd.pxi index bfbe27785b..69a2a4989e 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/operation.pxd.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/operation.pxd.pxi @@ -91,9 +91,11 @@ cdef class ReceiveStatusOnClientOperation(Operation): cdef grpc_metadata_array _c_trailing_metadata cdef grpc_status_code _c_code cdef grpc_slice _c_details + cdef const char* _c_error_string cdef tuple _trailing_metadata cdef object _code cdef str _details + cdef str _error_string cdef void c(self) cdef void un_c(self) diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/operation.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/operation.pyx.pxi index 239d0f3f95..454627f570 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/operation.pyx.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/operation.pyx.pxi @@ -199,6 +199,8 @@ cdef class ReceiveStatusOnClientOperation(Operation): &self._c_code) self.c_op.data.receive_status_on_client.status_details = ( &self._c_details) + self.c_op.data.receive_status_on_client.error_string = ( + &self._c_error_string) cdef void un_c(self): self._trailing_metadata = _metadata(&self._c_trailing_metadata) @@ -206,6 +208,11 @@ cdef class ReceiveStatusOnClientOperation(Operation): self._code = self._c_code self._details = _decode(_slice_bytes(self._c_details)) grpc_slice_unref(self._c_details) + if self._c_error_string != NULL: + self._error_string = _decode(self._c_error_string) + gpr_free(<void*>self._c_error_string) + else: + self._error_string = "" def trailing_metadata(self): return self._trailing_metadata @@ -216,6 +223,9 @@ cdef class ReceiveStatusOnClientOperation(Operation): def details(self): return self._details + def error_string(self): + return self._error_string + cdef class ReceiveCloseOnServerOperation(Operation): diff --git a/src/python/grpcio/grpc_core_dependencies.py b/src/python/grpcio/grpc_core_dependencies.py index bf6c2534a8..f3557368d4 100644 --- a/src/python/grpcio/grpc_core_dependencies.py +++ b/src/python/grpcio/grpc_core_dependencies.py @@ -64,7 +64,7 @@ CORE_SOURCE_FILES = [ 'src/core/lib/channel/channel_stack.cc', 'src/core/lib/channel/channel_stack_builder.cc', 'src/core/lib/channel/channel_trace.cc', - 'src/core/lib/channel/channel_trace_registry.cc', + 'src/core/lib/channel/channelz_registry.cc', 'src/core/lib/channel/connected_channel.cc', 'src/core/lib/channel/handshaker.cc', 'src/core/lib/channel/handshaker_factory.cc', diff --git a/src/python/grpcio_tests/tests/unit/_rpc_test.py b/src/python/grpcio_tests/tests/unit/_rpc_test.py index 54f01d9f8d..34e7831a98 100644 --- a/src/python/grpcio_tests/tests/unit/_rpc_test.py +++ b/src/python/grpcio_tests/tests/unit/_rpc_test.py @@ -225,6 +225,7 @@ class RPCTest(unittest.TestCase): self.assertEqual(expected_response, response) self.assertIs(grpc.StatusCode.OK, call.code()) + self.assertEqual("", call.debug_error_string()) def testSuccessfulUnaryRequestFutureUnaryResponse(self): request = b'\x07\x08' @@ -706,6 +707,13 @@ class RPCTest(unittest.TestCase): self.assertIs(grpc.StatusCode.UNKNOWN, exception_context.exception.code()) + # sanity checks on to make sure returned string contains default members + # of the error + debug_error_string = exception_context.exception.debug_error_string() + self.assertIn("created", debug_error_string) + self.assertIn("description", debug_error_string) + self.assertIn("file", debug_error_string) + self.assertIn("file_line", debug_error_string) def testFailedUnaryRequestFutureUnaryResponse(self): request = b'\x37\x17' diff --git a/src/ruby/bin/math_client.rb b/src/ruby/bin/math_client.rb index 15120c0c0d..66717d8bf3 100755 --- a/src/ruby/bin/math_client.rb +++ b/src/ruby/bin/math_client.rb @@ -27,15 +27,26 @@ $LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir) require 'grpc' require 'math_services_pb' require 'optparse' +require 'logger' include GRPC::Core::TimeConsts +module StdoutLogger + def logger + LOGGER + end + + LOGGER = Logger.new(STDOUT) +end + +GRPC.extend(StdoutLogger) + def do_div(stub) GRPC.logger.info('request_response') GRPC.logger.info('----------------') req = Math::DivArgs.new(dividend: 7, divisor: 3) GRPC.logger.info("div(7/3): req=#{req.inspect}") - resp = stub.div(req, timeout: INFINITE_FUTURE) + resp = stub.div(req) GRPC.logger.info("Answer: #{resp.inspect}") GRPC.logger.info('----------------') end @@ -56,7 +67,7 @@ def do_fib(stub) GRPC.logger.info('----------------') req = Math::FibArgs.new(limit: 11) GRPC.logger.info("fib(11): req=#{req.inspect}") - resp = stub.fib(req, timeout: INFINITE_FUTURE) + resp = stub.fib(req) resp.each do |r| GRPC.logger.info("Answer: #{r.inspect}") end @@ -71,7 +82,7 @@ def do_div_many(stub) reqs << Math::DivArgs.new(dividend: 5, divisor: 2) reqs << Math::DivArgs.new(dividend: 7, divisor: 2) GRPC.logger.info("div(7/3), div(5/2), div(7/2): reqs=#{reqs.inspect}") - resp = stub.div_many(reqs, timeout: INFINITE_FUTURE) + resp = stub.div_many(reqs) resp.each do |r| GRPC.logger.info("Answer: #{r.inspect}") end @@ -107,19 +118,16 @@ def main # The Math::Math:: module occurs because the service has the same name as its # package. That practice should be avoided by defining real services. - - p options if options['secure'] stub_opts = { :creds => test_creds, - GRPC::Core::Channel::SSL_TARGET => 'foo.test.google.fr' + GRPC::Core::Channel::SSL_TARGET => 'foo.test.google.fr', + timeout: INFINITE_FUTURE, } - p stub_opts - p options['host'] stub = Math::Math::Stub.new(options['host'], **stub_opts) GRPC.logger.info("... connecting securely on #{options['host']}") else - stub = Math::Math::Stub.new(options['host']) + stub = Math::Math::Stub.new(options['host'], :this_channel_is_insecure, timeout: INFINITE_FUTURE) GRPC.logger.info("... connecting insecurely on #{options['host']}") end |