Search in sources :

Example 1 with ConfigFileMetadata

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

the class ConfigFileFactoryManagerTest method testGetDefaultFactory.

@Test
public void testGetDefaultFactory() {
    ConfigFileMetadata configFileMetadata = assembleConfigFileMeta();
    ConfigFileFactory configFileFactory = configFileFactoryManager.getFactory(configFileMetadata);
    Assert.assertEquals(defaultConfigFileFactory, configFileFactory);
}
Also used : DefaultConfigFileMetadata(com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) Test(org.junit.Test)

Example 2 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata 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();
        }
    }
}
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)

Example 3 with ConfigFileMetadata

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

Example 4 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project spring-cloud-tencent by Tencent.

the class PolarisConfigFileLocator method getInternalConfigFiles.

private List<ConfigFileMetadata> getInternalConfigFiles() {
    String namespace = polarisContextProperties.getNamespace();
    String serviceName = polarisContextProperties.getService();
    if (StringUtils.isEmpty(serviceName)) {
        serviceName = environment.getProperty("spring.application.name");
    }
    List<ConfigFileMetadata> internalConfigFiles = new LinkedList<>();
    // priority: application-${profile} > application > boostrap-${profile} > boostrap
    String[] activeProfiles = environment.getActiveProfiles();
    for (String activeProfile : activeProfiles) {
        if (StringUtils.isEmpty(activeProfile)) {
            continue;
        }
        internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".properties"));
        internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".yml"));
    }
    internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.properties"));
    internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yml"));
    for (String activeProfile : activeProfiles) {
        if (StringUtils.isEmpty(activeProfile)) {
            continue;
        }
        internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".properties"));
        internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".yml"));
    }
    internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.properties"));
    internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yml"));
    return internalConfigFiles;
}
Also used : DefaultConfigFileMetadata(com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata) LinkedList(java.util.LinkedList) DefaultConfigFileMetadata(com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata)

Example 5 with ConfigFileMetadata

use of com.tencent.polaris.configuration.api.core.ConfigFileMetadata in project spring-cloud-tencent by Tencent.

the class PolarisConfigFileLocator method initInternalConfigFiles.

private void initInternalConfigFiles(CompositePropertySource compositePropertySource) {
    List<ConfigFileMetadata> internalConfigFiles = getInternalConfigFiles();
    for (ConfigFileMetadata configFile : internalConfigFiles) {
        PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName());
        compositePropertySource.addPropertySource(polarisPropertySource);
        polarisPropertySourceManager.addPropertySource(polarisPropertySource);
        LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile);
    }
}
Also used : DefaultConfigFileMetadata(com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata)

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