Skip to content

Commit

Permalink
P00000000 - revert the change of cacheloader and correct the cacheloa…
Browse files Browse the repository at this point in the history
…der and cachemnger usage

Signed-off-by: Ryan Liang <jiallian@amazon.com>
  • Loading branch information
RyanL1997 committed Jul 19, 2024
1 parent 8024f6f commit c2a7bbc
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ export const createLoadQuery = (
export const useLoadToCache = (
loadCacheType: LoadCacheType,
http: HttpStart,
notifications: NotificationsStart,
dataSourceMDSId?: string
notifications: NotificationsStart
) => {
const sqlService = new SQLService(http);
const [currentDataSourceName, setCurrentDataSourceName] = useState('');
Expand All @@ -297,13 +296,6 @@ export const useLoadToCache = (
);
const dataSourceMDSClientId = useRef('');

// Set dataSourceMDSClientId.current if dataSourceMDSId is provided
useEffect(() => {
if (dataSourceMDSId !== undefined) {
dataSourceMDSClientId.current = dataSourceMDSId;
}
}, [dataSourceMDSId]);

const {
data: pollingResult,
loading: _pollingLoading,
Expand All @@ -328,17 +320,15 @@ export const useLoadToCache = (

const startLoading = ({
dataSourceName,
dataSourceMDSId: startLoadingDataSourceMDSId,
dataSourceMDSId,
databaseName,
tableName,
}: StartLoadingParams) => {
setLoadStatus(DirectQueryLoadingStatus.SCHEDULED);
setCurrentDataSourceName(dataSourceName);
setCurrentDatabaseName(databaseName);
setCurrentTableName(tableName);
if (startLoadingDataSourceMDSId !== undefined) {
dataSourceMDSClientId.current = startLoadingDataSourceMDSId;
}
dataSourceMDSClientId.current = dataSourceMDSId || '';

let requestPayload: DirectQueryRequest = {
lang: 'sql',
Expand Down Expand Up @@ -418,58 +408,34 @@ export const useLoadToCache = (
return { loadStatus, startLoading, stopLoading };
};

export const useLoadDatabasesToCache = (
http: HttpStart,
notifications: NotificationsStart,
dataSourceMDSId: string
) => {
export const useLoadDatabasesToCache = (http: HttpStart, notifications: NotificationsStart) => {
const { loadStatus, startLoading, stopLoading } = useLoadToCache(
'databases',
http,
notifications,
dataSourceMDSId
notifications
);
return { loadStatus, startLoading, stopLoading };
};

export const useLoadTablesToCache = (
http: HttpStart,
notifications: NotificationsStart,
dataSourceMDSId: string
) => {
const { loadStatus, startLoading, stopLoading } = useLoadToCache(
'tables',
http,
notifications,
dataSourceMDSId
);
export const useLoadTablesToCache = (http: HttpStart, notifications: NotificationsStart) => {
const { loadStatus, startLoading, stopLoading } = useLoadToCache('tables', http, notifications);
return { loadStatus, startLoading, stopLoading };
};

export const useLoadTableColumnsToCache = (
http: HttpStart,
notifications: NotificationsStart,
dataSourceMDSId: string
) => {
export const useLoadTableColumnsToCache = (http: HttpStart, notifications: NotificationsStart) => {
const { loadStatus, startLoading, stopLoading } = useLoadToCache(
'tableColumns',
http,
notifications,
dataSourceMDSId
notifications
);
return { loadStatus, startLoading, stopLoading };
};

export const useLoadAccelerationsToCache = (
http: HttpStart,
notifications: NotificationsStart,
dataSourceMDSId: string
) => {
export const useLoadAccelerationsToCache = (http: HttpStart, notifications: NotificationsStart) => {
const { loadStatus, startLoading, stopLoading } = useLoadToCache(
'accelerations',
http,
notifications,
dataSourceMDSId
notifications
);
return { loadStatus, startLoading, stopLoading };
};
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const CreateAcceleration = ({
loadStatus,
startLoading,
stopLoading: stopLoadingTableFields,
} = useLoadTableColumnsToCache(http, notifications, dataSourceMDSId);
} = useLoadTableColumnsToCache(http, notifications);

const loadColumnsToAccelerationForm = (cachedTable: CachedTable) => {
const idPrefix = htmlIdGenerator()();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@ export const SelectorLoadDatabases = ({
notifications,
}: SelectorLoadDatabasesProps) => {
const [isLoading, setIsLoading] = useState(false);
if (dataSourceMDSId === undefined) {
dataSourceMDSId = '';
}
const {
loadStatus: loadDatabasesStatus,
startLoading: startDatabasesLoading,
stopLoading: stopDatabasesLoading,
} = useLoadDatabasesToCache(http, notifications, dataSourceMDSId);
} = useLoadDatabasesToCache(http, notifications);

const onClickRefreshDatabases = () => {
setIsLoading(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,16 @@ export const SelectorLoadObjects = ({
accelerationsStatus: false,
});
const isEitherLoading = isLoading.accelerationsStatus || isLoading.tableStatus;
if (dataSourceMDSId === undefined) {
dataSourceMDSId = '';
}
const {
loadStatus: loadTablesStatus,
startLoading: startLoadingTables,
stopLoading: stopLoadingTables,
} = useLoadTablesToCache(http, notifications, dataSourceMDSId);
} = useLoadTablesToCache(http, notifications);
const {
loadStatus: loadAccelerationsStatus,
startLoading: startLoadingAccelerations,
stopLoading: stopLoadingAccelerations,
} = useLoadAccelerationsToCache(http, notifications, dataSourceMDSId);
} = useLoadAccelerationsToCache(http, notifications);

const onClickRefreshDatabases = () => {
if (databaseName === '') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ export const AccelerationDataSourceSelector = ({
};

const loadDatabases = () => {
const dsCache = CatalogCacheManager.getOrCreateDataSource(accelerationFormData.dataSource);
const dsCache = CatalogCacheManager.getOrCreateDataSource(
accelerationFormData.dataSource,
dataSourceMDSId
);

if (dsCache.status === CachedDataSourceStatus.Updated && dsCache.databases.length > 0) {
const databaseLabels = dsCache.databases.map((db) => ({ label: db.name }));
Expand All @@ -121,7 +124,8 @@ export const AccelerationDataSourceSelector = ({
try {
dbCache = CatalogCacheManager.getDatabase(
accelerationFormData.dataSource,
accelerationFormData.database
accelerationFormData.database,
dataSourceMDSId
);
if (dbCache.status === CachedDataSourceStatus.Updated && dbCache.tables.length > 0) {
const tableLabels = dbCache.tables.map((tb) => ({ label: tb.name }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const AccelerationTable = ({
!isCatalogCacheFetching(accelerationsLoadStatus)
) {
setIsRefreshing(true);
startLoadingAccelerations({ dataSourceName });
startLoadingAccelerations({ dataSourceName, dataSourceMDSId });
} else {
setAccelerations(cachedDataSource.accelerations);
setUpdatedTime(cachedDataSource.lastUpdated);
Expand All @@ -125,7 +125,7 @@ export const AccelerationTable = ({
const handleRefresh = useCallback(() => {
if (!isCatalogCacheFetching(accelerationsLoadStatus)) {
setIsRefreshing(true);
startLoadingAccelerations({ dataSourceName });
startLoadingAccelerations({ dataSourceName, dataSourceMDSId });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [accelerationsLoadStatus]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,7 @@ export const AssociatedObjectsDetailsFlyout = ({
dataSourceMDSId,
application,
}: AssociatedObjectsFlyoutProps) => {
if (dataSourceMDSId === undefined) {
dataSourceMDSId = '';
}
const { loadStatus, startLoading } = useLoadTableColumnsToCache(
http,
notifications,
dataSourceMDSId
);
const { loadStatus, startLoading } = useLoadTableColumnsToCache(http, notifications);
const [tableColumns, setTableColumns] = useState<CachedColumn[] | undefined>([]);
const [schemaData, setSchemaData] = useState<any>([]);

Expand Down Expand Up @@ -222,6 +215,7 @@ export const AssociatedObjectsDetailsFlyout = ({
databaseName: tableDetail.database,
tableName: tableDetail.name,
handleRefresh,
dataSourceMDSId,
})
}
iconType="popout"
Expand Down Expand Up @@ -256,7 +250,8 @@ export const AssociatedObjectsDetailsFlyout = ({
const tables = CatalogCacheManager.getTable(
datasourceName,
tableDetail.database,
tableDetail.name
tableDetail.name,
dataSourceMDSId
);
if (tables?.columns) {
setTableColumns(tables?.columns);
Expand All @@ -265,6 +260,7 @@ export const AssociatedObjectsDetailsFlyout = ({
dataSourceName: datasourceName,
databaseName: tableDetail.database,
tableName: tableDetail.name,
dataSourceMDSId,
});
}
} catch (error) {
Expand All @@ -285,7 +281,8 @@ export const AssociatedObjectsDetailsFlyout = ({
columns = CatalogCacheManager.getTable(
datasourceName,
tableDetail.database,
tableDetail.name
tableDetail.name,
dataSourceMDSId
).columns;
setTableColumns(columns);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const AssociatedObjectsTab: React.FC<AssociatedObjectsTabProps> = (props)

const onRefreshButtonClick = () => {
if (!isCatalogCacheFetching(databasesLoadStatus, tablesLoadStatus, accelerationsLoadStatus)) {
startLoadingDatabases({ dataSourceName: datasource.name });
startLoadingDatabases({ dataSourceName: datasource.name, dataSourceMDSId });
setIsRefreshing(true);
}
};
Expand Down Expand Up @@ -172,13 +172,16 @@ export const AssociatedObjectsTab: React.FC<AssociatedObjectsTabProps> = (props)
// Load databases if empty or retrieve from cache if updated
useEffect(() => {
if (datasource.name) {
const datasourceCache = CatalogCacheManager.getOrCreateDataSource(datasource.name);
const datasourceCache = CatalogCacheManager.getOrCreateDataSource(
datasource.name,
dataSourceMDSId
);
if (
(datasourceCache.status === CachedDataSourceStatus.Empty ||
datasourceCache.status === CachedDataSourceStatus.Failed) &&
!isCatalogCacheFetching(databasesLoadStatus)
) {
startLoadingDatabases({ dataSourceName: datasource.name });
startLoadingDatabases({ dataSourceName: datasource.name, dataSourceMDSId });
} else if (datasourceCache.status === CachedDataSourceStatus.Updated) {
setCachedDatabases(datasourceCache.databases);
setIsFirstTimeLoading(false);
Expand All @@ -190,7 +193,10 @@ export const AssociatedObjectsTab: React.FC<AssociatedObjectsTabProps> = (props)
// Retrieve from cache upon load success
useEffect(() => {
const status = databasesLoadStatus.toLowerCase();
const datasourceCache = CatalogCacheManager.getOrCreateDataSource(datasource.name);
const datasourceCache = CatalogCacheManager.getOrCreateDataSource(
datasource.name,
dataSourceMDSId
);
if (status === DirectQueryLoadingStatus.SUCCESS) {
setCachedDatabases(datasourceCache.databases);
setIsFirstTimeLoading(false);
Expand All @@ -201,6 +207,7 @@ export const AssociatedObjectsTab: React.FC<AssociatedObjectsTabProps> = (props)
setDatabasesLoadFailed(true);
setIsFirstTimeLoading(false);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [datasource.name, databasesLoadStatus]);

const handleObjectsLoad = (
Expand All @@ -222,7 +229,11 @@ export const AssociatedObjectsTab: React.FC<AssociatedObjectsTabProps> = (props)
if (datasource.name && selectedDatabase) {
let databaseCache;
try {
databaseCache = CatalogCacheManager.getDatabase(datasource.name, selectedDatabase);
databaseCache = CatalogCacheManager.getDatabase(
datasource.name,
selectedDatabase,
dataSourceMDSId
);
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
Expand All @@ -238,7 +249,11 @@ export const AssociatedObjectsTab: React.FC<AssociatedObjectsTabProps> = (props)
databaseCache.status === CachedDataSourceStatus.Failed) &&
!isCatalogCacheFetching(tablesLoadStatus)
) {
startLoadingTables({ dataSourceName: datasource.name, databaseName: selectedDatabase });
startLoadingTables({
dataSourceName: datasource.name,
databaseName: selectedDatabase,
dataSourceMDSId,
});
setIsObjectsLoading(true);
} else if (databaseCache.status === CachedDataSourceStatus.Updated) {
setCachedTables(databaseCache.tables);
Expand All @@ -249,7 +264,7 @@ export const AssociatedObjectsTab: React.FC<AssociatedObjectsTabProps> = (props)
isRefreshing) &&
!isCatalogCacheFetching(accelerationsLoadStatus)
) {
startLoadingAccelerations({ dataSourceName: datasource.name });
startLoadingAccelerations({ dataSourceName: datasource.name, dataSourceMDSId });
setIsObjectsLoading(true);
} else if (accelerationsCache.status === CachedDataSourceStatus.Updated) {
setCachedAccelerations(accelerationsCache.accelerations);
Expand All @@ -264,7 +279,11 @@ export const AssociatedObjectsTab: React.FC<AssociatedObjectsTabProps> = (props)
const tablesStatus = tablesLoadStatus.toLowerCase();
let databaseCache;
try {
databaseCache = CatalogCacheManager.getDatabase(datasource.name, selectedDatabase);
databaseCache = CatalogCacheManager.getDatabase(
datasource.name,
selectedDatabase,
dataSourceMDSId
);
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
Expand All @@ -273,8 +292,8 @@ export const AssociatedObjectsTab: React.FC<AssociatedObjectsTabProps> = (props)
}
const accelerationsStatus = accelerationsLoadStatus.toLowerCase();
const accelerationsCache = CatalogCacheManager.getOrCreateAccelerationsByDataSource(
datasource.name
// dataSourceMDSId
datasource.name,
dataSourceMDSId
);
if (tablesStatus === DirectQueryLoadingStatus.SUCCESS) {
setCachedTables(databaseCache.tables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export const DirectQueryDataConnectionDetail: React.FC<DirectQueryDataConnection
const { search } = useLocation();
const queryParams = new URLSearchParams(search);
const dataSourceMDSId = queryParams.get('dataSourceMDSId');
const hookDataSourceMDSId = featureFlagStatus && dataSourceMDSId ? dataSourceMDSId : '';
const [datasourceDetails, setDatasourceDetails] = useState<DirectQueryDatasourceDetails>({
allowedRoles: [],
name: '',
Expand All @@ -82,20 +81,19 @@ export const DirectQueryDataConnectionDetail: React.FC<DirectQueryDataConnection
const {
loadStatus: databasesLoadStatus,
startLoading: startLoadingDatabases,
} = useLoadDatabasesToCache(http, notifications, hookDataSourceMDSId);
} = useLoadDatabasesToCache(http, notifications);

const { loadStatus: tablesLoadStatus, startLoading: startLoadingTables } = useLoadTablesToCache(
http,
notifications,
hookDataSourceMDSId
notifications
);

const [selectedDatabase, setSelectedDatabase] = useState<string>('');

const {
loadStatus: accelerationsLoadStatus,
startLoading: startLoadingAccelerations,
} = useLoadAccelerationsToCache(http, notifications, hookDataSourceMDSId);
} = useLoadAccelerationsToCache(http, notifications);

const cacheLoadingHooks = {
databasesLoadStatus,
Expand Down
Loading

0 comments on commit c2a7bbc

Please sign in to comment.