-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#23542] YSQL: Add new YSQL function yb_servers_metrics() to fetch me…
…trics such as cpu/memory usage from all nodes in cluster Summary: To enable adaptive parallelism in voyager, https://docs.google.com/document/d/1beD7zNtpmfYflXV1hVJ9mq_uqyCTJ9Es4titPEksSNE/edit#heading=h.3c3bf00hwf, a YSQL function yb_servers_metrics() is added which will fetch certain metrics for all nodes in the cluster. This allows voyager to monitor the state of the cluster, and adapt the parallelism while importing data to target YB cluster. A YSQL API is needed in order to provide deployment-agnostic API (not having to fetch metrics for YBA/YBM/on-prem using different mechanisms). Additionally, made a few changes to `MetricsSnapshotter` - Introduced a function for GetCpuUsageInInterval(int ms). - made the GetCpuUsage function static. - Introduced a `GetMemoryUsage` function to get memory usage (from proc/meminfo for linux and sysctl for macos) Sample output: ``` yugabyte=# select uuid, jsonb_pretty(metrics), status, error from yb_servers_metrics(); uuid | jsonb_pretty | status | error ----------------------------------+-----------------------------------------------------+--------+------- bf98c74dd7044b34943c5bff7bd3d0d1 | { +| OK | | "memory_free": "0", +| | | "memory_total": "17179869184", +| | | "cpu_usage_user": "0.135827", +| | | "cpu_usage_system": "0.118110", +| | | "memory_available": "0", +| | | "tserver_root_memory_limit": "11166914969", +| | | "tserver_root_memory_soft_limit": "9491877723",+| | | "tserver_root_memory_consumption": "52346880" +| | | } | | d105c3a6128640f5a25cc74435e48ae3 | { +| OK | | "memory_free": "0", +| | | "memory_total": "17179869184", +| | | "cpu_usage_user": "0.135189", +| | | "cpu_usage_system": "0.119284", +| | | "memory_available": "0", +| | | "tserver_root_memory_limit": "11166914969", +| | | "tserver_root_memory_soft_limit": "9491877723",+| | | "tserver_root_memory_consumption": "55074816" +| | | } | | a321e13e5bf24060a764b35894cd4070 | { +| OK | | "memory_free": "0", +| | | "memory_total": "17179869184", +| | | "cpu_usage_user": "0.135827", +| | | "cpu_usage_system": "0.118110", +| | | "memory_available": "0", +| | | "tserver_root_memory_limit": "11166914969", +| | | "tserver_root_memory_soft_limit": "9491877723",+| | | "tserver_root_memory_consumption": "62062592" +| | | } | | ``` **Upgrade/Rollback safety:** This is a new YSQL function, so there won't be any prior users of this function. In case of an upgrade/rollback, the sql migration (that adds the function to pg_proc) will only run when the upgrade is being finalized (i.e. after all tservers are updated). Hence, it will not be possible to get errors due to a subset of tservers not being upgraded because the function itself will not be available to call. Test Plan: ./yb_build.sh --java-test 'org.yb.pgsql.TestYbServersMetrics#testYBServersMetricsFunction' Reviewers: asaha, djiang, telgersma Reviewed By: djiang, telgersma Subscribers: hbhanawat, yql, ybase, amakala Differential Revision: https://phorge.dev.yugabyte.com/D37267
- Loading branch information
1 parent
294b7bb
commit 872b59e
Showing
31 changed files
with
624 additions
and
13 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
java/yb-pgsql/src/test/java/org/yb/pgsql/TestYbServersMetrics.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,131 @@ | ||
// 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 com.google.common.net.HostAndPort; | ||
import org.json.JSONObject; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.yb.minicluster.MiniYBClusterBuilder; | ||
import org.yb.YBTestRunner; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.List; | ||
|
||
import static org.yb.AssertionWrappers.*; | ||
|
||
@RunWith(value = YBTestRunner.class) | ||
public class TestYbServersMetrics extends BasePgSQLTest { | ||
private static final Logger LOG = LoggerFactory.getLogger(TestYbServersMetrics.class); | ||
private static final int NUM_TSERVERS = 3; | ||
private static final int RF = 3; | ||
private static ArrayList<String> expectedKeys = new ArrayList<String>(Arrays.asList( | ||
"memory_free", "memory_available", "memory_total", | ||
"tserver_root_memory_limit", "tserver_root_memory_soft_limit", | ||
"tserver_root_memory_consumption", | ||
"cpu_usage_user", "cpu_usage_system")); | ||
|
||
@Override | ||
public ConnectionBuilder getConnectionBuilder() { | ||
ConnectionBuilder cb = new ConnectionBuilder(miniCluster); | ||
cb.setLoadBalance(true); | ||
return cb; | ||
} | ||
|
||
@Override | ||
protected void customizeMiniClusterBuilder(MiniYBClusterBuilder builder){ | ||
super.customizeMiniClusterBuilder(builder); | ||
builder.numTservers(NUM_TSERVERS); | ||
builder.replicationFactor(RF); | ||
builder.tserverHeartbeatTimeoutMs(7000); | ||
} | ||
|
||
private void assertYbServersMetricsOutput(int expectedRows, int expectedStatusOkRows, | ||
int tserverNo) throws Exception{ | ||
ConnectionBuilder b = getConnectionBuilder(); | ||
if (tserverNo >= 0){ | ||
b = b.withTServer(tserverNo); | ||
} | ||
Connection conn = b.connect(); | ||
try { | ||
Statement st = conn.createStatement(); | ||
final long startTimeMillis = System.currentTimeMillis(); | ||
ResultSet rs = st.executeQuery("select * from yb_servers_metrics()"); | ||
final long result = System.currentTimeMillis() - startTimeMillis; | ||
// There is a timeout of 5000ms for each RPC call to tserver. | ||
assertLessThan(result, Long.valueOf(6000)); | ||
int row_count = 0; | ||
int ok_count = 0; | ||
List<String> errors = new ArrayList<String>(); | ||
while (rs.next()) { | ||
String uuid = rs.getString(1); | ||
String metrics = rs.getString(2); | ||
String status = rs.getString(3); | ||
String error = rs.getString(4); | ||
if (status.equals("OK")) { | ||
++ok_count; | ||
JSONObject metricsJson = new JSONObject(metrics); | ||
ArrayList<String> metricKeys = new ArrayList<String>(metricsJson.keySet()); | ||
assertTrue("Expected keys are not present. Present keys are:" | ||
+ metricKeys, | ||
metricKeys.containsAll(expectedKeys)); | ||
} else { | ||
assertEquals("{}", metrics); | ||
errors.add(error); | ||
} | ||
++row_count; | ||
} | ||
assertEquals("Unexpected tservers count", expectedRows, row_count); | ||
assertEquals("Unexpected OK tserver count. Errors: "+ errors, expectedStatusOkRows, ok_count); | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Failed to execute yb_servers_metrics query", e); | ||
} finally { | ||
conn.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testYBServersMetricsFunction() throws Exception { | ||
assertYbServersMetricsOutput(NUM_TSERVERS, NUM_TSERVERS, -1); | ||
|
||
// add a new tserver | ||
miniCluster.startTServer(getTServerFlags()); | ||
assertTrue(miniCluster.waitForTabletServers(4)); | ||
waitForTServerHeartbeat(); | ||
assertYbServersMetricsOutput(NUM_TSERVERS + 1, NUM_TSERVERS + 1, -1); | ||
|
||
// kill a tserver | ||
// closing root connection to avoid potential errors during clean up. | ||
connection.close(); | ||
List<HostAndPort> tserverList = new ArrayList<>(miniCluster.getTabletServers().keySet()); | ||
HostAndPort tserver = tserverList.get(tserverList.size() - 1); | ||
miniCluster.killTabletServerOnHostPort(tserver); | ||
// Initially we will get NUM_TSERVERS + 1 rows, with one of them having status as "ERROR" | ||
//killed last tserver, so connect to first | ||
assertYbServersMetricsOutput(NUM_TSERVERS + 1, NUM_TSERVERS, 0); | ||
|
||
// After the tserver is removed and updated in cache, | ||
// we will get NUM_TSERVERS rows, with all of them having status as "OK" | ||
Thread.sleep(2 * miniCluster.getClusterParameters().getTServerHeartbeatTimeoutMs()); | ||
assertYbServersMetricsOutput(NUM_TSERVERS, NUM_TSERVERS, 0); | ||
} | ||
|
||
} |
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
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.