Nacos long polling task

Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

  • Long polling task

Each LongPollingRunnable task handles 3000 listening configuration sets, and if more than 3000, multiple LongPollingRunnable will need to be started to execute.

LongPollingRunnable.run

LongPollingRunnable is a thread that finds the LongPollingRunnable method

  • Check the local configuration with the checkLocalConfig method.
  • Execute the checkUpdateDataIds method and set up a long polling mechanism on the server to obtain the modified data from the server
  • Iterate through the changedGroupKeys data set, call getServerConfig, and modify the data set according to dataId, Group,

Tenant reads the Tenant configuration information from the server and saves it to a local file.

public void run(a){
List<CacheData> cacheDatas=new ArrayList<CacheData>();
List<String> inInitializingCacheList = new ArrayList<String>();
 try{
 	for(CacheData cacheData:cacheMap.get().values()){
 		if(cahceData.getTaskId() == taskId){
 		cahceDatas.add(cacheData);
 		}
 		try{
 			checkLocalConfig(cacheData);
 			if(cacheData.isUseLocalConfigInfo()){ cahceData.checkListenerMd5(); }}catch(Exception e){
 		LOGGER.error("get local config info error",e);
 		}
 	}
 }
  List<String> changeGroupKeys = checkUpdateDataIds(cacheDatas,inInitalizingCacheList);
  for(String groupKey:changedGroupKeys){
  String[] keysGroupKey.parseKey(groupKey);
  String dataId = key[0];
  String group = key[1];
  String tenant = null;
  if(key.length == 3){
  tenant = key[2];
  }
  try{
  	String content = getServerClient(dataId,group,tenant,3000L);
  	CacheData cache = cacheMap.get().get(GroupKey.getKeyTenant(dataId,group,tenant));
  	cache.setContent(content);
  	LOGGER.info("[{}][data-received] dataId = {},group={},tenant={},md5={},content={}",agent.getName(),dataId,group,tenant); LOGGER.error(message,ice); }}for(CacheData cacheData:cacheDatas){
  if(! cacheData.isInitializing() || inInitializingCacheList.contais(GroupKey.getKeyTenant(cacheData.dataId,cahceData.group,cacheData.tenant))){ cacheData.checkListenerMd5(); cacheData.setInitializing(false);
    }
  }
    inInitializingCahceList.clear();
    // Continue timed execution of the current thread
    executorService.execute(this);
}catch(Throwable e){
    LOGGER.error("longPolling error:",e);
    executorService.schedule(this,taskPenaltyTime,TimeUnit.MILLISECONDS);
}
Copy the code

The cacheMap data is split based on taskId, and the cacheMap data is compared to check whether the data in the local configuration file is changed. If the data is changed, a notification is triggered. ${user}/nacos/config/ caches the server configuration information. CheckLocalConfig compares the data in the local disk with the data in the local disk. If the data in the memory is inconsistent with the data in the disk, it indicates that the data has changed.