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

fix(sqllab): excessive API calls for schemas #29279

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 9 additions & 10 deletions superset-frontend/src/hooks/apiResources/catalogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { useCallback, useEffect, useRef } from 'react';
import { useCallback, useEffect } from 'react';
import useEffectEvent from 'src/hooks/useEffectEvent';
import { api, JsonResponse } from './queryApi';

Expand Down Expand Up @@ -68,7 +68,6 @@ export const {
export const EMPTY_CATALOGS = [] as CatalogOption[];

export function useCatalogs(options: Params) {
const isMountedRef = useRef(false);
const { dbId, onSuccess, onError } = options || {};
const [trigger] = useLazyCatalogsQuery();
const result = useCatalogsQuery(
Expand All @@ -88,6 +87,10 @@ export function useCatalogs(options: Params) {
onError?.();
});

const resolver = useEffectEvent(() =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of duplicated code. Maybe something like this?

export function useCatalogs(options: Params) {
  const { dbId, onSuccess, onError } = options || {};
  const [trigger] = useLazyCatalogsQuery();
  const result = useCatalogsQuery(
    { dbId, forceRefresh: false },
    {
      skip: !dbId,
    },
  );

  const fetchData = useCallback(
    (forceRefresh: boolean) => {
      trigger({ dbId, forceRefresh }).then(({ isSuccess, isError, data }) => {
        if (isSuccess) {
          onSuccess?.(data || EMPTY_CATALOGS, forceRefresh);
        }
        if (isError) {
          onError?.();
        }
      });
    },
    [dbId, onError, onSuccess, trigger],
  );

  const refetch = useCallback(() => {
    if (dbId) {
      fetchData(true);
    }
  }, [dbId, fetchData]);

  useEffect(() => {
    if (dbId && !result.currentData) {
      fetchData(false);
    }
  }, [dbId, fetchData, result.currentData]);

  return {
    ...result,
    refetch,
  };
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good suggestion. One thing that including result.currentData in the dependencies array of useEffect can cause unintended fetchData calls. I'll change it to handle this within the fetchData function.

result.currentData ? undefined : trigger({ dbId, forceRefresh: false }),
);

const refetch = useCallback(() => {
if (dbId) {
trigger({ dbId, forceRefresh: true }).then(
Expand All @@ -104,21 +107,17 @@ export function useCatalogs(options: Params) {
}, [dbId, handleOnError, handleOnSuccess, trigger]);

useEffect(() => {
if (isMountedRef.current) {
const { requestId, isSuccess, isError, isFetching, data, originalArgs } =
result;
if (!originalArgs?.forceRefresh && requestId && !isFetching) {
if (dbId) {
resolver()?.then(({ isSuccess, isError, data }) => {
if (isSuccess) {
handleOnSuccess(data || EMPTY_CATALOGS, false);
}
if (isError) {
handleOnError();
}
}
} else {
isMountedRef.current = true;
});
}
}, [result, handleOnSuccess, handleOnError]);
}, [dbId, result, handleOnSuccess, handleOnError, resolver]);

return {
...result,
Expand Down
30 changes: 19 additions & 11 deletions superset-frontend/src/hooks/apiResources/schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('useSchemas hook', () => {
})}`,
).length,
).toBe(1);
expect(onSuccess).toHaveBeenCalledTimes(2);
expect(onSuccess).toHaveBeenCalledTimes(1);
act(() => {
result.current.refetch();
});
Expand All @@ -100,7 +100,7 @@ describe('useSchemas hook', () => {
})}`,
).length,
).toBe(1);
expect(onSuccess).toHaveBeenCalledTimes(3);
expect(onSuccess).toHaveBeenCalledTimes(2);
expect(result.current.data).toEqual(expectedResult);
});

Expand Down Expand Up @@ -149,28 +149,36 @@ describe('useSchemas hook', () => {
},
);

await waitFor(() => expect(result.current.data).toEqual(expectedResult));
await waitFor(() =>
expect(result.current.currentData).toEqual(expectedResult),
);
expect(fetchMock.calls(schemaApiRoute).length).toBe(1);
expect(onSuccess).toHaveBeenCalledTimes(2);
expect(onSuccess).toHaveBeenCalledTimes(1);

rerender({ dbId: 'db2' });
await waitFor(() => expect(result.current.data).toEqual(expectedResult2));
await waitFor(() =>
expect(result.current.currentData).toEqual(expectedResult2),
);
expect(fetchMock.calls(schemaApiRoute).length).toBe(2);
expect(onSuccess).toHaveBeenCalledTimes(4);
expect(onSuccess).toHaveBeenCalledTimes(2);

rerender({ dbId: expectDbId });
await waitFor(() => expect(result.current.data).toEqual(expectedResult));
await waitFor(() =>
expect(result.current.currentData).toEqual(expectedResult),
);
expect(fetchMock.calls(schemaApiRoute).length).toBe(2);
expect(onSuccess).toHaveBeenCalledTimes(5);
expect(onSuccess).toHaveBeenCalledTimes(2);

// clean up cache
act(() => {
store.dispatch(api.util.invalidateTags(['Schemas']));
});

await waitFor(() => expect(fetchMock.calls(schemaApiRoute).length).toBe(3));
await waitFor(() => expect(fetchMock.calls(schemaApiRoute).length).toBe(4));
expect(fetchMock.calls(schemaApiRoute)[2][0]).toContain(expectDbId);
await waitFor(() => expect(result.current.data).toEqual(expectedResult));
await waitFor(() =>
expect(result.current.currentData).toEqual(expectedResult),
);
});

test('returns correct schema list by a catalog', async () => {
Expand Down Expand Up @@ -201,7 +209,7 @@ describe('useSchemas hook', () => {

await waitFor(() => expect(fetchMock.calls(schemaApiRoute).length).toBe(1));
expect(result.current.data).toEqual(expectedResult3);
expect(onSuccess).toHaveBeenCalledTimes(2);
expect(onSuccess).toHaveBeenCalledTimes(1);

rerender({ dbId, catalog: 'catalog2' });
await waitFor(() => expect(fetchMock.calls(schemaApiRoute).length).toBe(2));
Expand Down
46 changes: 16 additions & 30 deletions superset-frontend/src/hooks/apiResources/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { useCallback, useEffect, useRef } from 'react';
import { useCallback, useEffect } from 'react';
import useEffectEvent from 'src/hooks/useEffectEvent';
import { api, JsonResponse } from './queryApi';

Expand Down Expand Up @@ -72,7 +72,6 @@ export const {
export const EMPTY_SCHEMAS = [] as SchemaOption[];

export function useSchemas(options: Params) {
const isMountedRef = useRef(false);
const { dbId, catalog, onSuccess, onError } = options || {};
const [trigger] = useLazySchemasQuery();
const result = useSchemasQuery(
Expand All @@ -92,20 +91,24 @@ export function useSchemas(options: Params) {
onError?.();
});

const resolver = useEffectEvent(() =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as before.

result.currentData
? undefined
: trigger({ dbId, catalog, forceRefresh: false }),
);

useEffect(() => {
if (dbId) {
trigger({ dbId, catalog, forceRefresh: false }).then(
({ isSuccess, isError, data }) => {
if (isSuccess) {
handleOnSuccess(data || EMPTY_SCHEMAS, true);
}
if (isError) {
handleOnError();
}
},
);
resolver()?.then(({ isSuccess, isError, data }) => {
if (isSuccess) {
handleOnSuccess(data || EMPTY_SCHEMAS, false);
}
if (isError) {
handleOnError();
}
});
}
}, [dbId, catalog, handleOnError, handleOnSuccess, trigger]);
}, [dbId, catalog, handleOnError, handleOnSuccess, resolver]);

const refetch = useCallback(() => {
if (dbId) {
Expand All @@ -122,23 +125,6 @@ export function useSchemas(options: Params) {
}
}, [dbId, catalog, handleOnError, handleOnSuccess, trigger]);

useEffect(() => {
if (isMountedRef.current) {
const { requestId, isSuccess, isError, isFetching, data, originalArgs } =
result;
if (!originalArgs?.forceRefresh && requestId && !isFetching) {
if (isSuccess) {
handleOnSuccess(data || EMPTY_SCHEMAS, false);
}
if (isError) {
handleOnError();
}
}
} else {
isMountedRef.current = true;
}
}, [catalog, result, handleOnSuccess, handleOnError]);

return {
...result,
refetch,
Expand Down
Loading