@Override
public void run(){
try{
getLogger().info("Starting configuration poller.");
String newConfigResponse = null;
NodeEndPoint endpointToGetConfig = null;
Collection<NodeEndPoint> endpoints = client.getAvailableNodeEndPoints();
if(endpoints.isEmpty()){
//If no nodes are available status, then get all the endpoints. This provides an
//oppurtunity to re-resolve the hostname by recreating InetSocketAddress instance in "NodeEndPoint".getInetSocketAddress().
endpoints = client.getAllNodeEndPoints();
}
currentIndex = (currentIndex+1)%endpoints.size();
Iterator<NodeEndPoint> iterator = endpoints.iterator();
for(int i =0;i<currentIndex;i++){
iterator.next();
}
endpointToGetConfig = iterator.next();
InetSocketAddress socketAddressToGetConfig = endpointToGetConfig.getInetSocketAddress();
getLogger().info("Endpoint to use for configuration access in this poll " + endpointToGetConfig.toString());
int retryCount = 0;
//If client is not initialized for the first time with the list of cache nodes, keep retrying till the call succeeds.
//To avoid execessive calls, there is a small retry interval between the retry attempts.
while(retryCount < MAX_RETRY_ATTEMPT || !client.isConfigurationInitialized()){
try{
if(client.isConfigurationProtocolSupported()){
try{
newConfigResponse = (String)client.getConfig(socketAddressToGetConfig,
ConfigurationType.CLUSTER,
configTranscoder);
}catch(OperationNotSupportedException e){
//Fallback to key based config access.
client.setIsConfigurationProtocolSupported(false);
continue;
}
} else {
newConfigResponse = (String)client.get(socketAddressToGetConfig,
ConfigurationType.CLUSTER.getValueWithNameSpace(),
configTranscoder);
}
//Operation succeeded and break out of the loop.
break;
}catch(OperationTimeoutException e){
retryCount++;
try{
Thread.sleep(RETRY_INTERVAL);
}catch (InterruptedException ex) {
getLogger().warn("Poller thread interrupted during the retry interval for config call. Continue with retry.", ex);
}
if(retryCount >= MAX_RETRY_ATTEMPT && client.isConfigurationInitialized()) {
getLogger().warn("Max retry attempt reached for config call. Stopping the current poll cycle.", e);
return;
}else if(retryCount == MAX_RETRY_ATTEMPT - 1){
//Fall back to config endpoint
socketAddressToGetConfig = client.getConfigurationNode().getInetSocketAddress();
}else {
//Reresolve on retry attempt
socketAddressToGetConfig = endpointToGetConfig.getInetSocketAddress(true);
}
}
}
if(newConfigResponse == null){
getLogger().warn("The configuration is null in the server " + endpointToGetConfig.getHostName());
trackPollingError();
return;
}
getLogger().debug("Retrieved configuration value:" + newConfigResponse);