Search in sources :

Example 6 with ConfigFile

use of com.tencent.polaris.api.plugin.configuration.ConfigFile 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);
}
Also used : ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) Test(org.junit.Test)

Example 7 with ConfigFile

use of com.tencent.polaris.api.plugin.configuration.ConfigFile 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);
}
Also used : ErrorCode(com.tencent.polaris.api.exception.ErrorCode) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) ConfigFileTestUtils(com.tencent.polaris.configuration.client.ConfigFileTestUtils) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) RetriableException(com.tencent.polaris.api.exception.RetriableException) Mockito.when(org.mockito.Mockito.when) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) SDKContext(com.tencent.polaris.client.api.SDKContext) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) ConfigFileConnector(com.tencent.polaris.api.plugin.configuration.ConfigFileConnector) ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) ServerCodes(com.tencent.polaris.api.exception.ServerCodes) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) Test(org.junit.Test)

Example 8 with ConfigFile

use of com.tencent.polaris.api.plugin.configuration.ConfigFile 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 9 with ConfigFile

use of com.tencent.polaris.api.plugin.configuration.ConfigFile in project polaris-java by polarismesh.

the class RemoteConfigFileRepo method doPull.

@Override
protected void doPull() {
    long startTime = System.currentTimeMillis();
    ConfigFile pullConfigFileReq = new ConfigFile(configFileMetadata.getNamespace(), configFileMetadata.getFileGroup(), configFileMetadata.getFileName());
    pullConfigFileReq.setVersion(notifiedVersion.get());
    LOGGER.info("[Config] start pull config file. config file = {}, version = {}", configFileMetadata, notifiedVersion.get());
    int retryTimes = 0;
    while (retryTimes < 3) {
        try {
            ConfigFileResponse response = configFileConnector.getConfigFile(pullConfigFileReq);
            retryPolicy.success();
            // 打印请求信息
            long pulledConfigFileVersion = response.getConfigFile() != null ? response.getConfigFile().getVersion() : -1;
            LOGGER.info("[Config] pull config file finished. config file = {}, code = {}, version = {}, duration = {} ms", configFileMetadata, response.getCode(), pulledConfigFileVersion, System.currentTimeMillis() - startTime);
            if (response.getCode() == ServerCodes.EXECUTE_SUCCESS) {
                ConfigFile pulledConfigFile = response.getConfigFile();
                // 本地配置文件落后,更新内存缓存
                if (remoteConfigFile.get() == null || pulledConfigFile.getVersion() >= remoteConfigFile.get().getVersion()) {
                    ConfigFile copiedConfigFile = deepCloneConfigFile(pulledConfigFile);
                    remoteConfigFile.set(copiedConfigFile);
                    // 配置有更新,触发回调
                    fireChangeEvent(copiedConfigFile.getContent());
                }
                return;
            }
            // 远端没有此配置文件
            if (response.getCode() == ServerCodes.NOT_FOUND_RESOURCE) {
                LOGGER.warn("[Config] config file not found, please check whether config file released. {}", configFileMetadata);
                // 删除配置文件
                if (remoteConfigFile.get() != null) {
                    remoteConfigFile.set(null);
                    // 删除配置文件也需要触发通知
                    fireChangeEvent(null);
                }
                return;
            }
            // 预期之外的状态码,重试
            LOGGER.error("[Config] pull response without expected code. retry times = {}, code = {}", retryTimes, response.getCode());
            retryPolicy.fail();
            retryTimes++;
            retryPolicy.executeDelay();
        } catch (Throwable t) {
            LOGGER.error("[Config] failed to pull config file. retry times = {}", retryTimes, t);
            retryPolicy.fail();
            retryTimes++;
            retryPolicy.executeDelay();
        }
    }
}
Also used : ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile)

Example 10 with ConfigFile

use of com.tencent.polaris.api.plugin.configuration.ConfigFile 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

ConfigFile (com.tencent.polaris.api.plugin.configuration.ConfigFile)11 ConfigFileResponse (com.tencent.polaris.api.plugin.configuration.ConfigFileResponse)6 ConfigFileMetadata (com.tencent.polaris.configuration.api.core.ConfigFileMetadata)6 Test (org.junit.Test)4 RetriableException (com.tencent.polaris.api.exception.RetriableException)2 ErrorCode (com.tencent.polaris.api.exception.ErrorCode)1 ServerCodes (com.tencent.polaris.api.exception.ServerCodes)1 ConfigFileConnector (com.tencent.polaris.api.plugin.configuration.ConfigFileConnector)1 SDKContext (com.tencent.polaris.client.api.SDKContext)1 ConfigFileProto (com.tencent.polaris.client.pb.ConfigFileProto)1 PolarisConfigGRPCGrpc (com.tencent.polaris.client.pb.PolarisConfigGRPCGrpc)1 ConfigFileTestUtils (com.tencent.polaris.configuration.client.ConfigFileTestUtils)1 Connection (com.tencent.polaris.plugins.connector.grpc.Connection)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RunWith (org.junit.runner.RunWith)1 ArgumentMatchers.anyList (org.mockito.ArgumentMatchers.anyList)1 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)1 Mock (org.mockito.Mock)1