Skip to content

Commit

Permalink
Skip isInLocalDatacenter call when possible
Browse files Browse the repository at this point in the history
Reviewed By: stuclar

Differential Revision: D19741151

fbshipit-source-id: f87a5109fa3bc14c0e76a1d3a1db517f56365560
  • Loading branch information
Pavlo Kushnir authored and facebook-github-bot committed Mar 26, 2020
1 parent 6f8c6d4 commit 5f46301
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions mcrouter/routes/McRouteHandleProvider-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,14 @@ McRouteHandleProvider<RouterInfo>::makePool(
}

SecurityMech mech = SecurityMech::NONE;
folly::Optional<SecurityMech> mechOverride;
folly::Optional<SecurityMech> withinDcMech;
folly::Optional<SecurityMech> crossDcMech;
folly::Optional<uint16_t> crossDcPort;
folly::Optional<uint16_t> withinDcPort;
// default to 0, which doesn't override
uint16_t port = 0;
if (proxy_.router().configApi().enableSecurityConfig()) {
if (auto jSecurityMech = json.get_ptr("security_mech")) {
auto mechStr = parseString(*jSecurityMech, "security_mech");
mech = parseSecurityMech(mechStr);
} else if (auto jUseSsl = json.get_ptr("use_ssl")) {
// deprecated - prefer security_mech
auto useSsl = parseBool(*jUseSsl, "use_ssl");
if (useSsl) {
mech = SecurityMech::TLS;
}
}

if (auto jSecurityMech = json.get_ptr("security_mech_within_dc")) {
auto mechStr = parseString(*jSecurityMech, "security_mech_within_dc");
withinDcMech = parseSecurityMech(mechStr);
Expand All @@ -219,6 +209,27 @@ McRouteHandleProvider<RouterInfo>::makePool(
crossDcMech = parseSecurityMech(mechStr);
}

if (withinDcMech.has_value() && crossDcMech.has_value() &&
withinDcMech.value() == crossDcMech.value()) {
// mech is used if nothing is specified in server ap
mech = withinDcMech.value();
// mechOverride overrides per-server values
mechOverride = withinDcMech.value();
withinDcMech.reset();
crossDcMech.reset();
} else {
if (auto jSecurityMech = json.get_ptr("security_mech")) {
auto mechStr = parseString(*jSecurityMech, "security_mech");
mech = parseSecurityMech(mechStr);
} else if (auto jUseSsl = json.get_ptr("use_ssl")) {
// deprecated - prefer security_mech
auto useSsl = parseBool(*jUseSsl, "use_ssl");
if (useSsl) {
mech = SecurityMech::TLS;
}
}
}

if (auto jPort = json.get_ptr("port_override_within_dc")) {
withinDcPort = parseInt(*jPort, "port_override_within_dc", 1, 65535);
}
Expand All @@ -227,8 +238,16 @@ McRouteHandleProvider<RouterInfo>::makePool(
crossDcPort = parseInt(*jPort, "port_override_cross_dc", 1, 65535);
}

if (auto jPort = json.get_ptr("port_override")) {
port = parseInt(*jPort, "port_override", 1, 65535);
if (withinDcPort.has_value() && crossDcPort.has_value() &&
withinDcPort.value() == crossDcPort.value()) {
port = withinDcPort.value();
withinDcPort.reset();
crossDcPort.reset();
} else {
// parse port override only if withinDc & crossDc are not present
if (auto jPort = json.get_ptr("port_override")) {
port = parseInt(*jPort, "port_override", 1, 65535);
}
}
}
bool disableRequestDeadlineCheck =
Expand Down Expand Up @@ -292,6 +311,10 @@ McRouteHandleProvider<RouterInfo>::makePool(
failureDomain);
checkLogic(ap != nullptr, "invalid server {}", server.stringPiece());

if (mechOverride.has_value()) {
ap->setSecurityMech(mechOverride.value());
}

if (withinDcMech.has_value() || crossDcMech.has_value() ||
withinDcPort.has_value() || crossDcPort.has_value()) {
bool isInLocalDc = isInLocalDatacenter(ap->getHost());
Expand Down Expand Up @@ -515,8 +538,7 @@ McRouteHandleProvider<RouterInfo>::buildCheckedRouteMap() {
RouteHandleFactory<RouteHandleIf>& factory,
const folly::dynamic& json) {
auto rh = factoryFunc(factory, json);
checkLogic(
rh != nullptr, folly::sformat("make{} returned nullptr", rhName));
checkLogic(rh != nullptr, "make{} returned nullptr", rhName);
return rh;
});
}
Expand Down

0 comments on commit 5f46301

Please sign in to comment.