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: cache sqlroles results #95852

Merged
merged 1 commit into from
Jan 26, 2023
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
4 changes: 1 addition & 3 deletions pkg/ui/workspaces/cluster-ui/src/api/userApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export type UserSQLRolesRequestMessage =
export type UserSQLRolesResponseMessage =
cockroach.server.serverpb.UserSQLRolesResponse;

export function getUserSQLRoles(
req: UserSQLRolesRequestMessage,
): Promise<UserSQLRolesResponseMessage> {
export function getUserSQLRoles(): Promise<UserSQLRolesResponseMessage> {
return fetchData(
cockroach.server.serverpb.UserSQLRolesResponse,
`/_status/sqlroles`,
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/workspaces/cluster-ui/src/store/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ export function* sagas(cacheInvalidationPeriod?: number): SagaIterator {
fork(indexStatsSaga),
fork(clusterLocksSaga),
fork(schemaInsightsSaga),
fork(uiConfigSaga),
fork(uiConfigSaga, cacheInvalidationPeriod),
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { merge } from "lodash";
import { DOMAIN_NAME } from "../utils";
import { DOMAIN_NAME, noopReducer } from "../utils";
import { cockroach } from "@cockroachlabs/crdb-protobuf-client";
export type UserSQLRolesRequest = cockroach.server.serverpb.UserSQLRolesRequest;

Expand Down Expand Up @@ -57,15 +57,17 @@ const uiConfigSlice = createSlice({
update: (state, action: PayloadAction<Partial<UIConfigState>>) => {
merge(state, action.payload);
},
refreshUserSQLRoles: (
state,
action?: PayloadAction<UserSQLRolesRequest>,
) => {
receivedUserSQLRoles: (state, action: PayloadAction<string[]>) => {
if (action?.payload) {
const resp = action.payload.toJSON();
state.userSQLRoles = resp["roles"];
state.userSQLRoles = action.payload;
}
},
invalidatedUserSQLRoles: state => {
state.userSQLRoles = [];
},
// Define actions that don't change state
refreshUserSQLRoles: noopReducer,
requestUserSQLRoles: noopReducer,
},
});

Expand Down
46 changes: 36 additions & 10 deletions pkg/ui/workspaces/cluster-ui/src/store/uiConfig/uiConfig.sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,48 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

import { all, call, put, takeLatest } from "redux-saga/effects";
import { actions, UserSQLRolesRequest } from "./uiConfig.reducer";
import { PayloadAction } from "@reduxjs/toolkit";
import { all, call, delay, put, takeLatest } from "redux-saga/effects";
import { actions } from "./uiConfig.reducer";
import { getUserSQLRoles } from "../../api/userApi";
import { CACHE_INVALIDATION_PERIOD, throttleWithReset } from "../utils";
import { rootActions } from "../reducers";
import { cockroach } from "@cockroachlabs/crdb-protobuf-client";

export function* refreshUserSQLRoles(
action: PayloadAction<UserSQLRolesRequest>,
): any {
export function* refreshUserSQLRolesSaga(): any {
yield put(actions.requestUserSQLRoles());
}

export function* requestUserSQLRolesSaga(): any {
try {
const result = yield call(getUserSQLRoles, action?.payload);
yield put(actions.refreshUserSQLRoles(result));
const result: cockroach.server.serverpb.UserSQLRolesResponse = yield call(
getUserSQLRoles,
);
yield put(actions.receivedUserSQLRoles(result.roles));
} catch (e) {
console.warn(e.message);
}
}

export function* uiConfigSaga() {
yield all([takeLatest(actions.refreshUserSQLRoles, refreshUserSQLRoles)]);
export function* receivedUserSQLRolesSaga(delayMs: number): any {
yield delay(delayMs);
yield put(actions.invalidatedUserSQLRoles());
}

export function* uiConfigSaga(
cacheInvalidationPeriod: number = CACHE_INVALIDATION_PERIOD,
): any {
yield all([
throttleWithReset(
cacheInvalidationPeriod,
actions.refreshUserSQLRoles,
[actions.invalidatedUserSQLRoles, rootActions.resetState],
refreshUserSQLRolesSaga,
),
takeLatest(actions.requestUserSQLRoles, requestUserSQLRolesSaga),
takeLatest(
actions.receivedUserSQLRoles,
receivedUserSQLRolesSaga,
cacheInvalidationPeriod,
),
]);
}