Browse Source

chore: remove unused index cache (#763)

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang 4 months ago
parent
commit
c786b3de5e

+ 1 - 9
client/src/http/Collection.service.ts

@@ -24,9 +24,7 @@ export class CollectionService extends BaseModel {
     return super.findAll({ path: `/collections`, params: data || {} });
   }
 
-  static getCollectionsNames(data?: {
-    db_name: string;
-  }): Promise<string[]> {
+  static getCollectionsNames(data?: { db_name: string }): Promise<string[]> {
     return super.findAll({ path: `/collections/names`, params: data || {} });
   }
 
@@ -149,12 +147,6 @@ export class CollectionService extends BaseModel {
     });
   }
 
-  static async flush() {
-    const path = `/collections/index/flush`;
-
-    return super.query({ path, data: {} });
-  }
-
   static queryData(collectionName: string, params: QueryParam) {
     return super.query({
       path: `/collections/${collectionName}/query`,

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

@@ -1,10 +1,9 @@
-import { useCallback, useContext, useMemo, useState } from 'react';
+import { useContext, useMemo, useState } from 'react';
 import { Link, useSearchParams } from 'react-router-dom';
 import { Theme } from '@mui/material';
 import { useTranslation } from 'react-i18next';
 import Highlighter from 'react-highlight-words';
 import { rootContext, authContext, dataContext } from '@/context';
-import { CollectionService } from '@/http';
 import { usePaginationHook } from '@/hooks';
 import AttuGrid from '@/components/grid/Grid';
 import CustomToolBar from '@/components/grid/ToolBar';
@@ -96,10 +95,6 @@ const Collections = () => {
   const QuestionIcon = icons.question;
   const SourceIcon = icons.source;
 
-  const clearIndexCache = useCallback(async () => {
-    await CollectionService.flush();
-  }, []);
-
   const formatCollections = useMemo(() => {
     const filteredCollections = search
       ? collections.filter(collection =>
@@ -319,7 +314,6 @@ const Collections = () => {
             fetchCollection(collection.collection_name);
           }
         } else {
-          clearIndexCache();
           fetchCollections();
         }
       },

+ 1 - 2
server/src/app.ts

@@ -21,7 +21,7 @@ import {
 } from './middleware';
 import { CLIENT_TTL, SimpleQueue, isElectron } from './utils';
 import { getIp } from './utils/Network';
-import { DescribeIndexRes, MilvusClient } from './types';
+import { MilvusClient } from './types';
 import { initWebSocket } from './socket';
 
 // initialize express app
@@ -35,7 +35,6 @@ export const clientCache = new LRUCache<
   {
     milvusClient: MilvusClient;
     address: string;
-    indexCache: LRUCache<string, DescribeIndexRes>;
     database: string;
     collectionsQueue: SimpleQueue<string>;
   }

+ 1 - 11
server/src/collections/collections.controller.ts

@@ -45,7 +45,6 @@ export class CollectionController {
     );
 
     this.router.get('/index', this.describeIndex.bind(this));
-    this.router.post('/index/flush', this.clearCache.bind(this));
 
     // get collection with index info
     this.router.get('/:name', this.describeCollection.bind(this));
@@ -175,7 +174,7 @@ export class CollectionController {
       res.send(
         result.data
           .sort((a, b) => {
-             // sort by name
+            // sort by name
             return a.name.localeCompare(b.name);
           })
           .map((item: any) => item.name)
@@ -645,13 +644,4 @@ export class CollectionController {
       next(error);
     }
   }
-
-  async clearCache(req: Request, res: Response, next: NextFunction) {
-    try {
-      const result = await this.collectionsService.clearCache(req.clientId);
-      res.send(result);
-    } catch (error) {
-      next(error);
-    }
-  }
 }

+ 26 - 61
server/src/collections/collections.service.ts

@@ -748,11 +748,8 @@ export class CollectionsService {
   }
 
   async createIndex(clientId: string, data: CreateIndexReq) {
-    const { milvusClient, indexCache, database } = clientCache.get(clientId);
-    const res = await milvusClient.createIndex(data);
-    const key = `${database}/${data.collection_name}`;
-    // clear cache;
-    indexCache.delete(key);
+    const { milvusClient } = clientCache.get(clientId);
+    await milvusClient.createIndex(data);
 
     // fetch new collections
     const newCollection = (await this.getAllCollections(
@@ -764,64 +761,37 @@ export class CollectionsService {
     return newCollection[0];
   }
 
-  /**
-   * This function is used to describe an index in Milvus.
-   * It first checks if the index description is cached, if so, it returns the cached value.
-   * If not, it calls the Milvus SDK's describeIndex function to get the index description.
-   * If the index is finished building, it caches the index description for future use.
-   * If the index is not finished building, it deletes any cached value for this index.
-   * @param data - The request data for describing an index. It contains the collection name.
-   * @returns - The response from the Milvus SDK's describeIndex function or the cached index description.
-   */
   async describeIndex(clientId: string, data: DescribeIndexReq) {
-    const { milvusClient, indexCache, database } = clientCache.get(clientId);
-
-    // Get the collection name from the request data
-    const key = `${database}/${data.collection_name}`;
-
-    // Try to get the index description from the cache
-    const value = indexCache.get(key);
+    const { milvusClient } = clientCache.get(clientId);
 
-    // If the index description is in the cache, return it
-    if (value) {
-      return value as DescribeIndexRes;
-    } else {
-      // If the index description is not in the cache, call the Milvus SDK's describeIndex function
-      const res = (await milvusClient.describeIndex(data)) as DescribeIndexRes;
-
-      res.index_descriptions.map(index => {
-        // get indexType
-        index.indexType = (index.params.find(p => p.key === 'index_type')
-          ?.value || '') as string;
-        // get metricType
-        const metricTypePair =
-          index.params.filter(v => v.key === 'metric_type') || [];
-        index.metricType = findKeyValue(
-          metricTypePair,
-          'metric_type'
-        ) as string;
-        // get index parameter pairs
-        const paramsJSONstring = findKeyValue(index.params, 'params'); // params is a json string
-        const params =
-          (paramsJSONstring &&
-            getKeyValueListFromJsonString(paramsJSONstring as string)) ||
-          [];
-        index.indexParameterPairs = [...metricTypePair, ...params];
-      });
+    // If the index description is not in the cache, call the Milvus SDK's describeIndex function
+    const res = (await milvusClient.describeIndex(data)) as DescribeIndexRes;
+
+    res.index_descriptions.map(index => {
+      // get indexType
+      index.indexType = (index.params.find(p => p.key === 'index_type')
+        ?.value || '') as string;
+      // get metricType
+      const metricTypePair =
+        index.params.filter(v => v.key === 'metric_type') || [];
+      index.metricType = findKeyValue(metricTypePair, 'metric_type') as string;
+      // get index parameter pairs
+      const paramsJSONstring = findKeyValue(index.params, 'params'); // params is a json string
+      const params =
+        (paramsJSONstring &&
+          getKeyValueListFromJsonString(paramsJSONstring as string)) ||
+        [];
+      index.indexParameterPairs = [...metricTypePair, ...params];
+    });
 
-      // Return the response from the Milvus SDK's describeIndex function
-      return res;
-    }
+    // Return the response from the Milvus SDK's describeIndex function
+    return res;
   }
 
   async dropIndex(clientId: string, data: DropIndexReq) {
-    const { milvusClient, indexCache, database } = clientCache.get(clientId);
-    const res = await milvusClient.dropIndex(data);
+    const { milvusClient, database } = clientCache.get(clientId);
+    await milvusClient.dropIndex(data);
 
-    const key = `${database}/${data.collection_name}`;
-
-    // clear cache;
-    indexCache.delete(key);
     // fetch new collections
     const newCollection = (await this.getAllCollections(
       clientId,
@@ -831,9 +801,4 @@ export class CollectionsService {
 
     return newCollection[0];
   }
-
-  async clearCache(clientId: string) {
-    const { indexCache } = clientCache.get(clientId);
-    return indexCache.clear();
-  }
 }

+ 2 - 12
server/src/milvus/milvus.service.ts

@@ -5,15 +5,9 @@ import {
   ClientConfig,
   CONNECT_STATUS,
 } from '@zilliz/milvus2-sdk-node';
-import { LRUCache } from 'lru-cache';
-import {
-  DEFAULT_MILVUS_PORT,
-  INDEX_TTL,
-  SimpleQueue,
-  HTTP_STATUS_CODE,
-} from '../utils';
+import { DEFAULT_MILVUS_PORT, SimpleQueue, HTTP_STATUS_CODE } from '../utils';
 import { clientCache } from '../app';
-import { DescribeIndexRes, AuthReq, AuthObject } from '../types';
+import { AuthReq, AuthObject } from '../types';
 import { cronsManager } from '../crons';
 import HttpErrors from 'http-errors';
 
@@ -115,10 +109,6 @@ export class MilvusService {
       clientCache.set(milvusClient.clientId, {
         milvusClient,
         address,
-        indexCache: new LRUCache<string, DescribeIndexRes>({
-          ttl: INDEX_TTL,
-          ttlAutopurge: true,
-        }),
         database: db,
         collectionsQueue: new SimpleQueue<string>(),
       });

+ 1 - 1
server/src/users/users.service.ts

@@ -131,7 +131,7 @@ export class UserService {
       'CollectionReadWrite',
     ];
 
-    let res: Record<string, any> = {};
+    const res: Record<string, any> = {};
 
     ['cluster', 'db', 'collection', 'default', 'custom', 'all'].forEach(
       type => {