Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: compatible with file.conf and registry.conf configurations #5668

Merged
merged 22 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/en-us/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Add changes here for all PR submitted to the develop branch.
### feature:
- [[#5476](https://github.com/seata/seata/pull/5476)] First support `native-image` for `seata-client`
- [[#5495](https://github.com/seata/seata/pull/5495)] console integration saga-statemachine-designer
- [[#5668](https://github.com/seata/seata/pull/5668)] compatible with file.conf and registry.conf configurations in version 1.4.2 and below

### bugfix:
- [[#5682](https://github.com/seata/seata/pull/5682)] fix saga mode replay context lost startParams
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### feature:
- [[#5476](https://github.com/seata/seata/pull/5476)] seata客户端,首次支持 `native-image`
- [[#5495](https://github.com/seata/seata/pull/5495)] 控制台集成Saga状态机设计器
- [[#5668](https://github.com/seata/seata/pull/5668)] 兼容1.4.2及以下版本的file.conf/registry.conf配置

### bugfix:
- [[#5682](https://github.com/seata/seata/pull/5682)] 修复saga模式下replay context丢失startParams问题
Expand Down
17 changes: 17 additions & 0 deletions common/src/main/java/io/seata/common/ConfigurationKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface ConfigurationKeys {
* The constant FILE_ROOT_REGISTRY.
*/
String FILE_ROOT_REGISTRY = "registry";

/**
* The constant FILE_ROOT_CONFIG.
*/
Expand All @@ -37,6 +38,22 @@ public interface ConfigurationKeys {
* The constant FILE_CONFIG_SPLIT_CHAR.
*/
String FILE_CONFIG_SPLIT_CHAR = ".";

/**
* The constant FILE_ROOT_PREFIX_REGISTRY.
*/
String FILE_ROOT_PREFIX_REGISTRY = FILE_ROOT_REGISTRY + FILE_CONFIG_SPLIT_CHAR;

/**
* The constant FILE_ROOT_PREFIX_CONFIG.
*/
String FILE_ROOT_PREFIX_CONFIG = FILE_ROOT_CONFIG + FILE_CONFIG_SPLIT_CHAR;

/**
* The constant SEATA_FILE_PREFIX_ROOT_CONFIG
*/
String SEATA_FILE_PREFIX_ROOT_CONFIG = SEATA_FILE_ROOT_CONFIG + FILE_CONFIG_SPLIT_CHAR;

/**
* The constant FILE_ROOT_TYPE.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.seata.config;

import java.util.Objects;
import java.util.Optional;

import io.seata.common.exception.NotSupportYetException;
import io.seata.common.loader.EnhancedServiceLoader;
Expand All @@ -42,32 +43,26 @@ public final class ConfigurationFactory {

private static final String ENV_SEATA_CONFIG_NAME = "SEATA_CONFIG_NAME";

public static Configuration CURRENT_FILE_INSTANCE;
public static volatile Configuration CURRENT_FILE_INSTANCE;

public static volatile FileConfiguration ORIGIN_FILE_INSTANCE_REGISTRY;

public static volatile FileConfiguration ORIGIN_FILE_INSTANCE = null;

static {
initOriginConfiguraction();
load();
maybeNeedOriginFileInstance();
}

private static void load() {
String seataConfigName = System.getProperty(SYSTEM_PROPERTY_SEATA_CONFIG_NAME);
if (seataConfigName == null) {
seataConfigName = System.getenv(ENV_SEATA_CONFIG_NAME);
}
if (seataConfigName == null) {
seataConfigName = REGISTRY_CONF_DEFAULT;
}
String envValue = System.getProperty(ENV_PROPERTY_KEY);
if (envValue == null) {
envValue = System.getenv(ENV_SYSTEM_KEY);
}
Configuration configuration = (envValue == null) ? new FileConfiguration(seataConfigName,
false) : new FileConfiguration(seataConfigName + "-" + envValue, false);
Configuration configuration = ORIGIN_FILE_INSTANCE_REGISTRY;
Configuration extConfiguration = null;
try {
extConfiguration = EnhancedServiceLoader.load(ExtConfigurationProvider.class).provide(configuration);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("load Configuration from :{}", extConfiguration == null ?
configuration.getClass().getSimpleName() : "Spring Configuration");
LOGGER.info("load Configuration from :{}",
extConfiguration == null ? configuration.getClass().getSimpleName() : "Spring Configuration");
}
} catch (EnhancedServiceNotFoundException e) {
if (LOGGER.isDebugEnabled()) {
Expand All @@ -79,6 +74,27 @@ private static void load() {
CURRENT_FILE_INSTANCE = extConfiguration == null ? configuration : extConfiguration;
}

private static void initOriginConfiguraction() {
String seataConfigName = System.getProperty(SYSTEM_PROPERTY_SEATA_CONFIG_NAME);
if (seataConfigName == null) {
seataConfigName = System.getenv(ENV_SEATA_CONFIG_NAME);
}
if (seataConfigName == null) {
seataConfigName = REGISTRY_CONF_DEFAULT;
}
String envValue = System.getProperty(ENV_PROPERTY_KEY);
if (envValue == null) {
envValue = System.getenv(ENV_SYSTEM_KEY);
}
seataConfigName = envValue == null ? seataConfigName : seataConfigName + "-" + envValue;
// create FileConfiguration for read registry.conf
ORIGIN_FILE_INSTANCE_REGISTRY = new FileConfiguration(seataConfigName, false);
}

public static FileConfiguration getOriginFileInstanceRegistry() {
return ORIGIN_FILE_INSTANCE_REGISTRY;
}

private static final String NAME_KEY = "name";
private static final String FILE_TYPE = "file";

Expand All @@ -100,28 +116,39 @@ public static Configuration getInstance() {
return instance;
}

private static Configuration buildConfiguration() {
String configTypeName = CURRENT_FILE_INSTANCE.getConfig(
ConfigurationKeys.FILE_ROOT_CONFIG + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR
+ ConfigurationKeys.FILE_ROOT_TYPE);
LOGGER.info("use configuration center type: {}", configTypeName);
private static void maybeNeedOriginFileInstance() {
if (ConfigType.File == getConfigType()) {
String pathDataId = String.join(ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR,
ConfigurationKeys.FILE_ROOT_CONFIG, FILE_TYPE, NAME_KEY);
String name = CURRENT_FILE_INSTANCE.getConfig(pathDataId);
// create FileConfiguration for read file.conf
ORIGIN_FILE_INSTANCE = new FileConfiguration(name);
}
}

private static ConfigType getConfigType() {
String configTypeName = CURRENT_FILE_INSTANCE.getConfig(ConfigurationKeys.FILE_ROOT_CONFIG
+ ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE);
if (StringUtils.isBlank(configTypeName)) {
throw new NotSupportYetException("config type can not be null");
}
ConfigType configType = ConfigType.getType(configTypeName);
return ConfigType.getType(configTypeName);
}

public static Optional<FileConfiguration> getOriginFileInstance() {
return Optional.ofNullable(ORIGIN_FILE_INSTANCE);
}

private static Configuration buildConfiguration() {
ConfigType configType = getConfigType();
Configuration extConfiguration = null;
Configuration configuration;
if (ConfigType.File == configType) {
String pathDataId = String.join(ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR,
ConfigurationKeys.FILE_ROOT_CONFIG, FILE_TYPE, NAME_KEY);
String name = CURRENT_FILE_INSTANCE.getConfig(pathDataId);
configuration = new FileConfiguration(name);
Configuration configuration = ORIGIN_FILE_INSTANCE;
if (configuration != null) {
try {
extConfiguration = EnhancedServiceLoader.load(ExtConfigurationProvider.class).provide(configuration);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("load Configuration from :{}", extConfiguration == null ?
configuration.getClass().getSimpleName() : "Spring Configuration");
LOGGER.info("load Configuration from :{}",
extConfiguration == null ? configuration.getClass().getSimpleName() : "Spring Configuration");
}
} catch (EnhancedServiceNotFoundException ignore) {

Expand Down Expand Up @@ -152,7 +179,9 @@ private static Configuration buildConfiguration() {

protected static void reload() {
ConfigurationCache.clear();
initOriginConfiguraction();
load();
maybeNeedOriginFileInstance();
instance = null;
getInstance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ private void setFailResult(ConfigFuture configFuture) {

}

public FileConfig getFileConfig() {
return fileConfig;
}

/**
* The type FileListener.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.seata.config.file;

import java.util.Map;

/**
* @author wangwei-ying
Expand All @@ -26,5 +27,9 @@ public interface FileConfig {
*/
String getString(String path);

/**
* @return the all config
*/
Map<String, Object> getAllConfig();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import io.seata.config.FileConfiguration;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
* @author wangwei-ying
Expand All @@ -50,4 +52,12 @@ public SimpleFileConfig(File file, String name) {
public String getString(String path) {
return fileConfig.getString(path);
}

@Override
public Map<String, Object> getAllConfig() {
return fileConfig.entrySet().stream().collect(HashMap::new, (m, e) ->
m.put(e.getKey(), e.getValue().unwrapped()),
HashMap::putAll);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -36,34 +37,44 @@
public class YamlFileConfig implements FileConfig {

private static final Logger LOGGER = LoggerFactory.getLogger(YamlFileConfig.class);
private Map configMap;
private final Map<String, Object> configMap = new HashMap<>();

public YamlFileConfig(File file, String name) throws IOException {
Yaml yaml = new Yaml();
try (InputStream is = new FileInputStream(file)) {
configMap = yaml.load(is);
flattenConfig("", yaml.loadAs(is, HashMap.class), configMap);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("file not found");
}
}

@Override
public String getString(String path) {
try {
Map config = configMap;
String[] dataId = path.split("\\.");
for (int i = 0; i < dataId.length - 1; i++) {
if (config.containsKey(dataId[i])) {
config = (Map) config.get(dataId[i]);
private void flattenConfig(String prefix, Map<String, Object> config, Map<String, Object> flatMap) {
for (Map.Entry<String, Object> entry : config.entrySet()) {
String key = prefix.isEmpty() ? entry.getKey() : prefix + "." + entry.getKey();
Object value = entry.getValue();
if (value != null) {
if (value instanceof Map) {
flattenConfig(key, (Map<String, Object>)value, flatMap);
} else {
return null;
flatMap.put(key, String.valueOf(value));
}
}
Object value = config.get(dataId[dataId.length - 1]);
}
}

@Override
public String getString(String path) {
try {
Object value = configMap.get(path);
return value == null ? null : String.valueOf(value);
} catch (Exception e) {
LOGGER.warn("get config data error" + path, e);
return null;
}
}

@Override
public Map<String, Object> getAllConfig() {
return configMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testConfigFileProperties() {
FileConfiguration configuration = mock(FileConfiguration.class);
Configuration currentConfiguration = EnhancedServiceLoader.load(ExtConfigurationProvider.class).provide(configuration);

assertEquals(STR_TEST_AAA, currentConfiguration.getConfig("config.type"));
assertEquals("file", currentConfiguration.getConfig("config.type"));
assertEquals(STR_TEST_BBB, currentConfiguration.getConfig("config.dataType"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
seata.config.type=aaa
seata.config.type=file
seata.config.data-type=bbb
seata.config.file.name=aaa

Expand Down
Loading