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

Conversation

justinpark
Copy link
Member

SUMMARY

Currently, the refresh logic for schemas is being triggered excessively due to unnecessary dependencies, causing it to be called every time a tab is switched.

This commit addresses the issue by modifying the logic to execute only when a trigger is needed.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

Before:

before--too-many-schemas.mov

After:

after--too-many-schemas.mov

TESTING INSTRUCTIONS

Switch the tabs between the same db selected tabs
Check the network tab and then only schemas called once

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@dosubot dosubot bot added api Related to the REST API change:frontend Requires changing the frontend sqllab Namespace | Anything related to the SQL Lab labels Jun 17, 2024
Copy link
Member

@michael-s-molina michael-s-molina left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @justinpark!

@@ -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.

@@ -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.

@pull-request-size pull-request-size bot added size/L and removed size/M labels Jun 18, 2024
@github-actions github-actions bot removed the api Related to the REST API label Jun 18, 2024
@justinpark justinpark merged commit 4537ab6 into apache:master Jun 18, 2024
35 of 36 checks passed
@michael-s-molina michael-s-molina added v4.0 Label added by the release manager to track PRs to be included in the 4.0 branch and removed v4.0 Label added by the release manager to track PRs to be included in the 4.0 branch labels Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
change:frontend Requires changing the frontend size/L sqllab Namespace | Anything related to the SQL Lab
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants