Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dubbo_proxy: Adds a routing matcher for the Dubbo protocol. #5571

Merged
merged 9 commits into from
Jan 23, 2019
11 changes: 10 additions & 1 deletion api/envoy/config/filter/network/dubbo_proxy/v2alpha1/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,14 @@ licenses(["notice"]) # Apache 2

api_proto_library(
name = "dubbo_proxy",
srcs = ["dubbo_proxy.proto"],
srcs = [
"dubbo_proxy.proto",
"route.proto",
],
deps = [
"//envoy/api/v2/core:base",
"//envoy/api/v2/route",
"//envoy/type:range",
"//envoy/type/matcher:string",
],
)
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
syntax = "proto3";

package envoy.extensions.filters.network.dubbo_proxy.v2alpha1;
option java_package = "io.envoyproxy.envoy.extensions.filters.network.dubbo_proxy.v2alpha1";
package envoy.config.filter.network.dubbo_proxy.v2alpha1;
option java_package = "io.envoyproxy.envoy.config.filter.network.dubbo_proxy.v2alpha1";
option go_package = "v2";

import "envoy/config/filter/network/dubbo_proxy/v2alpha1/route.proto";

import "google/protobuf/any.proto";

import "validate/validate.proto";
import "gogoproto/gogo.proto";

// [#protodoc-title: Dubbo Proxy]
// Dubbo Proxy filter configuration.

message DubboProxy {
// The human readable prefix to use when emitting statistics.
string stat_prefix = 1 [(validate.rules).string.min_bytes = 1];

// Configure the protocol used.
enum ProtocolType {
Dubbo = 0; // the default protocol.
}
ProtocolType protocol_type = 2 [(validate.rules).enum.defined_only = true];

// Configure the serialization protocol used.
enum SerializationType {
Hessian2 = 0; // the default serialization protocol.
}
SerializationType serialization_type = 3 [(validate.rules).enum.defined_only = true];

// The route table for the connection manager is static and is specified in this property.
repeated RouteConfiguration route_config = 4;

// A list of individual Dubbo filters that make up the filter chain for requests made to the
// Dubbo proxy. Order matters as the filters are processed sequentially. For backwards
// compatibility, if no dubbo_filters are specified, a default Dubbo router filter
// (`envoy.filters.dubbo.router`) is used.
repeated DubboFilter dubbo_filters = 5;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gengleilei I am new to dubbo, my question is how to setup individual Dubbo filters?

I can not find any other supported filters except envoy.filters.dubbo.router .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can refer to the Router (router_impl.h), you need to implement DubboFilters: : DecoderFilter interface, yaml configuration is as follows:
stat_prefix: ingress
route_config:
name: local_route
dubbo_filters:
- name: envoy.filters.dubbo.mock_filter
config:
"@type": type.googleapis.com/google.protobuf.Struct
value:
name: test_service

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IC, this is for advanced users, need to hack into the envoy code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Filter based extension mechanism is similar to HTTP. Dubbo does not define a unique Filter mechanism. of course, if you have good suggestions, we are also very welcome to build together.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got this error, any idea?

[2019-08-06 02:35:19.872][27][error][dubbo] [external/envoy/source/extensions/filters/network/dubbo_proxy/conn_manager.cc:132] [C35] dubbo error: invalid dubbo mes
sage magic number 20186

}

enum ProtocolType {
Dubbo = 0; // the default protocol.
}

enum SerializationType {
Hessian2 = 0; // the default serialization protocol.
}

// DubboFilter configures a Dubbo filter.
// [#comment:next free field: 3]
message DubboFilter {
// The name of the filter to instantiate. The name must match a supported
// filter.
string name = 1 [(validate.rules).string.min_bytes = 1];

// Filter specific configuration which depends on the filter being
// instantiated. See the supported filters for further documentation.
google.protobuf.Any config = 2;
}
103 changes: 103 additions & 0 deletions api/envoy/config/filter/network/dubbo_proxy/v2alpha1/route.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
syntax = "proto3";

package envoy.config.filter.network.dubbo_proxy.v2alpha1;
option java_package = "io.envoyproxy.envoy.config.filter.network.dubbo_proxy.v2alpha1";
option go_package = "v2";

import "envoy/api/v2/route/route.proto";
import "envoy/type/matcher/string.proto";
import "envoy/type/range.proto";

import "google/protobuf/wrappers.proto";

import "validate/validate.proto";
import "gogoproto/gogo.proto";

// [#protodoc-title: Dubbo route configuration]

message RouteConfiguration {
// The name of the route configuration. Reserved for future use in asynchronous route discovery.
string name = 1;

// The interface name of the service.
string interface = 2;

// Which group does the interface belong to.
string group = 3;

// The version number of the interface.
string version = 4;

// The list of routes that will be matched, in order, against incoming requests. The first route
// that matches will be used.
repeated Route routes = 5 [(gogoproto.nullable) = false];
}

message Route {
// Route matching prarameters.
RouteMatch match = 1 [(validate.rules).message.required = true, (gogoproto.nullable) = false];

// Route request to some upstream cluster.
RouteAction route = 2 [(validate.rules).message.required = true, (gogoproto.nullable) = false];
}

message MethodMatch {
// The name of the method.
envoy.type.matcher.StringMatcher name = 1;

// The parameter matching type.
message ParameterMatchSpecifier {
oneof parameter_match_specifier {
// If specified, header match will be performed based on the value of the header.
string exact_match = 3;

// If specified, header match will be performed based on range.
// The rule will match if the request header value is within this range.
// The entire request header value must represent an integer in base 10 notation: consisting
// of an optional plus or minus sign followed by a sequence of digits. The rule will not match
// if the header value does not represent an integer. Match will fail for empty values,
// floating point numbers or if only a subsequence of the header value is an integer.
//
// Examples:
//
// * For range [-10,0), route will match for header value -1, but not for 0,
// "somestring", 10.9,
// "-1somestring"
envoy.type.Int64Range range_match = 4;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the type of the parameter is not an integer, is this range matching useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, The type definition is not needed. I delete it.

}
}

// Method parameter definition.
// The key is the parameter index, starting from 0.
// The value is the parameter matching type.
map<uint32, ParameterMatchSpecifier> params_match = 2;
}

message RouteMatch {
// Method level routing matching.
MethodMatch method = 1;

// Specifies a set of headers that the route should match on. The router will check the request’s
// headers against all the specified headers in the route config. A match will happen if all the
// headers in the route are present in the request with the same values (or based on presence if
// the value field is not in the config).
repeated envoy.api.v2.route.HeaderMatcher headers = 2;
}

// [#comment:next free field: 2]
message RouteAction {
oneof cluster_specifier {
option (validate.required) = true;

// Indicates the upstream cluster to which the request should be routed.
string cluster = 1;

// Multiple upstream clusters can be specified for a given route. The
// request is routed to one of the upstream clusters based on weights
// assigned to each cluster.
//
// .. note::
// Currently ClusterWeight only supports the name and weight fields.
envoy.api.v2.route.WeightedCluster weighted_clusters = 2;
}
}
10 changes: 10 additions & 0 deletions source/extensions/filters/network/dubbo_proxy/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ envoy_cc_library(
hdrs = ["filter.h"],
deps = [
":decoder_lib",
":stats_lib",
"//include/envoy/network:connection_interface",
"//include/envoy/network:filter_interface",
"//include/envoy/stats:stats_interface",
Expand Down Expand Up @@ -151,3 +152,12 @@ envoy_cc_library(
"//source/common/buffer:buffer_lib",
],
)

envoy_cc_library(
name = "stats_lib",
hdrs = ["stats.h"],
deps = [
"//include/envoy/stats:stats_interface",
"//include/envoy/stats:stats_macros",
],
)
2 changes: 1 addition & 1 deletion source/extensions/filters/network/dubbo_proxy/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace NetworkFilters {
namespace DubboProxy {

Network::FilterFactoryCb DubboProxyFilterConfigFactory::createFilterFactoryFromProtoTyped(
const envoy::extensions::filters::network::dubbo_proxy::v2alpha1::DubboProxy& proto_config,
const envoy::config::filter::network::dubbo_proxy::v2alpha1::DubboProxy& proto_config,
Server::Configuration::FactoryContext& context) {
ASSERT(!proto_config.stat_prefix().empty());

Expand Down
4 changes: 2 additions & 2 deletions source/extensions/filters/network/dubbo_proxy/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ namespace DubboProxy {
*/
class DubboProxyFilterConfigFactory
: public Common::FactoryBase<
envoy::extensions::filters::network::dubbo_proxy::v2alpha1::DubboProxy> {
envoy::config::filter::network::dubbo_proxy::v2alpha1::DubboProxy> {
public:
DubboProxyFilterConfigFactory() : FactoryBase(NetworkFilterNames::get().DubboProxy) {}

private:
Network::FilterFactoryCb createFilterFactoryFromProtoTyped(
const envoy::extensions::filters::network::dubbo_proxy::v2alpha1::DubboProxy& proto_config,
const envoy::config::filter::network::dubbo_proxy::v2alpha1::DubboProxy& proto_config,
Server::Configuration::FactoryContext& context) override;
};

Expand Down
33 changes: 12 additions & 21 deletions source/extensions/filters/network/dubbo_proxy/filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,44 @@ namespace DubboProxy {

namespace {

using ConfigProtocolType =
envoy::extensions::filters::network::dubbo_proxy::v2alpha1::DubboProxy_ProtocolType;
using ConfigProtocolType = envoy::config::filter::network::dubbo_proxy::v2alpha1::ProtocolType;

typedef std::map<ConfigProtocolType, ProtocolType> ProtocolTypeMap;

static const ProtocolTypeMap& protocolTypeMap() {
CONSTRUCT_ON_FIRST_USE(
ProtocolTypeMap, {
{ConfigProtocolType::DubboProxy_ProtocolType_Dubbo, ProtocolType::Dubbo},
});
CONSTRUCT_ON_FIRST_USE(ProtocolTypeMap, {
{ConfigProtocolType::Dubbo, ProtocolType::Dubbo},
});
}

ProtocolType lookupProtocolType(ConfigProtocolType config_type) {
const auto& iter = protocolTypeMap().find(config_type);
if (iter == protocolTypeMap().end()) {
throw EnvoyException(fmt::format(
"unknown protocol {}",
envoy::extensions::filters::network::dubbo_proxy::v2alpha1::DubboProxy_ProtocolType_Name(
config_type)));
envoy::config::filter::network::dubbo_proxy::v2alpha1::ProtocolType_Name(config_type)));
}
return iter->second;
}

using ConfigSerializationType =
envoy::extensions::filters::network::dubbo_proxy::v2alpha1::DubboProxy_SerializationType;
envoy::config::filter::network::dubbo_proxy::v2alpha1::SerializationType;

typedef std::map<ConfigSerializationType, SerializationType> SerializationTypeMap;

static const SerializationTypeMap& serializationTypeMap() {
CONSTRUCT_ON_FIRST_USE(SerializationTypeMap,
{
{ConfigSerializationType::DubboProxy_SerializationType_Hessian2,
SerializationType::Hessian},
{ConfigSerializationType::Hessian2, SerializationType::Hessian},
});
}

SerializationType lookupSerializationType(ConfigSerializationType type) {
const auto& iter = serializationTypeMap().find(type);
if (iter == serializationTypeMap().end()) {
throw EnvoyException(fmt::format("unknown deserializer {}",
envoy::extensions::filters::network::dubbo_proxy::v2alpha1::
DubboProxy_SerializationType_Name(type)));
throw EnvoyException(fmt::format(
"unknown deserializer {}",
envoy::config::filter::network::dubbo_proxy::v2alpha1::SerializationType_Name(type)));
}

return iter->second;
Expand All @@ -67,7 +63,8 @@ SerializationType lookupSerializationType(ConfigSerializationType type) {
Filter::Filter(const std::string& stat_prefix, ConfigProtocolType protocol_type,
ConfigSerializationType serialization_type, Stats::Scope& scope,
TimeSource& time_source)
: stats_(generateStats(stat_prefix, scope)), protocol_type_(lookupProtocolType(protocol_type)),
: stats_(DubboFilterStats::generateStats(stat_prefix, scope)),
protocol_type_(lookupProtocolType(protocol_type)),
serialization_type_(lookupSerializationType(serialization_type)), time_source_(time_source) {}

Filter::~Filter() = default;
Expand Down Expand Up @@ -221,12 +218,6 @@ void Filter::onRpcResult(RpcResultPtr&& res) {
}
}

DubboFilterStats Filter::generateStats(const std::string& prefix, Stats::Scope& scope) {
return DubboFilterStats{ALL_DUBBO_FILTER_STATS(POOL_COUNTER_PREFIX(scope, prefix),
POOL_GAUGE_PREFIX(scope, prefix),
POOL_HISTOGRAM_PREFIX(scope, prefix))};
}

DecoderPtr Filter::createDecoder(ProtocolCallbacks& prot_callback) {
auto parser = createProtocol(prot_callback);
auto serializer = createDeserializer();
Expand Down
45 changes: 7 additions & 38 deletions source/extensions/filters/network/dubbo_proxy/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,25 @@
#include "extensions/filters/network/dubbo_proxy/decoder.h"
#include "extensions/filters/network/dubbo_proxy/deserializer.h"
#include "extensions/filters/network/dubbo_proxy/protocol.h"
#include "extensions/filters/network/dubbo_proxy/stats.h"

namespace Envoy {
namespace Extensions {
namespace NetworkFilters {
namespace DubboProxy {

/**
* All dubbo filter stats. @see stats_macros.h
*/
// clang-format off
#define ALL_DUBBO_FILTER_STATS(COUNTER, GAUGE, HISTOGRAM) \
COUNTER(request) \
COUNTER(request_twoway) \
COUNTER(request_oneway) \
COUNTER(request_event) \
COUNTER(request_invalid_type) \
COUNTER(request_decoding_error) \
GAUGE(request_active) \
HISTOGRAM(request_time_ms) \
COUNTER(response) \
COUNTER(response_success) \
COUNTER(response_error) \
COUNTER(response_exception) \
COUNTER(response_decoding_error) \
COUNTER(cx_destroy_local_with_active_rq) \
COUNTER(cx_destroy_remote_with_active_rq) \
// clang-format on

/**
* Struct definition for all dubbo proxy stats. @see stats_macros.h
*/
struct DubboFilterStats {
ALL_DUBBO_FILTER_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT, GENERATE_HISTOGRAM_STRUCT)
};

class Filter : public Network::Filter,
public Network::ConnectionCallbacks,
public ProtocolCallbacks,
public DecoderCallbacks,
Logger::Loggable<Logger::Id::dubbo> {
public:
using ConfigProtocolType = envoy::extensions::filters::network::dubbo_proxy::v2alpha1::DubboProxy_ProtocolType;
using ConfigSerializationType = envoy::extensions::filters::network::dubbo_proxy::v2alpha1::DubboProxy_SerializationType;
using ConfigProtocolType = envoy::config::filter::network::dubbo_proxy::v2alpha1::ProtocolType;
using ConfigSerializationType =
envoy::config::filter::network::dubbo_proxy::v2alpha1::SerializationType;

Filter(const std::string& stat_prefix, ConfigProtocolType protocol_type,
ConfigSerializationType serialization_type, Stats::Scope& scope,
TimeSource& time_source);
ConfigSerializationType serialization_type, Stats::Scope& scope, TimeSource& time_source);
virtual ~Filter();

// Network::ReadFilter
Expand Down Expand Up @@ -93,8 +65,8 @@ class Filter : public Network::Filter,
// ActiveMessage tracks downstream requests for which no response has been received.
struct ActiveMessage {
ActiveMessage(Filter& parent, int32_t request_id)
: parent_(parent),
request_timer_(new Stats::Timespan(parent_.stats_.request_time_ms_, parent_.time_source_)),
: parent_(parent), request_timer_(new Stats::Timespan(parent_.stats_.request_time_ms_,
parent_.time_source_)),
request_id_(request_id) {
parent_.stats_.request_active_.inc();
}
Expand All @@ -110,9 +82,6 @@ class Filter : public Network::Filter,
};
typedef std::unique_ptr<ActiveMessage> ActiveMessagePtr;

DubboFilterStats generateStats(const std::string& prefix,
Stats::Scope& scope);

// Downstream request decoder, callbacks, and buffer.
DecoderPtr request_decoder_;
Buffer::OwnedImpl request_buffer_;
Expand Down
Loading