mirror of https://github.com/grpc/grpc.git
commit
1517f80908
75 changed files with 2628 additions and 587 deletions
@ -0,0 +1,63 @@ |
||||
# gRPC Concepts Overview |
||||
|
||||
Remote Procedure Calls (RPCs) provide a useful abstraction for building |
||||
distributed applications and services. The libraries in this repository |
||||
provide a concrete implementation of the gRPC protocol, layered over HTTP/2. |
||||
These libraries enable communication between clients and servers using any |
||||
combination of the supported languages. |
||||
|
||||
|
||||
## Interface |
||||
|
||||
Developers using gRPC start with a language agnostic description of an RPC service (a collection |
||||
of methods). From this description, gRPC will generate client and server side interfaces |
||||
in any of the supported languages. The server implements |
||||
the service interface, which can be remotely invoked by the client interface. |
||||
|
||||
By default, gRPC uses [Protocol Buffers](https://github.com/google/protobuf) as the |
||||
Interface Definition Language (IDL) for describing both the service interface |
||||
and the structure of the payload messages. It is possible to use other |
||||
alternatives if desired. |
||||
|
||||
### Invoking & handling remote calls |
||||
Starting from an interface definition in a .proto file, gRPC provides |
||||
Protocol Compiler plugins that generate Client- and Server-side APIs. |
||||
gRPC users call into these APIs on the Client side and implement |
||||
the corresponding API on the server side. |
||||
|
||||
#### Synchronous vs. asynchronous |
||||
Synchronous RPC calls, that block until a response arrives from the server, are |
||||
the closest approximation to the abstraction of a procedure call that RPC |
||||
aspires to. |
||||
|
||||
On the other hand, networks are inherently asynchronous and in many scenarios, |
||||
it is desirable to have the ability to start RPCs without blocking the current |
||||
thread. |
||||
|
||||
The gRPC programming surface in most languages comes in both synchronous and |
||||
asynchronous flavors. |
||||
|
||||
|
||||
## Streaming |
||||
|
||||
gRPC supports streaming semantics, where either the client or the server (or both) |
||||
send a stream of messages on a single RPC call. The most general case is |
||||
Bidirectional Streaming where a single gRPC call establishes a stream in which both |
||||
the client and the server can send a stream of messages to each other. The streamed |
||||
messages are delivered in the order they were sent. |
||||
|
||||
|
||||
# Protocol |
||||
|
||||
The [gRPC protocol](doc/PROTOCOL-HTTP2.md) specifies the abstract requirements for communication between |
||||
clients and servers. A concrete embedding over HTTP/2 completes the picture by |
||||
fleshing out the details of each of the required operations. |
||||
|
||||
## Abstract gRPC protocol |
||||
A gRPC call comprises of a bidirectional stream of messages, initiated by the client. In the client-to-server direction, this stream begins with a mandatory `Call Header`, followed by optional `Initial-Metadata`, followed by zero or more `Payload Messages`. The server-to-client direction contains an optional `Initial-Metadata`, followed by zero or more `Payload Messages` terminated with a mandatory `Status` and optional `Status-Metadata` (a.k.a.,`Trailing-Metadata`). |
||||
|
||||
## Implementation over HTTP/2 |
||||
The abstract protocol defined above is implemented over [HTTP/2](https://http2.github.io/). gRPC bidirectional streams are mapped to HTTP/2 streams. The contents of `Call Header` and `Initial Metadata` are sent as HTTP/2 headers and subject to HPACK compression. `Payload Messages` are serialized into a byte stream of length prefixed gRPC frames which are then fragmented into HTTP/2 frames at the sender and reassembled at the receiver. `Status` and `Trailing-Metadata` are sent as HTTP/2 trailing headers (a.k.a., trailers). |
||||
|
||||
## Flow Control |
||||
gRPC uses the flow control mechanism in HTTP/2. This enables fine-grained control of memory used for buffering in-flight messages. |
@ -0,0 +1,6 @@ |
||||
# gRPC Basics: C++ sample code |
||||
|
||||
The files in this folder are the samples used in [gRPC Basics: C++][], |
||||
a detailed tutorial for using gRPC in C++. |
||||
|
||||
[gRPC Basics: C++]:https://grpc.io/docs/tutorials/basic/c.html |
@ -0,0 +1,185 @@ |
||||
/*
|
||||
* |
||||
* 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/channelz.h" |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.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" |
||||
#include "src/core/lib/gprpp/memory.h" |
||||
#include "src/core/lib/iomgr/error.h" |
||||
#include "src/core/lib/slice/slice_internal.h" |
||||
#include "src/core/lib/surface/channel.h" |
||||
#include "src/core/lib/transport/connectivity_state.h" |
||||
#include "src/core/lib/transport/error_utils.h" |
||||
|
||||
namespace grpc_core { |
||||
namespace channelz { |
||||
|
||||
namespace { |
||||
|
||||
// TODO(ncteisen): move this function to a common helper location.
|
||||
//
|
||||
// returns an allocated string that represents tm according to RFC-3339, and,
|
||||
// more specifically, follows:
|
||||
// https://developers.google.com/protocol-buffers/docs/proto3#json
|
||||
//
|
||||
// "Uses RFC 3339, where generated output will always be Z-normalized and uses
|
||||
// 0, 3, 6 or 9 fractional digits."
|
||||
char* fmt_time(gpr_timespec tm) { |
||||
char time_buffer[35]; |
||||
char ns_buffer[11]; // '.' + 9 digits of precision
|
||||
struct tm* tm_info = localtime((const time_t*)&tm.tv_sec); |
||||
strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%dT%H:%M:%S", tm_info); |
||||
snprintf(ns_buffer, 11, ".%09d", tm.tv_nsec); |
||||
// This loop trims off trailing zeros by inserting a null character that the
|
||||
// right point. We iterate in chunks of three because we want 0, 3, 6, or 9
|
||||
// fractional digits.
|
||||
for (int i = 7; i >= 1; i -= 3) { |
||||
if (ns_buffer[i] == '0' && ns_buffer[i + 1] == '0' && |
||||
ns_buffer[i + 2] == '0') { |
||||
ns_buffer[i] = '\0'; |
||||
// Edge case in which all fractional digits were 0.
|
||||
if (i == 1) { |
||||
ns_buffer[0] = '\0'; |
||||
} |
||||
} else { |
||||
break; |
||||
} |
||||
} |
||||
char* full_time_str; |
||||
gpr_asprintf(&full_time_str, "%s%sZ", time_buffer, ns_buffer); |
||||
return full_time_str; |
||||
} |
||||
|
||||
// TODO(ncteisen); move this to json library
|
||||
grpc_json* add_num_str(grpc_json* parent, grpc_json* it, const char* name, |
||||
int64_t num) { |
||||
char* num_str; |
||||
gpr_asprintf(&num_str, "%" PRId64, num); |
||||
return grpc_json_create_child(it, parent, name, num_str, GRPC_JSON_STRING, |
||||
true); |
||||
} |
||||
|
||||
} // namespace
|
||||
|
||||
ChannelNode::ChannelNode(grpc_channel* channel, size_t channel_tracer_max_nodes) |
||||
: channel_(channel), target_(nullptr), channel_uuid_(-1) { |
||||
trace_.Init(channel_tracer_max_nodes); |
||||
target_ = UniquePtr<char>(grpc_channel_get_target(channel_)); |
||||
channel_uuid_ = ChannelzRegistry::Register(this); |
||||
gpr_atm_no_barrier_store(&last_call_started_millis_, |
||||
(gpr_atm)ExecCtx::Get()->Now()); |
||||
} |
||||
|
||||
ChannelNode::~ChannelNode() { |
||||
trace_.Destroy(); |
||||
ChannelzRegistry::Unregister(channel_uuid_); |
||||
} |
||||
|
||||
void ChannelNode::RecordCallStarted() { |
||||
gpr_atm_no_barrier_fetch_add(&calls_started_, (gpr_atm)1); |
||||
gpr_atm_no_barrier_store(&last_call_started_millis_, |
||||
(gpr_atm)ExecCtx::Get()->Now()); |
||||
} |
||||
|
||||
grpc_connectivity_state ChannelNode::GetConnectivityState() { |
||||
if (channel_ == nullptr) { |
||||
return GRPC_CHANNEL_SHUTDOWN; |
||||
} else { |
||||
return grpc_channel_check_connectivity_state(channel_, false); |
||||
} |
||||
} |
||||
|
||||
char* ChannelNode::RenderJSON() { |
||||
// We need to track these three json objects to build our object
|
||||
grpc_json* top_level_json = grpc_json_create(GRPC_JSON_OBJECT); |
||||
grpc_json* json = top_level_json; |
||||
grpc_json* json_iterator = nullptr; |
||||
// create and fill the ref child
|
||||
json_iterator = grpc_json_create_child(json_iterator, json, "ref", nullptr, |
||||
GRPC_JSON_OBJECT, false); |
||||
json = json_iterator; |
||||
json_iterator = nullptr; |
||||
json_iterator = add_num_str(json, json_iterator, "channelId", channel_uuid_); |
||||
// reset json iterators to top level object
|
||||
json = top_level_json; |
||||
json_iterator = nullptr; |
||||
// create and fill the data child.
|
||||
grpc_json* data = grpc_json_create_child(json_iterator, json, "data", nullptr, |
||||
GRPC_JSON_OBJECT, false); |
||||
json = data; |
||||
json_iterator = nullptr; |
||||
// create and fill the connectivity state child.
|
||||
grpc_connectivity_state connectivity_state = GetConnectivityState(); |
||||
json_iterator = grpc_json_create_child(json_iterator, json, "state", nullptr, |
||||
GRPC_JSON_OBJECT, false); |
||||
json = json_iterator; |
||||
grpc_json_create_child(nullptr, json, "state", |
||||
grpc_connectivity_state_name(connectivity_state), |
||||
GRPC_JSON_STRING, false); |
||||
// reset the parent to be the data object.
|
||||
json = data; |
||||
json_iterator = grpc_json_create_child( |
||||
json_iterator, json, "target", target_.get(), GRPC_JSON_STRING, false); |
||||
// fill in the channel trace if applicable
|
||||
grpc_json* trace = trace_->RenderJSON(); |
||||
if (trace != nullptr) { |
||||
// we manuall link up and fill the child since it was created for us in
|
||||
// ChannelTrace::RenderJSON
|
||||
json_iterator = grpc_json_link_child(json, trace, json_iterator); |
||||
trace->parent = json; |
||||
trace->value = nullptr; |
||||
trace->key = "trace"; |
||||
trace->owns_value = false; |
||||
} |
||||
// reset the parent to be the data object.
|
||||
json = data; |
||||
json_iterator = nullptr; |
||||
// We use -1 as sentinel values since proto default value for integers is
|
||||
// zero, and the confuses the parser into thinking the value weren't present
|
||||
json_iterator = |
||||
add_num_str(json, json_iterator, "callsStarted", calls_started_); |
||||
json_iterator = |
||||
add_num_str(json, json_iterator, "callsSucceeded", calls_succeeded_); |
||||
json_iterator = |
||||
add_num_str(json, json_iterator, "callsFailed", calls_failed_); |
||||
gpr_timespec ts = |
||||
grpc_millis_to_timespec(last_call_started_millis_, GPR_CLOCK_REALTIME); |
||||
json_iterator = |
||||
grpc_json_create_child(json_iterator, json, "lastCallStartedTimestamp", |
||||
fmt_time(ts), GRPC_JSON_STRING, true); |
||||
// render and return the over json object
|
||||
char* json_str = grpc_json_dump_to_string(top_level_json, 0); |
||||
grpc_json_destroy(top_level_json); |
||||
return json_str; |
||||
} |
||||
|
||||
} // namespace channelz
|
||||
} // namespace grpc_core
|
@ -0,0 +1,85 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2018 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_H |
||||
#define GRPC_CORE_LIB_CHANNEL_CHANNELZ_H |
||||
|
||||
#include <grpc/impl/codegen/port_platform.h> |
||||
|
||||
#include <grpc/grpc.h> |
||||
|
||||
#include "src/core/lib/channel/channel_trace.h" |
||||
#include "src/core/lib/gprpp/manual_constructor.h" |
||||
#include "src/core/lib/gprpp/ref_counted.h" |
||||
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
||||
#include "src/core/lib/iomgr/error.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/json/json.h" |
||||
|
||||
namespace grpc_core { |
||||
namespace channelz { |
||||
|
||||
namespace testing { |
||||
class ChannelNodePeer; |
||||
} |
||||
|
||||
class ChannelNode : public RefCounted<ChannelNode> { |
||||
public: |
||||
ChannelNode(grpc_channel* channel, size_t channel_tracer_max_nodes); |
||||
~ChannelNode(); |
||||
|
||||
void RecordCallStarted(); |
||||
void RecordCallFailed() { |
||||
gpr_atm_no_barrier_fetch_add(&calls_failed_, (gpr_atm(1))); |
||||
} |
||||
void RecordCallSucceeded() { |
||||
gpr_atm_no_barrier_fetch_add(&calls_succeeded_, (gpr_atm(1))); |
||||
} |
||||
|
||||
char* RenderJSON(); |
||||
|
||||
ChannelTrace* trace() { return trace_.get(); } |
||||
|
||||
void set_channel_destroyed() { |
||||
GPR_ASSERT(channel_ != nullptr); |
||||
channel_ = nullptr; |
||||
} |
||||
|
||||
intptr_t channel_uuid() { return channel_uuid_; } |
||||
|
||||
private: |
||||
// testing peer friend.
|
||||
friend class testing::ChannelNodePeer; |
||||
|
||||
// helper for getting connectivity state.
|
||||
grpc_connectivity_state GetConnectivityState(); |
||||
|
||||
grpc_channel* channel_ = nullptr; |
||||
UniquePtr<char> target_; |
||||
gpr_atm calls_started_ = 0; |
||||
gpr_atm calls_succeeded_ = 0; |
||||
gpr_atm calls_failed_ = 0; |
||||
gpr_atm last_call_started_millis_ = 0; |
||||
intptr_t channel_uuid_; |
||||
ManualConstructor<ChannelTrace> trace_; |
||||
}; |
||||
|
||||
} // namespace channelz
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_H */ |
@ -0,0 +1,3 @@ |
||||
include grpc_version.py |
||||
recursive-include grpc_testing *.py |
||||
global-exclude *.pyc |
@ -0,0 +1,10 @@ |
||||
gRPC Python Testing Package |
||||
=========================== |
||||
|
||||
Testing utilities for gRPC Python |
||||
|
||||
Dependencies |
||||
------------ |
||||
|
||||
Depends on the `grpcio` package, available from PyPI via `pip install grpcio`. |
||||
|
@ -0,0 +1,216 @@ |
||||
/*
|
||||
* |
||||
* 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 <stdlib.h> |
||||
#include <string.h> |
||||
|
||||
#include <gtest/gtest.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
|
||||
#include "src/core/lib/channel/channel_trace.h" |
||||
#include "src/core/lib/channel/channelz.h" |
||||
#include "src/core/lib/channel/channelz_registry.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/json/json.h" |
||||
#include "src/core/lib/surface/channel.h" |
||||
|
||||
#include "test/core/util/test_config.h" |
||||
#include "test/cpp/util/channel_trace_proto_helper.h" |
||||
|
||||
#include <grpc/support/string_util.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
|
||||
namespace grpc_core { |
||||
namespace channelz { |
||||
namespace testing { |
||||
|
||||
// testing peer to access channel internals
|
||||
class ChannelNodePeer { |
||||
public: |
||||
ChannelNodePeer(ChannelNode* channel) : channel_(channel) {} |
||||
grpc_millis last_call_started_millis() { |
||||
return (grpc_millis)gpr_atm_no_barrier_load( |
||||
&channel_->last_call_started_millis_); |
||||
} |
||||
|
||||
private: |
||||
ChannelNode* channel_; |
||||
}; |
||||
|
||||
namespace { |
||||
|
||||
grpc_json* GetJsonChild(grpc_json* parent, const char* key) { |
||||
EXPECT_NE(parent, nullptr); |
||||
for (grpc_json* child = parent->child; child != nullptr; |
||||
child = child->next) { |
||||
if (child->key != nullptr && strcmp(child->key, key) == 0) return child; |
||||
} |
||||
return nullptr; |
||||
} |
||||
|
||||
class ChannelFixture { |
||||
public: |
||||
ChannelFixture(int max_trace_nodes) { |
||||
grpc_arg client_a[2]; |
||||
client_a[0].type = GRPC_ARG_INTEGER; |
||||
client_a[0].key = |
||||
const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENTS_PER_NODE); |
||||
client_a[0].value.integer = max_trace_nodes; |
||||
client_a[1].type = GRPC_ARG_INTEGER; |
||||
client_a[1].key = const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ); |
||||
client_a[1].value.integer = true; |
||||
grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a}; |
||||
channel_ = |
||||
grpc_insecure_channel_create("fake_target", &client_args, nullptr); |
||||
} |
||||
|
||||
~ChannelFixture() { grpc_channel_destroy(channel_); } |
||||
|
||||
grpc_channel* channel() { return channel_; } |
||||
|
||||
private: |
||||
grpc_channel* channel_; |
||||
}; |
||||
|
||||
struct validate_channel_data_args { |
||||
int64_t calls_started; |
||||
int64_t calls_failed; |
||||
int64_t calls_succeeded; |
||||
}; |
||||
|
||||
void ValidateChildInteger(grpc_json* json, int64_t expect, const char* key) { |
||||
grpc_json* gotten_json = GetJsonChild(json, key); |
||||
ASSERT_NE(gotten_json, nullptr); |
||||
int64_t gotten_number = (int64_t)strtol(gotten_json->value, nullptr, 0); |
||||
EXPECT_EQ(gotten_number, expect); |
||||
} |
||||
|
||||
void ValidateCounters(char* json_str, validate_channel_data_args args) { |
||||
grpc_json* json = grpc_json_parse_string(json_str); |
||||
ASSERT_NE(json, nullptr); |
||||
grpc_json* data = GetJsonChild(json, "data"); |
||||
ValidateChildInteger(data, args.calls_started, "callsStarted"); |
||||
ValidateChildInteger(data, args.calls_failed, "callsFailed"); |
||||
ValidateChildInteger(data, args.calls_succeeded, "callsSucceeded"); |
||||
grpc_json_destroy(json); |
||||
} |
||||
|
||||
void ValidateChannel(ChannelNode* channel, validate_channel_data_args args) { |
||||
char* json_str = channel->RenderJSON(); |
||||
grpc::testing::ValidateChannelProtoJsonTranslation(json_str); |
||||
ValidateCounters(json_str, args); |
||||
gpr_free(json_str); |
||||
} |
||||
|
||||
grpc_millis GetLastCallStartedMillis(ChannelNode* channel) { |
||||
ChannelNodePeer peer(channel); |
||||
return peer.last_call_started_millis(); |
||||
} |
||||
|
||||
void ChannelzSleep(int64_t sleep_us) { |
||||
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
||||
gpr_time_from_micros(sleep_us, GPR_TIMESPAN))); |
||||
grpc_core::ExecCtx::Get()->InvalidateNow(); |
||||
} |
||||
|
||||
} // anonymous namespace
|
||||
|
||||
class ChannelzChannelTest : public ::testing::TestWithParam<size_t> {}; |
||||
|
||||
TEST_P(ChannelzChannelTest, BasicChannel) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
ChannelFixture channel(GetParam()); |
||||
ChannelNode* channelz_channel = |
||||
grpc_channel_get_channelz_node(channel.channel()); |
||||
char* json_str = channelz_channel->RenderJSON(); |
||||
ValidateCounters(json_str, {0, 0, 0}); |
||||
gpr_free(json_str); |
||||
} |
||||
|
||||
TEST(ChannelzChannelTest, ChannelzDisabled) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
grpc_channel* channel = |
||||
grpc_insecure_channel_create("fake_target", nullptr, nullptr); |
||||
ChannelNode* channelz_channel = grpc_channel_get_channelz_node(channel); |
||||
ASSERT_EQ(channelz_channel, nullptr); |
||||
grpc_channel_destroy(channel); |
||||
} |
||||
|
||||
TEST_P(ChannelzChannelTest, BasicChannelAPIFunctionality) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
ChannelFixture channel(GetParam()); |
||||
ChannelNode* channelz_channel = |
||||
grpc_channel_get_channelz_node(channel.channel()); |
||||
channelz_channel->RecordCallStarted(); |
||||
channelz_channel->RecordCallFailed(); |
||||
channelz_channel->RecordCallSucceeded(); |
||||
ValidateChannel(channelz_channel, {1, 1, 1}); |
||||
channelz_channel->RecordCallStarted(); |
||||
channelz_channel->RecordCallFailed(); |
||||
channelz_channel->RecordCallSucceeded(); |
||||
channelz_channel->RecordCallStarted(); |
||||
channelz_channel->RecordCallFailed(); |
||||
channelz_channel->RecordCallSucceeded(); |
||||
ValidateChannel(channelz_channel, {3, 3, 3}); |
||||
} |
||||
|
||||
TEST_P(ChannelzChannelTest, LastCallStartedMillis) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
ChannelFixture channel(GetParam()); |
||||
ChannelNode* channelz_channel = |
||||
grpc_channel_get_channelz_node(channel.channel()); |
||||
// start a call to set the last call started timestamp
|
||||
channelz_channel->RecordCallStarted(); |
||||
grpc_millis millis1 = GetLastCallStartedMillis(channelz_channel); |
||||
// time gone by should not affect the timestamp
|
||||
ChannelzSleep(100); |
||||
grpc_millis millis2 = GetLastCallStartedMillis(channelz_channel); |
||||
EXPECT_EQ(millis1, millis2); |
||||
// calls succeeded or failed should not affect the timestamp
|
||||
ChannelzSleep(100); |
||||
channelz_channel->RecordCallFailed(); |
||||
channelz_channel->RecordCallSucceeded(); |
||||
grpc_millis millis3 = GetLastCallStartedMillis(channelz_channel); |
||||
EXPECT_EQ(millis1, millis3); |
||||
// another call started should affect the timestamp
|
||||
// sleep for extra long to avoid flakes (since we cache Now())
|
||||
ChannelzSleep(5000); |
||||
channelz_channel->RecordCallStarted(); |
||||
grpc_millis millis4 = GetLastCallStartedMillis(channelz_channel); |
||||
EXPECT_NE(millis1, millis4); |
||||
} |
||||
|
||||
INSTANTIATE_TEST_CASE_P(ChannelzChannelTestSweep, ChannelzChannelTest, |
||||
::testing::Values(0, 1, 2, 6, 10, 15)); |
||||
|
||||
} // namespace testing
|
||||
} // namespace channelz
|
||||
} // namespace grpc_core
|
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc_test_init(argc, argv); |
||||
grpc_init(); |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
int ret = RUN_ALL_TESTS(); |
||||
grpc_shutdown(); |
||||
return ret; |
||||
} |
@ -0,0 +1,299 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include "src/core/lib/surface/channel.h" |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/time.h> |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "test/core/end2end/cq_verifier.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
static void run_one_request(grpc_end2end_test_config config, |
||||
grpc_end2end_test_fixture f, |
||||
bool request_is_success) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/foo"), nullptr, |
||||
deadline, nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op->flags = 0; |
||||
op->reserved = nullptr; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op->flags = 0; |
||||
op->reserved = nullptr; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op->flags = 0; |
||||
op->reserved = nullptr; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op->data.recv_status_on_client.error_string = nullptr; |
||||
op->flags = 0; |
||||
op->reserved = nullptr; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
||||
nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), 1); |
||||
cq_verify(cqv); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op->flags = 0; |
||||
op->reserved = nullptr; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = |
||||
request_is_success ? GRPC_STATUS_OK : GRPC_STATUS_UNIMPLEMENTED; |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op->flags = 0; |
||||
op->reserved = nullptr; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op->flags = 0; |
||||
op->reserved = nullptr; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102), |
||||
nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), 1); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), 1); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo")); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
} |
||||
|
||||
static void test_channelz(grpc_end2end_test_config config) { |
||||
grpc_end2end_test_fixture f; |
||||
|
||||
grpc_arg client_a; |
||||
client_a.type = GRPC_ARG_INTEGER; |
||||
client_a.key = const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ); |
||||
client_a.value.integer = true; |
||||
grpc_channel_args client_args = {1, &client_a}; |
||||
|
||||
f = begin_test(config, "test_channelz", &client_args, nullptr); |
||||
grpc_core::channelz::ChannelNode* channelz_channel = |
||||
grpc_channel_get_channelz_node(f.client); |
||||
|
||||
GPR_ASSERT(channelz_channel != nullptr); |
||||
char* json = channelz_channel->RenderJSON(); |
||||
GPR_ASSERT(json != nullptr); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"callsStarted\":\"0\"")); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"callsFailed\":\"0\"")); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"callsSucceeded\":\"0\"")); |
||||
gpr_free(json); |
||||
|
||||
// one successful request
|
||||
run_one_request(config, f, true); |
||||
|
||||
json = channelz_channel->RenderJSON(); |
||||
GPR_ASSERT(json != nullptr); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"callsStarted\":\"1\"")); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"callsFailed\":\"0\"")); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"callsSucceeded\":\"1\"")); |
||||
gpr_free(json); |
||||
|
||||
// one failed request
|
||||
run_one_request(config, f, false); |
||||
|
||||
json = channelz_channel->RenderJSON(); |
||||
GPR_ASSERT(json != nullptr); |
||||
gpr_log(GPR_INFO, "%s", json); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"callsStarted\":\"2\"")); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"callsFailed\":\"1\"")); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"callsSucceeded\":\"1\"")); |
||||
// channel tracing is not enables, so these should not be preset.
|
||||
GPR_ASSERT(nullptr == strstr(json, "\"trace\"")); |
||||
GPR_ASSERT(nullptr == strstr(json, "\"description\":\"Channel created\"")); |
||||
GPR_ASSERT(nullptr == strstr(json, "\"severity\":\"CT_INFO\"")); |
||||
gpr_free(json); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
static void test_channelz_with_channel_trace(grpc_end2end_test_config config) { |
||||
grpc_end2end_test_fixture f; |
||||
|
||||
grpc_arg client_a[2]; |
||||
client_a[0].type = GRPC_ARG_INTEGER; |
||||
client_a[0].key = |
||||
const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENTS_PER_NODE); |
||||
client_a[0].value.integer = 5; |
||||
client_a[1].type = GRPC_ARG_INTEGER; |
||||
client_a[1].key = const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ); |
||||
client_a[1].value.integer = true; |
||||
grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a}; |
||||
|
||||
f = begin_test(config, "test_channelz_with_channel_trace", &client_args, |
||||
nullptr); |
||||
grpc_core::channelz::ChannelNode* channelz_channel = |
||||
grpc_channel_get_channelz_node(f.client); |
||||
|
||||
GPR_ASSERT(channelz_channel != nullptr); |
||||
char* json = channelz_channel->RenderJSON(); |
||||
GPR_ASSERT(json != nullptr); |
||||
gpr_log(GPR_INFO, "%s", json); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"trace\"")); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"description\":\"Channel created\"")); |
||||
GPR_ASSERT(nullptr != strstr(json, "\"severity\":\"CT_INFO\"")); |
||||
gpr_free(json); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
static void test_channelz_disabled(grpc_end2end_test_config config) { |
||||
grpc_end2end_test_fixture f; |
||||
|
||||
f = begin_test(config, "test_channelz_disabled", nullptr, nullptr); |
||||
grpc_core::channelz::ChannelNode* channelz_channel = |
||||
grpc_channel_get_channelz_node(f.client); |
||||
GPR_ASSERT(channelz_channel == nullptr); |
||||
// one successful request
|
||||
run_one_request(config, f, true); |
||||
GPR_ASSERT(channelz_channel == nullptr); |
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void channelz(grpc_end2end_test_config config) { |
||||
test_channelz(config); |
||||
test_channelz_with_channel_trace(config); |
||||
test_channelz_disabled(config); |
||||
} |
||||
|
||||
void channelz_pre_init(void) {} |
File diff suppressed because it is too large
Load Diff
@ -1,12 +0,0 @@ |
||||
Debug |
||||
Debug-DLL |
||||
Release |
||||
Release-DLL |
||||
*.suo |
||||
*.user |
||||
test_bin |
||||
*.opensdf |
||||
*.sdf |
||||
third_party/*.user |
||||
/packages |
||||
/IntDir |
@ -1,11 +0,0 @@ |
||||
# Pre-generated MS Visual Studio project & solution files: DELETED |
||||
|
||||
**The pre-generated MS Visual Studio project & solution files are no longer available, please use cmake instead (it can generate Visual Studio projects for you).** |
||||
|
||||
**Pre-generated MS Visual Studio projects used to be the recommended way to build on Windows, but there were some limitations:** |
||||
- **hard to build dependencies, expecially boringssl (deps usually support cmake quite well)** |
||||
- **the nuget-based openssl & zlib dependencies are hard to maintain and update. We've received issues indicating that they are flawed.** |
||||
- **.proto codegen is hard to support in Visual Studio directly (but we have a pretty decent support in cmake)** |
||||
- **It's a LOT of generated files. We prefer not to have too much generated code in our github repo.** |
||||
|
||||
See [INSTALL.md](/INSTALL.md) for detailed instructions how to build using cmake on Windows. |
Loading…
Reference in new issue