Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client id #251

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions milvus/grpc/BaseClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import crypto from 'crypto';
import protobuf, { Root, Type } from 'protobufjs';
import { Client, ChannelOptions } from '@grpc/grpc-js';
import {
Expand All @@ -25,6 +26,8 @@ const schemaProtoPath = path.resolve(
* Base gRPC client, setup all configuration here
*/
export class BaseClient {
// Client ID
clientId: string = `${crypto.randomUUID()}`;
// flags to indicate that if the connection is established and its state
connectStatus = CONNECT_STATUS.NOT_CONNECTED;
connectPromise = Promise.resolve();
Expand Down Expand Up @@ -103,6 +106,11 @@ export class BaseClient {
config.username = config.username || '';
config.password = config.password || '';

// overwrite ID if necessary
if (config.id) {
this.clientId = config.id;
}

// Assign the configuration object.
this.config = config;

Expand Down
1 change: 1 addition & 0 deletions milvus/grpc/GrpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class GRPCClient extends User {
typeof this.config.retryDelay === 'undefined'
? DEFAULT_RETRY_DELAY
: this.config.retryDelay,
clientId: this.clientId,
});
// interceptors
const interceptors = [metaInterceptor, retryInterceptor];
Expand Down
1 change: 1 addition & 0 deletions milvus/types/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ChannelOptions } from '@grpc/grpc-js';
* Configuration options for the Milvus client.
*/
export interface ClientConfig {
id?: string;
// optional proto file paths
// refer to https://github.com/milvus-io/milvus-proto
protoFilePath?: {
Expand Down
14 changes: 8 additions & 6 deletions milvus/utils/Grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ export const getMetaInterceptor = (
export const getRetryInterceptor = ({
maxRetries = 3,
retryDelay = 30,
clientId = ''
}: {
maxRetries: number;
retryDelay: number;
clientId: string;
}) =>
function (options: any, nextCall: any) {
let savedMetadata: any;
Expand Down Expand Up @@ -153,7 +155,7 @@ export const getRetryInterceptor = ({
const _timeout = deadline.getTime() - startTime.getTime();
// log
logger.debug(
`[DB:${dbname}:${methodName}] executed failed, status: ${JSON.stringify(
`[${clientId}>${dbname}>${methodName}] executed failed, status: ${JSON.stringify(
status
)}, timeout set: ${_timeout}ms, retry after ${_retryDelay} ms.`
);
Expand All @@ -173,7 +175,7 @@ export const getRetryInterceptor = ({
}, _retryDelay);
} else {
logger.debug(
`[DB:${dbname}:${methodName}] retry run out of ${retries} times. ${JSON.stringify(
`[${clientId}>${dbname}>${methodName}] retry run out of ${retries} times. ${JSON.stringify(
status
)}`
);
Expand All @@ -185,7 +187,7 @@ export const getRetryInterceptor = ({
}
} else {
logger.debug(
`[DB:${dbname}:${methodName}] retried successfully in ${
`[${clientId}>${dbname}>${methodName}] retried successfully in ${
Date.now() - startTime.getTime()
}ms.`
);
Expand All @@ -210,7 +212,7 @@ export const getRetryInterceptor = ({
case grpcStatus.UNIMPLEMENTED:
// const returnMsg = { error_code: 'Success', reason: '' };
logger.debug(
`[DB:${dbname}:${methodName}] returns ${JSON.stringify(
`[${clientId}>${dbname}>${methodName}] returns ${JSON.stringify(
status
)}`
);
Expand All @@ -223,7 +225,7 @@ export const getRetryInterceptor = ({
default:
// OK
logger.debug(
`[DB:${dbname}:${methodName}] executed in ${
`[${clientId}>${dbname}>${methodName}] executed in ${
Date.now() - startTime.getTime()
}ms, returns ${JSON.stringify(savedReceiveMessage)}`
);
Expand All @@ -237,7 +239,7 @@ export const getRetryInterceptor = ({
},
sendMessage: function (message: any, next: any) {
logger.debug(
`[DB:${dbname}:${methodName}] sending ${JSON.stringify(message)}`
`[${clientId}>${dbname}>${methodName}] sending ${JSON.stringify(message)}`
);
savedSendMessage = message;
next(message);
Expand Down
16 changes: 11 additions & 5 deletions test/MilvusClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,42 @@ import { MilvusClient, ERROR_REASONS, CONNECT_STATUS } from '../milvus';
import sdkInfo from '../sdk.json';
import { IP } from './tools';

const milvusClient = new MilvusClient({ address: IP });
const milvusClient = new MilvusClient({
address: IP,
});

describe(`Milvus client`, () => {
afterEach(() => {
jest.clearAllMocks();
});

// it(`should create a grpc client with cert file successfully`, () => {
// it(`should create a grpc client with cert file successfully`, async () => {
// const milvusClient = new MilvusClient({
// address: 'localhost:19530',
// address: IP,
// tls: {
// rootCertPath: `test/cert/ca.pem`,
// privateKeyPath: `test/cert/client.key`,
// certChainPath: `test/cert/client.pem`,
// serverName: 'localhost',
// serverName: IP,
// },
// id: '1',
// });

// expect(milvusClient.client).toBeDefined();
// expect(milvusClient.tlsMode).toEqual(2);
// expect(milvusClient.clientId).toEqual('1');
// });

it(`should create a grpc client without SSL credentials when ssl is false`, () => {
const milvusClient = new MilvusClient({
address: 'localhost:19530',
address: IP,
ssl: false,
username: 'username',
password: 'password',
id: '1',
});

expect(milvusClient.clientId).toEqual('1');
expect(milvusClient.client).toBeDefined();
});

Expand Down