Skip to content

Commit

Permalink
ysql: Pass Postgres port to yb_servers function
Browse files Browse the repository at this point in the history
Summary:
Currently the port output column is hardcoded as 5433.

This revision passes the port via new field, pg_port, in the ServerRegistrationPB object.

Test Plan:
Verify on local cluster that the port is 5433 where tserver uses default proxy address:
```
yugabyte=# select * from yb_servers();
   host    | port | num_connections | node_type | cloud  |   region    | zone  | public_ip
-----------+------+-----------------+-----------+--------+-------------+-------+-----------
 127.0.0.1 | 5433 |               0 | primary   | cloud1 | datacenter1 | rack1 |
```

Reviewers: mihnea, nkumar, dmitry

Reviewed By: dmitry

Subscribers: bogdan, yql

Differential Revision: https://phabricator.dev.yugabyte.com/D12122
  • Loading branch information
tedyu committed Aug 5, 2021
1 parent a1b75e4 commit c4308a8
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 6 deletions.
13 changes: 12 additions & 1 deletion java/yb-pgsql/src/test/java/org/yb/pgsql/TestLoadBalance.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,18 @@ public void testYBServersFunction() throws Exception {
String publicIp = rs.getString(8);
Integer portInMap = hostPorts.get(host);
AssertionWrappers.assertNotNull(portInMap);
AssertionWrappers.assertEquals("port should be equal", 5433, port);
HostAndPort hp = HostAndPort.fromParts(host, portInMap);
MiniYBDaemon daemon = hostPortsDaemonMap.get(hp);
String[] cmds = daemon.getCommandLine();
int pg_port = 5433;
for (String cmd : cmds) {
if (cmd.contains("pgsql_proxy_bind_address")) {
int idx = cmd.indexOf(":");
pg_port = Integer.parseInt(cmd.substring(idx+1));
break;
}
}
AssertionWrappers.assertEquals("port should be equal", pg_port, port);
AssertionWrappers.assertEquals("primary", node_type);
AssertionWrappers.assertEquals("connections has been hardcoded to 0", 0, connections);
AssertionWrappers.assertEquals("cloud1", cloud);
Expand Down
2 changes: 1 addition & 1 deletion src/postgres/src/backend/utils/misc/pg_yb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ yb_servers(PG_FUNCTION_ARGS)
const char *node_type = is_primary ? "primary" : "read_replica";
// TODO: Remove hard coding of port and num_connections
values[0] = CStringGetTextDatum(server->host);
values[1] = Int64GetDatum(5433);
values[1] = Int64GetDatum(server->pgPort);
values[2] = Int64GetDatum(0);
values[3] = CStringGetTextDatum(node_type);
values[4] = CStringGetTextDatum(server->cloud);
Expand Down
3 changes: 2 additions & 1 deletion src/yb/client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,8 @@ Status YBClient::ListLiveTabletServers(
auto ts = std::make_unique<YBTabletServerPlacementInfo>(
entry.instance_id().permanent_uuid(),
DesiredHostPort(entry.registration().common(), data_->cloud_info_pb_).host(),
entry.registration().common().placement_uuid(), cloud, region, zone, isPrimary, publicIp);
entry.registration().common().placement_uuid(), cloud, region, zone, isPrimary,
publicIp, entry.registration().common().pg_port());
tablet_servers->push_back(std::move(ts));
}
return Status::OK();
Expand Down
11 changes: 9 additions & 2 deletions src/yb/client/tablet_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ class YBTabletServerPlacementInfo : public YBTabletServer {
YBTabletServerPlacementInfo(std::string uuid, std::string hostname,
std::string placement_uuid = "",
std::string cloud = "", std::string region = "",
std::string zone = "", bool primary = true, std::string publicIp = "")
std::string zone = "", bool primary = true, std::string publicIp = "",
uint16_t pg_port = 0)
: YBTabletServer(uuid, hostname, placement_uuid),
cloud_(std::move(cloud)), region_(std::move(region)),
zone_(std::move(zone)), is_primary_(primary), public_ip_(std::move(publicIp)) {}
zone_(std::move(zone)), is_primary_(primary), public_ip_(std::move(publicIp)),
pg_port_(pg_port) {}

const std::string& publicIp() const {
return public_ip_;
Expand All @@ -82,13 +84,18 @@ class YBTabletServerPlacementInfo : public YBTabletServer {
return is_primary_;
}

uint16_t pg_port() const {
return pg_port_;
}

private:

const std::string cloud_;
const std::string region_;
const std::string zone_;
const bool is_primary_;
const std::string public_ip_;
const uint16_t pg_port_;
};
} // namespace client
} // namespace yb
Expand Down
1 change: 1 addition & 0 deletions src/yb/common/wire_protocol.proto
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ message ServerRegistrationPB {
repeated HostPortPB private_rpc_addresses = 1;
repeated HostPortPB broadcast_addresses = 5;
repeated HostPortPB http_addresses = 2;
optional uint32 pg_port = 6 [default = 5433];

optional CloudInfoPB cloud_info = 3;
// Placement uuid of the tserver's cluster.
Expand Down
2 changes: 1 addition & 1 deletion src/yb/server/server_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class RpcAndWebServerBase : public RpcServerBase {
void GetStatusPB(ServerStatusPB* status) const override;

// Centralized method to get the Registration information for either the Master or Tserver.
CHECKED_STATUS GetRegistration(
virtual CHECKED_STATUS GetRegistration(
ServerRegistrationPB* reg, RpcOnly rpc_only = RpcOnly::kFalse) const;

protected:
Expand Down
6 changes: 6 additions & 0 deletions src/yb/tserver/tablet_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ Status TabletServer::Init() {
return Status::OK();
}

Status TabletServer::GetRegistration(ServerRegistrationPB* reg, server::RpcOnly rpc_only) const {
RETURN_NOT_OK(RpcAndWebServerBase::GetRegistration(reg, rpc_only));
reg->set_pg_port(pgsql_proxy_bind_address().port());
return Status::OK();
}

Status TabletServer::WaitInited() {
return tablet_manager_->WaitForAllBootstrapsToFinish();
}
Expand Down
3 changes: 3 additions & 0 deletions src/yb/tserver/tablet_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class TabletServer : public server::RpcAndWebServerBase, public TabletServerIf {
// complete by calling WaitInited().
CHECKED_STATUS Init();

CHECKED_STATUS GetRegistration(ServerRegistrationPB* reg,
server::RpcOnly rpc_only = server::RpcOnly::kFalse) const override;

// Waits for the tablet server to complete the initialization.
CHECKED_STATUS WaitInited();

Expand Down
1 change: 1 addition & 0 deletions src/yb/yql/pggate/pg_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,7 @@ Status PgSession::ListTabletServers(YBCServerDescriptor **servers, int *numofser
const char *zoneC = YBCPAllocStdString(zone);
const char *publicIpC = YBCPAllocStdString(publicIp);
servers[i] = reinterpret_cast<YBCServerDescriptor *>(YBCPAlloc(sizeof(YBCServerDescriptor)));
servers[i]->pgPort = tablet_servers[i]->pg_port();
servers[i]->host = hostC;
servers[i]->cloud = cloudC;
servers[i]->region = regionC;
Expand Down
1 change: 1 addition & 0 deletions src/yb/yql/pggate/ybc_pg_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ typedef struct PgServerDescriptor {
const char *zone;
bool isPrimary;
const char *publicIp;
uint16_t pgPort;
} YBCServerDescriptor;

#ifdef __cplusplus
Expand Down

0 comments on commit c4308a8

Please sign in to comment.