Browse Source

fix: switch db concurency issue

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang 2 months ago
parent
commit
387e2171b0

+ 29 - 26
client/src/context/hooks/useCollectionsManagement.ts

@@ -1,4 +1,4 @@
-import { useCallback, useRef, useState } from 'react';
+import { useCallback, useRef, useState, useEffect } from 'react';
 import { CollectionService, MilvusService } from '@/http';
 import { WS_EVENTS, WS_EVENTS_TYPE, LOADING_STATE } from '@server/utils/Const';
 import { checkIndexing, checkLoading } from '@server/utils/Shared';
@@ -12,6 +12,12 @@ export function useCollectionsManagement(database: string) {
   const [collections, setCollections] = useState<CollectionObject[]>([]);
   const [loading, setLoading] = useState(true);
   const requestIdRef = useRef(0);
+  const databaseRef = useRef(database);
+
+  // Update the ref when database changes
+  useEffect(() => {
+    databaseRef.current = database;
+  }, [database]);
 
   const detectLoadingIndexing = useCallback(
     (collections: CollectionObject[]) => {
@@ -25,7 +31,7 @@ export function useCollectionsManagement(database: string) {
           name: WS_EVENTS.COLLECTION_UPDATE,
           type: WS_EVENTS_TYPE.START,
           payload: {
-            database,
+            database: databaseRef.current,
             collections: LoadingOrBuildingCollections.map(
               c => c.collection_name
             ),
@@ -33,15 +39,15 @@ export function useCollectionsManagement(database: string) {
         });
       }
     },
-    [database]
+    [] // Removing database from dependencies
   );
 
   const updateCollections = useCallback(
     (props: { collections: CollectionFullObject[]; database?: string }) => {
       const { collections: updated = [], database: remote } = props;
       if (
-        remote !== database &&
-        database !== undefined &&
+        remote !== databaseRef.current &&
+        databaseRef.current !== undefined &&
         remote !== undefined
       ) {
         return;
@@ -55,13 +61,21 @@ export function useCollectionsManagement(database: string) {
         return Array.from(prevMap.values());
       });
     },
-    [database, detectLoadingIndexing]
+    [detectLoadingIndexing] // Removed database dependency
   );
 
+  const refreshCollectionsDebounceMapRef = useRef<
+    Map<
+      string,
+      { timer: NodeJS.Timeout | null; names: string[]; pending: Set<string> }
+    >
+  >(new Map());
+
   const fetchCollections = async () => {
     const currentRequestId = ++requestIdRef.current;
     try {
       setLoading(true);
+
       setCollections([]);
       const res = await CollectionService.getAllCollections();
       if (currentRequestId === requestIdRef.current) {
@@ -83,24 +97,6 @@ export function useCollectionsManagement(database: string) {
     return res;
   };
 
-  const _fetchCollections = useCallback(
-    async (collectionNames: string[]) => {
-      const res = await CollectionService.getCollections({
-        db_name: database,
-        collections: collectionNames,
-      });
-      updateCollections({ collections: res });
-    },
-    [database, updateCollections]
-  );
-
-  const refreshCollectionsDebounceMapRef = useRef<
-    Map<
-      string,
-      { timer: NodeJS.Timeout | null; names: string[]; pending: Set<string> }
-    >
-  >(new Map());
-
   const batchRefreshCollections = useCallback(
     (collectionNames: string[], key: string = 'default') => {
       let ref = refreshCollectionsDebounceMapRef.current.get(key);
@@ -133,7 +129,14 @@ export function useCollectionsManagement(database: string) {
               return collection && !collection.schema;
             });
             batch.forEach(name => ref!.pending.add(name));
-            await _fetchCollections(batch);
+            // call api
+            const res = await CollectionService.getCollections({
+              db_name: databaseRef.current,
+              collections: batch,
+            });
+            updateCollections({
+              collections: res.filter(c => c.db_name === databaseRef.current),
+            });
             batch.forEach(name => ref!.pending.delete(name));
             ref!.names = ref!.names.slice(batch.length);
           }
@@ -144,7 +147,7 @@ export function useCollectionsManagement(database: string) {
         ref!.timer = null;
       }, 200);
     },
-    [collections, _fetchCollections]
+    [collections, updateCollections] // Removed database dependency
   );
 
   const createCollection = async (data: any) => {

+ 0 - 1
client/src/pages/databases/collections/Collections.tsx

@@ -97,7 +97,6 @@ const Collections = () => {
   const classes = useStyles();
 
   const QuestionIcon = icons.question;
-  const SourceIcon = icons.source;
 
   const formatCollections = useMemo(() => {
     const filteredCollections = search

+ 2 - 0
server/src/collections/collections.service.ts

@@ -425,6 +425,7 @@ export class CollectionsService {
         consistency_level: undefined,
         replicas: undefined,
         loaded: undefined,
+        db_name: database,
       } as CollectionLazyObject;
     }
     // get collection schema and properties
@@ -491,6 +492,7 @@ export class CollectionsService {
       loaded: status === LOADING_STATE.LOADED,
       status,
       properties: collectionInfo.properties,
+      db_name: database,
     };
   }
 

+ 2 - 0
server/src/types/collections.type.ts

@@ -70,6 +70,7 @@ export type CollectionFullObject = {
   status: LOADING_STATE;
   loaded: boolean;
   properties: KeyValuePair[];
+  db_name: string;
 };
 
 export type CollectionLazyObject = {
@@ -87,6 +88,7 @@ export type CollectionLazyObject = {
   replicas: undefined;
   loaded: undefined;
   properties: undefined;
+  db_name: string;
 };
 
 export type CollectionObject = CollectionFullObject | CollectionLazyObject;