-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#7794] YSQL: Add option to forward pggate RPCs to local tserver
Summary: Each postgres process connects to all the Tservers in the cluster which leads to a quadratic growth in the number of connections in the cluster. To reduce the number of connections, we can forward all the rpcs to the local Tserver which can then forward the rpc to the appropriate Tserver. This feature involves 2 changes: 1. Forward all the rpcs from pggate to the local tserver. 2. Ensure the local tserver sends the rpcs to the appropriate tserver. First change involves creating a RemoteTabletServer for the local tserver and using that for all the PGSQL read/write rpcs. This needs the postgres process to be passed the UUID of the tserver process so that it can identify the HostPort of the local tserver from the list of all tservers. The second change involves creating the ForwardRpc class which forwards the read/write request protobufs to the appropriate tserver by using the TabletInvoker. This class is invoked by the TabletServiceImpl on realizing that the request is not meant for itself. This feature is controlled by the GFLAG: ysql_forward_rpcs_to_local_tserver which allows us to completely enable/disable the forwarding of rpcs to the local tserver. Test Plan: All java tests for pggate. Reviewers: amitanand, rskannan, rsami, timur, dmitry, mihnea Reviewed By: mihnea Subscribers: dmitry, zyu, bogdan, yql Differential Revision: https://phabricator.dev.yugabyte.com/D10274
- Loading branch information
Sudheer
committed
May 21, 2021
1 parent
3933cbd
commit 6077bc8
Showing
20 changed files
with
686 additions
and
9 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
java/yb-pgsql/src/test/java/org/yb/pgsql/TestPgRpcForwarding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright (c) YugaByte, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed under the License | ||
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
// or implied. See the License for the specific language governing permissions and limitations | ||
// under the License. | ||
// | ||
|
||
package org.yb.pgsql; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.yb.util.YBTestRunnerNonTsanOnly; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.sql.*; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import com.google.common.net.HostAndPort; | ||
import org.yb.minicluster.MiniYBDaemon; | ||
import org.postgresql.util.PSQLException; | ||
import static org.yb.AssertionWrappers.*; | ||
|
||
@RunWith(value=YBTestRunnerNonTsanOnly.class) | ||
public class TestPgRpcForwarding extends BasePgSQLTest { | ||
private static final Logger LOG = LoggerFactory.getLogger(TestPgRpcForwarding.class); | ||
private static final int SLEEP_DURATION = 5000; | ||
|
||
@Override | ||
protected Map<String, String> getTServerFlags() { | ||
Map<String, String> flags = super.getTServerFlags(); | ||
flags.put("ysql_forward_rpcs_to_local_tserver", "true"); | ||
return flags; | ||
} | ||
|
||
private int getForwardRpcCount() throws Exception { | ||
Map<HostAndPort, MiniYBDaemon> tservers = miniCluster.getTabletServers(); | ||
int count = 0; | ||
|
||
for (Map.Entry<HostAndPort,MiniYBDaemon> entry : tservers.entrySet()) { | ||
HostAndPort hostPort = entry.getKey(); | ||
int port = tservers.get(hostPort).getWebPort(); | ||
|
||
// Call the prometheus-metrics endpoint and grep for the multi touch hit metric in the list. | ||
URL url = null; | ||
BufferedReader br = null; | ||
try { | ||
url = new URL(String.format("http://%s:%d/prometheus-metrics", hostPort.getHost(), port)); | ||
br = new BufferedReader(new InputStreamReader(url.openStream()));; | ||
} catch (Exception ex) { | ||
LOG.error("Encountered error for reading metrics endpoint" + ex); | ||
throw new InternalError(ex.getMessage()); | ||
} | ||
String line = null; | ||
while ((line = br.readLine()) != null) { | ||
if (line.contains("TabletServerForwardService_Write_count") || | ||
line.contains("TabletServerForwardService_Read_count")) { | ||
String inp[] = line.split(" "); | ||
String x = inp[inp.length - 2].trim(); | ||
int val = Integer.parseInt(x); | ||
count += val; | ||
} | ||
} | ||
} | ||
LOG.info("Forward service count is " + count); | ||
return count; | ||
|
||
} | ||
|
||
@Test | ||
public void testRpcForwarding() throws Exception { | ||
int numRows = 100; | ||
try (Statement statement = connection.createStatement()) { | ||
statement.execute("create table t(a int primary key, b int)"); | ||
|
||
int count1 = getForwardRpcCount(); | ||
for (int i = 1; i <= numRows; ++i) { | ||
statement.execute("insert into t values(" + i + "," + i + ")"); | ||
} | ||
int count2 = getForwardRpcCount(); | ||
assertTrue(count2 > count1); | ||
|
||
for (int i = 1; i <= numRows; ++i) { | ||
ResultSet rs = statement.executeQuery(String.format("select * from t where a=%d", i)); | ||
while (rs.next()) { | ||
int a = rs.getInt("a"); | ||
int b = rs.getInt("b"); | ||
assertTrue(a == b); | ||
} | ||
} | ||
int count3 = getForwardRpcCount(); | ||
assertTrue(count3 > count2); | ||
} catch (PSQLException ex) { | ||
LOG.error("Unexpected exception:", ex); | ||
throw ex; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.