Make registry generic

pull/15341/head
ncteisen 7 years ago
parent 3be52f9bcc
commit 96bc3825a3
  1. 16
      src/core/lib/channel/channelz_registry.cc
  2. 26
      src/core/lib/channel/channelz_registry.h
  3. 3
      test/core/channel/channel_trace_test.cc

@ -68,26 +68,10 @@ ChannelzRegistry::~ChannelzRegistry() {
gpr_mu_destroy(&mu_); gpr_mu_destroy(&mu_);
} }
intptr_t ChannelzRegistry::Register(grpc_core::ChannelTrace* channel_trace) {
intptr_t prior = gpr_atm_no_barrier_fetch_add(&uuid_, 1);
gpr_mu_lock(&mu_);
avl_ = grpc_avl_add(avl_, (void*)prior, channel_trace, nullptr);
gpr_mu_unlock(&mu_);
return prior;
}
void ChannelzRegistry::Unregister(intptr_t uuid) { void ChannelzRegistry::Unregister(intptr_t uuid) {
gpr_mu_lock(&mu_); gpr_mu_lock(&mu_);
avl_ = grpc_avl_remove(avl_, (void*)uuid, nullptr); avl_ = grpc_avl_remove(avl_, (void*)uuid, nullptr);
gpr_mu_unlock(&mu_); gpr_mu_unlock(&mu_);
} }
grpc_core::ChannelTrace* ChannelzRegistry::Get(intptr_t uuid) {
gpr_mu_lock(&mu_);
grpc_core::ChannelTrace* ret = static_cast<grpc_core::ChannelTrace*>(
grpc_avl_get(avl_, (void*)uuid, nullptr));
gpr_mu_unlock(&mu_);
return ret;
}
} // namespace grpc_core } // namespace grpc_core

@ -45,13 +45,29 @@ class ChannelzRegistry {
// Returned the singleton instance of ChannelzRegistry; // Returned the singleton instance of ChannelzRegistry;
static ChannelzRegistry* Default(); static ChannelzRegistry* Default();
// globally registers a ChannelTrace. Returns its unique uuid // globally registers a channelz Object. Returns its unique uuid
intptr_t Register(grpc_core::ChannelTrace* channel_trace); template <typename Object>
// globally unregisters the ChannelTrace that is associated to uuid. intptr_t Register(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 Unregister(intptr_t uuid); void Unregister(intptr_t uuid);
// if object with uuid has previously been registered, returns the // if object with uuid has previously been registered, returns the
// ChannelTrace associated with that uuid. Else returns nullptr. // Object associated with that uuid. Else returns nullptr.
grpc_core::ChannelTrace* Get(intptr_t uuid); template <typename Object>
Object* Get(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: private:
GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW

@ -99,7 +99,8 @@ void ValidateTraceDataMatchedUuidLookup(RefCountedPtr<ChannelTrace> tracer) {
intptr_t uuid = tracer->GetUuid(); intptr_t uuid = tracer->GetUuid();
if (uuid == -1) return; // Doesn't make sense to lookup if tracing disabled if (uuid == -1) return; // Doesn't make sense to lookup if tracing disabled
char* tracer_json_str = tracer->RenderTrace(); char* tracer_json_str = tracer->RenderTrace();
ChannelTrace* uuid_lookup = ChannelzRegistry::Default()->Get(uuid); ChannelTrace* uuid_lookup =
ChannelzRegistry::Default()->Get<ChannelTrace>(uuid);
char* uuid_lookup_json_str = uuid_lookup->RenderTrace(); char* uuid_lookup_json_str = uuid_lookup->RenderTrace();
EXPECT_EQ(strcmp(tracer_json_str, uuid_lookup_json_str), 0); EXPECT_EQ(strcmp(tracer_json_str, uuid_lookup_json_str), 0);
gpr_free(tracer_json_str); gpr_free(tracer_json_str);

Loading…
Cancel
Save