Skip to content

Commit d1ae6a3

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 08b3097 commit d1ae6a3

17 files changed

Lines changed: 2018 additions & 35 deletions

‎console/icon/SettingsIcon.svg‎

Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 {
18+
buildClusterAutoScalingQuery,
19+
buildClusterDetailsQuery,
20+
} from "./clusterConfiguration";
21+
import { buildObjectCreationTimesQuery } from "./objectCreationTimes";
22+
23+
const OPTIONS = { sessionVariables: { transaction_isolation: "serializable" } };
24+
25+
async function clusterIdByName(name: string) {
26+
const client = await getMaterializeClient();
27+
const {
28+
rows: [cluster],
29+
} = await client.query("SELECT id FROM mz_clusters WHERE name = $1", [name]);
30+
return cluster.id as string;
31+
}
32+
33+
describe("cluster configuration queries", () => {
34+
it("read a managed cluster's replication factor, and none for an unmanaged one", async () => {
35+
await testdrive(`
36+
> CREATE CLUSTER managed (SIZE 'scale=1,workers=1', REPLICATION FACTOR 2);
37+
> CREATE CLUSTER unmanaged (REPLICAS (r1 (SIZE 'scale=1,workers=1')));
38+
`);
39+
40+
const replicationFactor = async (name: string) =>
41+
(
42+
await executeSqlHttp(
43+
buildClusterDetailsQuery(await clusterIdByName(name)).compile(),
44+
OPTIONS,
45+
)
46+
).rows.at(0)?.replicationFactor;
47+
48+
expect(await replicationFactor("managed")).toEqual(2);
49+
expect(await replicationFactor("unmanaged")).toBeNull();
50+
});
51+
52+
it("tells a cluster's creation from a replica's with the same id", async () => {
53+
await testdrive(`> SET cluster to quickstart;`);
54+
const client = await getMaterializeClient();
55+
// Clusters and replicas take ids from separate counters, so the first of
56+
// each share an id.
57+
const {
58+
rows: [shared],
59+
} = await client.query(
60+
`SELECT c.id FROM mz_clusters c JOIN mz_cluster_replicas r ON r.id = c.id LIMIT 1`,
61+
);
62+
expect(shared?.id).toBeDefined();
63+
64+
const { rows } = await executeSqlHttp(
65+
buildObjectCreationTimesQuery({
66+
objectIds: [shared.id],
67+
objectTypes: ["cluster", "cluster-replica"],
68+
}).compile(),
69+
OPTIONS,
70+
);
71+
72+
expect(rows.map(({ id, objectType }) => [id, objectType]).sort()).toEqual([
73+
[shared.id, "cluster"],
74+
[shared.id, "cluster-replica"],
75+
]);
76+
expect(rows.every(({ occurredAt }) => occurredAt instanceof Date)).toBe(
77+
true,
78+
);
79+
});
80+
81+
it("reads a cluster's autoscaling policy as the drawer parses it", async () => {
82+
await testdrive(`
83+
$ postgres-execute connection=postgres://mz_system@\${testdrive.materialize-internal-sql-addr}/materialize
84+
ALTER SYSTEM SET enable_auto_scaling_strategy = true
85+
86+
> CREATE CLUSTER lingering (SIZE 'scale=1,workers=1', AUTO SCALING STRATEGY = (ON HYDRATION (HYDRATION SIZE = 'scale=1,workers=2', LINGER DURATION = '120s')));
87+
> CREATE CLUSTER defaulted (SIZE 'scale=1,workers=1', AUTO SCALING STRATEGY = (ON HYDRATION (HYDRATION SIZE = 'scale=1,workers=2')));
88+
> CREATE CLUSTER steady (SIZE 'scale=1,workers=1');
89+
`);
90+
91+
const policy = async (name: string) => {
92+
const { rows } = await executeSqlHttp(
93+
buildClusterAutoScalingQuery(await clusterIdByName(name)).compile(),
94+
OPTIONS,
95+
);
96+
return parseAutoScaling(rows.at(0) ?? null).policy;
97+
};
98+
99+
expect(await policy("lingering")).toEqual({
100+
hydrationSize: "scale=1,workers=2",
101+
lingerMs: 120_000,
102+
});
103+
// An omitted linger arrives as a null duration, the server's default.
104+
expect(await policy("defaulted")).toEqual({
105+
hydrationSize: "scale=1,workers=2",
106+
lingerMs: undefined,
107+
});
108+
expect(await policy("steady")).toBeNull();
109+
});
110+
});
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
import { sql } from "kysely";
12+
13+
import { executeSqlV2, queryBuilder } from "~/api/materialize";
14+
15+
import { buildObjectCreationTimesQuery } from "./objectCreationTimes";
16+
17+
/** First version with `mz_internal.mz_cluster_auto_scaling_strategies`. */
18+
export const AUTO_SCALING_MIN_VERSION = "26.34.0";
19+
20+
/** A cluster's replication factor, a lookup on `mz_clusters_ind`. */
21+
export function buildClusterDetailsQuery(clusterId: string) {
22+
return queryBuilder
23+
.selectFrom("mz_clusters")
24+
.select(
25+
sql<number | null>`replication_factor::int4`.as("replicationFactor"),
26+
)
27+
.where("id", "=", clusterId);
28+
}
29+
30+
/** A cluster's autoscaling strategy and any in-flight burst, as JSON. */
31+
export function buildClusterAutoScalingQuery(clusterId: string) {
32+
return queryBuilder
33+
.selectFrom("mz_cluster_auto_scaling_strategies")
34+
.select(["strategy", "state"])
35+
.where("cluster_id", "=", clusterId);
36+
}
37+
38+
/** The configuration the cluster list doesn't carry, read in one request. */
39+
export async function fetchClusterConfiguration({
40+
params: { clusterId, replicaIds, includeAutoScaling },
41+
queryKey,
42+
requestOptions,
43+
}: {
44+
params: {
45+
clusterId: string;
46+
replicaIds: string[];
47+
/** The autoscaling relation only exists from `AUTO_SCALING_MIN_VERSION`. */
48+
includeAutoScaling: boolean;
49+
};
50+
queryKey: QueryKey;
51+
requestOptions?: RequestInit;
52+
}) {
53+
const details = buildClusterDetailsQuery(clusterId).compile();
54+
const creationTimes = buildObjectCreationTimesQuery({
55+
objectIds: [clusterId, ...replicaIds],
56+
objectTypes: ["cluster", "cluster-replica"],
57+
}).compile();
58+
const sessionVariables = { transaction_isolation: "serializable" };
59+
if (!includeAutoScaling) {
60+
const [detailsRes, creationTimesRes] = await executeSqlV2({
61+
queries: [details, creationTimes] as const,
62+
queryKey,
63+
requestOptions,
64+
sessionVariables,
65+
});
66+
return {
67+
details: detailsRes.rows.at(0),
68+
creationTimes: creationTimesRes.rows,
69+
autoScaling: undefined,
70+
};
71+
}
72+
const [detailsRes, creationTimesRes, autoScalingRes] = await executeSqlV2({
73+
queries: [
74+
details,
75+
creationTimes,
76+
buildClusterAutoScalingQuery(clusterId).compile(),
77+
] as const,
78+
queryKey,
79+
requestOptions,
80+
sessionVariables,
81+
});
82+
return {
83+
details: detailsRes.rows.at(0),
84+
creationTimes: creationTimesRes.rows,
85+
// No row means no strategy and no burst.
86+
autoScaling: autoScalingRes.rows.at(0) ?? null,
87+
};
88+
}

‎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.tsx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,19 @@ export const AlterClusterModal = ({
5353
clusterSizes,
5454
isOpen,
5555
maxReplicas,
56+
replicationFactor,
5657
onClose,
5758
}: {
5859
cluster: Cluster;
5960
clusterSizes: string[];
6061
isOpen: boolean;
6162
maxReplicas: number | undefined;
63+
/**
64+
* The cluster's replication factor. Defaults to its replica count, which
65+
* also counts a hydration burst's or a graceful resize's extra replicas, so
66+
* submitting it while one runs would keep them.
67+
*/
68+
replicationFactor?: number;
6269
onClose: () => void;
6370
}) => {
6471
const { track } = useSegment();
@@ -67,6 +74,7 @@ export const AlterClusterModal = ({
6774
>(undefined);
6875
const toast = useToast();
6976
const { shadows } = useTheme<MaterializeTheme>();
77+
const currentReplicas = replicationFactor ?? cluster.replicas.length;
7078

7179
const {
7280
register,
@@ -79,7 +87,7 @@ export const AlterClusterModal = ({
7987
defaultValues: {
8088
name: cluster?.name,
8189
size: cluster?.size || undefined,
82-
replicas: cluster?.replicas.length.toString(),
90+
replicas: currentReplicas.toString(),
8391
},
8492
});
8593

@@ -118,7 +126,7 @@ export const AlterClusterModal = ({
118126
if (replicas === "0") {
119127
// The user is asking for zero replicas. Warn them that this will deactive
120128
// the cluster, unless the cluster already has zero replicas.
121-
if (cluster?.replicas.length !== 0) {
129+
if (currentReplicas !== 0) {
122130
warning = "Setting replicas to 0 will deactivate the cluster.";
123131
}
124132
} 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");

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import AlterClusterMenuItem from "./AlterClusterMenuItem";
2626
import { useOwners } from "./queries";
2727
import { useShowSystemObjects } from "./useShowSystemObjects";
2828

29-
export const ClusterDetailBreadcrumbs = (props: { crumbs: Breadcrumb[] }) => {
29+
export const ClusterDetailBreadcrumbs = (props: {
30+
crumbs: Breadcrumb[];
31+
/** Off where the page has its own way to alter and drop the cluster. */
32+
showActions?: boolean;
33+
}) => {
3034
const [showSystemObjects] = useShowSystemObjects();
3135
const { clusterId, clusterName } = useParams<ClusterParams>();
3236
const { data: clusters, getClusterById } = useAllClusters();
@@ -74,7 +78,7 @@ export const ClusterDetailBreadcrumbs = (props: { crumbs: Breadcrumb[] }) => {
7478
label="Show the new cluster details page"
7579
optOutLabel="Show classic cluster details page"
7680
/>
77-
{cluster && (
81+
{cluster && props.showActions !== false && (
7882
<OverflowMenuContainer
7983
cluster={{ ...cluster, isOwner: isOwner(cluster.ownerId) }}
8084
/>

0 commit comments

Comments
 (0)