Skip to content

Commit 5822cce

Browse files
committed
YSQL: Add {active_ssts,wals}_size to yb_tablet_metadata
1 parent 213e8f9 commit 5822cce

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

src/postgres/src/backend/utils/misc/pg_yb_utils.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8396,6 +8396,17 @@ YbUseMinimalCatalogCachesPreload()
83968396
return false;
83978397
}
83988398

8399+
/* Sort replica indices lexicographically while keeping parallel arrays aligned. */
8400+
static int
8401+
replica_index_cmp(const void *a, const void *b, void *arg)
8402+
{
8403+
const char **replicas = (const char **) arg;
8404+
int ia = *(const int *) a;
8405+
int ib = *(const int *) b;
8406+
8407+
return strcmp(replicas[ia], replicas[ib]);
8408+
}
8409+
83998410
/*
84008411
* Returns the metadata for all tablets in the cluster.
84018412
* The returned data structure is a row type with the following columns:
@@ -8416,16 +8427,6 @@ YbUseMinimalCatalogCachesPreload()
84168427
* column for simpler querying and self-explanatory access. The size arrays are
84178428
* parallel to replicas (same index refers to the same replica).
84188429
*/
8419-
static int
8420-
replica_index_cmp(const void *a, const void *b, void *arg)
8421-
{
8422-
const char **replicas = (const char **) arg;
8423-
int ia = *(const int *) a;
8424-
int ib = *(const int *) b;
8425-
8426-
return strcmp(replicas[ia], replicas[ib]);
8427-
}
8428-
84298430
Datum
84308431
yb_get_tablet_metadata(PG_FUNCTION_ARGS)
84318432
{

src/yb/yql/pgwrapper/pg_mini-test.cc

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3307,20 +3307,50 @@ TEST_F(PgMiniTest, TabletMetadataOidMatchesPgClass) {
33073307
TEST_F(PgMiniTest, TabletMetadataSizeColumns) {
33083308
auto pg_conn = ASSERT_RESULT(Connect());
33093309
ASSERT_OK(pg_conn.Execute(
3310-
"CREATE TABLE size_test (id INT PRIMARY KEY, data TEXT)"));
3311-
ASSERT_OK(pg_conn.Execute(
3312-
"INSERT INTO size_test SELECT g, repeat('x', 100) FROM generate_series(1, 100) g"));
3313-
3314-
// Verify that the size columns exist and are non-null arrays whose length
3315-
// matches the replicas array length.
3316-
auto result = ASSERT_RESULT(pg_conn.FetchRow<bool>(
3317-
"SELECT bool_and("
3318-
" active_ssts_size IS NOT NULL AND"
3319-
" wals_size IS NOT NULL AND"
3320-
" array_length(active_ssts_size, 1) = array_length(replicas, 1) AND"
3321-
" array_length(wals_size, 1) = array_length(replicas, 1)"
3322-
") FROM yb_tablet_metadata WHERE relname = 'size_test'"));
3323-
ASSERT_TRUE(result) << "Size array columns must be non-null and parallel to replicas";
3310+
"CREATE TABLE size_test (id INT PRIMARY KEY, data TEXT) SPLIT INTO 1 TABLETS"));
3311+
3312+
// All three arrays must exist and have one element per replica.
3313+
auto [replicas_len, ssts_len, wals_len] = ASSERT_RESULT(
3314+
(pg_conn.FetchRow<int32_t, int32_t, int32_t>(
3315+
"SELECT array_length(replicas, 1),"
3316+
" array_length(active_ssts_size, 1),"
3317+
" array_length(wals_size, 1) "
3318+
"FROM yb_tablet_metadata WHERE relname = 'size_test'")));
3319+
ASSERT_EQ(replicas_len, ssts_len);
3320+
ASSERT_EQ(replicas_len, wals_len);
3321+
ASSERT_EQ(static_cast<size_t>(replicas_len), NumTabletServers());
3322+
3323+
// Unnest all three arrays in parallel to verify ordering and alignment.
3324+
// Uses WITH ORDINALITY to join corresponding elements in a single snapshot.
3325+
auto result = ASSERT_RESULT(pg_conn.Fetch(
3326+
"SELECT r.val, s.val, w.val "
3327+
"FROM yb_tablet_metadata tm, "
3328+
" unnest(tm.replicas) WITH ORDINALITY AS r(val, ord), "
3329+
" unnest(tm.active_ssts_size) WITH ORDINALITY AS s(val, ord), "
3330+
" unnest(tm.wals_size) WITH ORDINALITY AS w(val, ord) "
3331+
"WHERE tm.relname = 'size_test' "
3332+
" AND r.ord = s.ord AND r.ord = w.ord "
3333+
"ORDER BY r.ord"));
3334+
3335+
auto num_rows = PQntuples(result.get());
3336+
ASSERT_EQ(static_cast<size_t>(num_rows), NumTabletServers());
3337+
3338+
for (int i = 0; i < num_rows; ++i) {
3339+
auto replica = ASSERT_RESULT(GetValue<std::string>(result.get(), i, 0));
3340+
auto sst_size = ASSERT_RESULT(GetValue<int64_t>(result.get(), i, 1));
3341+
auto wal_size = ASSERT_RESULT(GetValue<int64_t>(result.get(), i, 2));
3342+
ASSERT_FALSE(replica.empty());
3343+
ASSERT_GE(sst_size, 0) << "replica " << replica;
3344+
ASSERT_GE(wal_size, 0) << "replica " << replica;
3345+
}
3346+
3347+
// Every replica address must correspond to a live tserver.
3348+
auto valid_count = ASSERT_RESULT(pg_conn.FetchRow<int64_t>(
3349+
"SELECT count(*) FROM yb_servers() s "
3350+
"WHERE s.host || ':' || s.port IN ("
3351+
" SELECT unnest(replicas) FROM yb_tablet_metadata WHERE relname = 'size_test'"
3352+
")"));
3353+
ASSERT_EQ(static_cast<size_t>(valid_count), NumTabletServers());
33243354
}
33253355

33263356
TEST_F(PgMiniTest, TestYbGetLocalTserverUuid) {

0 commit comments

Comments
 (0)