stream info: prefer reference rather than shared pointer for upstreamClusterInfo()#44042
Conversation
Signed-off-by: wbpcode/wangbaiping <wbphub@gmail.com>
|
/retest |
… dev-make-streaminfo-upstream-cluster-info-better
| if (const auto cluster_info = parent_.callbacks()->streamInfo().upstreamClusterInfo()) { | ||
| stream_info_.setUpstreamClusterInfo( | ||
| parent_.callbacks()->streamInfo().upstreamClusterInfoSharedPtr()); |
There was a problem hiding this comment.
There is a behavior change in this section - in the old version it was possible for this block to setUpstreamClusterInfo(nullptr) if there are no clusters, vs. after this change setUpstreamClusterInfo will not be called in that case.
I don't know if this is important, since ~everything else that consumes the value appears to check "not nullopt and not nullptr" before doing anything. One way it could matter is if it's possible for the value to transition from not-null to null here.
There was a problem hiding this comment.
Yeah. The difference is senseless because at any position where the data is actually be used, we only care an actual valid cluster info.
One way it could matter is if it's possible for the value to transition from not-null to null here.
It's impossible. The upstream request's stream_info is empty here. And we will populate all necessary info here.
I think we even needn't the if check here now. Note, before this PR, the only reason to check whether returned absl::optionalUpstream::ClusterInfoConstSharedPtr has value is to ensure we can access the return value safely and the setUpstreamClusterInfo accept a const Upstream::ClusterInfoConstSharedPtr& as input prameter.
There was a problem hiding this comment.
I too thought the check could be removed, then I thought without the check you'd be copying a SharedPtr unnecessarily which could be expensive, but then I realize you'd only be copying a nullptr SharedPtr unnecessarily, which presumably is not expensive since it has no refcount. :)
So I'm happy with removing the check or not as you prefer.
| if (const auto cluster_info = stream_info->upstreamClusterInfo()) { | ||
| logging_info_->setClusterInfo(stream_info->upstreamClusterInfoSharedPtr()); |
There was a problem hiding this comment.
This behavior differs on what was previously set-but-nullptr too.
There was a problem hiding this comment.
This not actually change the behavior because the ExtAuthzLoggingInfo will keep an Upstream::ClusterInfoConstSharedPtr field directly.
Then,
before:
- set-but-nullptr: the cluster info is null in the
ExtAuthzLoggingInfo. - not-set: the cluster info is null in the
ExtAuthzLoggingInfo. - set-and-not-nullptr: the cluster info is valid in the
ExtAuthzLoggingInfo.
now:
- set-but-nullptr: the cluster info is null in the
ExtAuthzLoggingInfo. - not-set: the cluster info is null in the
ExtAuthzLoggingInfo. - set-and-not-nullptr: the cluster info is valid in the
ExtAuthzLoggingInfo.
TBH, we can also skip the if (const auto cluster_info = stream_info->upstreamClusterInfo()) check because no effect to final result.
| // No cluster info | ||
| { | ||
| EXPECT_CALL(Const(stream_info), upstreamClusterInfo()).WillOnce(Return(absl::nullopt)); | ||
| EXPECT_CALL(Const(stream_info), upstreamClusterInfo()) | ||
| .WillOnce(Return(OptRef<const Upstream::ClusterInfo>{})); | ||
| EXPECT_TRUE(TestUtility::jsonStringEqual(formatter.format(formatter_context, stream_info), | ||
| expected_json_map)); | ||
| } | ||
| // Empty cluster info (nullptr) | ||
| // Empty cluster info (nullptr shared_ptr) | ||
| { | ||
| EXPECT_CALL(Const(stream_info), upstreamClusterInfo()).WillOnce(Return(nullptr)); | ||
| EXPECT_CALL(Const(stream_info), upstreamClusterInfo()) | ||
| .WillOnce(Return(OptRef<const Upstream::ClusterInfo>{})); |
There was a problem hiding this comment.
If we're sticking with the no distinction between unset and set-to-nullptr model then I think this is now testing the same thing twice.
| ->access()); | ||
|
|
||
| EXPECT_EQ(absl::nullopt, stream_info.upstreamClusterInfo()); | ||
| EXPECT_FALSE(stream_info.upstreamClusterInfo()); |
There was a problem hiding this comment.
Little bit more readable (and better failure message) as
| EXPECT_FALSE(stream_info.upstreamClusterInfo()); | |
| EXPECT_THAT(stream_info.upstreamClusterInfo(), testing::IsNull()); |
There was a problem hiding this comment.
Maybe EXPECT_FALSE(!stream_info.upstreamClusterInfo().has_value());?
There was a problem hiding this comment.
My bad, as it was is fine - I was thinking it should have been IsNull before, when it was a pointer, but now that it's an optref just the plain EXPECT_FALSE works for me.
| stream_info.setUpstreamClusterInfo(cluster_info); | ||
| EXPECT_NE(absl::nullopt, stream_info.upstreamClusterInfo()); | ||
| EXPECT_EQ("fake_cluster", stream_info.upstreamClusterInfo().value()->name()); | ||
| EXPECT_TRUE(stream_info.upstreamClusterInfo()); |
There was a problem hiding this comment.
Similarly, but also changing to an ASSERT because otherwise the next line will crash if this line is failing:
| EXPECT_TRUE(stream_info.upstreamClusterInfo()); | |
| ASSERT_THAT(stream_info.upstreamClusterInfo(), testing::NotNull()); |
| // No cluster info | ||
| { | ||
| EXPECT_CALL(Const(stream_info), upstreamClusterInfo()).WillOnce(Return(absl::nullopt)); | ||
| EXPECT_CALL(Const(stream_info), upstreamClusterInfo()) | ||
| .WillOnce(Return(OptRef<const Upstream::ClusterInfo>{})); | ||
| verifyOpenTelemetryOutput( | ||
| formatter.format({&request_header, &response_header, &response_trailer}, stream_info), | ||
| expected); | ||
| } | ||
| // Empty cluster info (nullptr) | ||
| // Empty cluster info (nullptr shared_ptr) | ||
| { | ||
| EXPECT_CALL(Const(stream_info), upstreamClusterInfo()).WillOnce(Return(nullptr)); | ||
| EXPECT_CALL(Const(stream_info), upstreamClusterInfo()) | ||
| .WillOnce(Return(OptRef<const Upstream::ClusterInfo>{})); |
There was a problem hiding this comment.
In this one, too, I think the same thing is being tested twice if we're sticking with discarding the distinction.
| * @return Upstream Connection's ClusterInfo. | ||
| * This returns an optional to differentiate between unset(absl::nullopt), | ||
| * no route or cluster does not exist(nullptr), and set to a valid cluster(not nullptr). |
There was a problem hiding this comment.
This comment deletion is related to most of my comments on this PR.
The other changes suggest the distinction probably doesn't matter, but there are a couple of cases where the behavior is changed, and I'm not certain whether the old behavior of
if (explicitly set to nullptr) {set another thing to nullptr}
meaningfully differs from the new behavior
if (explicitly set to nullptr) {don't set the other thing}
| // Only set cluster info in logging info once. | ||
| if (logging_info_->clusterInfo() == nullptr) { | ||
| logging_info_->setClusterInfo(stream_info->upstreamClusterInfo()); | ||
| logging_info_->setClusterInfo(stream_info->upstreamClusterInfoSharedPtr()); |
There was a problem hiding this comment.
this is same case with the ext authz. Only two different state in logging info: nullptr or valid cluster info.
There was a problem hiding this comment.
Oh yeah, these ones are definitely safe because it's explicitly checked whether the destination is already nullptr, and the only case where the change would make a difference is if the destination is not nullptr.
Co-authored-by: Raven Black <ravenblack@gmail.com> Signed-off-by: code <wbphub@gmail.com>
Signed-off-by: wbpcode/wangbaiping <wbphub@gmail.com>
Signed-off-by: wbpcode/wangbaiping <wbphub@gmail.com>
…nch 'dev-make-streaminfo-upstream-cluster-info-better' of ssh://ssh.github.com:443/wbpcode/envoy into dev-make-streaminfo-upstream-cluster-info-better
|
/retest |
…ClusterInfo() (envoyproxy#44042) Commit Message: stream info: return reference rather than copy of shared pointer Additional Description: Rather then return a copy of shared pointer, we prefer to use reference except we do want to extend the lifetime of the object. **NOTE: the previous upstreamClusterInfo will return an absl::optional<Upstream::ClusterInfoConstSharedPtr> and now, the new upstreamClusterInfoSharedPtr will only return Upstream::ClusterInfoConstSharedPtr directly. Because we can find in the whole code base, no one want to distinguish it is nullptr or never been set.** See envoyproxy#44025 for the code style change. Risk Level: low. Even for the forks, it should be super easy to address this change. Testing: n/a. Docs Changes: n/a. Release Notes: n/a. Platform Specific Features: n/a. --------- Signed-off-by: wbpcode/wangbaiping <wbphub@gmail.com> Signed-off-by: code <wbphub@gmail.com> Co-authored-by: Raven Black <ravenblack@gmail.com>
…ClusterInfo() (envoyproxy#44042) Commit Message: stream info: return reference rather than copy of shared pointer Additional Description: Rather then return a copy of shared pointer, we prefer to use reference except we do want to extend the lifetime of the object. **NOTE: the previous upstreamClusterInfo will return an absl::optional<Upstream::ClusterInfoConstSharedPtr> and now, the new upstreamClusterInfoSharedPtr will only return Upstream::ClusterInfoConstSharedPtr directly. Because we can find in the whole code base, no one want to distinguish it is nullptr or never been set.** See envoyproxy#44025 for the code style change. Risk Level: low. Even for the forks, it should be super easy to address this change. Testing: n/a. Docs Changes: n/a. Release Notes: n/a. Platform Specific Features: n/a. --------- Signed-off-by: wbpcode/wangbaiping <wbphub@gmail.com> Signed-off-by: code <wbphub@gmail.com> Co-authored-by: Raven Black <ravenblack@gmail.com> Signed-off-by: Xuyang Tao <taoxuy@google.com>
…ClusterInfo() (envoyproxy#44042) Commit Message: stream info: return reference rather than copy of shared pointer Additional Description: Rather then return a copy of shared pointer, we prefer to use reference except we do want to extend the lifetime of the object. **NOTE: the previous upstreamClusterInfo will return an absl::optional<Upstream::ClusterInfoConstSharedPtr> and now, the new upstreamClusterInfoSharedPtr will only return Upstream::ClusterInfoConstSharedPtr directly. Because we can find in the whole code base, no one want to distinguish it is nullptr or never been set.** See envoyproxy#44025 for the code style change. Risk Level: low. Even for the forks, it should be super easy to address this change. Testing: n/a. Docs Changes: n/a. Release Notes: n/a. Platform Specific Features: n/a. --------- Signed-off-by: wbpcode/wangbaiping <wbphub@gmail.com> Signed-off-by: code <wbphub@gmail.com> Co-authored-by: Raven Black <ravenblack@gmail.com> Signed-off-by: Jonathan Wu <jtwu@google.com>
…ClusterInfo() (envoyproxy#44042) Commit Message: stream info: return reference rather than copy of shared pointer Additional Description: Rather then return a copy of shared pointer, we prefer to use reference except we do want to extend the lifetime of the object. **NOTE: the previous upstreamClusterInfo will return an absl::optional<Upstream::ClusterInfoConstSharedPtr> and now, the new upstreamClusterInfoSharedPtr will only return Upstream::ClusterInfoConstSharedPtr directly. Because we can find in the whole code base, no one want to distinguish it is nullptr or never been set.** See envoyproxy#44025 for the code style change. Risk Level: low. Even for the forks, it should be super easy to address this change. Testing: n/a. Docs Changes: n/a. Release Notes: n/a. Platform Specific Features: n/a. --------- Signed-off-by: wbpcode/wangbaiping <wbphub@gmail.com> Signed-off-by: code <wbphub@gmail.com> Co-authored-by: Raven Black <ravenblack@gmail.com> Signed-off-by: Nick Shipilov <nick.shipilov.n@gmail.com>
…ClusterInfo() (envoyproxy#44042) Commit Message: stream info: return reference rather than copy of shared pointer Additional Description: Rather then return a copy of shared pointer, we prefer to use reference except we do want to extend the lifetime of the object. **NOTE: the previous upstreamClusterInfo will return an absl::optional<Upstream::ClusterInfoConstSharedPtr> and now, the new upstreamClusterInfoSharedPtr will only return Upstream::ClusterInfoConstSharedPtr directly. Because we can find in the whole code base, no one want to distinguish it is nullptr or never been set.** See envoyproxy#44025 for the code style change. Risk Level: low. Even for the forks, it should be super easy to address this change. Testing: n/a. Docs Changes: n/a. Release Notes: n/a. Platform Specific Features: n/a. --------- Signed-off-by: wbpcode/wangbaiping <wbphub@gmail.com> Signed-off-by: code <wbphub@gmail.com> Co-authored-by: Raven Black <ravenblack@gmail.com>
Commit Message: stream info: return reference rather than copy of shared pointer
Additional Description:
Rather then return a copy of shared pointer, we prefer to use reference except we do want to extend the lifetime of the object.
NOTE: the previous upstreamClusterInfo will return an absl::optionalUpstream::ClusterInfoConstSharedPtr and now, the new upstreamClusterInfoSharedPtr will only return Upstream::ClusterInfoConstSharedPtr directly. Because we can find in the whole code base, no one want to distinguish it is nullptr or never been set.
See #44025 for the code style change.
Risk Level: low. Even for the forks, it should be super easy to address this change.
Testing: n/a.
Docs Changes: n/a.
Release Notes: n/a.
Platform Specific Features: n/a.