Skip to content

Commit

Permalink
reduce extraneous task log requests (apache#27233)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbovenzi authored Oct 25, 2022
1 parent 8d449ae commit e73e90e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
23 changes: 18 additions & 5 deletions airflow/www/static/js/api/useTaskLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,41 @@
import axios, { AxiosResponse } from 'axios';
import { useQuery } from 'react-query';
import { useAutoRefresh } from 'src/context/autorefresh';
import type { API } from 'src/types';
import type { API, TaskInstance } from 'src/types';

import { getMetaValue } from 'src/utils';

const taskLogApi = getMetaValue('task_log_api');

interface Props extends API.GetLogVariables {
state?: TaskInstance['state'];
}

const useTaskLog = ({
dagId, dagRunId, taskId, taskTryNumber, mapIndex, fullContent,
}: API.GetLogVariables) => {
dagId, dagRunId, taskId, taskTryNumber, mapIndex, fullContent, state,
}: Props) => {
let url: string = '';
if (taskLogApi) {
url = taskLogApi.replace('_DAG_RUN_ID_', dagRunId).replace('_TASK_ID_', taskId).replace(/-1$/, taskTryNumber.toString());
}

const { isRefreshOn } = useAutoRefresh();

// Only refresh is the state is pending
const isStatePending = state === 'deferred'
|| state === 'scheduled'
|| state === 'running'
|| state === 'up_for_reschedule'
|| state === 'up_for_retry'
|| state === 'queued'
|| state === 'restarting';

return useQuery(
['taskLogs', dagId, dagRunId, taskId, mapIndex, taskTryNumber, fullContent],
['taskLogs', dagId, dagRunId, taskId, mapIndex, taskTryNumber, fullContent, state],
() => axios.get<AxiosResponse, string>(url, { headers: { Accept: 'text/plain' }, params: { map_index: mapIndex, full_content: fullContent } }),
{
placeholderData: '',
refetchInterval: isRefreshOn && (autoRefreshInterval || 1) * 1000,
refetchInterval: isStatePending && isRefreshOn && (autoRefreshInterval || 1) * 1000,
},
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ interface Props {
mapIndex?: TaskInstance['mapIndex'];
executionDate: DagRun['executionDate'];
tryNumber: TaskInstance['tryNumber'];
state?: TaskInstance['state'];
}

const Logs = ({
Expand All @@ -96,6 +97,7 @@ const Logs = ({
mapIndex,
executionDate,
tryNumber,
state,
}: Props) => {
const [internalIndexes, externalIndexes] = getLinkIndexes(tryNumber);
const [selectedTryNumber, setSelectedTryNumber] = useState<number | undefined>();
Expand All @@ -105,7 +107,6 @@ const Logs = ({
const [fileSourceFilters, setFileSourceFilters] = useState<Array<FileSourceOption>>([]);
const { timezone } = useTimezone();

//
const taskTryNumber = selectedTryNumber || tryNumber || 1;
const { data, isSuccess } = useTaskLog({
dagId,
Expand All @@ -114,6 +115,7 @@ const Logs = ({
mapIndex,
taskTryNumber,
fullContent: shouldRequestFullContent,
state,
});

const params = new URLSearchParamsWrapper({
Expand Down
3 changes: 2 additions & 1 deletion airflow/www/static/js/dag/details/taskInstance/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const TaskInstance = ({
operator={operator}
/>
)}
<Tabs size="lg" index={selectedTabIndex} onChange={handleTabsChange}>
<Tabs size="lg" index={selectedTabIndex} onChange={handleTabsChange} isLazy>
<TabList>
<Tab>
<Text as="strong">Details</Text>
Expand Down Expand Up @@ -183,6 +183,7 @@ const TaskInstance = ({
mapIndex={mapIndex}
executionDate={executionDate}
tryNumber={instance?.tryNumber}
state={instance?.state}
/>
</TabPanel>
)}
Expand Down

0 comments on commit e73e90e

Please sign in to comment.