milvus-io/milvus-sdk-java

How to support connection pooling in milvus-java-sdk?(如何在milvus-java-sdk中支持连接池?)

Closed this issue · 4 comments

How can milvus-java-sdk's milvusClient support connection pooling? I noticed that the Python version of milvus-sdk can simply configure multiple connections by specifying an alias, but I couldn't find a similar feature in the java-sdk. If the java-sdk does not currently support connection pooling, could you please provide some ideas on how to implement this feature?

中文:请问milvus-java-sdk的milvusClient如何支持连接池?我注意到Python版本的milvus-sdk可以通过指定alias来简单配置多个连接,但在java-sdk中没有找到相关功能。如果当前java-sdk暂时不支持连接池,请问有实现该功能的思路吗?

Thank you for your help!感谢回复!
yhmo commented

Java sdk doesn't have connection pool. Each MilvusClient object maintains a long connection.
You can use a Map<String, MilvusClient> to build your simple connection pool.

You can use LinkedBlockingQueue to build connection pool,if you have more milvus IP,you can use ConcurrentHashMap<String,LinkedBlockingQueue>. Also,you can use Apache-commonPool,etc. to achieve it.
Follow is a simple example:
public class MilvusConnectionPool {

private final BlockingQueue<MilvusServiceClient> pool = new LinkedBlockingQueue<>(); ;
private String poolName;
public MilvusConnectionPool(String poolName,MilvusConfig milvusConfig,int poolSize){
    this.poolName = poolName;
    init(milvusConfig,poolSize);
}

private void init(MilvusConfig milvusConfig,int poolSize){
    int maxPoolSize = 1;
    if(poolSize>0){
        maxPoolSize = poolSize;
    }
    for (int i = 0; i < maxPoolSize; i++) {
        try {
            ConnectParam connectParam = ConnectParam.newBuilder()
                    .withHost(milvusConfig.getHost())
                    .withPort(milvusConfig.getPort())
                    .withAuthorization(milvusConfig.getUser(), milvusConfig.getPassword())
                    .build();
            MilvusServiceClient client = new MilvusServiceClient(connectParam);
            client.withRetry(3);
            pool.offer(client);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("[ERROR]init milvus connection pool throw exception!");
        }
    }
    System.out.println(String.format("[INFO]%s-milvus连接池【%s:%s】初始化完成,共有%s个连接",this.poolName,milvusConfig.getHost(),milvusConfig.getPort(),pool.size()));
}

public MilvusServiceClient getConnection() throws InterruptedException {
    return pool.take();
}

public void releaseConnection(MilvusServiceClient client) {
    if (client == null) return;
    pool.offer(client);
}

public void closeAll() {
    for (MilvusServiceClient client : pool) {
        client.close();
    }
    pool.clear();
    System.out.println(String.format("[INFO]%s-milvus连接池释放所有连接完成!",this.poolName));
}

}

I believe connection pool is a great feature and it would be great to have it.

yhmo commented

The latest version v2.4.3 and v2.3.9 have been supported connection pool, close this issue.