This is an automated email from the ASF dual-hosted git repository.
alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git
The following commit(s) were added to refs/heads/master by this push:
new 5544e4b27 [rpc] add local_address() to Connection
5544e4b27 is described below
commit 5544e4b27ee1643aadcff78d0ba3529b3ca40aca
Author: Alexey Serbin <[email protected]>
AuthorDate: Thu Oct 13 19:30:31 2022 -0700
[rpc] add local_address() to Connection
This patch adds a means to get local address of the Connection object.
A follow-up patch will be using this to distinguish between RPC
arrived through a dedicated endpoint.
Change-Id: I4aa8a035d8f0c150ad823f7817c39e689edf3d19
Reviewed-on: http://gerrit.cloudera.org:8080/19229
Reviewed-by: Attila Bukor <[email protected]>
Tested-by: Alexey Serbin <[email protected]>
---
src/kudu/rpc/connection.cc | 6 ++++++
src/kudu/rpc/connection.h | 5 ++++-
src/kudu/rpc/inbound_call.cc | 7 +++++++
src/kudu/rpc/inbound_call.h | 8 +++++++-
src/kudu/rpc/rpc_context.cc | 4 ++++
src/kudu/rpc/rpc_context.h | 4 ++++
6 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/src/kudu/rpc/connection.cc b/src/kudu/rpc/connection.cc
index 11298d7ec..369a30275 100644
--- a/src/kudu/rpc/connection.cc
+++ b/src/kudu/rpc/connection.cc
@@ -416,6 +416,12 @@ void Connection::CancelOutboundCall(const
shared_ptr<OutboundCall> &call) {
}
}
+Status Connection::GetLocalAddress(Sockaddr* addr) const {
+ DCHECK(socket_);
+ DCHECK(addr);
+ return socket_->GetSocketAddress(addr);
+}
+
// Inject a cancellation when 'call' is in state
'FLAGS_rpc_inject_cancellation_state'.
void inline Connection::MaybeInjectCancellation(const shared_ptr<OutboundCall>
&call) {
if (PREDICT_FALSE(call->ShouldInjectCancellation())) {
diff --git a/src/kudu/rpc/connection.h b/src/kudu/rpc/connection.h
index e1907b435..d0afbe536 100644
--- a/src/kudu/rpc/connection.h
+++ b/src/kudu/rpc/connection.h
@@ -141,7 +141,10 @@ class Connection : public RefCountedThreadSafe<Connection>
{
void CancelOutboundCall(const std::shared_ptr<OutboundCall> &call);
// The address of the remote end of the connection.
- const Sockaddr &remote() const { return remote_; }
+ const Sockaddr& remote() const { return remote_; }
+
+ // The address of the local end of the connection.
+ Status GetLocalAddress(Sockaddr* addr) const;
// Set the user credentials for an outbound connection.
void set_outbound_connection_id(ConnectionId conn_id) {
diff --git a/src/kudu/rpc/inbound_call.cc b/src/kudu/rpc/inbound_call.cc
index 2e0e222ca..e2c916125 100644
--- a/src/kudu/rpc/inbound_call.cc
+++ b/src/kudu/rpc/inbound_call.cc
@@ -77,6 +77,8 @@ InboundCall::~InboundCall() {}
Status InboundCall::ParseFrom(unique_ptr<InboundTransfer> transfer) {
TRACE_EVENT_FLOW_BEGIN0("rpc", "InboundCall", this);
+ RETURN_NOT_OK(conn_->GetLocalAddress(&local_addr_));
+
TRACE_EVENT0("rpc", "InboundCall::ParseFrom");
RETURN_NOT_OK(serialization::ParseMessage(transfer->data(), &header_,
&serialized_request_));
@@ -273,6 +275,11 @@ const Sockaddr& InboundCall::remote_address() const {
return conn_->remote();
}
+const Sockaddr& InboundCall::local_address() const {
+ DCHECK(local_addr_.is_initialized());
+ return local_addr_;
+}
+
const scoped_refptr<Connection>& InboundCall::connection() const {
return conn_;
}
diff --git a/src/kudu/rpc/inbound_call.h b/src/kudu/rpc/inbound_call.h
index 1c83c7be4..d0dd75e5f 100644
--- a/src/kudu/rpc/inbound_call.h
+++ b/src/kudu/rpc/inbound_call.h
@@ -36,6 +36,7 @@
#include "kudu/rpc/transfer.h"
#include "kudu/util/faststring.h"
#include "kudu/util/monotime.h"
+#include "kudu/util/net/sockaddr.h"
#include "kudu/util/slice.h"
#include "kudu/util/status.h"
@@ -48,7 +49,6 @@ class MessageLite;
namespace kudu {
class Histogram;
-class Sockaddr;
class Trace;
namespace rpc {
@@ -147,6 +147,8 @@ class InboundCall {
const Sockaddr& remote_address() const;
+ const Sockaddr& local_address() const;
+
const scoped_refptr<Connection>& connection() const;
Trace* trace();
@@ -243,6 +245,10 @@ class InboundCall {
// The connection on which this inbound call arrived.
scoped_refptr<Connection> conn_;
+ // Local address of the connection above. This field caches the result of the
+ // Connection::GetLocalAddress() call. Set by ParseFrom().
+ Sockaddr local_addr_;
+
// The header of the incoming call. Set by ParseFrom()
RequestHeader header_;
diff --git a/src/kudu/rpc/rpc_context.cc b/src/kudu/rpc/rpc_context.cc
index 1c5e67f8f..c7101cc8c 100644
--- a/src/kudu/rpc/rpc_context.cc
+++ b/src/kudu/rpc/rpc_context.cc
@@ -175,6 +175,10 @@ const Sockaddr& RpcContext::remote_address() const {
return call_->remote_address();
}
+const Sockaddr& RpcContext::local_address() const {
+ return call_->local_address();
+}
+
std::string RpcContext::requestor_string() const {
return call_->remote_user().ToString() + " at " +
call_->remote_address().ToString();
diff --git a/src/kudu/rpc/rpc_context.h b/src/kudu/rpc/rpc_context.h
index 6b68ad78f..e66941c32 100644
--- a/src/kudu/rpc/rpc_context.h
+++ b/src/kudu/rpc/rpc_context.h
@@ -192,6 +192,10 @@ class RpcContext {
// Return the remote IP address and port which sent the current RPC call.
const Sockaddr& remote_address() const;
+ // Return the local IP address and port of the connection which is used
+ // to receive the current RPC call.
+ const Sockaddr& local_address() const;
+
// A string identifying the requestor -- both the user info and the IP
address.
// Suitable for use in log messages.
std::string requestor_string() const;