Package io.milvus.pool
Class ClientPool<C,T>
- java.lang.Object
-
- io.milvus.pool.ClientPool<C,T>
-
- Direct Known Subclasses:
MilvusClientV1Pool,MilvusClientV2Pool
public class ClientPool<C,T> extends Object
-
-
Field Summary
Fields Modifier and Type Field Description protected PoolClientFactory<C,T>clientFactoryprotected org.apache.commons.pool2.impl.GenericKeyedObjectPool<String,T>clientPoolprotected PoolConfigconfigprotected static org.slf4j.Loggerlogger
-
Constructor Summary
Constructors Modifier Constructor Description protectedClientPool()protectedClientPool(PoolConfig config, PoolClientFactory<C,T> clientFactory)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Release/disconnect idle clients of all key groups.voidclear(String key)Release/disconnect idle clients of a key group.voidclose()Release/disconnect all clients of all key groups, close the pool.voidconfigForKey(String key, C config)Set<String>configKeys()floatfetchClientPerSecond(String key)intgetActiveClientNumber(String key)Return the number of active clients of a key group Threadsafe method.TgetClient(String key)Get a client object from the cache.CgetConfig(String key)intgetIdleClientNumber(String key)Return the number of idle clients of a key group Threadsafe method.intgetTotalActiveClientNumber()Return the number of active clients of all key group Threadsafe method.intgetTotalIdleClientNumber()Return the number of idle clients of all key group Threadsafe method.voidpreparePool(String key)Create minIdlePerKey clients for the pool of the key.voidremoveConfig(String key)voidreturnClient(String key, T grpcClient)Return a client object.
-
-
-
Field Detail
-
logger
protected static final org.slf4j.Logger logger
-
config
protected PoolConfig config
-
clientFactory
protected PoolClientFactory<C,T> clientFactory
-
-
Constructor Detail
-
ClientPool
protected ClientPool()
-
ClientPool
protected ClientPool(PoolConfig config, PoolClientFactory<C,T> clientFactory)
-
-
Method Detail
-
removeConfig
public void removeConfig(String key)
-
preparePool
public void preparePool(String key)
Create minIdlePerKey clients for the pool of the key. Call this method before business can reduce the latency of the first time to getClient().
-
getClient
public T getClient(String key)
Get a client object from the cache. If the cache is empty, it will fetch a client from the underlying pool. The cache maintains a list of clients. The cache will increase the ref-count of this client when it is fetched by getClient(), and decrease the ref-count when it is returned by returnClient(). The cache balances the caller to multiple clients according to the ref-count of each client. getClient() will return the client which has the minimum ref-count to the caller. If the average ref-count of clients is smaller than a threshold, the cache will retire a client which has the maximum ref-count, wait its ref-count to be zero and return it to the underlying pool. If the number of clients hits the MaxTotalPerKey value, this method will be blocked for MaxBlockWaitDuration. If no idle client available after MaxBlockWaitDuration, this method will return a null object to caller.- Parameters:
key- the key of a group where the client belong- Returns:
- MilvusClient or MilvusClientV2
-
returnClient
public void returnClient(String key, T grpcClient)
Return a client object. Once a client is returned, it becomes idle state and wait the next caller. The caller should ensure the client is returned. Otherwise, the client will keep in active state and cannot be used by the next caller. Throw exceptions if the key doesn't exist or the client is not belong to this key group.- Parameters:
key- the key of a group where the client belonggrpcClient- the client object to return
-
close
public void close()
Release/disconnect all clients of all key groups, close the pool.
-
clear
public void clear()
Release/disconnect idle clients of all key groups.
-
clear
public void clear(String key)
Release/disconnect idle clients of a key group.- Parameters:
key- the key of a group
-
getIdleClientNumber
public int getIdleClientNumber(String key)
Return the number of idle clients of a key group Threadsafe method.- Parameters:
key- the key of a group
-
getActiveClientNumber
public int getActiveClientNumber(String key)
Return the number of active clients of a key group Threadsafe method.- Parameters:
key- the key of a group
-
getTotalIdleClientNumber
public int getTotalIdleClientNumber()
Return the number of idle clients of all key group Threadsafe method.
-
getTotalActiveClientNumber
public int getTotalActiveClientNumber()
Return the number of active clients of all key group Threadsafe method.
-
fetchClientPerSecond
public float fetchClientPerSecond(String key)
-
-