forked from milvus-io/milvus-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientPoolExample.java
More file actions
350 lines (330 loc) · 15.9 KB
/
ClientPoolExample.java
File metadata and controls
350 lines (330 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.milvus.v2;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.milvus.pool.MilvusClientV2Pool;
import io.milvus.pool.PoolConfig;
import io.milvus.v1.CommonUtils;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.common.ConsistencyLevel;
import io.milvus.v2.common.DataType;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.collection.request.DropCollectionReq;
import io.milvus.v2.service.database.request.CreateDatabaseReq;
import io.milvus.v2.service.database.request.DropDatabaseReq;
import io.milvus.v2.service.vector.request.InsertReq;
import io.milvus.v2.service.vector.request.QueryReq;
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.request.data.FloatVec;
import io.milvus.v2.service.vector.response.InsertResp;
import io.milvus.v2.service.vector.response.QueryResp;
import io.milvus.v2.service.vector.response.SearchResp;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ClientPoolExample {
public static String ServerUri = "http://localhost:19530";
public static String CollectionName = "java_sdk_example_pool_v2";
public static String VectorFieldName = "vector";
public static int DIM = 128;
public static List<String> dbNames = Arrays.asList("AA", "BB", "CC");
private static void printKeyClientNumber(MilvusClientV2Pool pool, String key) {
System.out.printf("Key '%s': %d idle clients and %d active clients%n",
key, pool.getIdleClientNumber(key), pool.getActiveClientNumber(key));
}
private static void printClientNumber(MilvusClientV2Pool pool) {
System.out.println("======================================================================");
System.out.printf("Total %d idle clients and %d active clients%n",
pool.getTotalIdleClientNumber(), pool.getTotalActiveClientNumber());
for (String dbName : dbNames) {
printKeyClientNumber(pool, dbName);
}
System.out.println("======================================================================");
}
public static void createDatabases(MilvusClientV2Pool pool) {
// get a client, the client uses the default config to connect milvus(to the default database)
String tempKey = "temp";
MilvusClientV2 client = pool.getClient(tempKey);
if (client == null) {
throw new RuntimeException("Unable to create client");
}
try {
for (String dbName : dbNames) {
client.createDatabase(CreateDatabaseReq.builder()
.databaseName(dbName)
.build());
System.out.println("Database created: " + dbName);
}
} catch (Exception e) {
String msg = String.format("Failed to create database, error: %s%n", e.getMessage());
System.out.printf(msg);
throw new RuntimeException(msg);
} finally {
pool.returnClient(tempKey, client);
pool.clear(tempKey); // this call will destroy the temp client
}
// predefine a connect config for each key(name of a database)
// the ClientPool will use different config to create client to connect to specific database
for (String dbName : dbNames) {
ConnectConfig config = ConnectConfig.builder()
.uri(ServerUri)
.dbName(dbName)
.build();
pool.configForKey(dbName, config);
}
}
public static void createCollections(MilvusClientV2Pool pool) {
for (String dbName : dbNames) {
// this client connects to the database of dbName because we have predefined
// a ConnectConfig for this key
MilvusClientV2 client = pool.getClient(dbName);
if (client == null) {
throw new RuntimeException("Unable to create client");
}
try {
client.dropCollection(DropCollectionReq.builder()
.collectionName(CollectionName)
.build());
client.createCollection(CreateCollectionReq.builder()
.collectionName(CollectionName)
.primaryFieldName("id")
.idType(DataType.Int64)
.autoID(Boolean.TRUE)
.vectorFieldName(VectorFieldName)
.dimension(DIM)
.build());
System.out.printf("Collection '%s' created in database '%s'%n", CollectionName, dbName);
} catch (Exception e) {
String msg = String.format("Failed to create collection, error: %s%n", e.getMessage());
System.out.printf(msg);
throw new RuntimeException(msg);
} finally {
pool.returnClient(dbName, client);
}
}
}
public static Thread runInsertThread(MilvusClientV2Pool pool, String dbName, int repeatRequests) {
Thread t = new Thread(() -> {
Gson gson = new Gson();
for (int i = 0; i < repeatRequests; i++) {
MilvusClientV2 client = null;
while (client == null) {
try {
// getClient() might exceeds the borrowMaxWaitMillis and throw exception
// retry to call until it return a client
client = pool.getClient(dbName);
} catch (Exception e) {
System.out.printf("Failed to get client, will retry, error: %s%n", e.getMessage());
}
}
try {
int rowCount = 1;
List<JsonObject> rows = new ArrayList<>();
for (int j = 0; j < rowCount; j++) {
JsonObject row = new JsonObject();
row.add(VectorFieldName, gson.toJsonTree(CommonUtils.generateFloatVector(DIM)));
rows.add(row);
}
InsertResp insertR = client.insert(InsertReq.builder()
.collectionName(CollectionName)
.data(rows)
.build());
// System.out.printf("%d rows inserted%n", insertR.getInsertCnt());
} catch (Exception e) {
System.out.printf("Failed to inserted, error: %s%n", e.getMessage());
} finally {
pool.returnClient(dbName, client); // make sure the client is returned after use
}
}
System.out.printf("Insert thread %s finished%n", Thread.currentThread().getName());
printKeyClientNumber(pool, dbName);
});
t.start();
return t;
}
public static Thread runSearchThread(MilvusClientV2Pool pool, String dbName, int repeatRequests) {
Thread t = new Thread(() -> {
for (int i = 0; i < repeatRequests; i++) {
MilvusClientV2 client = null;
while (client == null) {
try {
// getClient() might exceeds the borrowMaxWaitMillis and throw exception
// retry to call until it return a client
client = pool.getClient(dbName);
} catch (Exception e) {
System.out.printf("Failed to get client, will retry, error: %s%n", e.getMessage());
}
}
try {
SearchResp result = client.search(SearchReq.builder()
.collectionName(CollectionName)
.consistencyLevel(ConsistencyLevel.EVENTUALLY)
.annsField(VectorFieldName)
.limit(10)
.data(Collections.singletonList(new FloatVec(CommonUtils.generateFloatVector(DIM))))
.build());
// System.out.printf("A search request returns %d items with nq %d%n",
// result.getSearchResults().get(0).size(), result.getSearchResults().size());
} catch (Exception e) {
System.out.printf("Failed to search, error: %s%n", e.getMessage());
} finally {
pool.returnClient(dbName, client); // make sure the client is returned after use
}
}
System.out.printf("Search thread %s finished%n", Thread.currentThread().getName());
printKeyClientNumber(pool, dbName);
});
t.start();
return t;
}
public static void verifyRowCount(MilvusClientV2Pool pool, long expectedCount) {
for (String dbName : dbNames) {
// this client connects to the database of dbName because we have predefined
// a ConnectConfig for this key
MilvusClientV2 client = pool.getClient(dbName);
if (client == null) {
throw new RuntimeException("Unable to create client");
}
try {
QueryResp countR = client.query(QueryReq.builder()
.collectionName(CollectionName)
.outputFields(Collections.singletonList("count(*)"))
.consistencyLevel(ConsistencyLevel.STRONG)
.build());
long rowCount = (long) countR.getQueryResults().get(0).getEntity().get("count(*)");
System.out.printf("%d rows persisted in collection '%s' of database '%s'%n",
rowCount, CollectionName, dbName);
if (rowCount != expectedCount) {
throw new RuntimeException("The persisted row count is not equal to expected");
}
} catch (Exception e) {
String msg = String.format("Failed to get row count, error: %s%n", e.getMessage());
System.out.printf(msg);
throw new RuntimeException(msg);
} finally {
pool.returnClient(dbName, client);
}
}
}
public static void dropCollections(MilvusClientV2Pool pool) {
for (String dbName : dbNames) {
// this client connects to the database of dbName because we have predefined
// a ConnectConfig for this key
MilvusClientV2 client = pool.getClient(dbName);
if (client == null) {
throw new RuntimeException("Unable to create client");
}
try {
client.dropCollection(DropCollectionReq.builder()
.collectionName(CollectionName)
.build());
System.out.printf("Collection '%s' dropped in database '%s'%n", CollectionName, dbName);
} catch (Exception e) {
String msg = String.format("Failed to drop collection, error: %s%n", e.getMessage());
System.out.printf(msg);
throw new RuntimeException(msg);
} finally {
pool.returnClient(dbName, client);
}
}
}
public static void dropDatabases(MilvusClientV2Pool pool) {
// get a client, the client uses the default config to connect milvus(to the default database)
String tempKey = "temp";
MilvusClientV2 client = pool.getClient(tempKey);
if (client == null) {
throw new RuntimeException("Unable to create client");
}
try {
for (String dbName : dbNames) {
client.dropDatabase(DropDatabaseReq.builder()
.databaseName(dbName)
.build());
System.out.println("Database dropped: " + dbName);
}
} catch (Exception e) {
String msg = String.format("Failed to drop database, error: %s%n", e.getMessage());
System.out.printf(msg);
throw new RuntimeException(msg);
} finally {
pool.returnClient(tempKey, client);
pool.clear(tempKey); // this call will destroy the temp client
}
}
public static void main(String[] args) throws InterruptedException {
ConnectConfig defaultConfig = ConnectConfig.builder()
.uri(ServerUri)
.build();
// read this issue for more details about the pool configurations:
// https://github.com/milvus-io/milvus-sdk-java/issues/1577
PoolConfig poolConfig = PoolConfig.builder()
.maxIdlePerKey(1) // max idle clients per key
.maxTotalPerKey(5) // max total(idle + active) clients per key
.maxTotal(1000) // max total clients for all keys
.maxBlockWaitDuration(Duration.ofSeconds(5L)) // getClient() will wait 5 seconds if no idle client available
.minEvictableIdleDuration(Duration.ofSeconds(10L)) // if number of idle clients is larger than maxIdlePerKey, redundant idle clients will be evicted after 10 seconds
.build();
MilvusClientV2Pool pool;
try {
pool = new MilvusClientV2Pool(poolConfig, defaultConfig);
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
// create some databases
createDatabases(pool);
// create a collection in each database
createCollections(pool);
List<Thread> threadList = new ArrayList<>();
int threadCount = 100;
int repeatRequests = 100;
long start = System.currentTimeMillis();
// for each database, we create threadCount of threads to call insert() for repeatRequests times
// each insert request will insert one row
// for each database, we create threadCount of threads to call search() for repeatRequests times
for (int k = 0; k < threadCount; k++) {
for (String dbName : dbNames) {
threadList.add(runInsertThread(pool, dbName, repeatRequests));
threadList.add(runSearchThread(pool, dbName, repeatRequests));
}
printClientNumber(pool);
}
for (Thread t : threadList) {
t.join();
}
printClientNumber(pool);
// check row count of each collection, there are threadCount*repeatRequests rows were inserted by multiple threads
verifyRowCount(pool, threadCount * repeatRequests);
// drop collections
dropCollections(pool);
// drop databases, only after database is empty, it is able to be dropped
dropDatabases(pool);
long end = System.currentTimeMillis();
System.out.printf("%d insert requests and %d search requests finished in %.3f seconds%n",
threadCount * repeatRequests * dbNames.size(), threadCount * repeatRequests * dbNames.size(), (end - start) * 0.001);
printClientNumber(pool);
pool.clear(); // clear idle clients
printClientNumber(pool);
pool.close();
}
}