Skip to content

Commit 1bcb91e

Browse files
committed
console: configure a cluster from a drawer on the redesigned cluster page
Adds a gear button next to the redesigned cluster page's title. It opens a configuration drawer in the connect drawer's format, with cards for the cluster's overview, its replicas, and its autoscaling policy, and replaces the Replicas tab. Editing reuses the existing alter cluster, new replica, and drop flows.
1 parent e2f7657 commit 1bcb91e

27 files changed

Lines changed: 2057 additions & 54 deletions

‎console/icon/SettingsIcon.svg‎

Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright Materialize, Inc. and contributors. All rights reserved.
2+
//
3+
// Use of this software is governed by the Business Source License
4+
// included in the LICENSE file.
5+
//
6+
// As of the Change Date specified in that file, in accordance with
7+
// the Business Source License, use of this software will be governed
8+
// by the Apache License, Version 2.0.
9+
10+
import { parseAutoScaling } from "~/platform/clusters/ClusterPage/Configuration/clusterConfigurationModel";
11+
import {
12+
executeSqlHttp,
13+
getMaterializeClient,
14+
} from "~/test/sql/materializeSqlClient";
15+
import { testdrive } from "~/test/sql/mzcompose";
16+
17+
import { buildClusterAutoScalingQuery } from "./clusterConfiguration";
18+
import { buildObjectCreationTimesQuery } from "./objectCreationTimes";
19+
20+
const OPTIONS = { sessionVariables: { transaction_isolation: "serializable" } };
21+
22+
async function clusterIdByName(name: string) {
23+
const client = await getMaterializeClient();
24+
const {
25+
rows: [cluster],
26+
} = await client.query("SELECT id FROM mz_clusters WHERE name = $1", [name]);
27+
return cluster.id as string;
28+
}
29+
30+
describe("cluster configuration queries", () => {
31+
it("tells a cluster's creation from a replica's with the same id", async () => {
32+
await testdrive(`> SET cluster to quickstart;`);
33+
const client = await getMaterializeClient();
34+
// Clusters and replicas take ids from separate counters, so the first of
35+
// each share an id.
36+
const {
37+
rows: [shared],
38+
} = await client.query(
39+
`SELECT c.id FROM mz_clusters c JOIN mz_cluster_replicas r ON r.id = c.id LIMIT 1`,
40+
);
41+
expect(shared?.id).toBeDefined();
42+
43+
const { rows } = await executeSqlHttp(
44+
buildObjectCreationTimesQuery({
45+
objectIds: [shared.id],
46+
objectTypes: ["cluster", "cluster-replica"],
47+
}).compile(),
48+
OPTIONS,
49+
);
50+
51+
expect(rows.map(({ id, objectType }) => [id, objectType]).sort()).toEqual([
52+
[shared.id, "cluster"],
53+
[shared.id, "cluster-replica"],
54+
]);
55+
expect(rows.every(({ occurredAt }) => occurredAt instanceof Date)).toBe(
56+
true,
57+
);
58+
});
59+
60+
it("reads a cluster's autoscaling policy as the drawer parses it", async () => {
61+
await testdrive(`
62+
$ postgres-execute connection=postgres://mz_system@\${testdrive.materialize-internal-sql-addr}/materialize
63+
ALTER SYSTEM SET enable_auto_scaling_strategy = true
64+
65+
> CREATE CLUSTER lingering (SIZE 'scale=1,workers=1', AUTO SCALING STRATEGY = (ON HYDRATION (HYDRATION SIZE = 'scale=1,workers=2', LINGER DURATION = '120s')));
66+
> CREATE CLUSTER defaulted (SIZE 'scale=1,workers=1', AUTO SCALING STRATEGY = (ON HYDRATION (HYDRATION SIZE = 'scale=1,workers=2')));
67+
> CREATE CLUSTER steady (SIZE 'scale=1,workers=1');
68+
`);
69+
70+
const policy = async (name: string) => {
71+
const { rows } = await executeSqlHttp(
72+
buildClusterAutoScalingQuery(await clusterIdByName(name)).compile(),
73+
OPTIONS,
74+
);
75+
return parseAutoScaling(rows.at(0) ?? null).policy;
76+
};
77+
78+
expect(await policy("lingering")).toEqual({
79+
hydrationSize: "scale=1,workers=2",
80+
lingerMs: 120_000,
81+
});
82+
// An omitted linger arrives as a null duration, the server's default.
83+
expect(await policy("defaulted")).toEqual({
84+
hydrationSize: "scale=1,workers=2",
85+
lingerMs: undefined,
86+
});
87+
expect(await policy("steady")).toBeNull();
88+
});
89+
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright Materialize, Inc. and contributors. All rights reserved.
2+
//
3+
// Use of this software is governed by the Business Source License
4+
// included in the LICENSE file.
5+
//
6+
// As of the Change Date specified in that file, in accordance with
7+
// the Business Source License, use of this software will be governed
8+
// by the Apache License, Version 2.0.
9+
10+
import { QueryKey } from "@tanstack/react-query";
11+
12+
import { executeSqlV2, queryBuilder } from "~/api/materialize";
13+
14+
import { buildObjectCreationTimesQuery } from "./objectCreationTimes";
15+
16+
/** First version with `mz_internal.mz_cluster_auto_scaling_strategies`. */
17+
export const AUTO_SCALING_MIN_VERSION = "26.34.0";
18+
19+
/** A cluster's autoscaling strategy and any in-flight burst, as JSON. */
20+
export function buildClusterAutoScalingQuery(clusterId: string) {
21+
return queryBuilder
22+
.selectFrom("mz_cluster_auto_scaling_strategies")
23+
.select(["strategy", "state"])
24+
.where("cluster_id", "=", clusterId);
25+
}
26+
27+
/** The configuration the cluster list doesn't carry, read in one request. */
28+
export async function fetchClusterConfiguration({
29+
params: { clusterId, replicaIds, includeAutoScaling },
30+
queryKey,
31+
requestOptions,
32+
}: {
33+
params: {
34+
clusterId: string;
35+
replicaIds: string[];
36+
/** The autoscaling relation only exists from `AUTO_SCALING_MIN_VERSION`. */
37+
includeAutoScaling: boolean;
38+
};
39+
queryKey: QueryKey;
40+
requestOptions?: RequestInit;
41+
}) {
42+
const creationTimes = buildObjectCreationTimesQuery({
43+
objectIds: [clusterId, ...replicaIds],
44+
objectTypes: ["cluster", "cluster-replica"],
45+
}).compile();
46+
const sessionVariables = { transaction_isolation: "serializable" };
47+
if (!includeAutoScaling) {
48+
const [creationTimesRes] = await executeSqlV2({
49+
queries: [creationTimes] as const,
50+
queryKey,
51+
requestOptions,
52+
sessionVariables,
53+
});
54+
return { creationTimes: creationTimesRes.rows, autoScaling: undefined };
55+
}
56+
const [creationTimesRes, autoScalingRes] = await executeSqlV2({
57+
queries: [
58+
creationTimes,
59+
buildClusterAutoScalingQuery(clusterId).compile(),
60+
] as const,
61+
queryKey,
62+
requestOptions,
63+
sessionVariables,
64+
});
65+
return {
66+
creationTimes: creationTimesRes.rows,
67+
// No row means no strategy and no burst.
68+
autoScaling: autoScalingRes.rows.at(0) ?? null,
69+
};
70+
}

‎console/src/api/materialize/cluster/clusterList.test.sql.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe("buildClusterSubscribe", () => {
3535
disk: true,
3636
managed: true,
3737
size: "bootstrap",
38+
replicationFactor: 1,
3839
replicas: [
3940
{
4041
disk: true,
@@ -72,6 +73,7 @@ describe("buildClusterSubscribe", () => {
7273
disk: true,
7374
managed: true,
7475
size: "bootstrap",
76+
replicationFactor: 1,
7577
replicas: [
7678
{
7779
disk: true,
@@ -152,4 +154,22 @@ describe("buildClusterSubscribe", () => {
152154
await testdrive(`> DROP CLUSTER IF EXISTS test_multi;`);
153155
}
154156
});
157+
158+
it("reads a managed cluster's replication factor, and none for an unmanaged one", async () => {
159+
await testdrive(`
160+
> CREATE CLUSTER managed (SIZE 'scale=1,workers=1', REPLICATION FACTOR 2);
161+
> CREATE CLUSTER unmanaged (REPLICAS (r1 (SIZE 'scale=1,workers=1')));
162+
`);
163+
const { rows } = await executeSqlHttp(
164+
buildClustersQuery({
165+
queryOwnership: false,
166+
includeSystemObjects: false,
167+
}).compile(),
168+
);
169+
const factorOf = (name: string) =>
170+
rows.find((row) => row.name === name)?.replicationFactor;
171+
172+
expect(factorOf("managed")).toEqual(2);
173+
expect(factorOf("unmanaged")).toBeNull();
174+
});
155175
});

‎console/src/api/materialize/cluster/clusterList.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import * as Sentry from "@sentry/react";
1111
import { QueryKey } from "@tanstack/react-query";
12-
import { InferResult } from "kysely";
12+
import { InferResult, sql } from "kysely";
1313

1414
import { executeSqlV2, queryBuilder } from "~/api/materialize";
1515
import { getOwners, jsonArrayFrom } from "~/api/materialize/expressionBuilders";
@@ -59,6 +59,9 @@ export const buildClustersQuery = ({
5959
"c.disk",
6060
"c.managed",
6161
"c.size",
62+
sql<number | null>`${sql.ref("c.replication_factor")}::int4`.as(
63+
"replicationFactor",
64+
),
6265
"c.owner_id as ownerId",
6366
jsonArrayFrom<{
6467
id: string;

‎console/src/api/materialize/cluster/objectCreationTimes.ts‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,37 @@ import { sql } from "kysely";
1313
import { executeSqlV2, queryBuilder } from "~/api/materialize";
1414

1515
/**
16-
* When each of the given indexes and materialized views was created, for those
17-
* created at or after `startDate`. The literal id list makes this a lookup on
18-
* `mz_object_lifetimes_ind`.
16+
* When each of the given objects of `objectTypes` was created, for those
17+
* created at or after `startDate` when given. The literal id list makes this a
18+
* lookup on `mz_object_lifetimes_ind`.
19+
*
20+
* NOTE: clusters, replicas, roles and schemas share the `u<N>` id form from
21+
* separate counters, so one id can name objects of several requested types.
22+
* Match rows on `objectType` as well as `id`.
1923
*/
2024
export function buildObjectCreationTimesQuery({
2125
objectIds,
26+
objectTypes,
2227
startDate,
2328
}: {
2429
objectIds: string[];
25-
startDate: string;
30+
objectTypes: string[];
31+
startDate?: string;
2632
}) {
27-
return (
28-
queryBuilder
29-
.selectFrom("mz_object_lifetimes")
30-
.select(["id", "occurred_at as occurredAt"])
31-
.where("id", "in", objectIds)
32-
// Clusters, replicas, roles and schemas share the `u<N>` id form, so an id
33-
// alone can match another kind's creation.
34-
.where("object_type", "in", ["index", "materialized-view"])
35-
.where("event_type", "=", "create")
36-
.where("occurred_at", ">=", sql<Date>`${sql.lit(startDate)}::timestamptz`)
37-
);
33+
let qb = queryBuilder
34+
.selectFrom("mz_object_lifetimes")
35+
.select(["id", "object_type as objectType", "occurred_at as occurredAt"])
36+
.where("id", "in", objectIds)
37+
.where("object_type", "in", objectTypes)
38+
.where("event_type", "=", "create");
39+
if (startDate) {
40+
qb = qb.where(
41+
"occurred_at",
42+
">=",
43+
sql<Date>`${sql.lit(startDate)}::timestamptz`,
44+
);
45+
}
46+
return qb;
3847
}
3948

4049
export async function fetchObjectCreationTimes({

‎console/src/icons/index.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export { default as ReturnArrowIcon } from "~/icon/ReturnArrowIcon.svg?react";
7474
export { default as SchemaIcon } from "~/icon/SchemaIcon.svg?react";
7575
export { default as SearchIcon } from "~/icon/SearchIcon.svg?react";
7676
export { default as SecretIcon } from "~/icon/SecretIcon.svg?react";
77+
export { default as SettingsIcon } from "~/icon/SettingsIcon.svg?react";
7778
export { default as ShieldIcon } from "~/icon/ShieldIcon.svg?react";
7879
export { default as SinkIcon } from "~/icon/SinkIcon.svg?react";
7980
export { default as SinksIcon } from "~/icon/SinksIcon.svg?react";

‎console/src/platform/clusters/AlterClusterModal.test.tsx‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ describe("ClusterOverview", () => {
3939
server.use(emptyClustersResponse);
4040
});
4141

42+
it("starts the replicas at the replication factor, not a running burst's replica count", async () => {
43+
const replica = (id: string, size: string) => ({
44+
id,
45+
name: id,
46+
size,
47+
disk: true,
48+
statuses: [],
49+
});
50+
renderComponent(
51+
<AlterClusterModal
52+
cluster={buildCluster({ replicationFactor: 1 }, [
53+
replica("r1", "25cc"),
54+
replica("r2", "800cc"),
55+
])}
56+
clusterSizes={[]}
57+
isOpen={true}
58+
maxReplicas={5}
59+
onClose={mockOnClose}
60+
/>,
61+
);
62+
63+
expect(await screen.findByLabelText("Replicas")).toHaveDisplayValue("1");
64+
});
65+
4266
it("Displays validation errors", async () => {
4367
const user = userEvent.setup();
4468
renderComponent(<FormWithClusterProvider />);

‎console/src/platform/clusters/AlterClusterModal.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export const AlterClusterModal = ({
6767
>(undefined);
6868
const toast = useToast();
6969
const { shadows } = useTheme<MaterializeTheme>();
70+
// Not the replica count, which also counts a hydration burst's or a graceful
71+
// resize's extra replicas, so submitting it while one runs would keep them.
72+
const currentReplicas = cluster.replicationFactor ?? cluster.replicas.length;
7073

7174
const {
7275
register,
@@ -79,7 +82,7 @@ export const AlterClusterModal = ({
7982
defaultValues: {
8083
name: cluster?.name,
8184
size: cluster?.size || undefined,
82-
replicas: cluster?.replicas.length.toString(),
85+
replicas: currentReplicas.toString(),
8386
},
8487
});
8588

@@ -118,7 +121,7 @@ export const AlterClusterModal = ({
118121
if (replicas === "0") {
119122
// The user is asking for zero replicas. Warn them that this will deactive
120123
// the cluster, unless the cluster already has zero replicas.
121-
if (cluster?.replicas.length !== 0) {
124+
if (currentReplicas !== 0) {
122125
warning = "Setting replicas to 0 will deactivate the cluster.";
123126
}
124127
} else if (size !== cluster?.size) {

‎console/src/platform/clusters/ClusterDetail.test.tsx‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ describe("ClusterDetail redesign preview", () => {
153153
expect(screen.getByText("1 replica")).toBeVisible();
154154
expect(screen.getByRole("link", { name: "Metrics" })).toBeVisible();
155155
expect(screen.getByRole("link", { name: "Objects" })).toBeVisible();
156-
expect(screen.getByRole("link", { name: "Replicas" })).toBeVisible();
156+
expect(
157+
screen.queryByRole("link", { name: "Replicas" }),
158+
).not.toBeInTheDocument();
159+
expect(
160+
screen.getByRole("button", { name: "Configure cluster" }),
161+
).toBeVisible();
157162
expect(
158163
screen.queryByRole("link", { name: "Materialized Views" }),
159164
).not.toBeInTheDocument();
@@ -182,6 +187,36 @@ describe("ClusterDetail redesign preview", () => {
182187
expect(screen.queryByText("small")).not.toBeInTheDocument();
183188
});
184189

190+
it("opens the configuration drawer from the gear next to the title", async () => {
191+
const user = userEvent.setup();
192+
await renderClusterDetail("/u1/default");
193+
194+
await user.click(
195+
await screen.findByRole("button", { name: "Configure cluster" }),
196+
);
197+
198+
expect(
199+
await screen.findByRole("dialog", { name: "Configure default" }),
200+
).toBeVisible();
201+
expect(screen.getByRole("button", { name: /^Overview/ })).toHaveAttribute(
202+
"aria-pressed",
203+
"true",
204+
);
205+
});
206+
207+
it("opens the classic Replicas tab's links on the drawer's replicas", async () => {
208+
await renderClusterDetail("/u1/default/replicas");
209+
210+
await expectPathname("/u1/default");
211+
expect(
212+
await screen.findByRole("dialog", { name: "Configure default" }),
213+
).toBeVisible();
214+
expect(screen.getByRole("button", { name: /^Replicas/ })).toHaveAttribute(
215+
"aria-pressed",
216+
"true",
217+
);
218+
});
219+
185220
it("opens the Objects tab on its first object type", async () => {
186221
const user = userEvent.setup();
187222
await renderClusterDetail("/u1/default");

0 commit comments

Comments
 (0)