From 3d2deaf3003a02438165d592420adb8d0135530f Mon Sep 17 00:00:00 2001 From: Shenoy Pratik Date: Mon, 25 Mar 2024 14:19:48 -0700 Subject: [PATCH] Catch no database found errors from cache manager (#296) * catch no database errors Signed-off-by: Shenoy Pratik * update relese notes Signed-off-by: Shenoy Pratik --------- Signed-off-by: Shenoy Pratik --- .../SQLPage/sql_catalog_tree/s3_tree.tsx | 17 ++++++++ .../sql_catalog_tree/s3_tree_helpers.tsx | 43 ++++++++++++------- ...-query-workbench.release-notes-2.13.0.0.md | 1 + 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/public/components/SQLPage/sql_catalog_tree/s3_tree.tsx b/public/components/SQLPage/sql_catalog_tree/s3_tree.tsx index aac963f..9624771 100644 --- a/public/components/SQLPage/sql_catalog_tree/s3_tree.tsx +++ b/public/components/SQLPage/sql_catalog_tree/s3_tree.tsx @@ -73,6 +73,22 @@ export const S3Tree = ({ dataSource, updateSQLQueries, refreshTree }: S3TreeProp stopLoading: stopLoadingAccelerations, } = catalogCacheRefs.useLoadAccelerationsToCache(); + const refreshDatabasesinTree = () => { + const currentTree = [...treeData]; + currentTree.map((db) => { + setTreeData( + produce((draft) => { + const databaseToUpdate = draft.find((database) => database.name === db.name); + if (databaseToUpdate) { + databaseToUpdate.isExpanded = false; + databaseToUpdate.isLoading = false; + databaseToUpdate.values = []; + } + }) + ); + }); + }; + const updateDatabaseState = (databaseName: string, isLoading: boolean, values?: TreeItem[]) => { setTreeData( produce((draft) => { @@ -220,6 +236,7 @@ export const S3Tree = ({ dataSource, updateSQLQueries, refreshTree }: S3TreeProp useEffect(() => { const status = loadDatabasesStatus.toLowerCase(); if (status === AsyncQueryStatus.Success) { + refreshDatabasesinTree(); setIsTreeLoading({ status: false, message: '' }); } else if (status === AsyncQueryStatus.Failed || status === AsyncQueryStatus.Cancelled) { setIsTreeLoading({ status: false, message: 'Failed to load databases' }); diff --git a/public/components/SQLPage/sql_catalog_tree/s3_tree_helpers.tsx b/public/components/SQLPage/sql_catalog_tree/s3_tree_helpers.tsx index 479b201..c64439c 100644 --- a/public/components/SQLPage/sql_catalog_tree/s3_tree_helpers.tsx +++ b/public/components/SQLPage/sql_catalog_tree/s3_tree_helpers.tsx @@ -22,6 +22,7 @@ import { TREE_ITEM_TABLE_NAME_DEFAULT_NAME, } from '../../../../common/constants'; import { CachedDataSourceStatus, TreeItem, TreeItemType } from '../../../../common/types'; +import { useToast } from '../../../../common/utils/toast_helper'; import { catalogCacheRefs } from '../../../framework/catalog_cache_refs'; export const handleQuery = ( @@ -105,24 +106,36 @@ export const loadTreeItem = (elements: string[], type: TreeItemType, values?: an }; export const isEitherObjectCacheEmpty = (dataSourceName: string, databaseName: string) => { - const dbCache = catalogCacheRefs.CatalogCacheManager!.getDatabase(dataSourceName, databaseName); - const dsCache = catalogCacheRefs.CatalogCacheManager!.getOrCreateAccelerationsByDataSource( - dataSourceName - ); - return ( - dbCache.status === CachedDataSourceStatus.Empty || - dsCache.status === CachedDataSourceStatus.Empty || - dbCache.status === CachedDataSourceStatus.Failed || - dsCache.status === CachedDataSourceStatus.Failed - ); + try { + const dbCache = catalogCacheRefs.CatalogCacheManager!.getDatabase(dataSourceName, databaseName); + const dsCache = catalogCacheRefs.CatalogCacheManager!.getOrCreateAccelerationsByDataSource( + dataSourceName + ); + return ( + dbCache.status === CachedDataSourceStatus.Empty || + dsCache.status === CachedDataSourceStatus.Empty || + dbCache.status === CachedDataSourceStatus.Failed || + dsCache.status === CachedDataSourceStatus.Failed + ); + } catch (error) { + console.error(error); + return false; + } }; export const getTablesFromCache = (dataSourceName: string, databaseName: string) => { - const dbCache = catalogCacheRefs.CatalogCacheManager!.getDatabase(dataSourceName, databaseName); - if (dbCache.status === CachedDataSourceStatus.Updated) { - const tables = dbCache.tables.map((tb) => tb.name); - return tables; - } else { + const { setToast } = useToast(); + try { + const dbCache = catalogCacheRefs.CatalogCacheManager!.getDatabase(dataSourceName, databaseName); + if (dbCache.status === CachedDataSourceStatus.Updated) { + const tables = dbCache.tables.map((tb) => tb.name); + return tables; + } else { + return []; + } + } catch (error) { + console.error(error); + setToast('Your cache is outdated, refresh databases and tables', 'warning'); return []; } }; diff --git a/release-notes/opensearch-query-workbench.release-notes-2.13.0.0.md b/release-notes/opensearch-query-workbench.release-notes-2.13.0.0.md index 17e5aaa..f19960a 100644 --- a/release-notes/opensearch-query-workbench.release-notes-2.13.0.0.md +++ b/release-notes/opensearch-query-workbench.release-notes-2.13.0.0.md @@ -10,6 +10,7 @@ Compatible with OpenSearch and OpenSearch Dashboards 2.13.0 - Refactor async calls and minor bug fixes ([#274](https://github.com/opensearch-project/dashboards-query-workbench/pull/274)) - Add empty tree state for SQL sidebar ([#292](https://github.com/opensearch-project/dashboards-query-workbench/pull/292)) +- Catch no database found errors from cache manager ([#296](https://github.com/opensearch-project/dashboards-query-workbench/pull/296)) ### Infrastructure