Skip to content

Commit

Permalink
optimize: extract the default values for some properties (apache#4932)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuwenlin committed Sep 15, 2022
1 parent bdb54a0 commit a986644
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 35 deletions.
2 changes: 2 additions & 0 deletions changes/en-us/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Add changes here for all PR submitted to the develop branch.
- [[#4911](https://github.com/seata/seata/pull/4911)] add license checker workflow
- [[#4917](https://github.com/seata/seata/pull/4917)] upgrade package-lock.json fix vulnerabilities
- [[#4924](https://github.com/seata/seata/pull/4924)] optimize pom dependencies
- [[#4932](https://github.com/seata/seata/pull/4932)] extract the default values for some properties

### test:
- [[#4794](https://github.com/seata/seata/pull/4794)] try to fix the test `DataSourceProxyTest.getResourceIdTest()`
Expand All @@ -56,5 +57,6 @@ Thanks to these contributors for their code commits. Please report an unintended
- [AlbumenJ](https://github.com/AlbumenJ)
- [doubleDimple](https://github.com/doubleDimple)
- [jsbxyyx](https://github.com/jsbxyyx)
- [tuwenlin](https://github.com/tuwenlin)

Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
2 changes: 2 additions & 0 deletions changes/zh-cn/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- [[#4911](https://github.com/seata/seata/pull/4911)] 添加 header 和license 检测
- [[#4917](https://github.com/seata/seata/pull/4917)] 升级 package-lock.json 修复漏洞
- [[#4924](https://github.com/seata/seata/pull/4924)] 优化 pom 依赖
- [[#4932](https://github.com/seata/seata/pull/4932)] 抽取部分配置的默认值

### test:
- [[#4794](https://github.com/seata/seata/pull/4794)] 重构代码,尝试修复单元测试 `DataSourceProxyTest.getResourceIdTest()`
Expand All @@ -55,5 +56,6 @@
- [AlbumenJ](https://github.com/AlbumenJ)
- [doubleDimple](https://github.com/doubleDimple)
- [jsbxyyx](https://github.com/jsbxyyx)
- [tuwenlin](https://github.com/tuwenlin)

同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
74 changes: 74 additions & 0 deletions common/src/main/java/io/seata/common/DefaultValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,78 @@ public interface DefaultValues {
*/
int DEFAULT_XA_CONNECTION_TWO_PHASE_HOLD_TIMEOUT = 10000;

/**
* the constant DEFAULT_COMMITING_RETRY_PERIOD
*/
int DEFAULT_COMMITING_RETRY_PERIOD = 1000;

/**
* the constant DEFAULT_ASYNC_COMMITTING_RETRY_PERIOD
*/
int DEFAULT_ASYNC_COMMITTING_RETRY_PERIOD = 1000;

/**
* the constant DEFAULT_ROLLBACKING_RETRY_PERIOD
*/
int DEFAULT_ROLLBACKING_RETRY_PERIOD = 1000;

/**
* the constant DEFAULT_TIMEOUT_RETRY_PERIOD
*/
int DEFAULT_TIMEOUT_RETRY_PERIOD = 1000;

/**
* the constant DEFAULT_UNDO_LOG_DELETE_PERIOD
*/
long DEFAULT_UNDO_LOG_DELETE_PERIOD = 24 * 60 * 60 * 1000;

/**
* the constant DEFAULT_SERVICE_SESSION_RELOAD_READ_SIZE
*/
int DEFAULT_SERVICE_SESSION_RELOAD_READ_SIZE = 100;

/**
*the constant DEFAULT_PROMETHEUS_PORT
*/
int DEFAULT_PROMETHEUS_PORT = 9898;

/**
* the const DEFAULT_METRICS_ENABLED
*/
boolean DEFAULT_METRICS_ENABLED = false;

/**
* the const DEFAULT_METRICS_REGISTRY_TYPE
*/
String DEFAULT_METRICS_REGISTRY_TYPE = "compact";

/**
* the const DEFAULT_METRICS_EXPORTER_LIST
*/
String DEFAULT_METRICS_EXPORTER_LIST = "prometheus";

/**
* the const DEFAULT_MAX_COMMIT_RETRY_TIMEOUT
*/
long DEFAULT_MAX_COMMIT_RETRY_TIMEOUT = 100;

/**
* the const DEFAULT_MAX_ROLLBACK_RETRY_TIMEOUT
*/
long DEFAULT_MAX_ROLLBACK_RETRY_TIMEOUT = 100;

/**
* the const DEFAULT_ROLLBACK_RETRY_TIMEOUT_UNLOCK_ENABLE
*/
boolean DEFAULT_ROLLBACK_RETRY_TIMEOUT_UNLOCK_ENABLE = false;

/**
* DEFAULT_DISTRIBUTED_LOCK_EXPIRE_TIME
*/
long DEFAULT_DISTRIBUTED_LOCK_EXPIRE_TIME = 10000;

/**
* DEFAULT_ENABLE_BRANCH_ASYNC_REMOVE
*/
boolean DEFAULT_ENABLE_BRANCH_ASYNC_REMOVE = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.seata.common.DefaultValues.DEFAULT_METRICS_EXPORTER_LIST;

/**
* Exporter Factory for load all configured exporters
*
Expand All @@ -37,7 +39,7 @@ public class ExporterFactory {
public static List<Exporter> getInstanceList() {
List<Exporter> exporters = new ArrayList<>();
String exporterTypeNameList = ConfigurationFactory.getInstance().getConfig(
ConfigurationKeys.METRICS_PREFIX + ConfigurationKeys.METRICS_EXPORTER_LIST, null);
ConfigurationKeys.METRICS_PREFIX + ConfigurationKeys.METRICS_EXPORTER_LIST, DEFAULT_METRICS_EXPORTER_LIST);
if (!StringUtils.isNullOrEmpty(exporterTypeNameList)) {
String[] exporterTypeNames = exporterTypeNameList.split(",");
for (String exporterTypeName : exporterTypeNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.seata.config.ConfigurationFactory;
import io.seata.core.constants.ConfigurationKeys;

import static io.seata.common.DefaultValues.DEFAULT_METRICS_REGISTRY_TYPE;

/**
* Registry Factory for load configured metrics registry
*
Expand All @@ -32,7 +34,7 @@ public class RegistryFactory {
public static Registry getInstance() {
RegistryType registryType;
String registryTypeName = ConfigurationFactory.getInstance().getConfig(
ConfigurationKeys.METRICS_PREFIX + ConfigurationKeys.METRICS_REGISTRY_TYPE, null);
ConfigurationKeys.METRICS_PREFIX + ConfigurationKeys.METRICS_REGISTRY_TYPE, DEFAULT_METRICS_REGISTRY_TYPE);
if (!StringUtils.isNullOrEmpty(registryTypeName)) {
try {
registryType = RegistryType.getType(registryTypeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.seata.metrics.exporter.Exporter;
import io.seata.metrics.registry.Registry;

import static io.seata.common.DefaultValues.DEFAULT_PROMETHEUS_PORT;
import static io.seata.core.constants.ConfigurationKeys.METRICS_EXPORTER_PROMETHEUS_PORT;

/**
Expand All @@ -46,7 +47,7 @@ public class PrometheusExporter extends Collector implements Collector.Describab

public PrometheusExporter() throws IOException {
int port = ConfigurationFactory.getInstance().getInt(
ConfigurationKeys.METRICS_PREFIX + METRICS_EXPORTER_PROMETHEUS_PORT, 9898);
ConfigurationKeys.METRICS_PREFIX + METRICS_EXPORTER_PROMETHEUS_PORT, DEFAULT_PROMETHEUS_PORT);
this.server = new HTTPServer(port, true);
this.register();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import static io.seata.common.DefaultValues.DEFAULT_METRICS_ENABLED;
import static io.seata.common.DefaultValues.DEFAULT_METRICS_EXPORTER_LIST;
import static io.seata.common.DefaultValues.DEFAULT_METRICS_REGISTRY_TYPE;
import static io.seata.common.DefaultValues.DEFAULT_PROMETHEUS_PORT;
import static io.seata.spring.boot.autoconfigure.StarterConstants.METRICS_PREFIX;

/**
Expand All @@ -26,17 +30,17 @@
@Component
@ConfigurationProperties(prefix = METRICS_PREFIX)
public class MetricsProperties {
private Boolean enabled = false;
private String registryType = "compact";
private String exporterList = "prometheus";
private Integer exporterPrometheusPort = 9898;
private boolean enabled = DEFAULT_METRICS_ENABLED;
private String registryType = DEFAULT_METRICS_REGISTRY_TYPE;
private String exporterList = DEFAULT_METRICS_EXPORTER_LIST;
private int exporterPrometheusPort = DEFAULT_PROMETHEUS_PORT;


public Boolean getEnabled() {
return enabled;
}

public MetricsProperties setEnabled(Boolean enabled) {
public MetricsProperties setEnabled(boolean enabled) {
this.enabled = enabled;
return this;
}
Expand All @@ -59,11 +63,11 @@ public MetricsProperties setExporterList(String exporterList) {
return this;
}

public Integer getExporterPrometheusPort() {
public int getExporterPrometheusPort() {
return exporterPrometheusPort;
}

public MetricsProperties setExporterPrometheusPort(Integer exporterPrometheusPort) {
public MetricsProperties setExporterPrometheusPort(int exporterPrometheusPort) {
this.exporterPrometheusPort = exporterPrometheusPort;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import static io.seata.common.DefaultValues.DEFAULT_ASYNC_COMMITTING_RETRY_PERIOD;
import static io.seata.common.DefaultValues.DEFAULT_COMMITING_RETRY_PERIOD;
import static io.seata.common.DefaultValues.DEFAULT_ROLLBACKING_RETRY_PERIOD;
import static io.seata.common.DefaultValues.DEFAULT_TIMEOUT_RETRY_PERIOD;
import static io.seata.spring.boot.autoconfigure.StarterConstants.SERVER_RECOVERY_PREFIX;

/**
Expand All @@ -27,10 +31,10 @@
@ConfigurationProperties(prefix = SERVER_RECOVERY_PREFIX)
public class ServerRecoveryProperties {

private int committingRetryPeriod = 1000;
private int asyncCommittingRetryPeriod = 1000;
private int rollbackingRetryPeriod = 1000;
private int timeoutRetryPeriod = 1000;
private int committingRetryPeriod = DEFAULT_COMMITING_RETRY_PERIOD;
private int asyncCommittingRetryPeriod = DEFAULT_ASYNC_COMMITTING_RETRY_PERIOD;
private int rollbackingRetryPeriod = DEFAULT_ROLLBACKING_RETRY_PERIOD;
private int timeoutRetryPeriod = DEFAULT_TIMEOUT_RETRY_PERIOD;

public int getCommittingRetryPeriod() {
return committingRetryPeriod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import static io.seata.common.DefaultValues.DEFAULT_UNDO_LOG_DELETE_PERIOD;
import static io.seata.core.protocol.transaction.UndoLogDeleteRequest.DEFAULT_SAVE_DAYS;
import static io.seata.spring.boot.autoconfigure.StarterConstants.SERVER_UNDO_PREFIX;

/**
Expand All @@ -26,23 +28,23 @@
@Component
@ConfigurationProperties(prefix = SERVER_UNDO_PREFIX)
public class ServerUndoProperties {
private Integer logSaveDays = 7;
private Long logDeletePeriod = 86400000L;
private int logSaveDays = DEFAULT_SAVE_DAYS;
private long logDeletePeriod = DEFAULT_UNDO_LOG_DELETE_PERIOD;

public Integer getLogSaveDays() {
public int getLogSaveDays() {
return logSaveDays;
}

public ServerUndoProperties setLogSaveDays(Integer logSaveDays) {
public ServerUndoProperties setLogSaveDays(int logSaveDays) {
this.logSaveDays = logSaveDays;
return this;
}

public Long getLogDeletePeriod() {
public long getLogDeletePeriod() {
return logDeletePeriod;
}

public ServerUndoProperties setLogDeletePeriod(Long logDeletePeriod) {
public ServerUndoProperties setLogDeletePeriod(long logDeletePeriod) {
this.logDeletePeriod = logDeletePeriod;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import static io.seata.common.DefaultValues.DEFAULT_SERVICE_SESSION_RELOAD_READ_SIZE;
import static io.seata.spring.boot.autoconfigure.StarterConstants.STORE_FILE_PREFIX;

/**
Expand All @@ -30,7 +31,7 @@ public class StoreFileProperties {
private Integer maxBranchSessionSize = 16384;
private Integer maxGlobalSessionSize = 512;
private Integer fileWriteBufferCacheSize = 16384;
private Integer sessionReloadReadSize = 100;
private Integer sessionReloadReadSize = DEFAULT_SERVICE_SESSION_RELOAD_READ_SIZE;
private String flushDiskMode = "async";

public String getDir() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static io.seata.common.ConfigurationKeys.STORE_MODE;
import static io.seata.common.ConfigurationKeys.STORE_SESSION_MODE;
import static io.seata.common.Constants.OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT;
import static io.seata.common.DefaultValues.SERVER_DEFAULT_STORE_MODE;
import static io.seata.common.DefaultValues.SERVICE_OFFSET_SPRING_BOOT;
import static io.seata.core.constants.ConfigurationKeys.ENV_SEATA_PORT_KEY;
import static io.seata.core.constants.ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL;
Expand Down Expand Up @@ -64,9 +65,9 @@ public void onApplicationEvent(ApplicationEvent event) {
Configuration config = ConfigurationFactory.getInstance();
// Load by priority
System.setProperty("sessionMode",
config.getConfig(STORE_SESSION_MODE, config.getConfig(STORE_MODE, "file")));
config.getConfig(STORE_SESSION_MODE, config.getConfig(STORE_MODE, SERVER_DEFAULT_STORE_MODE)));
System.setProperty("lockMode",
config.getConfig(STORE_LOCK_MODE, config.getConfig(STORE_MODE, "file")));
config.getConfig(STORE_LOCK_MODE, config.getConfig(STORE_MODE, SERVER_DEFAULT_STORE_MODE)));

String[] args = environmentPreparedEvent.getArgs();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
import static io.seata.common.Constants.RETRY_ROLLBACKING;
import static io.seata.common.Constants.TX_TIMEOUT_CHECK;
import static io.seata.common.Constants.UNDOLOG_DELETE;
import static io.seata.common.DefaultValues.DEFAULT_ASYNC_COMMITTING_RETRY_PERIOD;
import static io.seata.common.DefaultValues.DEFAULT_COMMITING_RETRY_PERIOD;
import static io.seata.common.DefaultValues.DEFAULT_MAX_COMMIT_RETRY_TIMEOUT;
import static io.seata.common.DefaultValues.DEFAULT_MAX_ROLLBACK_RETRY_TIMEOUT;
import static io.seata.common.DefaultValues.DEFAULT_ROLLBACKING_RETRY_PERIOD;
import static io.seata.common.DefaultValues.DEFAULT_ROLLBACK_RETRY_TIMEOUT_UNLOCK_ENABLE;
import static io.seata.common.DefaultValues.DEFAULT_TIMEOUT_RETRY_PERIOD;
import static io.seata.common.DefaultValues.DEFAULT_UNDO_LOG_DELETE_PERIOD;

/**
* The type Default coordinator.
Expand All @@ -89,30 +97,31 @@ public class DefaultCoordinator extends AbstractTCInboundHandler implements Tran
* The constant COMMITTING_RETRY_PERIOD.
*/
protected static final long COMMITTING_RETRY_PERIOD = CONFIG.getLong(ConfigurationKeys.COMMITING_RETRY_PERIOD,
1000L);
DEFAULT_COMMITING_RETRY_PERIOD);

/**
* The constant ASYNC_COMMITTING_RETRY_PERIOD.
*/
protected static final long ASYNC_COMMITTING_RETRY_PERIOD = CONFIG.getLong(
ConfigurationKeys.ASYNC_COMMITING_RETRY_PERIOD, 1000L);
ConfigurationKeys.ASYNC_COMMITING_RETRY_PERIOD, DEFAULT_ASYNC_COMMITTING_RETRY_PERIOD);

/**
* The constant ROLLBACKING_RETRY_PERIOD.
*/
protected static final long ROLLBACKING_RETRY_PERIOD = CONFIG.getLong(ConfigurationKeys.ROLLBACKING_RETRY_PERIOD,
1000L);
DEFAULT_ROLLBACKING_RETRY_PERIOD);

/**
* The constant TIMEOUT_RETRY_PERIOD.
*/
protected static final long TIMEOUT_RETRY_PERIOD = CONFIG.getLong(ConfigurationKeys.TIMEOUT_RETRY_PERIOD, 1000L);
protected static final long TIMEOUT_RETRY_PERIOD = CONFIG.getLong(ConfigurationKeys.TIMEOUT_RETRY_PERIOD,
DEFAULT_TIMEOUT_RETRY_PERIOD);

/**
* The Transaction undo log delete period.
*/
protected static final long UNDO_LOG_DELETE_PERIOD = CONFIG.getLong(
ConfigurationKeys.TRANSACTION_UNDO_LOG_DELETE_PERIOD, 24 * 60 * 60 * 1000);
ConfigurationKeys.TRANSACTION_UNDO_LOG_DELETE_PERIOD, DEFAULT_UNDO_LOG_DELETE_PERIOD);

/**
* The Transaction undo log delay delete period
Expand All @@ -132,13 +141,13 @@ public class DefaultCoordinator extends AbstractTCInboundHandler implements Tran
private static final int BRANCH_ASYNC_POOL_SIZE = Runtime.getRuntime().availableProcessors();

private static final Duration MAX_COMMIT_RETRY_TIMEOUT = ConfigurationFactory.getInstance().getDuration(
ConfigurationKeys.MAX_COMMIT_RETRY_TIMEOUT, DurationUtil.DEFAULT_DURATION, 100);
ConfigurationKeys.MAX_COMMIT_RETRY_TIMEOUT, DurationUtil.DEFAULT_DURATION, DEFAULT_MAX_COMMIT_RETRY_TIMEOUT);

private static final Duration MAX_ROLLBACK_RETRY_TIMEOUT = ConfigurationFactory.getInstance().getDuration(
ConfigurationKeys.MAX_ROLLBACK_RETRY_TIMEOUT, DurationUtil.DEFAULT_DURATION, 100);
ConfigurationKeys.MAX_ROLLBACK_RETRY_TIMEOUT, DurationUtil.DEFAULT_DURATION, DEFAULT_MAX_ROLLBACK_RETRY_TIMEOUT);

private static final boolean ROLLBACK_RETRY_TIMEOUT_UNLOCK_ENABLE = ConfigurationFactory.getInstance().getBoolean(
ConfigurationKeys.ROLLBACK_RETRY_TIMEOUT_UNLOCK_ENABLE, false);
ConfigurationKeys.ROLLBACK_RETRY_TIMEOUT_UNLOCK_ENABLE, DEFAULT_ROLLBACK_RETRY_TIMEOUT_UNLOCK_ENABLE);

private final ScheduledThreadPoolExecutor retryRollbacking =
new ScheduledThreadPoolExecutor(1, new NamedThreadFactory(RETRY_ROLLBACKING, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import io.seata.metrics.registry.RegistryFactory;
import io.seata.server.event.EventBusManager;

import static io.seata.common.DefaultValues.DEFAULT_METRICS_ENABLED;

/**
* Metrics manager for init
*
Expand All @@ -47,7 +49,7 @@ public Registry getRegistry() {

public void init() {
boolean enabled = ConfigurationFactory.getInstance().getBoolean(
ConfigurationKeys.METRICS_PREFIX + ConfigurationKeys.METRICS_ENABLED, true);
ConfigurationKeys.METRICS_PREFIX + ConfigurationKeys.METRICS_ENABLED, DEFAULT_METRICS_ENABLED);
if (enabled) {
registry = RegistryFactory.getInstance();
if (registry != null) {
Expand Down
Loading

0 comments on commit a986644

Please sign in to comment.