Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: improve db page nodes/regions query #118904

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -73,8 +73,7 @@ function newDatabaseDetailsResponse(): DatabaseDetailsResponse {
},
stats: {
replicaData: {
replicas: [],
regions: [],
storeIDs: [],
},
indexStats: { num_index_recommendations: 0 },
},
Expand Down Expand Up @@ -309,7 +308,10 @@ const getDatabaseZoneConfig: DatabaseDetailsQuery<DatabaseZoneConfigRow> = {

// Database Stats
type DatabaseDetailsStats = {
replicaData: SqlApiQueryResponse<DatabaseReplicasRegionsRow>;
replicaData: {
storeIDs: number[];
error?: Error;
};
indexStats: SqlApiQueryResponse<DatabaseIndexUsageStatsResponse>;
};

Expand Down Expand Up @@ -354,44 +356,31 @@ function formatSpanStatsExecutionResult(
}

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 replicasAndRegionsPerDbRange 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
)
SELECT
array_agg(DISTINCT replica_val) AS replicas,
array_agg(DISTINCT region_val) AS regions
FROM replicasAndRegionsPerDbRange, unnest(replicas) AS replica_val, unnest(regions) AS region_val`,
`
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 @@ -78,9 +78,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 @@ -99,7 +100,7 @@ const deriveDatabaseDetails = (
name: database,
spanStats: spanStats?.data?.results.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 @@ -69,8 +69,7 @@ describe("DatabaseDetails sagas", () => {
},
stats: {
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 @@ -189,8 +189,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 @@ -254,16 +254,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
Loading