|
20 | 20 | import io.milvus.v2.service.vector.response.SearchResp; |
21 | 21 | import org.apache.calcite.DataContext; |
22 | 22 | import org.apache.calcite.rex.RexNode; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
23 | 25 |
|
24 | 26 | import java.time.Duration; |
25 | 27 | import java.util.*; |
26 | 28 | import java.util.concurrent.ConcurrentHashMap; |
27 | 29 | import java.util.stream.Collectors; |
28 | 30 |
|
29 | 31 | public class MilvusHandler { |
| 32 | + private static final Logger logger = LoggerFactory.getLogger(MilvusHandler.class); |
30 | 33 | private static Map<String, MilvusClientV2Pool> clientPools = new ConcurrentHashMap<>(); |
31 | 34 | private static Gson gson = new GsonBuilder() |
32 | 35 | .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE) |
@@ -236,20 +239,27 @@ private Object[] toRowWithScore(Map<String, Object> entity, List<FieldSchema> fi |
236 | 239 | } |
237 | 240 |
|
238 | 241 | public static void returnClient(MilvusClientV2 client, MilvusConfig milvusConfig) { |
239 | | - String key = milvusConfig.url + milvusConfig.token; |
| 242 | + String key = getClientPoolKey(milvusConfig); |
240 | 243 | if (clientPools.containsKey(key)) { |
241 | 244 | clientPools.get(key).returnClient(key, client); |
| 245 | + } else { |
| 246 | + logger.warn("client pool {} is not found", key); |
| 247 | + client.close(); |
242 | 248 | } |
243 | 249 | } |
244 | 250 |
|
245 | 251 | public static MilvusClientV2 getClient(MilvusConfig milvusConfig) { |
246 | | - String key = milvusConfig.url + milvusConfig.token; |
| 252 | + String key = getClientPoolKey(milvusConfig); |
247 | 253 | if (!clientPools.containsKey(key)) { |
248 | 254 | openClientPool(key, milvusConfig); |
249 | 255 | } |
250 | 256 | return clientPools.get(key).getClient(key); |
251 | 257 | } |
252 | 258 |
|
| 259 | + private static String getClientPoolKey(MilvusConfig milvusConfig) { |
| 260 | + return milvusConfig.url + "|" + milvusConfig.token; |
| 261 | + } |
| 262 | + |
253 | 263 | private static synchronized void openClientPool(String key, MilvusConfig milvusConfig) { |
254 | 264 | if (clientPools.containsKey(key)) { |
255 | 265 | return; |
|
0 commit comments