chenBright commented on code in PR #3767:
URL: https://github.com/apache/brpc/pull/3767#discussion_r4726004714
##########
src/brpc/server.cpp:
##########
@@ -784,6 +866,17 @@ int Server::InitALPNOptions(const ServerSSLOptions*
options) {
LOG(ERROR) << "Server does not support alpn=" << alpn;
return -1;
}
+ // Negotiating a protocol without an installed handler makes the client
+ // fail on its first request, reject the configuration instead.
+ std::unordered_set<std::string> whitelist;
+ ParseEnabledProtocols(_options.enabled_protocols, &whitelist);
+ if (!whitelist.empty() && whitelist.count(protocol->name) == 0) {
+ LOG(ERROR) << "alpn=" << alpn << " is not in "
+ << "ServerOptions.enabled_protocols=`"
+ << _options.enabled_protocols << '\'';
+ errno = EINVAL;
+ return -1;
Review Comment:
Could the ALPN allowlist check be completed before taking ownership of the
options and reserving session data? For example, with `enabled_protocols="h2"`
and `alpns="http,h2"`, `Reserve()` has already created data through the
caller's factory. When this returns, the server is still `READY`, so rollback
skips the session pool's `Reset()`. If the caller destroys the factory after
startup fails, a subsequent retry or `Server` destruction will call
`DestroyData()` through an invalid factory.
##########
src/brpc/server.cpp:
##########
@@ -1580,6 +1703,24 @@ int
Server::AddServiceInternal(google::protobuf::Service* service,
butil::StringPiece restful_mappings = svc_opt.restful_mappings;
restful_mappings.trim_spaces();
if (!restful_mappings.empty()) {
+ // Services added while Start() is running (e.g. from bthread_init_fn)
+ // were not seen by the validation of the user-provided options, so
+ // check that their restful mappings can be served as well.
+ if (_in_start) {
+ std::unordered_set<std::string> whitelist;
+ ParseEnabledProtocols(_options.enabled_protocols, &whitelist);
+ if (!whitelist.empty() && whitelist.count("http") == 0 &&
+ whitelist.count("h2") == 0) {
+ LOG(ERROR) << "Fail to add restful service=`" <<
sd->full_name()
+ << "': ServerOptions.enabled_protocols=`"
+ << _options.enabled_protocols
+ << "' enables neither http nor h2";
+ // The service is already registered, undo it like the
+ // RESTful mapping failures below.
+ RemoveService(service);
+ return -1;
Review Comment:
`RemoveService()` here directly deletes `service` when `SERVER_OWNS_SERVICE`
is set. If the caller keeps the same service for a retry after receiving
`AddService() == -1`, as in the new test, the pointer will be dangling.
Consider checking the protocol required by REST before writing to the method
and service tables, so rejecting registration requires no rollback and does not
change object ownership.
##########
src/brpc/server.cpp:
##########
@@ -686,24 +687,120 @@ int Server::AddBuiltinServices() {
return 0;
}
-BUTIL_FORCE_INLINE bool is_http_protocol(const char* name) {
- if (name[0] != 'h') {
- return false;
+BUTIL_FORCE_INLINE bool is_rdma_handshake_protocol(const char* name) {
+ return strcmp(name, "rdma_handshake") == 0;
+}
+
+// Restful services are served through http/h2 requests. Empty maps may be
+// left by RemoveService(), so check the number of mappings instead of the
+// existence of the map.
+static bool HasRestfulServices(const Server::ServiceMap& services,
+ const RestfulMap* global_restful_map) {
+ if (global_restful_map != nullptr && global_restful_map->size() != 0) {
+ return true;
+ }
+ for (Server::ServiceMap::const_iterator it = services.begin();
+ it != services.end(); ++it) {
+ if (it->second.restful_map != nullptr &&
+ it->second.restful_map->size() != 0) {
+ return true;
+ }
}
- return strcmp(name, "http") == 0 || strcmp(name, "h2") == 0;
+ return false;
}
-BUTIL_FORCE_INLINE bool is_rdma_handshake_protocol(const char* name) {
- return strcmp(name, "rdma_handshake") == 0;
+static std::string JoinWithAnd(const std::vector<std::string>& strs) {
+ std::string out;
+ for (size_t i = 0; i < strs.size(); ++i) {
+ if (i != 0) {
+ out.append(i + 1 == strs.size() ? " and " : ", ");
+ }
+ out.append(strs[i]);
+ }
+ return out;
}
-Acceptor* Server::BuildAcceptor() {
+static void ParseEnabledProtocols(const std::string& enabled_protocols,
+ std::unordered_set<std::string>* whitelist) {
+ for (butil::StringSplitter sp(enabled_protocols.c_str(), ' '); sp; ++sp) {
+ whitelist->insert(std::string(sp.field(), sp.length()));
+ }
+}
+
+// Returns true if `a' and `b' list the same set of protocols, in which case
the
+// handlers built from one of them also match the other.
+static bool SameEnabledProtocols(const std::string& a, const std::string& b) {
+ std::unordered_set<std::string> wa;
+ std::unordered_set<std::string> wb;
+ ParseEnabledProtocols(a, &wa);
+ ParseEnabledProtocols(b, &wb);
+ return wa == wb;
+}
+
+int Server::ValidateEnabledProtocols(const ServerOptions& options) const {
std::unordered_set<std::string> whitelist;
- for (butil::StringSplitter sp(_options.enabled_protocols.c_str(), ' ');
- sp; ++sp) {
- std::string protocol(sp.field(), sp.length());
- whitelist.insert(protocol);
+ ParseEnabledProtocols(options.enabled_protocols, &whitelist);
+ if (whitelist.empty()) {
+ return 0;
+ }
+ const bool http_in_whitelist =
+ whitelist.count("http") != 0 || whitelist.count("h2") != 0;
+ std::vector<Protocol> protocols;
+ ListProtocols(&protocols);
+ for (size_t i = 0; i < protocols.size(); ++i) {
+ if (protocols[i].process_request != nullptr) {
+ whitelist.erase(protocols[i].name);
+ }
+ }
+ if (!whitelist.empty()) {
+ std::ostringstream err;
+ err << "ServerOptions.enabled_protocols has unknown protocols=`";
+ for (const auto& protocol : whitelist) {
+ err << protocol << ' ';
+ }
+ err << '\'';
+ LOG(ERROR) << err.str();
+ errno = EINVAL;
+ return -1;
+ }
+ if (http_in_whitelist) {
+ return 0;
+ }
+ std::vector<std::string> features;
+ std::vector<std::string> fixes;
+ if (_builtin_service_count > 0) {
+ // Builtin services added by a previous Start() stay registered, so the
+ // configuration cannot get rid of them anymore.
+ features.push_back("builtin services are already registered");
+ fixes.push_back("create a new Server with "
+ "ServerOptions.has_builtin_services=false");
+ } else if (options.has_builtin_services) {
+ features.push_back("ServerOptions.has_builtin_services is true");
+ fixes.push_back("set ServerOptions.has_builtin_services=false");
+ }
+ if (HasRestfulServices(_fullname_service_map, _global_restful_map)) {
+ features.push_back("restful services are added");
+ fixes.push_back("remove the restful mappings");
+ }
+ if (options.http_master_service != nullptr) {
+ features.push_back("ServerOptions.http_master_service is set");
+ fixes.push_back("clear ServerOptions.http_master_service");
+ }
+ if (features.empty()) {
+ return 0;
}
+ LOG(ERROR) << "ServerOptions.enabled_protocols=`"
+ << options.enabled_protocols << "' enables neither http nor "
+ << "h2, but " << JoinWithAnd(features)
+ << ". They are served through http/h2, add `http' (or `h2') to "
+ << "enabled_protocols, or " << JoinWithAnd(fixes) << '.';
Review Comment:
When built-in services are already registered and
`has_builtin_services=false` is now set, adding only `http` as suggested by the
log will still fail the later built-in service consistency check. Please state
both conditions required to reuse the instance, or only suggest a new-instance
solution that will work directly in this state.
##########
src/brpc/server.h:
##########
@@ -73,6 +73,13 @@ struct SocketSSLContext;
struct ServerOptions {
ServerOptions(); // Constructed with default options.
+ // NOTE: pointers in this struct are owned by the caller until they are
+ // adopted by a Server. A Server::Start() rejected by the validation of
+ // this struct (e.g. an invalid enabled_protocols) adopts nothing, so the
+ // caller still owns and must free them. Once Start() gets past that
+ // validation, the Server owns them and frees them in its destructor even
+ // if Start() fails later, e.g. when the port cannot be bound.
Review Comment:
This description overrides the ownership conventions for all the fields
below. By default, `auth`, the session data factory, and `health_reporter` are
managed by the caller. Also, if a retry reuses an `http_master_service` whose
ownership was taken over on the previous attempt, an early validation failure
will not return it to the caller. Consider describing this behavior as “early
rejection preserves existing ownership” and limiting the ownership transfer
rules to fields that explicitly transfer ownership.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]