Skip to content

Commit

Permalink
Merge pull request #120059 from xinhaoz/backport23.1-118904
Browse files Browse the repository at this point in the history
release-23.1: ui: improve db page nodes/regions query
  • Loading branch information
xinhaoz authored Mar 8, 2024
2 parents 5861f17 + 99baa70 commit e0fe00b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 55 deletions.
35 changes: 12 additions & 23 deletions pkg/ui/workspaces/cluster-ui/src/api/databaseDetailsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ function newDatabaseDetailsResponse(): DatabaseDetailsResponse {
range_count: 0,
},
replicaData: {
replicas: [],
regions: [],
storeIDs: [],
},
indexStats: { num_index_recommendations: 0 },
},
Expand Down Expand Up @@ -294,7 +293,10 @@ const getDatabaseZoneConfig: DatabaseDetailsQuery<DatabaseZoneConfigRow> = {
// Database Stats
type DatabaseDetailsStats = {
spanStats: SqlApiQueryResponse<DatabaseSpanStatsRow>;
replicaData: SqlApiQueryResponse<DatabaseReplicasRegionsRow>;
replicaData: {
storeIDs: number[];
error?: Error;
};
indexStats: SqlApiQueryResponse<DatabaseIndexUsageStatsResponse>;
};

Expand Down Expand Up @@ -345,44 +347,31 @@ const getDatabaseSpanStats: DatabaseDetailsQuery<DatabaseSpanStatsRow> = {
};

type DatabaseReplicasRegionsRow = {
replicas: number[];
regions: string[];
store_ids: number[];
};

const getDatabaseReplicasAndRegions: DatabaseDetailsQuery<DatabaseReplicasRegionsRow> =
{
createStmt: dbName => {
// This query is meant to retrieve the per-database set of store ids.
return {
sql: Format(
`WITH
replicasAndregions as (
SELECT
r.replicas,
ARRAY(SELECT DISTINCT split_part(split_part(unnest(replica_localities),',',1),'=',2)) as regions
FROM crdb_internal.tables as t
JOIN %1.crdb_internal.table_spans as s ON s.descriptor_id = t.table_id
JOIN crdb_internal.ranges_no_leases as r ON s.start_key < r.end_key AND s.end_key > r.start_key
WHERE t.database_name = $1
),
unique_replicas AS (SELECT array_agg(distinct(unnest(replicas))) as replicas FROM replicasAndRegions),
unique_regions AS (SELECT array_agg(distinct(unnest(regions))) as regions FROM replicasAndRegions)
SELECT replicas, regions FROM unique_replicas CROSS JOIN unique_regions`,
`
SELECT array_agg(DISTINCT unnested_store_ids) AS store_ids
FROM [SHOW RANGES FROM DATABASE %1], unnest(replicas) AS unnested_store_ids
`,
[new Identifier(dbName)],
),
arguments: [dbName],
};
},
addToDatabaseDetail: (
txn_result: SqlTxnResult<DatabaseReplicasRegionsRow>,
resp: DatabaseDetailsResponse,
) => {
if (!txnResultIsEmpty(txn_result)) {
resp.stats.replicaData.regions = txn_result.rows[0].regions;
resp.stats.replicaData.replicas = txn_result.rows[0].replicas;
}
if (txn_result.error) {
resp.stats.replicaData.error = txn_result.error;
}
resp.stats.replicaData.storeIDs = txn_result?.rows[0]?.store_ids ?? [];
},
handleMaxSizeError: (_dbName, _response, _dbDetail) => {
return Promise.resolve(false);
Expand Down
7 changes: 4 additions & 3 deletions pkg/ui/workspaces/cluster-ui/src/databases/combiners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ const deriveDatabaseDetails = (
isTenant: boolean,
): DatabasesPageDataDatabase => {
const dbStats = dbDetails?.data?.results.stats;
const nodes = dbStats?.replicaData.replicas || [];
// TODO #118957 (xinhaoz) Use store id to regions mapping.
const stores = dbStats?.replicaData.storeIDs || [];
const nodesByRegionString = getNodesByRegionString(
nodes,
stores,
nodeRegionsByID,
isTenant,
);
Expand All @@ -86,7 +87,7 @@ const deriveDatabaseDetails = (
name: database,
spanStats: dbStats?.spanStats,
tables: dbDetails?.data?.results.tablesResp,
nodes: nodes,
nodes: stores,
nodesByRegionString,
numIndexRecommendations,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ describe("DatabaseDetails sagas", () => {
range_count: 20,
},
replicaData: {
replicas: [1, 2, 3],
regions: ["this", "is", "a", "region"],
storeIDs: [1, 2, 3],
},
indexStats: { num_index_recommendations: 4 },
},
Expand Down
3 changes: 1 addition & 2 deletions pkg/ui/workspaces/db-console/src/util/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ describe("rest api", function () {
{
rows: [
{
replicas: [1, 2, 3],
regions: ["gcp-europe-west1", "gcp-europe-west2"],
store_ids: [1, 2, 3],
},
],
},
Expand Down
19 changes: 0 additions & 19 deletions pkg/ui/workspaces/db-console/src/util/fakeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,6 @@ function stubGet(path: string, writer: $protobuf.Writer, prefix: string) {
fetchMock.get(`${prefix}${path}`, writer.finish());
}

export function createMockDatabaseRangesForTable(
numRangesCreate: number,
numNodes: number,
): clusterUiApi.DatabaseDetailsRow[] {
const res = [];
const replicas = [];
for (let i = 1; i <= numNodes; i++) {
replicas.push(i);
}
for (let i = 0; i < numRangesCreate; i++) {
res.push({
replicas: replicas,
regions: ["gcp-europe-west1", "gcp-europe-west2"],
range_size: 10,
});
}
return res;
}

export function stubSqlApiCall<T>(
req: clusterUiApi.SqlExecutionRequest,
mockTxnResults: mockSqlTxnResult<T>[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,13 @@ describe("Databases Page", function () {
{
rows: [
{
replicas: [1, 2, 3],
regions: ["gcp-europe-west1", "gcp-europe-west2"],
store_ids: [1, 2, 3],
},
{
replicas: [1, 2, 3],
regions: ["gcp-europe-west1", "gcp-europe-west2"],
store_ids: [1, 2, 3],
},
{
replicas: [1, 2, 3],
regions: ["gcp-europe-west1", "gcp-europe-west2"],
store_ids: [1, 2, 3],
},
],
},
Expand Down

0 comments on commit e0fe00b

Please sign in to comment.