Authored by Gino Zhang

优化ESClientMgr 减少并发锁的影响

... ... @@ -18,32 +18,14 @@ public class ESClientMgr {
private volatile Map<String, IElasticsearchClient> nameToClientMap = new ConcurrentHashMap<String, IElasticsearchClient>(50);
public IElasticsearchClient getClient(String indexName)
{
if(nameToClientMap.containsKey(indexName))
{
return nameToClientMap.get(indexName);
}
IElasticsearchClient client = null;
public IElasticsearchClient getClient(String indexName) {
return nameToClientMap.computeIfAbsent(indexName, (name) -> {
IYohoIndexClientFactory factory = new YohoIndexClientFactoryImpl();
String clusterName = Configuration.getString("search.es.cluster.name");
String servers = Configuration.getString("search.es.servers");
Assert.notNull(clusterName, "ClusterName for ES cannot be null.");
Assert.notNull(servers, "Servers for ES cannot be null.");
synchronized (this)
{
if(nameToClientMap.containsKey(indexName))
{
// double check
return nameToClientMap.get(indexName);
}
client = factory.createIndexClient(clusterName, servers, indexName);
nameToClientMap.putIfAbsent(indexName, client);
return factory.createIndexClient(clusterName, servers, name);
});
}
return client;
}
}
... ...