use of com.tencent.polaris.api.plugin.configuration.ConfigFileResponse in project polaris-java by polarismesh.
the class DefaultConfigFileLongPollingService method doLongPolling.
private void doLongPolling() {
while (!Thread.currentThread().isInterrupted()) {
try {
List<ConfigFile> watchConfigFiles = assembleWatchConfigFiles();
LOGGER.info("[Config] do long polling. config file size = {}, delay time = {}", watchConfigFiles.size(), retryPolicy.getCurrentDelayTime());
ConfigFileResponse response = configFileConnector.watchConfigFiles(watchConfigFiles);
retryPolicy.success();
int responseCode = response.getCode();
// 感知到配置文件发布事件
if (responseCode == ServerCodes.EXECUTE_SUCCESS && response.getConfigFile() != null) {
ConfigFile changedConfigFile = response.getConfigFile();
ConfigFileMetadata metadata = new DefaultConfigFileMetadata(changedConfigFile.getNamespace(), changedConfigFile.getFileGroup(), changedConfigFile.getFileName());
long newNotifiedVersion = changedConfigFile.getVersion();
long oldNotifiedVersion = notifiedVersion.get(metadata);
long maxVersion = Math.max(newNotifiedVersion, oldNotifiedVersion);
// 更新版本号
notifiedVersion.put(metadata, maxVersion);
LOGGER.info("[Config] received change event by long polling. file = {}, new version = {}, old version = {}", metadata, newNotifiedVersion, oldNotifiedVersion);
// 通知 RemoteConfigFileRepo 拉取最新的配置文件
RemoteConfigFileRepo remoteConfigFileRepo = configFilePool.get(metadata);
remoteConfigFileRepo.onLongPollNotified(maxVersion);
continue;
}
// 没有变更
if (responseCode == ServerCodes.DATA_NO_CHANGE) {
LOGGER.info("[Config] long polling result: data no change");
continue;
}
// 预期之外的状态码,退避重试
LOGGER.error("[Config] long polling result with unexpect code. code = {}", responseCode);
retryPolicy.fail();
retryPolicy.executeDelay();
} catch (Throwable t) {
LOGGER.error("[Config] long polling failed.", t);
retryPolicy.fail();
retryPolicy.executeDelay();
}
}
}
use of com.tencent.polaris.api.plugin.configuration.ConfigFileResponse in project polaris-java by polarismesh.
the class RemoteConfigFileRepoTest method testPullNotFoundConfigFile.
@Test
public void testPullNotFoundConfigFile() {
ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
ConfigFileResponse configFileResponse = new ConfigFileResponse(ServerCodes.NOT_FOUND_RESOURCE, "", null);
when(configFileConnector.getConfigFile(any())).thenReturn(configFileResponse);
RemoteConfigFileRepo remoteConfigFileRepo = new RemoteConfigFileRepo(sdkContext, configFileLongPollingService, configFileConnector, configFileMetadata);
verify(configFileConnector).getConfigFile(any());
verify(configFileLongPollingService).addConfigFile(remoteConfigFileRepo);
Assert.assertNull(remoteConfigFileRepo.getContent());
Assert.assertEquals(0, remoteConfigFileRepo.getConfigFileVersion());
}
use of com.tencent.polaris.api.plugin.configuration.ConfigFileResponse in project polaris-java by polarismesh.
the class ConfigFileLongPollingServiceTest method testNotReceivedPushEvent.
@Test
public void testNotReceivedPushEvent() throws InterruptedException {
// 初始化 LongPollingService
DefaultConfigFileLongPollingService longPollingService = new DefaultConfigFileLongPollingService(sdkContext, configFileConnector);
RemoteConfigFileRepo remoteConfigFileRepo = mock(RemoteConfigFileRepo.class);
ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
when(remoteConfigFileRepo.getConfigFileMetadata()).thenReturn(configFileMetadata);
longPollingService.addConfigFile(remoteConfigFileRepo);
// 第一次收到变更事件,第二次也没有变化
when(configFileConnector.watchConfigFiles(anyList())).then(invocation -> {
return new ConfigFileResponse(ServerCodes.DATA_NO_CHANGE, "", null);
}).then(invocation -> {
TimeUnit.SECONDS.sleep(6);
return new ConfigFileResponse(ServerCodes.DATA_NO_CHANGE, "", null);
});
// 因为LongPolling会在 5s 后开始执行
TimeUnit.SECONDS.sleep(7);
verify(configFileConnector, times(2)).watchConfigFiles(anyList());
// 没有触发回调
verify(remoteConfigFileRepo, times(0)).onLongPollNotified(anyLong());
}
use of com.tencent.polaris.api.plugin.configuration.ConfigFileResponse in project polaris-java by polarismesh.
the class ConfigFileLongPollingServiceTest method testReceivedPushEvent.
@Test
public void testReceivedPushEvent() throws InterruptedException {
// 初始化 LongPollingService
DefaultConfigFileLongPollingService longPollingService = new DefaultConfigFileLongPollingService(sdkContext, configFileConnector);
RemoteConfigFileRepo remoteConfigFileRepo = mock(RemoteConfigFileRepo.class);
ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
when(remoteConfigFileRepo.getConfigFileMetadata()).thenReturn(configFileMetadata);
longPollingService.addConfigFile(remoteConfigFileRepo);
// 构造监听响应对象
ConfigFile configFile = new ConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName);
String content = "hello world";
long version = 100;
configFile.setContent(content);
configFile.setVersion(version);
ConfigFileResponse configFileResponse = new ConfigFileResponse(ServerCodes.EXECUTE_SUCCESS, "", configFile);
// 第一次收到变更事件,第二次没有变化
when(configFileConnector.watchConfigFiles(anyList())).thenReturn(configFileResponse).then(invocation -> {
TimeUnit.SECONDS.sleep(30);
return new ConfigFileResponse(ServerCodes.DATA_NO_CHANGE, "", null);
});
// 因为LongPolling会在 5s 后开始执行
TimeUnit.SECONDS.sleep(6);
verify(configFileConnector, times(2)).watchConfigFiles(anyList());
// 验证触发回调
verify(remoteConfigFileRepo).onLongPollNotified(version);
}
use of com.tencent.polaris.api.plugin.configuration.ConfigFileResponse in project polaris-java by polarismesh.
the class ConfigFileLongPollingServiceTest method testSecondReceivedVersionLessThanFirstReceived.
@Test
public void testSecondReceivedVersionLessThanFirstReceived() throws InterruptedException {
// 初始化 LongPollingService
DefaultConfigFileLongPollingService longPollingService = new DefaultConfigFileLongPollingService(sdkContext, configFileConnector);
RemoteConfigFileRepo remoteConfigFileRepo = mock(RemoteConfigFileRepo.class);
ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
when(remoteConfigFileRepo.getConfigFileMetadata()).thenReturn(configFileMetadata);
longPollingService.addConfigFile(remoteConfigFileRepo);
// 第一次版本号为100,第二次版本号为99
when(configFileConnector.watchConfigFiles(anyList())).then(invocation -> {
ConfigFile configFile = new ConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName);
String content = "hello world";
long version = 100;
configFile.setContent(content);
configFile.setVersion(version);
return new ConfigFileResponse(ServerCodes.EXECUTE_SUCCESS, "", configFile);
}).then(invocation -> {
ConfigFile configFile = new ConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName);
String content = "hello world";
long version = 99;
configFile.setContent(content);
configFile.setVersion(version);
return new ConfigFileResponse(ServerCodes.EXECUTE_SUCCESS, "", configFile);
}).then(invocation -> {
TimeUnit.SECONDS.sleep(30);
return new ConfigFileResponse(ServerCodes.DATA_NO_CHANGE, "", null);
});
// 因为LongPolling会在 5s 后开始执行
TimeUnit.SECONDS.sleep(7);
verify(configFileConnector, times(3)).watchConfigFiles(anyList());
// 没有触发回调
verify(remoteConfigFileRepo, times(2)).onLongPollNotified(100);
}
Aggregations