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

background asset wipe #26128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions js_modules/dagster-ui/packages/ui-core/client.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 102 additions & 3 deletions js_modules/dagster-ui/packages/ui-core/src/assets/useWipeAssets.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import {useLayoutEffect, useRef, useState} from 'react';

import {RefetchQueriesFunction, gql, useMutation} from '../apollo-client';
import {AssetWipeMutation, AssetWipeMutationVariables} from './types/useWipeAssets.types';
import {RefetchQueriesFunction, gql, useLazyQuery, useMutation} from '../apollo-client';
import {
AssetWipeMutation,
AssetWipeMutationVariables,
BackgroundAssetWipeMutation,
BackgroundAssetWipeMutationVariables,
BackgroundAssetWipeStatusQuery,
} from './types/useWipeAssets.types';
import {showCustomAlert} from '../app/CustomAlertProvider';
import {PYTHON_ERROR_FRAGMENT} from '../app/PythonErrorFragment';
import {PartitionsByAssetSelector} from '../graphql/types';
Expand All @@ -21,6 +27,14 @@ export function useWipeAssets({
ASSET_WIPE_MUTATION,
{refetchQueries},
);
const [requestBackgroundAssetWipe] = useMutation<
BackgroundAssetWipeMutation,
BackgroundAssetWipeMutationVariables
>(BACKGROUND_ASSET_WIPE_MUTATION, {refetchQueries});
const [
getBackgroundWipeStatus,
{error: backgroundWipeStatusError, data: backgroundWipeStatusData},
Copy link
Member

Choose a reason for hiding this comment

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

This object should also include functions startPolling and stopPolling that you can use. You might not need startPolling since you can provide the pollInterval at the callsite, but I think you will want to explicitly stop polling when the wipe has succeeded or failed.

] = useLazyQuery<BackgroundAssetWipeStatusQuery>(BACKGROUND_ASSET_WIPE_STATUS);

const [isWiping, setIsWiping] = useState(false);
const [wipedCount, setWipedCount] = useState(0);
Expand All @@ -30,6 +44,31 @@ export function useWipeAssets({

const didCancel = useRef(false);

if (isWiping && backgroundWipeStatusError) {
setFailedCount(1);
onComplete?.();
setIsWiping(false);
}

if (isWiping && backgroundWipeStatusData) {
const data = backgroundWipeStatusData.backgroundAssetWipeStatus;
switch (data.__typename) {
case 'BackgroundAssetWipeInProgress':
console.log('Background asset wipe in progress');
break;
case 'BackgroundAssetWipeSuccess':
setWipedCount(1);
onComplete?.();
setIsWiping(false);
break;
case 'BackgroundAssetWipeFailed':
setFailedCount(1);
onComplete?.();
setIsWiping(false);
break;
}
}
Comment on lines +47 to +70
Copy link
Member

Choose a reason for hiding this comment

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

Avoid setting state or performing side effects within the render path. These should occur in callbacks or effects.


const wipeAssets = async (assetPartitionRanges: PartitionsByAssetSelector[]) => {
if (!assetPartitionRanges.length) {
return;
Expand Down Expand Up @@ -66,13 +105,43 @@ export function useWipeAssets({
setIsWiping(false);
};

const backgroundWipeAssets = async (assetPartitionRanges: PartitionsByAssetSelector[]) => {
Copy link
Member

Choose a reason for hiding this comment

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

Naming nit: maybe wipeAssetsInBackground to emphasize the verb?

if (!assetPartitionRanges.length) {
return;
}
setIsWiping(true);
const result = await requestBackgroundAssetWipe({
variables: {assetPartitionRanges},
refetchQueries,
});
const data = result.data?.backgroundWipeAssets;
switch (data?.__typename) {
case 'AssetNotFoundError':
case 'PythonError':
setFailedCount(assetPartitionRanges.length);
onComplete?.();
setIsWiping(false);
return;
Comment on lines +118 to +124
Copy link
Member

Choose a reason for hiding this comment

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

It would be helpful to surface the errors.

case 'AssetNotFoundError': {
  showCustomAlert({
    title: 'Asset not found',
    description: 'The specificed asset could not be found.',
  });
  setFailedCount(...);
  // etc.
  return;
}
case 'PythonError': {
  showCustomAlert({
    title: 'Python error',
    body: <PythonErrorInfo error={runs} />,
  });
  // etc.
}

case 'AssetWipeInProgress':
getBackgroundWipeStatus({variables: {workToken: data.workToken}, pollInterval: 1000});
break;
case 'UnauthorizedError':
showCustomAlert({
title: 'Could not wipe asset materializations',
body: 'You do not have permission to do this.',
});
onClose();
return;
}
};

useLayoutEffect(() => {
return () => {
didCancel.current = true;
};
}, []);

return {wipeAssets, isWiping, isDone, wipedCount, failedCount};
return {backgroundWipeAssets, wipeAssets, isWiping, isDone, wipedCount, failedCount};
}

export const ASSET_WIPE_MUTATION = gql`
Expand All @@ -95,3 +164,33 @@ export const ASSET_WIPE_MUTATION = gql`

${PYTHON_ERROR_FRAGMENT}
`;

export const BACKGROUND_ASSET_WIPE_MUTATION = gql`
mutation BackgroundAssetWipeMutation($assetPartitionRanges: [PartitionsByAssetSelector!]!) {
backgroundWipeAssets(assetPartitionRanges: $assetPartitionRanges) {
... on AssetWipeInProgress {
workToken
}
...PythonErrorFragment
}
}

${PYTHON_ERROR_FRAGMENT}
`;

export const BACKGROUND_ASSET_WIPE_STATUS = gql`
query BackgroundAssetWipeStatus($workToken: String!) {
backgroundAssetWipeStatus(workToken: $workToken) {
... on BackgroundAssetWipeSuccess {
completedAt
}
... on BackgroundAssetWipeInProgress {
startedAt
}
... on BackgroundAssetWipeFailed {
failedAt
message
}
}
}
`;
Loading