Skip to content

Commit

Permalink
fix example and add milvus version validate function
Browse files Browse the repository at this point in the history
Signed-off-by: nameczz <[email protected]>
  • Loading branch information
nameczz committed Jan 17, 2022
1 parent b143973 commit f79eb69
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 19 deletions.
3 changes: 3 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ function removeDistPackageJson(source) {
function writeSdkJson(path) {
try {
const version = packageJson.version;
const milvusVersion = packageJson.milvusVersion;

const content = {
version,
milvusVersion,
};
let data = JSON.stringify(content);
fs.writeFileSync(path, data);
Expand Down
6 changes: 3 additions & 3 deletions example/Cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { IP } from "../const";
const milvusClient = new MilvusClient(IP);
const dataManager = milvusClient.dataManager;

console.log(MilvusClient.getSdkVersion());

const test = async () => {
let res: any = await dataManager.getMetric({
request: { metric_type: "system_info" },
});
console.log("--- metric ---", res.response.nodes_info[0].infos);
res.response.nodes_info.forEach((v: any) => {
console.log(v.infos);
});

// res = await dataManager.getQuerySegmentInfo({
// collectionName: COLLECTION_NAME,
Expand Down
1 change: 0 additions & 1 deletion example/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { genCollectionParams } from "../utils/test";
const milvusClient = new MilvusClient(IP);
const collectionManager = milvusClient.collectionManager;
const COLLECTION_NAME = GENERATE_NAME();
console.log(MilvusClient.getSdkVersion());

const test = async () => {
const createRes = await collectionManager.createCollection(
Expand Down
15 changes: 10 additions & 5 deletions example/HelloMilvus.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { MilvusClient } from "@zilliz/milvus2-sdk-node";
import { DataType } from "@zilliz/milvus2-sdk-node/dist/milvus/types/Common";
// todo need change Insert to Data after new release
import { InsertReq } from "@zilliz/milvus2-sdk-node/dist/milvus/types/Insert";
// import { MilvusClient } from "@zilliz/milvus2-sdk-node";
import { MilvusClient } from "../dist/milvus";
import { DataType } from "../dist/milvus/types/Common";
import { InsertReq } from "../dist/milvus/types/Data";

// import { DataType } from "@zilliz/milvus2-sdk-node/dist/milvus/types/Common";
// import { InsertReq } from "@zilliz/milvus2-sdk-node/dist/milvus/types/Data";

const milvusClient = new MilvusClient("localhost:19530");
const collectionManager = milvusClient.collectionManager;

console.log("Milvus SDK Version: ", MilvusClient.getSdkVersion());
console.log("Milvus SDK Info: ", milvusClient.sdkInfo);

const generateInsertData = function generateInsertData(
fields: { isVector: boolean; dim?: number; name: string; isBool?: boolean }[],
Expand All @@ -33,6 +36,8 @@ const generateInsertData = function generateInsertData(
};

const hello_milvus = async () => {
const checkVersion = await milvusClient.checkVersion();
console.log("--- check version ---", checkVersion);
const collectionName = "hello_milvus";
const dim = "4";
const createRes = await collectionManager.createCollection({
Expand Down
1 change: 0 additions & 1 deletion example/LoadCollectionStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { genCollectionParams } from "../utils/test";
const milvusClient = new MilvusClient(IP);
const collectionManager = milvusClient.collectionManager;
const COLLECTION_NAME = GENERATE_NAME();
console.log(MilvusClient.getSdkVersion());

const test = async () => {
await collectionManager.createCollection(
Expand Down
2 changes: 1 addition & 1 deletion milvus/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ export class Data extends Client {
let flushRes = null;
while (!isFlushed) {
flushRes = await this.getFlushState({ segmentIDs: segIDs });
await sleep(1000);
await sleep(100);
isFlushed = flushRes.flushed;
}
// Before Milvus pre-GA will throw error
Expand Down
26 changes: 25 additions & 1 deletion milvus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Index } from "./MilvusIndex";
import { Data } from "./Data";
import sdkInfo from "../sdk.json";
import { ERROR_REASONS } from "./const/ErrorReason";
import { ErrorCode } from "./types/Response";

const protoPath = path.resolve(__dirname, "../grpc-proto/milvus.proto");
export class MilvusClient {
Expand Down Expand Up @@ -49,12 +50,35 @@ export class MilvusClient {
this.dataManager = new Data(this.client, this.collectionManager);
}

static getSdkVersion() {
get sdkInfo() {
return {
version: sdkInfo.version,
recommandMilvus: sdkInfo.milvusVersion,
};
}

/**
* @ignore
* Everytime build sdk will rewrite sdk.json depend on version, milvusVersion fields in package.json.
* @returns
*/
async checkVersion() {
const res = await this.dataManager.getMetric({
request: { metric_type: "system_info" },
});
// Each node contains the same system info, so get version from first one.
const curMilvusVersion =
res.response.nodes_info[0]?.infos?.system_info?.build_version;
if (curMilvusVersion !== this.sdkInfo.recommandMilvus) {
console.warn("------- Warning ---------");
console.warn(
`Node sdk ${this.sdkInfo.version} recommend Milvus Version ${this.sdkInfo.recommandMilvus}.\nDifferent version may cause some error.`
);
return { error_code: ErrorCode.SUCCESS, match: false };
}
return { error_code: ErrorCode.SUCCESS, match: true };
}

closeConnection() {
this.client.close();
// closed -> 4, connected -> 0
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "@zilliz/milvus2-sdk-node",
"author": "zizhao.chen@zilliz.com",
"author": "ued@zilliz.com",
"version": "1.0.20",
"milvusVersion": "v2.0.0",
"main": "dist/milvus",
"files": [
"dist"
Expand Down Expand Up @@ -36,4 +37,4 @@
"publishConfig": {
"access": "public"
}
}
}
3 changes: 2 additions & 1 deletion sdk.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"version": "v1.0.0"
"version": "v1.0.0",
"milvusVersion": "v2.0.0"
}
16 changes: 12 additions & 4 deletions test/MilvusClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MilvusClient } from "../milvus";
import sdkInfo from "../sdk.json";
import { IP } from "../const";
import { ERROR_REASONS } from "../milvus/const/ErrorReason";
import { ErrorCode } from "../milvus/types/Response";

const milvusClient = new MilvusClient(IP);

Expand All @@ -13,12 +14,19 @@ describe("Milvus client ", () => {
expect(error.message).toEqual(ERROR_REASONS.MILVUS_ADDRESS_IS_REQUIRED);
}
});

it("Expect get node sdk info", async () => {
expect(milvusClient.sdkInfo.version).toEqual(sdkInfo.version);
expect(milvusClient.sdkInfo.recommandMilvus).toEqual(sdkInfo.milvusVersion);
});

it("Check version should success", async () => {
const res = await milvusClient.checkVersion();
expect(res.error_code).toEqual(ErrorCode.SUCCESS);
});

it("Expect close connection success", async () => {
const res = milvusClient.closeConnection();
expect(res).toEqual(4);
});

it("Expect get node sdk vertion", async () => {
expect(MilvusClient.getSdkVersion().version).toEqual(sdkInfo.version);
});
});

0 comments on commit f79eb69

Please sign in to comment.