Search in sources :

Example 11 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project polaris-java by polarismesh.

the class ConfigFileManagerTest method testGetConfigFile.

@Test
public void testGetConfigFile() {
    ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
    ConfigFile mockedConfigFile = mock(ConfigFile.class);
    when(configFileFactoryManager.getFactory(any())).thenReturn(configFileFactory);
    when(configFileFactory.createConfigFile(configFileMetadata)).thenReturn(mockedConfigFile);
    // 第一次获取
    ConfigFile configFile = defaultConfigFileManager.getConfigFile(configFileMetadata);
    verify(configFileFactoryManager).getFactory(configFileMetadata);
    verify(configFileFactory).createConfigFile(configFileMetadata);
    Assert.assertEquals(mockedConfigFile, configFile);
    // 第二次获取,经过缓存
    ConfigFile configFile2 = defaultConfigFileManager.getConfigFile(configFileMetadata);
    verify(configFileFactoryManager).getFactory(configFileMetadata);
    verify(configFileFactory).createConfigFile(configFileMetadata);
    Assert.assertEquals(mockedConfigFile, configFile2);
}
Also used : ConfigFile(com.tencent.polaris.configuration.api.core.ConfigFile) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) Test(org.junit.Test)

Example 12 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project polaris-java by polarismesh.

the class DefaultConfigFileLongPollingService method addConfigFile.

@Override
public void addConfigFile(RemoteConfigFileRepo remoteConfigFileRepo) {
    ConfigFileMetadata configFileMetadata = remoteConfigFileRepo.getConfigFileMetadata();
    long version = remoteConfigFileRepo.getConfigFileVersion();
    LOGGER.info("[Config] add long polling config file. file = {}, version = {}", configFileMetadata, version);
    configFilePool.putIfAbsent(configFileMetadata, remoteConfigFileRepo);
    // 长轮询起始的配置文件版本号应该以第一次同步拉取为准
    notifiedVersion.putIfAbsent(configFileMetadata, version);
    if (!started.get()) {
        startLongPollingTask();
    }
}
Also used : ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata)

Example 13 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project polaris-java by polarismesh.

the class DefaultConfigFileLongPollingService method assembleWatchConfigFiles.

private List<ConfigFile> assembleWatchConfigFiles() {
    List<ConfigFile> watchConfigFiles = Lists.newArrayList();
    for (Map.Entry<ConfigFileMetadata, RemoteConfigFileRepo> entry : configFilePool.entrySet()) {
        RemoteConfigFileRepo remoteConfigFileRepo = entry.getValue();
        ConfigFileMetadata metadata = remoteConfigFileRepo.getConfigFileMetadata();
        ConfigFile configFile = new ConfigFile(metadata.getNamespace(), metadata.getFileGroup(), metadata.getFileName());
        configFile.setVersion(notifiedVersion.get(metadata));
        watchConfigFiles.add(configFile);
    }
    return watchConfigFiles;
}
Also used : ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) Map(java.util.Map) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata)

Example 14 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project polaris-java by polarismesh.

the class RemoteConfigFileRepoTest method testPullWithUnexpectedResponseCode.

@Test
public void testPullWithUnexpectedResponseCode() {
    ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
    ConfigFileResponse configFileResponse = new ConfigFileResponse(50000, "", null);
    when(configFileConnector.getConfigFile(any())).thenReturn(configFileResponse);
    RemoteConfigFileRepo remoteConfigFileRepo = new RemoteConfigFileRepo(sdkContext, configFileLongPollingService, configFileConnector, configFileMetadata);
    // 重试三次
    verify(configFileConnector, times(3)).getConfigFile(any());
    verify(configFileLongPollingService).addConfigFile(remoteConfigFileRepo);
    Assert.assertNull(remoteConfigFileRepo.getContent());
    Assert.assertEquals(0, remoteConfigFileRepo.getConfigFileVersion());
}
Also used : ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) Test(org.junit.Test)

Example 15 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project polaris-java by polarismesh.

the class RemoteConfigFileRepoTest method testNotifyAndPullSecondTime.

@Test
public void testNotifyAndPullSecondTime() throws InterruptedException {
    ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
    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.getConfigFile(any())).thenReturn(configFileResponse);
    RemoteConfigFileRepo remoteConfigFileRepo = new RemoteConfigFileRepo(sdkContext, configFileLongPollingService, configFileConnector, configFileMetadata);
    AtomicInteger cbCnt = new AtomicInteger();
    // 增加两个listener
    remoteConfigFileRepo.addChangeListener((configFileMetadata1, newContent) -> {
        cbCnt.getAndIncrement();
    });
    remoteConfigFileRepo.addChangeListener((configFileMetadata1, newContent) -> {
        cbCnt.getAndIncrement();
    });
    TimeUnit.MILLISECONDS.sleep(500);
    // 第一次初始化拉取配置
    verify(configFileConnector).getConfigFile(any());
    verify(configFileLongPollingService).addConfigFile(remoteConfigFileRepo);
    Assert.assertEquals(0, cbCnt.get());
    Assert.assertEquals(content, remoteConfigFileRepo.getContent());
    Assert.assertEquals(version, remoteConfigFileRepo.getConfigFileVersion());
    // 变更通知,重新拉取配置
    long newVersion = version + 1;
    String newContent = "hello world2";
    configFile.setContent(newContent);
    configFile.setVersion(newVersion);
    ConfigFileResponse configFileResponse2 = new ConfigFileResponse(ServerCodes.EXECUTE_SUCCESS, "", configFile);
    when(configFileConnector.getConfigFile(any())).thenReturn(configFileResponse2);
    remoteConfigFileRepo.onLongPollNotified(newVersion);
    try {
        TimeUnit.MILLISECONDS.sleep(500);
    } catch (InterruptedException e) {
    // ignore
    }
    verify(configFileConnector, times(2)).getConfigFile(any());
    verify(configFileLongPollingService).addConfigFile(remoteConfigFileRepo);
    // 触发回调
    Assert.assertEquals(2, cbCnt.get());
    Assert.assertEquals(newContent, remoteConfigFileRepo.getContent());
    Assert.assertEquals(newVersion, remoteConfigFileRepo.getConfigFileVersion());
    // 变更通知的版本号小于内存里缓存的版本号,则不会触发重新拉取配置
    long smallVersion = 100;
    remoteConfigFileRepo.onLongPollNotified(smallVersion);
    try {
        TimeUnit.MILLISECONDS.sleep(500);
    } catch (InterruptedException e) {
    // ignore
    }
    verify(configFileConnector, times(2)).getConfigFile(any());
    verify(configFileLongPollingService).addConfigFile(remoteConfigFileRepo);
    // 不触发回调,所以还是2次
    Assert.assertEquals(2, cbCnt.get());
    Assert.assertEquals(newContent, remoteConfigFileRepo.getContent());
    Assert.assertEquals(newVersion, remoteConfigFileRepo.getConfigFileVersion());
}
Also used : ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) Test(org.junit.Test)

Aggregations

ConfigFileMetadata (com.tencent.polaris.configuration.api.core.ConfigFileMetadata)17 Test (org.junit.Test)12 ConfigFileResponse (com.tencent.polaris.api.plugin.configuration.ConfigFileResponse)8 ConfigFile (com.tencent.polaris.api.plugin.configuration.ConfigFile)7 RetriableException (com.tencent.polaris.api.exception.RetriableException)4 DefaultConfigFileMetadata (com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata)3 ErrorCode (com.tencent.polaris.api.exception.ErrorCode)2 ServerCodes (com.tencent.polaris.api.exception.ServerCodes)2 ConfigFileConnector (com.tencent.polaris.api.plugin.configuration.ConfigFileConnector)2 SDKContext (com.tencent.polaris.client.api.SDKContext)2 ConfigFileTestUtils (com.tencent.polaris.configuration.client.ConfigFileTestUtils)2 TimeUnit (java.util.concurrent.TimeUnit)2 RunWith (org.junit.runner.RunWith)2 ArgumentMatchers.anyList (org.mockito.ArgumentMatchers.anyList)2 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)2 Mock (org.mockito.Mock)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.times (org.mockito.Mockito.times)2 Mockito.verify (org.mockito.Mockito.verify)2 Mockito.when (org.mockito.Mockito.when)2