From 7d195f322076ad3de9af96fd32dca6931877c07e Mon Sep 17 00:00:00 2001 From: "ruiyi.jiang" Date: Fri, 21 Jul 2023 14:48:39 +0800 Subject: [PATCH 1/3] fix #225 and fix that server failed to get newer client if address changed. Signed-off-by: ruiyi.jiang --- client/src/pages/connect/AuthForm.tsx | 3 +-- client/src/utils/Format.ts | 2 +- server/src/middlewares/index.ts | 13 +++++------- server/src/milvus/milvus.service.ts | 29 ++++++++++++++------------- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/client/src/pages/connect/AuthForm.tsx b/client/src/pages/connect/AuthForm.tsx index 6679a9f4..8a9ee818 100644 --- a/client/src/pages/connect/AuthForm.tsx +++ b/client/src/pages/connect/AuthForm.tsx @@ -9,7 +9,6 @@ import { ITextfieldConfig } from '../../components/customInput/Types'; import { useFormValidation } from '../../hooks/Form'; import { formatForm } from '../../utils/Form'; import { MilvusHttp } from '../../http/Milvus'; -import { formatAddress } from '../../utils/Format'; import { useNavigate } from 'react-router-dom'; import { rootContext } from '../../context/Root'; import { authContext } from '../../context/Auth'; @@ -183,7 +182,7 @@ export const AuthForm = (props: any) => { const handleConnect = async (event: React.FormEvent) => { event.preventDefault(); - const address = formatAddress(form.address); + const address = form.address; const data = { ...form, address }; await MilvusHttp.connect(data); diff --git a/client/src/utils/Format.ts b/client/src/utils/Format.ts index 5b093123..250c40f0 100644 --- a/client/src/utils/Format.ts +++ b/client/src/utils/Format.ts @@ -144,7 +144,7 @@ export const getCreateFieldType = (config: Field): CreateFieldType => { // Trim the address export const formatAddress = (address: string): string => { // remove http or https prefix from address - const ip = address.replace(/(http|https):\/\//, ''); + const ip = address.replace(/(http):\/\//, ''); return ip.includes(':') ? ip : `${ip}:${DEFAULT_MILVUS_PORT}`; }; diff --git a/server/src/middlewares/index.ts b/server/src/middlewares/index.ts index f1445afc..8348344d 100644 --- a/server/src/middlewares/index.ts +++ b/server/src/middlewares/index.ts @@ -1,4 +1,4 @@ -import { Request, Response, NextFunction, Errback } from 'express'; +import { Request, Response, NextFunction } from 'express'; import morgan from 'morgan'; import chalk from 'chalk'; import { MilvusService } from '../milvus/milvus.service'; @@ -15,9 +15,8 @@ export const ReqHeaderMiddleware = ( const insightCache = req.app.get(INSIGHT_CACHE); // all ape requests need set milvus address in header. // server will set activeaddress in milvus service. - const milvusAddress = MilvusService.formatAddress( - (req.headers[MILVUS_ADDRESS] as string) || '' - ); + const milvusAddress = (req.headers[MILVUS_ADDRESS] as string) || ''; + // console.log('------ Request headers -------', req.headers); // only api request has MILVUS_ADDRESS. // When client run in express, we dont need static files like: xx.js run this logic. @@ -25,9 +24,7 @@ export const ReqHeaderMiddleware = ( if (milvusAddress && insightCache.has(milvusAddress)) { MilvusService.activeAddress = milvusAddress; // insight cache will update expire time when use insightCache.get - MilvusService.activeMilvusClient = insightCache.get( - MilvusService.formatAddress(milvusAddress) - ); + MilvusService.activeMilvusClient = insightCache.get(milvusAddress); } const CONNECT_URL = `/api/v1/milvus/connect`; @@ -35,7 +32,7 @@ export const ReqHeaderMiddleware = ( if (req.url !== CONNECT_URL && !MilvusService.activeMilvusClient) { throw HttpErrors( HTTP_STATUS_CODE.FORBIDDEN, - 'Can not find your connection, please connect Milvus again' + 'Can not find your connection, please check your connection settings.' ); } diff --git a/server/src/milvus/milvus.service.ts b/server/src/milvus/milvus.service.ts index 722eb894..fd3117b9 100644 --- a/server/src/milvus/milvus.service.ts +++ b/server/src/milvus/milvus.service.ts @@ -23,8 +23,8 @@ export class MilvusService { } static formatAddress(address: string) { - // remove http or https prefix from address - const ip = address.replace(/(http|https):\/\//, ''); + // remove http prefix from address + const ip = address.replace(/(http):\/\//, ''); return ip.includes(':') ? ip : `${ip}:${DEFAULT_MILVUS_PORT}`; } @@ -32,7 +32,7 @@ export class MilvusService { if (!MilvusService.activeMilvusClient) { throw HttpErrors( HTTP_STATUS_CODE.FORBIDDEN, - 'Can not find your connection, please connect Milvus again' + 'Can not find your connection, please check your settings.' ); // throw new Error('Please connect milvus first'); @@ -50,27 +50,28 @@ export class MilvusService { const { address, username, password } = data; // grpc only need address without http const milvusAddress = MilvusService.formatAddress(address); - const hasAuth = username !== undefined && password !== undefined; try { - const milvusClient: MilvusClient = hasAuth - ? new MilvusClient({ - address: milvusAddress, - username, - password, - }) - : new MilvusClient({ address }); + const milvusClient: MilvusClient = new MilvusClient({ + address: milvusAddress, + username, + password, + }); // don't break attu await milvusClient.connectPromise.catch(error => { - throw HttpErrors(HTTP_STATUS_CODE.BAD_REQUEST, error); + cache.dump(); + throw HttpErrors(HTTP_STATUS_CODE.FORBIDDEN, error); }); // check healthy const res = await milvusClient.checkHealth(); + console.log('res', milvusAddress, res); + if (res.isHealthy) { MilvusService.activeAddress = address; + cache.dump(); cache.set(address, milvusClient); return { address }; } else { @@ -78,8 +79,8 @@ export class MilvusService { } } catch (error) { // if milvus is not working, delete connection. - cache.del(address); - throw HttpErrors(HTTP_STATUS_CODE.BAD_REQUEST, error); + cache.dump(); + throw HttpErrors(HTTP_STATUS_CODE.FORBIDDEN, error); } } From c905c9dd422fde7c1ff90e7221db55d058bf9361 Mon Sep 17 00:00:00 2001 From: "ruiyi.jiang" Date: Fri, 21 Jul 2023 14:51:38 +0800 Subject: [PATCH 2/3] update comments Signed-off-by: ruiyi.jiang --- server/src/milvus/milvus.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/milvus/milvus.service.ts b/server/src/milvus/milvus.service.ts index fd3117b9..89d25dbb 100644 --- a/server/src/milvus/milvus.service.ts +++ b/server/src/milvus/milvus.service.ts @@ -32,7 +32,7 @@ export class MilvusService { if (!MilvusService.activeMilvusClient) { throw HttpErrors( HTTP_STATUS_CODE.FORBIDDEN, - 'Can not find your connection, please check your settings.' + 'Can not find your connection, please check your connection settings.' ); // throw new Error('Please connect milvus first'); From ae4239a1e9ad2041c23337caf08c09f834a8b442 Mon Sep 17 00:00:00 2001 From: "ruiyi.jiang" Date: Fri, 21 Jul 2023 14:52:52 +0800 Subject: [PATCH 3/3] remove unused code Signed-off-by: ruiyi.jiang --- server/src/milvus/milvus.service.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/server/src/milvus/milvus.service.ts b/server/src/milvus/milvus.service.ts index 89d25dbb..2387860e 100644 --- a/server/src/milvus/milvus.service.ts +++ b/server/src/milvus/milvus.service.ts @@ -60,18 +60,14 @@ export class MilvusService { // don't break attu await milvusClient.connectPromise.catch(error => { - cache.dump(); throw HttpErrors(HTTP_STATUS_CODE.FORBIDDEN, error); }); // check healthy const res = await milvusClient.checkHealth(); - console.log('res', milvusAddress, res); - if (res.isHealthy) { MilvusService.activeAddress = address; - cache.dump(); cache.set(address, milvusClient); return { address }; } else {