refactor: Change the distributed ID logic
This commit is contained in:
parent
66db5207d3
commit
9f8c19cafb
@ -17,7 +17,7 @@ package com.alibaba.nacos.config.server.configuration;
|
|||||||
|
|
||||||
import com.alibaba.nacos.config.server.filter.NacosWebFilter;
|
import com.alibaba.nacos.config.server.filter.NacosWebFilter;
|
||||||
import com.alibaba.nacos.config.server.filter.TransferToLeaderFilter;
|
import com.alibaba.nacos.config.server.filter.TransferToLeaderFilter;
|
||||||
import com.alibaba.nacos.config.server.service.transaction.ConditionOnEmbedStoreType;
|
import com.alibaba.nacos.config.server.service.transaction.ConditionOnDistributedStore;
|
||||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Conditional;
|
import org.springframework.context.annotation.Conditional;
|
||||||
@ -42,7 +42,7 @@ public class NacosConfigConfiguration {
|
|||||||
return registration;
|
return registration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Conditional(ConditionOnEmbedStoreType.class)
|
@Conditional(ConditionOnDistributedStore.class)
|
||||||
@Bean
|
@Bean
|
||||||
public FilterRegistrationBean transferToLeaderRegistration() {
|
public FilterRegistrationBean transferToLeaderRegistration() {
|
||||||
FilterRegistrationBean<TransferToLeaderFilter> registration = new FilterRegistrationBean<>();
|
FilterRegistrationBean<TransferToLeaderFilter> registration = new FilterRegistrationBean<>();
|
||||||
@ -58,7 +58,7 @@ public class NacosConfigConfiguration {
|
|||||||
return new NacosWebFilter();
|
return new NacosWebFilter();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Conditional(ConditionOnEmbedStoreType.class)
|
@Conditional(ConditionOnDistributedStore.class)
|
||||||
@Bean
|
@Bean
|
||||||
public TransferToLeaderFilter transferToLeader() {
|
public TransferToLeaderFilter transferToLeader() {
|
||||||
return new TransferToLeaderFilter();
|
return new TransferToLeaderFilter();
|
||||||
|
@ -54,7 +54,7 @@ import java.sql.Timestamp;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
import static com.alibaba.nacos.core.utils.ApplicationUtils.LOCAL_IP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 软负载客户端发布数据专用控制器
|
* 软负载客户端发布数据专用控制器
|
||||||
|
@ -23,7 +23,6 @@ import com.alibaba.nacos.config.server.service.LocalDataSourceServiceImpl;
|
|||||||
import com.alibaba.nacos.config.server.service.PersistService;
|
import com.alibaba.nacos.config.server.service.PersistService;
|
||||||
import com.alibaba.nacos.config.server.service.dump.DumpService;
|
import com.alibaba.nacos.config.server.service.dump.DumpService;
|
||||||
import com.alibaba.nacos.config.server.utils.LogUtil;
|
import com.alibaba.nacos.config.server.utils.LogUtil;
|
||||||
import com.alibaba.nacos.config.server.utils.PaginationHelper;
|
|
||||||
import com.alibaba.nacos.config.server.utils.PropertyUtil;
|
import com.alibaba.nacos.config.server.utils.PropertyUtil;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -82,7 +81,7 @@ public class ConfigOpsController {
|
|||||||
@GetMapping(value = "/derby")
|
@GetMapping(value = "/derby")
|
||||||
public RestResult<Object> derbyOps(@RequestParam(value = "sql") String sql) {
|
public RestResult<Object> derbyOps(@RequestParam(value = "sql") String sql) {
|
||||||
String selectSign = "select";
|
String selectSign = "select";
|
||||||
if (!PropertyUtil.isUseMysql()) {
|
if (!PropertyUtil.isUseExternalDB()) {
|
||||||
LocalDataSourceServiceImpl dataSourceService = (LocalDataSourceServiceImpl) DynamicDataSource
|
LocalDataSourceServiceImpl dataSourceService = (LocalDataSourceServiceImpl) DynamicDataSource
|
||||||
.getInstance().getDataSource();
|
.getInstance().getDataSource();
|
||||||
if (StringUtils.startsWithIgnoreCase(sql, selectSign)) {
|
if (StringUtils.startsWithIgnoreCase(sql, selectSign)) {
|
||||||
|
@ -43,7 +43,6 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static com.alibaba.nacos.config.server.utils.LogUtil.pullLog;
|
import static com.alibaba.nacos.config.server.utils.LogUtil.pullLog;
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ConfigServlet inner for aop
|
* ConfigServlet inner for aop
|
||||||
@ -142,7 +141,7 @@ public class ConfigServletInner {
|
|||||||
if (isBeta) {
|
if (isBeta) {
|
||||||
md5 = cacheItem.getMd54Beta();
|
md5 = cacheItem.getMd54Beta();
|
||||||
lastModified = cacheItem.getLastModifiedTs4Beta();
|
lastModified = cacheItem.getLastModifiedTs4Beta();
|
||||||
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
configInfoBase = persistService.findConfigInfo4Beta(dataId, group, tenant);
|
configInfoBase = persistService.findConfigInfo4Beta(dataId, group, tenant);
|
||||||
} else {
|
} else {
|
||||||
file = DiskUtil.targetBetaFile(dataId, group, tenant);
|
file = DiskUtil.targetBetaFile(dataId, group, tenant);
|
||||||
@ -159,7 +158,7 @@ public class ConfigServletInner {
|
|||||||
lastModified = cacheItem.tagLastModifiedTs.get(autoTag);
|
lastModified = cacheItem.tagLastModifiedTs.get(autoTag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, autoTag);
|
configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, autoTag);
|
||||||
} else {
|
} else {
|
||||||
file = DiskUtil.targetTagFile(dataId, group, tenant, autoTag);
|
file = DiskUtil.targetTagFile(dataId, group, tenant, autoTag);
|
||||||
@ -170,7 +169,7 @@ public class ConfigServletInner {
|
|||||||
} else {
|
} else {
|
||||||
md5 = cacheItem.getMd5();
|
md5 = cacheItem.getMd5();
|
||||||
lastModified = cacheItem.getLastModifiedTs();
|
lastModified = cacheItem.getLastModifiedTs();
|
||||||
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
configInfoBase = persistService.findConfigInfo(dataId, group, tenant);
|
configInfoBase = persistService.findConfigInfo(dataId, group, tenant);
|
||||||
} else {
|
} else {
|
||||||
file = DiskUtil.targetFile(dataId, group, tenant);
|
file = DiskUtil.targetFile(dataId, group, tenant);
|
||||||
@ -202,7 +201,7 @@ public class ConfigServletInner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, tag);
|
configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, tag);
|
||||||
} else {
|
} else {
|
||||||
file = DiskUtil.targetTagFile(dataId, group, tenant, tag);
|
file = DiskUtil.targetTagFile(dataId, group, tenant, tag);
|
||||||
@ -232,14 +231,14 @@ public class ConfigServletInner {
|
|||||||
response.setHeader("Pragma", "no-cache");
|
response.setHeader("Pragma", "no-cache");
|
||||||
response.setDateHeader("Expires", 0);
|
response.setDateHeader("Expires", 0);
|
||||||
response.setHeader("Cache-Control", "no-cache,no-store");
|
response.setHeader("Cache-Control", "no-cache,no-store");
|
||||||
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
response.setDateHeader("Last-Modified", lastModified);
|
response.setDateHeader("Last-Modified", lastModified);
|
||||||
} else {
|
} else {
|
||||||
fis = new FileInputStream(file);
|
fis = new FileInputStream(file);
|
||||||
response.setDateHeader("Last-Modified", file.lastModified());
|
response.setDateHeader("Last-Modified", file.lastModified());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
out = response.getWriter();
|
out = response.getWriter();
|
||||||
out.print(configInfoBase.getContent());
|
out.print(configInfoBase.getContent());
|
||||||
out.flush();
|
out.flush();
|
||||||
|
@ -26,7 +26,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
import static com.alibaba.nacos.core.utils.ApplicationUtils.LOCAL_IP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* health service
|
* health service
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.alibaba.nacos.config.server.model.app;
|
package com.alibaba.nacos.config.server.model.app;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
import static com.alibaba.nacos.core.utils.ApplicationUtils.LOCAL_IP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* app info
|
* app info
|
||||||
|
@ -28,7 +28,6 @@ import org.springframework.dao.DataAccessException;
|
|||||||
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.support.TransactionTemplate;
|
import org.springframework.transaction.support.TransactionTemplate;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
@ -43,7 +42,6 @@ import java.util.regex.Pattern;
|
|||||||
import static com.alibaba.nacos.config.server.service.RowMapperManager.CONFIG_INFO4BETA_ROW_MAPPER;
|
import static com.alibaba.nacos.config.server.service.RowMapperManager.CONFIG_INFO4BETA_ROW_MAPPER;
|
||||||
import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog;
|
import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog;
|
||||||
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base data source
|
* Base data source
|
||||||
@ -125,7 +123,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
|
|||||||
* 事务的超时时间需要与普通操作区分开
|
* 事务的超时时间需要与普通操作区分开
|
||||||
*/
|
*/
|
||||||
tjt.setTimeout(TRANSACTION_QUERY_TIMEOUT);
|
tjt.setTimeout(TRANSACTION_QUERY_TIMEOUT);
|
||||||
if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isUseExternalDB()) {
|
||||||
try {
|
try {
|
||||||
reload();
|
reload();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -33,7 +33,6 @@ import java.util.*;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
|
||||||
import static com.alibaba.nacos.config.server.utils.LogUtil.*;
|
import static com.alibaba.nacos.config.server.utils.LogUtil.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,7 +76,7 @@ public class ConfigService {
|
|||||||
"[dump-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
|
"[dump-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
|
||||||
+ "lastModifiedNew={}",
|
+ "lastModifiedNew={}",
|
||||||
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
|
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
|
||||||
} else if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
|
} else if (PropertyUtil.isUseExternalDB()) {
|
||||||
DiskUtil.saveToDisk(dataId, group, tenant, content);
|
DiskUtil.saveToDisk(dataId, group, tenant, content);
|
||||||
}
|
}
|
||||||
updateMd5(groupKey, md5, lastModifiedTs);
|
updateMd5(groupKey, md5, lastModifiedTs);
|
||||||
@ -122,7 +121,7 @@ public class ConfigService {
|
|||||||
"[dump-beta-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
|
"[dump-beta-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
|
||||||
+ "lastModifiedNew={}",
|
+ "lastModifiedNew={}",
|
||||||
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
|
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
|
||||||
} else if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
|
} else if (PropertyUtil.isUseExternalDB()) {
|
||||||
DiskUtil.saveBetaToDisk(dataId, group, tenant, content);
|
DiskUtil.saveBetaToDisk(dataId, group, tenant, content);
|
||||||
}
|
}
|
||||||
String[] betaIpsArr = betaIps.split(",");
|
String[] betaIpsArr = betaIps.split(",");
|
||||||
@ -161,7 +160,7 @@ public class ConfigService {
|
|||||||
"[dump-tag-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
|
"[dump-tag-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
|
||||||
+ "lastModifiedNew={}",
|
+ "lastModifiedNew={}",
|
||||||
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
|
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
|
||||||
} else if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
|
} else if (PropertyUtil.isUseExternalDB()) {
|
||||||
DiskUtil.saveTagToDisk(dataId, group, tenant, tag, content);
|
DiskUtil.saveTagToDisk(dataId, group, tenant, tag, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,7 +192,7 @@ public class ConfigService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE);
|
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE);
|
||||||
if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isUseExternalDB()) {
|
||||||
String loacalMd5 = DiskUtil.getLocalConfigMd5(dataId, group, tenant);
|
String loacalMd5 = DiskUtil.getLocalConfigMd5(dataId, group, tenant);
|
||||||
if (md5.equals(loacalMd5)) {
|
if (md5.equals(loacalMd5)) {
|
||||||
dumpLog.warn(
|
dumpLog.warn(
|
||||||
@ -218,7 +217,7 @@ public class ConfigService {
|
|||||||
static public void reloadConfig() {
|
static public void reloadConfig() {
|
||||||
String aggreds = null;
|
String aggreds = null;
|
||||||
try {
|
try {
|
||||||
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
ConfigInfoBase config = persistService.findConfigInfoBase(AggrWhitelist.AGGRIDS_METADATA,
|
ConfigInfoBase config = persistService.findConfigInfoBase(AggrWhitelist.AGGRIDS_METADATA,
|
||||||
"DEFAULT_GROUP");
|
"DEFAULT_GROUP");
|
||||||
if (config != null) {
|
if (config != null) {
|
||||||
@ -237,7 +236,7 @@ public class ConfigService {
|
|||||||
|
|
||||||
String clientIpWhitelist = null;
|
String clientIpWhitelist = null;
|
||||||
try {
|
try {
|
||||||
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
ConfigInfoBase config = persistService.findConfigInfoBase(
|
ConfigInfoBase config = persistService.findConfigInfoBase(
|
||||||
ClientIpWhiteList.CLIENT_IP_WHITELIST_METADATA, "DEFAULT_GROUP");
|
ClientIpWhiteList.CLIENT_IP_WHITELIST_METADATA, "DEFAULT_GROUP");
|
||||||
if (config != null) {
|
if (config != null) {
|
||||||
@ -257,7 +256,7 @@ public class ConfigService {
|
|||||||
|
|
||||||
String switchContent = null;
|
String switchContent = null;
|
||||||
try {
|
try {
|
||||||
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
ConfigInfoBase config = persistService.findConfigInfoBase(SwitchService.SWITCH_META_DATAID,
|
ConfigInfoBase config = persistService.findConfigInfoBase(SwitchService.SWITCH_META_DATAID,
|
||||||
"DEFAULT_GROUP");
|
"DEFAULT_GROUP");
|
||||||
if (config != null) {
|
if (config != null) {
|
||||||
@ -325,7 +324,7 @@ public class ConfigService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isUseExternalDB()) {
|
||||||
DiskUtil.removeConfigInfo(dataId, group, tenant);
|
DiskUtil.removeConfigInfo(dataId, group, tenant);
|
||||||
}
|
}
|
||||||
CACHE.remove(groupKey);
|
CACHE.remove(groupKey);
|
||||||
@ -359,7 +358,7 @@ public class ConfigService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isUseExternalDB()) {
|
||||||
DiskUtil.removeConfigInfo4Beta(dataId, group, tenant);
|
DiskUtil.removeConfigInfo4Beta(dataId, group, tenant);
|
||||||
}
|
}
|
||||||
EventDispatcher.fireEvent(new LocalDataChangeEvent(groupKey, true, CACHE.get(groupKey).getIps4Beta()));
|
EventDispatcher.fireEvent(new LocalDataChangeEvent(groupKey, true, CACHE.get(groupKey).getIps4Beta()));
|
||||||
@ -394,7 +393,7 @@ public class ConfigService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isUseExternalDB()) {
|
||||||
DiskUtil.removeConfigInfo4Tag(dataId, group, tenant, tag);
|
DiskUtil.removeConfigInfo4Tag(dataId, group, tenant, tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ import com.alibaba.nacos.common.utils.IoUtils;
|
|||||||
import com.alibaba.nacos.common.utils.Md5Utils;
|
import com.alibaba.nacos.common.utils.Md5Utils;
|
||||||
import com.alibaba.nacos.config.server.constant.Constants;
|
import com.alibaba.nacos.config.server.constant.Constants;
|
||||||
import com.alibaba.nacos.config.server.utils.LogUtil;
|
import com.alibaba.nacos.config.server.utils.LogUtil;
|
||||||
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -29,8 +30,6 @@ import java.io.FileInputStream;
|
|||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 磁盘操作工具类。
|
* 磁盘操作工具类。
|
||||||
* <p>
|
* <p>
|
||||||
@ -112,9 +111,9 @@ public class DiskUtil {
|
|||||||
static public File targetFile(String dataId, String group, String tenant) {
|
static public File targetFile(String dataId, String group, String tenant) {
|
||||||
File file = null;
|
File file = null;
|
||||||
if (StringUtils.isBlank(tenant)) {
|
if (StringUtils.isBlank(tenant)) {
|
||||||
file = new File(NACOS_HOME, BASE_DIR);
|
file = new File(ApplicationUtils.getNacosHome(), BASE_DIR);
|
||||||
} else {
|
} else {
|
||||||
file = new File(NACOS_HOME, TENANT_BASE_DIR);
|
file = new File(ApplicationUtils.getNacosHome(), TENANT_BASE_DIR);
|
||||||
file = new File(file, tenant);
|
file = new File(file, tenant);
|
||||||
}
|
}
|
||||||
file = new File(file, group);
|
file = new File(file, group);
|
||||||
@ -128,9 +127,9 @@ public class DiskUtil {
|
|||||||
static public File targetBetaFile(String dataId, String group, String tenant) {
|
static public File targetBetaFile(String dataId, String group, String tenant) {
|
||||||
File file = null;
|
File file = null;
|
||||||
if (StringUtils.isBlank(tenant)) {
|
if (StringUtils.isBlank(tenant)) {
|
||||||
file = new File(NACOS_HOME, BETA_DIR);
|
file = new File(ApplicationUtils.getNacosHome(), BETA_DIR);
|
||||||
} else {
|
} else {
|
||||||
file = new File(NACOS_HOME, TENANT_BETA_DIR);
|
file = new File(ApplicationUtils.getNacosHome(), TENANT_BETA_DIR);
|
||||||
file = new File(file, tenant);
|
file = new File(file, tenant);
|
||||||
}
|
}
|
||||||
file = new File(file, group);
|
file = new File(file, group);
|
||||||
@ -144,9 +143,9 @@ public class DiskUtil {
|
|||||||
static public File targetTagFile(String dataId, String group, String tenant, String tag) {
|
static public File targetTagFile(String dataId, String group, String tenant, String tag) {
|
||||||
File file = null;
|
File file = null;
|
||||||
if (StringUtils.isBlank(tenant)) {
|
if (StringUtils.isBlank(tenant)) {
|
||||||
file = new File(NACOS_HOME, TAG_DIR);
|
file = new File(ApplicationUtils.getNacosHome(), TAG_DIR);
|
||||||
} else {
|
} else {
|
||||||
file = new File(NACOS_HOME, TENANT_TAG_DIR);
|
file = new File(ApplicationUtils.getNacosHome(), TENANT_TAG_DIR);
|
||||||
file = new File(file, tenant);
|
file = new File(file, tenant);
|
||||||
}
|
}
|
||||||
file = new File(file, group);
|
file = new File(file, group);
|
||||||
@ -176,7 +175,7 @@ public class DiskUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static public File heartBeatFile() {
|
static public File heartBeatFile() {
|
||||||
return new File(NACOS_HOME, "status" + File.separator + "heartBeat.txt");
|
return new File(ApplicationUtils.getNacosHome(), "status" + File.separator + "heartBeat.txt");
|
||||||
}
|
}
|
||||||
|
|
||||||
static public String relativePath(String dataId, String group) {
|
static public String relativePath(String dataId, String group) {
|
||||||
@ -184,13 +183,13 @@ public class DiskUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static public void clearAll() {
|
static public void clearAll() {
|
||||||
File file = new File(NACOS_HOME, BASE_DIR);
|
File file = new File(ApplicationUtils.getNacosHome(), BASE_DIR);
|
||||||
if (FileUtils.deleteQuietly(file)) {
|
if (FileUtils.deleteQuietly(file)) {
|
||||||
LogUtil.defaultLog.info("clear all config-info success.");
|
LogUtil.defaultLog.info("clear all config-info success.");
|
||||||
} else {
|
} else {
|
||||||
LogUtil.defaultLog.warn("clear all config-info failed.");
|
LogUtil.defaultLog.warn("clear all config-info failed.");
|
||||||
}
|
}
|
||||||
File fileTenant = new File(NACOS_HOME, TENANT_BASE_DIR);
|
File fileTenant = new File(ApplicationUtils.getNacosHome(), TENANT_BASE_DIR);
|
||||||
if (FileUtils.deleteQuietly(fileTenant)) {
|
if (FileUtils.deleteQuietly(fileTenant)) {
|
||||||
LogUtil.defaultLog.info("clear all config-info-tenant success.");
|
LogUtil.defaultLog.info("clear all config-info-tenant success.");
|
||||||
} else {
|
} else {
|
||||||
@ -199,13 +198,13 @@ public class DiskUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static public void clearAllBeta() {
|
static public void clearAllBeta() {
|
||||||
File file = new File(NACOS_HOME, BETA_DIR);
|
File file = new File(ApplicationUtils.getNacosHome(), BETA_DIR);
|
||||||
if (FileUtils.deleteQuietly(file)) {
|
if (FileUtils.deleteQuietly(file)) {
|
||||||
LogUtil.defaultLog.info("clear all config-info-beta success.");
|
LogUtil.defaultLog.info("clear all config-info-beta success.");
|
||||||
} else {
|
} else {
|
||||||
LogUtil.defaultLog.warn("clear all config-info-beta failed.");
|
LogUtil.defaultLog.warn("clear all config-info-beta failed.");
|
||||||
}
|
}
|
||||||
File fileTenant = new File(NACOS_HOME, TENANT_BETA_DIR);
|
File fileTenant = new File(ApplicationUtils.getNacosHome(), TENANT_BETA_DIR);
|
||||||
if (FileUtils.deleteQuietly(fileTenant)) {
|
if (FileUtils.deleteQuietly(fileTenant)) {
|
||||||
LogUtil.defaultLog.info("clear all config-info-beta-tenant success.");
|
LogUtil.defaultLog.info("clear all config-info-beta-tenant success.");
|
||||||
} else {
|
} else {
|
||||||
@ -214,13 +213,13 @@ public class DiskUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static public void clearAllTag() {
|
static public void clearAllTag() {
|
||||||
File file = new File(NACOS_HOME, TAG_DIR);
|
File file = new File(ApplicationUtils.getNacosHome(), TAG_DIR);
|
||||||
if (FileUtils.deleteQuietly(file)) {
|
if (FileUtils.deleteQuietly(file)) {
|
||||||
LogUtil.defaultLog.info("clear all config-info-tag success.");
|
LogUtil.defaultLog.info("clear all config-info-tag success.");
|
||||||
} else {
|
} else {
|
||||||
LogUtil.defaultLog.warn("clear all config-info-tag failed.");
|
LogUtil.defaultLog.warn("clear all config-info-tag failed.");
|
||||||
}
|
}
|
||||||
File fileTenant = new File(NACOS_HOME, TENANT_TAG_DIR);
|
File fileTenant = new File(ApplicationUtils.getNacosHome(), TENANT_TAG_DIR);
|
||||||
if (FileUtils.deleteQuietly(fileTenant)) {
|
if (FileUtils.deleteQuietly(fileTenant)) {
|
||||||
LogUtil.defaultLog.info("clear all config-info-tag-tenant success.");
|
LogUtil.defaultLog.info("clear all config-info-tag-tenant success.");
|
||||||
} else {
|
} else {
|
||||||
|
@ -16,9 +16,6 @@
|
|||||||
package com.alibaba.nacos.config.server.service;
|
package com.alibaba.nacos.config.server.service;
|
||||||
|
|
||||||
import com.alibaba.nacos.config.server.utils.PropertyUtil;
|
import com.alibaba.nacos.config.server.utils.PropertyUtil;
|
||||||
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* datasource adapter
|
* datasource adapter
|
||||||
@ -38,7 +35,11 @@ public class DynamicDataSource {
|
|||||||
|
|
||||||
public synchronized DataSourceService getDataSource() {
|
public synchronized DataSourceService getDataSource() {
|
||||||
try {
|
try {
|
||||||
if (useMemoryDB()) {
|
|
||||||
|
// Embedded storage is used by default in stand-alone mode
|
||||||
|
// In cluster mode, external databases are used by default
|
||||||
|
|
||||||
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
if (localDataSourceService == null) {
|
if (localDataSourceService == null) {
|
||||||
localDataSourceService = new LocalDataSourceServiceImpl();
|
localDataSourceService = new LocalDataSourceServiceImpl();
|
||||||
localDataSourceService.init();
|
localDataSourceService.init();
|
||||||
@ -57,18 +58,4 @@ public class DynamicDataSource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断顺序:
|
|
||||||
* 1、单机模式:mysql
|
|
||||||
* 2、单机模式:derby
|
|
||||||
* 3、集群模式:mysql
|
|
||||||
* 4、集群模式:derby-cluster
|
|
||||||
*
|
|
||||||
* @return Whether to use derby storage
|
|
||||||
*/
|
|
||||||
private boolean useMemoryDB() {
|
|
||||||
return (ApplicationUtils.getStandaloneMode() && !PropertyUtil.isUseMysql())
|
|
||||||
|| PropertyUtil.isEmbeddedDistributedStorage();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -47,11 +47,11 @@ import org.springframework.transaction.support.TransactionTemplate;
|
|||||||
*/
|
*/
|
||||||
public class LocalDataSourceServiceImpl implements DataSourceService {
|
public class LocalDataSourceServiceImpl implements DataSourceService {
|
||||||
|
|
||||||
private final String JDBC_DRIVER_NAME = "org.apache.derby.jdbc.EmbeddedDriver";
|
private final String jdbcDriverName = "org.apache.derby.jdbc.EmbeddedDriver";
|
||||||
private final String USER_NAME = "nacos";
|
private final String userName = "nacos";
|
||||||
private final String PASSWORD = "nacos";
|
private final String password = "nacos";
|
||||||
private final String DERBY_BASE_DIR = "data" + File.separator + "derby-data";
|
private final String derbyBaseDir = "data" + File.separator + "derby-data";
|
||||||
private final String DERBY_SHUTDOWN_ERR_MSG = "Derby system shutdown.";
|
private final String derbyShutdownErrMsg = "Derby system shutdown.";
|
||||||
|
|
||||||
private volatile JdbcTemplate jt;
|
private volatile JdbcTemplate jt;
|
||||||
private volatile TransactionTemplate tjt;
|
private volatile TransactionTemplate tjt;
|
||||||
@ -62,10 +62,11 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
|
|||||||
@PostConstruct
|
@PostConstruct
|
||||||
@Override
|
@Override
|
||||||
public synchronized void init() throws Exception {
|
public synchronized void init() throws Exception {
|
||||||
if (!PropertyUtil.isUseMysql()) {
|
if (!PropertyUtil.isUseExternalDB()) {
|
||||||
if (!initialize) {
|
if (!initialize) {
|
||||||
LogUtil.defaultLog.info("use local db service for init");
|
LogUtil.defaultLog.info("use local db service for init");
|
||||||
final String jdbcUrl = "jdbc:derby:" + Paths.get(ApplicationUtils.getNacosHome(), DERBY_BASE_DIR).toString() + ";create=true";
|
final String jdbcUrl = "jdbc:derby:" + Paths.get(ApplicationUtils.getNacosHome(),
|
||||||
|
derbyBaseDir).toString() + ";create=true";
|
||||||
initialize(jdbcUrl);
|
initialize(jdbcUrl);
|
||||||
initialize = true;
|
initialize = true;
|
||||||
}
|
}
|
||||||
@ -90,7 +91,8 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
|
|||||||
|
|
||||||
public void cleanAndReopenDerby() throws Exception {
|
public void cleanAndReopenDerby() throws Exception {
|
||||||
doDerbyClean();
|
doDerbyClean();
|
||||||
final String jdbcUrl = "jdbc:derby:" + Paths.get(ApplicationUtils.getNacosHome(), DERBY_BASE_DIR).toString() + ";create=true";
|
final String jdbcUrl = "jdbc:derby:" + Paths.get(ApplicationUtils.getNacosHome(),
|
||||||
|
derbyBaseDir).toString() + ";create=true";
|
||||||
initialize(jdbcUrl);
|
initialize(jdbcUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,19 +110,19 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
|
|||||||
|
|
||||||
// An error is thrown when the Derby shutdown is executed, which should be ignored
|
// An error is thrown when the Derby shutdown is executed, which should be ignored
|
||||||
|
|
||||||
if (!StringUtils.contains(e.getMessage().toLowerCase(), DERBY_SHUTDOWN_ERR_MSG.toLowerCase())) {
|
if (!StringUtils.contains(e.getMessage().toLowerCase(), derbyShutdownErrMsg.toLowerCase())) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DiskUtils.deleteDirectory(Paths.get(ApplicationUtils.getNacosHome(), DERBY_BASE_DIR).toString());
|
DiskUtils.deleteDirectory(Paths.get(ApplicationUtils.getNacosHome(), derbyBaseDir).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void initialize(String jdbcUrl) {
|
private synchronized void initialize(String jdbcUrl) {
|
||||||
HikariDataSource ds = new HikariDataSource();
|
HikariDataSource ds = new HikariDataSource();
|
||||||
ds.setDriverClassName(JDBC_DRIVER_NAME);
|
ds.setDriverClassName(jdbcDriverName);
|
||||||
ds.setJdbcUrl(jdbcUrl);
|
ds.setJdbcUrl(jdbcUrl);
|
||||||
ds.setUsername(USER_NAME);
|
ds.setUsername(userName);
|
||||||
ds.setPassword(PASSWORD);
|
ds.setPassword(password);
|
||||||
ds.setMaximumPoolSize(80);
|
ds.setMaximumPoolSize(80);
|
||||||
ds.setConnectionTimeout(10000L);
|
ds.setConnectionTimeout(10000L);
|
||||||
DataSourceTransactionManager tm = new DataSourceTransactionManager();
|
DataSourceTransactionManager tm = new DataSourceTransactionManager();
|
||||||
@ -157,7 +159,8 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCurrentDBUrl() {
|
public String getCurrentDBUrl() {
|
||||||
return "jdbc:derby:" + ApplicationUtils.getNacosHome() + File.separator + DERBY_BASE_DIR + ";create=true";
|
return "jdbc:derby:" + ApplicationUtils.getNacosHome() + File.separator + derbyBaseDir
|
||||||
|
+ ";create=true";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -22,7 +22,6 @@ import com.alibaba.nacos.config.server.service.DynamicDataSource;
|
|||||||
import com.alibaba.nacos.config.server.utils.PropertyUtil;
|
import com.alibaba.nacos.config.server.utils.PropertyUtil;
|
||||||
import com.alibaba.nacos.config.server.utils.TimeUtils;
|
import com.alibaba.nacos.config.server.utils.TimeUtils;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.jdbc.core.PreparedStatementCreator;
|
import org.springframework.jdbc.core.PreparedStatementCreator;
|
||||||
@ -34,7 +33,6 @@ import javax.annotation.PostConstruct;
|
|||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
|
||||||
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -276,7 +274,7 @@ public class GroupCapacityPersistService {
|
|||||||
public List<GroupCapacity> getCapacityList4CorrectUsage(long lastId, int pageSize) {
|
public List<GroupCapacity> getCapacityList4CorrectUsage(long lastId, int pageSize) {
|
||||||
String sql = "SELECT id, group_id FROM group_capacity WHERE id>? LIMIT ?";
|
String sql = "SELECT id, group_id FROM group_capacity WHERE id>? LIMIT ?";
|
||||||
|
|
||||||
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
sql = "SELECT id, group_id FROM group_capacity WHERE id>? OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY";
|
sql = "SELECT id, group_id FROM group_capacity WHERE id>? OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY";
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -21,7 +21,6 @@ import com.alibaba.nacos.config.server.service.DynamicDataSource;
|
|||||||
import com.alibaba.nacos.config.server.utils.PropertyUtil;
|
import com.alibaba.nacos.config.server.utils.PropertyUtil;
|
||||||
import com.alibaba.nacos.config.server.utils.TimeUtils;
|
import com.alibaba.nacos.config.server.utils.TimeUtils;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.jdbc.core.PreparedStatementCreator;
|
import org.springframework.jdbc.core.PreparedStatementCreator;
|
||||||
@ -33,7 +32,6 @@ import javax.annotation.PostConstruct;
|
|||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
|
||||||
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -226,7 +224,7 @@ public class TenantCapacityPersistService {
|
|||||||
public List<TenantCapacity> getCapacityList4CorrectUsage(long lastId, int pageSize) {
|
public List<TenantCapacity> getCapacityList4CorrectUsage(long lastId, int pageSize) {
|
||||||
String sql = "SELECT id, tenant_id FROM tenant_capacity WHERE id>? LIMIT ?";
|
String sql = "SELECT id, tenant_id FROM tenant_capacity WHERE id>? LIMIT ?";
|
||||||
|
|
||||||
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
sql = "SELECT id, tenant_id FROM tenant_capacity WHERE id>? OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY";
|
sql = "SELECT id, tenant_id FROM tenant_capacity WHERE id>? OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,8 +54,7 @@ import java.util.Random;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
import static com.alibaba.nacos.core.utils.ApplicationUtils.LOCAL_IP;
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
|
||||||
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -81,7 +80,7 @@ public class DumpService {
|
|||||||
// If using embedded distributed storage, you need to wait for the
|
// If using embedded distributed storage, you need to wait for the
|
||||||
// underlying master to complete the selection
|
// underlying master to complete the selection
|
||||||
|
|
||||||
if (PropertyUtil.isEmbeddedDistributedStorage()) {
|
if (PropertyUtil.isEmbeddedStorage()) {
|
||||||
|
|
||||||
LogUtil.dumpLog.info("With embedded distributed storage, you need to wait for " +
|
LogUtil.dumpLog.info("With embedded distributed storage, you need to wait for " +
|
||||||
"the underlying master to complete before you can perform the dump operation.");
|
"the underlying master to complete before you can perform the dump operation.");
|
||||||
@ -179,7 +178,7 @@ public class DumpService {
|
|||||||
throw new RuntimeException(
|
throw new RuntimeException(
|
||||||
"Nacos Server did not start because dumpservice bean construction failure :\n" + e.getMessage());
|
"Nacos Server did not start because dumpservice bean construction failure :\n" + e.getMessage());
|
||||||
}
|
}
|
||||||
if (!STANDALONE_MODE) {
|
if (!ApplicationUtils.getStandaloneMode()) {
|
||||||
Runnable heartbeat = () -> {
|
Runnable heartbeat = () -> {
|
||||||
String heartBeatTime = TimeUtils.getCurrentTime().toString();
|
String heartBeatTime = TimeUtils.getCurrentTime().toString();
|
||||||
// write disk
|
// write disk
|
||||||
@ -417,7 +416,7 @@ public class DumpService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean canExecute() {
|
private boolean canExecute() {
|
||||||
if (!PropertyUtil.isEmbeddedDistributedStorage()) {
|
if (!PropertyUtil.isEmbeddedStorage()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -37,7 +37,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
import static com.alibaba.nacos.core.utils.ApplicationUtils.LOCAL_IP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据聚合服务。
|
* 数据聚合服务。
|
||||||
@ -107,7 +107,7 @@ public class MergeDatumService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean canExecute() {
|
private boolean canExecute() {
|
||||||
if (!PropertyUtil.isEmbeddedDistributedStorage()) {
|
if (!PropertyUtil.isEmbeddedStorage()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -35,7 +35,7 @@ import java.sql.Timestamp;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
import static com.alibaba.nacos.core.utils.ApplicationUtils.LOCAL_IP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merge task processor
|
* Merge task processor
|
||||||
|
@ -50,7 +50,7 @@ import java.util.List;
|
|||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
import static com.alibaba.nacos.core.utils.ApplicationUtils.LOCAL_IP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Async notify service
|
* Async notify service
|
||||||
|
@ -33,7 +33,7 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
import static com.alibaba.nacos.core.utils.ApplicationUtils.LOCAL_IP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通知服务。数据库变更后,通知所有server,包括自己,加载新数据。
|
* 通知服务。数据库变更后,通知所有server,包括自己,加载新数据。
|
||||||
|
@ -24,7 +24,7 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
import static com.alibaba.nacos.core.utils.ApplicationUtils.LOCAL_IP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Config trace
|
* Config trace
|
||||||
|
@ -16,20 +16,21 @@
|
|||||||
|
|
||||||
package com.alibaba.nacos.config.server.service.transaction;
|
package com.alibaba.nacos.config.server.service.transaction;
|
||||||
|
|
||||||
import java.util.Objects;
|
import com.alibaba.nacos.config.server.utils.PropertyUtil;
|
||||||
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
import org.springframework.context.annotation.Condition;
|
import org.springframework.context.annotation.Condition;
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 没有开启 embeddedDistributedStorage 以及 spring.datasource.platform 设置了参数
|
* when embeddedStorage==false
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
||||||
*/
|
*/
|
||||||
public class ConditionOnDefaultStoreType implements Condition {
|
public class ConditionOnDefaultStore implements Condition {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||||
return !Objects.equals(context.getEnvironment().getProperty("embeddedDistributedStorage"), "true");
|
return !(PropertyUtil.isEmbeddedStorage() && !ApplicationUtils.getStandaloneMode());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,21 +16,21 @@
|
|||||||
|
|
||||||
package com.alibaba.nacos.config.server.service.transaction;
|
package com.alibaba.nacos.config.server.service.transaction;
|
||||||
|
|
||||||
import java.util.Objects;
|
import com.alibaba.nacos.config.server.utils.PropertyUtil;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
import org.springframework.context.annotation.Condition;
|
import org.springframework.context.annotation.Condition;
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* when embeddedStorage==true and nacos.standalone=false
|
||||||
|
*
|
||||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
||||||
*/
|
*/
|
||||||
public class ConditionOnEmbedStoreType implements Condition {
|
public class ConditionOnDistributedStore implements Condition {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||||
boolean result = Objects.equals(context.getEnvironment().getProperty("embeddedDistributedStorage"), "true") &&
|
return PropertyUtil.isEmbeddedStorage() && !ApplicationUtils.getStandaloneMode();
|
||||||
StringUtils.isBlank(context.getEnvironment().getProperty("spring.datasource.platform"));
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -64,7 +64,7 @@ import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog;
|
|||||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
@Conditional(ConditionOnEmbedStoreType.class)
|
@Conditional(ConditionOnDistributedStore.class)
|
||||||
@Component
|
@Component
|
||||||
public class DistributedDatabaseOperateImpl extends LogProcessor4CP implements BaseDatabaseOperate, DatabaseOperate {
|
public class DistributedDatabaseOperateImpl extends LogProcessor4CP implements BaseDatabaseOperate, DatabaseOperate {
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog;
|
|||||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
||||||
*/
|
*/
|
||||||
@Primary
|
@Primary
|
||||||
@Conditional(ConditionOnDefaultStoreType.class)
|
@Conditional(ConditionOnDefaultStore.class)
|
||||||
@Component
|
@Component
|
||||||
public class LocalDatabaseOperateImpl implements BaseDatabaseOperate, DatabaseOperate {
|
public class LocalDatabaseOperateImpl implements BaseDatabaseOperate, DatabaseOperate {
|
||||||
|
|
||||||
|
@ -215,7 +215,7 @@ public class PaginationHelper<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isDerby() {
|
private boolean isDerby() {
|
||||||
return (ApplicationUtils.getStandaloneMode() && !PropertyUtil.isUseMysql()) ||
|
return (ApplicationUtils.getStandaloneMode() && !PropertyUtil.isUseExternalDB()) ||
|
||||||
PropertyUtil.isEmbeddedDistributedStorage();
|
PropertyUtil.isEmbeddedStorage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,6 @@ import com.alibaba.nacos.core.utils.ApplicationUtils;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.springframework.context.ApplicationContextInitializer;
|
import org.springframework.context.ApplicationContextInitializer;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* properties utils
|
* properties utils
|
||||||
@ -80,13 +78,11 @@ public class PropertyUtil implements ApplicationContextInitializer<ConfigurableA
|
|||||||
/**
|
/**
|
||||||
* 单机模式使用db
|
* 单机模式使用db
|
||||||
*/
|
*/
|
||||||
private static boolean useMysql = false;
|
private static boolean useExternalDB = false;
|
||||||
/**
|
/**
|
||||||
* 内嵌分布式存储
|
* 内嵌存储 value = ${nacos.standalone}
|
||||||
*/
|
*/
|
||||||
private static boolean embeddedDistributedStorage = false;
|
private static boolean embeddedStorage = ApplicationUtils.getStandaloneMode();
|
||||||
|
|
||||||
private Environment env;
|
|
||||||
|
|
||||||
public static int getNotifyConnectTimeout() {
|
public static int getNotifyConnectTimeout() {
|
||||||
return notifyConnectTimeout;
|
return notifyConnectTimeout;
|
||||||
@ -212,37 +208,37 @@ public class PropertyUtil implements ApplicationContextInitializer<ConfigurableA
|
|||||||
return ApplicationUtils.getStandaloneMode();
|
return ApplicationUtils.getStandaloneMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isUseMysql() {
|
public static boolean isUseExternalDB() {
|
||||||
return useMysql;
|
return useExternalDB;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setUseMysql(boolean useMysql) {
|
public static void setUseExternalDB(boolean useExternalDB) {
|
||||||
PropertyUtil.useMysql = useMysql;
|
PropertyUtil.useExternalDB = useExternalDB;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isEmbeddedDistributedStorage() {
|
public static boolean isEmbeddedStorage() {
|
||||||
return embeddedDistributedStorage;
|
return embeddedStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setEmbeddedDistributedStorage(boolean embeddedDistributedStorage) {
|
public static void setEmbeddedStorage(boolean embeddedStorage) {
|
||||||
PropertyUtil.embeddedDistributedStorage = embeddedDistributedStorage && !isUseMysql();
|
PropertyUtil.embeddedStorage = embeddedStorage && !isUseExternalDB();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isEnableDistributedID() {
|
public static boolean isEnableDistributedID() {
|
||||||
return !ApplicationUtils.getStandaloneMode() && isEmbeddedDistributedStorage();
|
return !ApplicationUtils.getStandaloneMode() && isEmbeddedStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadSetting() {
|
private void loadSetting() {
|
||||||
try {
|
try {
|
||||||
setNotifyConnectTimeout(Integer.parseInt(env.getProperty("notifyConnectTimeout", "100")));
|
setNotifyConnectTimeout(Integer.parseInt(ApplicationUtils.getProperty("notifyConnectTimeout", "100")));
|
||||||
logger.info("notifyConnectTimeout:{}", notifyConnectTimeout);
|
logger.info("notifyConnectTimeout:{}", notifyConnectTimeout);
|
||||||
setNotifySocketTimeout(Integer.parseInt(env.getProperty("notifySocketTimeout", "200")));
|
setNotifySocketTimeout(Integer.parseInt(ApplicationUtils.getProperty("notifySocketTimeout", "200")));
|
||||||
logger.info("notifySocketTimeout:{}", notifySocketTimeout);
|
logger.info("notifySocketTimeout:{}", notifySocketTimeout);
|
||||||
setHealthCheck(Boolean.parseBoolean(env.getProperty("isHealthCheck", "true")));
|
setHealthCheck(Boolean.parseBoolean(ApplicationUtils.getProperty("isHealthCheck", "true")));
|
||||||
logger.info("isHealthCheck:{}", isHealthCheck);
|
logger.info("isHealthCheck:{}", isHealthCheck);
|
||||||
setMaxHealthCheckFailCount(Integer.parseInt(env.getProperty("maxHealthCheckFailCount", "12")));
|
setMaxHealthCheckFailCount(Integer.parseInt(ApplicationUtils.getProperty("maxHealthCheckFailCount", "12")));
|
||||||
logger.info("maxHealthCheckFailCount:{}", maxHealthCheckFailCount);
|
logger.info("maxHealthCheckFailCount:{}", maxHealthCheckFailCount);
|
||||||
setMaxContent(Integer.parseInt(env.getProperty("maxContent", String.valueOf(maxContent))));
|
setMaxContent(Integer.parseInt(ApplicationUtils.getProperty("maxContent", String.valueOf(maxContent))));
|
||||||
logger.info("maxContent:{}", maxContent);
|
logger.info("maxContent:{}", maxContent);
|
||||||
// 容量管理
|
// 容量管理
|
||||||
setManageCapacity(getBoolean("isManageCapacity", isManageCapacity));
|
setManageCapacity(getBoolean("isManageCapacity", isManageCapacity));
|
||||||
@ -255,8 +251,8 @@ public class PropertyUtil implements ApplicationContextInitializer<ConfigurableA
|
|||||||
setDefaultMaxAggrSize(getInt("defaultMaxAggrSize", defaultMaxAggrSize));
|
setDefaultMaxAggrSize(getInt("defaultMaxAggrSize", defaultMaxAggrSize));
|
||||||
setCorrectUsageDelay(getInt("correctUsageDelay", correctUsageDelay));
|
setCorrectUsageDelay(getInt("correctUsageDelay", correctUsageDelay));
|
||||||
setInitialExpansionPercent(getInt("initialExpansionPercent", initialExpansionPercent));
|
setInitialExpansionPercent(getInt("initialExpansionPercent", initialExpansionPercent));
|
||||||
setUseMysql(getString("spring.datasource.platform", "").equals("mysql"));
|
setUseExternalDB(getString("spring.datasource.platform", "").equals("mysql"));
|
||||||
setEmbeddedDistributedStorage(getBoolean("embeddedDistributedStorage", embeddedDistributedStorage));
|
setEmbeddedStorage(getBoolean("embeddedStorage", embeddedStorage));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("read application.properties failed", e);
|
logger.error("read application.properties failed", e);
|
||||||
}
|
}
|
||||||
@ -271,7 +267,7 @@ public class PropertyUtil implements ApplicationContextInitializer<ConfigurableA
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getString(String key, String defaultValue) {
|
private String getString(String key, String defaultValue) {
|
||||||
String value = env.getProperty(key);
|
String value = getProperty(key);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
@ -280,16 +276,16 @@ public class PropertyUtil implements ApplicationContextInitializer<ConfigurableA
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getProperty(String key) {
|
public String getProperty(String key) {
|
||||||
return env.getProperty(key);
|
return ApplicationUtils.getProperty(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getProperty(String key, String defaultValue) {
|
public String getProperty(String key, String defaultValue) {
|
||||||
return env.getProperty(key, defaultValue);
|
return ApplicationUtils.getProperty(key, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||||
env = configurableApplicationContext.getEnvironment();
|
ApplicationUtils.injectEnvironment(configurableApplicationContext.getEnvironment());
|
||||||
loadSetting();
|
loadSetting();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ package com.alibaba.nacos.console.controller;
|
|||||||
|
|
||||||
|
|
||||||
import com.alibaba.nacos.common.utils.VersionUtils;
|
import com.alibaba.nacos.common.utils.VersionUtils;
|
||||||
import com.alibaba.nacos.core.utils.SystemUtils;
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@ -37,10 +37,10 @@ public class ServerStateController {
|
|||||||
@GetMapping("/state")
|
@GetMapping("/state")
|
||||||
public ResponseEntity serverState() {
|
public ResponseEntity serverState() {
|
||||||
Map<String,String> serverState = new HashMap<>(3);
|
Map<String,String> serverState = new HashMap<>(3);
|
||||||
serverState.put("standalone_mode",SystemUtils.STANDALONE_MODE ?
|
serverState.put("standalone_mode", ApplicationUtils.getStandaloneMode() ?
|
||||||
SystemUtils.STANDALONE_MODE_ALONE : SystemUtils.STANDALONE_MODE_CLUSTER);
|
ApplicationUtils.STANDALONE_MODE_ALONE : ApplicationUtils.STANDALONE_MODE_CLUSTER);
|
||||||
|
|
||||||
serverState.put("function_mode", SystemUtils.FUNCTION_MODE);
|
serverState.put("function_mode", ApplicationUtils.getFunctionMode());
|
||||||
serverState.put("version", VersionUtils.VERSION);
|
serverState.put("version", VersionUtils.VERSION);
|
||||||
|
|
||||||
return ResponseEntity.ok().body(serverState);
|
return ResponseEntity.ok().body(serverState);
|
||||||
|
@ -133,8 +133,9 @@ nacos.core.auth.caching.enabled=true
|
|||||||
nacos.istio.mcp.server.enabled=false
|
nacos.istio.mcp.server.enabled=false
|
||||||
|
|
||||||
#*************** Embed Storage Related Configurations ***************#
|
#*************** Embed Storage Related Configurations ***************#
|
||||||
### Whether to open embedded distributed storage in nacos cluster mode
|
### This value is true in stand-alone mode and false in cluster mode
|
||||||
embeddedDistributedStorage=true
|
### If this value is set to true in cluster mode, nacos's distributed storage engine is turned on
|
||||||
|
embeddedStorage=true
|
||||||
|
|
||||||
#*************** Consistency Related Configurations ***************#
|
#*************** Consistency Related Configurations ***************#
|
||||||
|
|
||||||
|
@ -1,93 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.alibaba.nacos.core.distributed.id;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
|
||||||
*/
|
|
||||||
public class AcquireId implements Serializable {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = -2073195123719428170L;
|
|
||||||
|
|
||||||
private long minId;
|
|
||||||
|
|
||||||
private long maxId;
|
|
||||||
|
|
||||||
private String applicant;
|
|
||||||
|
|
||||||
public static AcquireIdBuilder builder() {
|
|
||||||
return new AcquireIdBuilder();
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getMinId() {
|
|
||||||
return minId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMinId(long minId) {
|
|
||||||
this.minId = minId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getMaxId() {
|
|
||||||
return maxId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMaxId(long maxId) {
|
|
||||||
this.maxId = maxId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getApplicant() {
|
|
||||||
return applicant;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setApplicant(String applicant) {
|
|
||||||
this.applicant = applicant;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final class AcquireIdBuilder {
|
|
||||||
private long minId;
|
|
||||||
private long maxId;
|
|
||||||
private String applicant;
|
|
||||||
|
|
||||||
private AcquireIdBuilder() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public AcquireIdBuilder minId(long minId) {
|
|
||||||
this.minId = minId;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AcquireIdBuilder maxId(long maxId) {
|
|
||||||
this.maxId = maxId;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AcquireIdBuilder applicant(String applicant) {
|
|
||||||
this.applicant = applicant;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AcquireId build() {
|
|
||||||
AcquireId acquireId = new AcquireId();
|
|
||||||
acquireId.setMinId(minId);
|
|
||||||
acquireId.setMaxId(maxId);
|
|
||||||
acquireId.setApplicant(applicant);
|
|
||||||
return acquireId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.alibaba.nacos.core.distributed.id;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
|
||||||
*/
|
|
||||||
public class AcquireIdException extends RuntimeException {
|
|
||||||
|
|
||||||
public AcquireIdException() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public AcquireIdException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public AcquireIdException(String message, Throwable cause) {
|
|
||||||
super(message, cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
public AcquireIdException(Throwable cause) {
|
|
||||||
super(cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected AcquireIdException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
|
||||||
super(message, cause, enableSuppression, writableStackTrace);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,140 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
package com.alibaba.nacos.core.distributed.id;
|
|
||||||
|
|
||||||
import com.alibaba.nacos.consistency.IdGenerator;
|
|
||||||
import com.alibaba.nacos.core.utils.GlobalExecutor;
|
|
||||||
import com.alibaba.nacos.core.utils.Loggers;
|
|
||||||
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
|
||||||
import com.alibaba.nacos.core.utils.ThreadUtils;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
|
|
||||||
*/
|
|
||||||
public class DefaultIdGenerator implements IdGenerator {
|
|
||||||
|
|
||||||
private static final double SWAP_CRITICAL_VALUE = 0.2D;
|
|
||||||
|
|
||||||
private DefaultIdStore idStore;
|
|
||||||
|
|
||||||
// tow buffer
|
|
||||||
|
|
||||||
private volatile long[] bufferOne = new long[]{-1L, -1L};
|
|
||||||
private volatile long[] bufferTwo = new long[]{-1L, -1L};
|
|
||||||
|
|
||||||
private long currentId;
|
|
||||||
private boolean bufferIndex = true;
|
|
||||||
private volatile boolean inAcquire = false;
|
|
||||||
|
|
||||||
private String resource;
|
|
||||||
|
|
||||||
public DefaultIdGenerator(String resource) {
|
|
||||||
this.resource = resource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init() {
|
|
||||||
idStore = ApplicationUtils.getBean(DefaultIdStore.class);
|
|
||||||
// The first request requires an asynchronous request
|
|
||||||
idStore.firstAcquire(resource, Integer.MAX_VALUE, this, bufferIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long currentId() {
|
|
||||||
return currentId;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO 两个 buffer 都用完的情况需要处理
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized long nextId() {
|
|
||||||
long tmp = currentId + 1;
|
|
||||||
long[] buffer = current();
|
|
||||||
if (tmp > buffer[1]) {
|
|
||||||
|
|
||||||
// The currently used buffer has been used up, and the standby buffer was not applied successfully
|
|
||||||
|
|
||||||
if (inAcquire) {
|
|
||||||
int waitCnt = 5;
|
|
||||||
for (; ; ) {
|
|
||||||
waitCnt--;
|
|
||||||
if (waitCnt < 0) {
|
|
||||||
if (inAcquire) {
|
|
||||||
throw new AcquireIdException("[" + resource + "] ID resource application failed");
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ThreadUtils.sleep(10);
|
|
||||||
Loggers.ID_GENERATOR.warn("[{}] The current ID buffer has been used up and is being applied", resource);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
swap();
|
|
||||||
tmp = current()[0];
|
|
||||||
}
|
|
||||||
if (needToAcquire(tmp, current())) {
|
|
||||||
inAcquire = true;
|
|
||||||
doAcquire();
|
|
||||||
}
|
|
||||||
currentId = tmp;
|
|
||||||
return currentId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<Object, Object> info() {
|
|
||||||
Map<Object, Object> info = new HashMap<>(8);
|
|
||||||
info.put("currentId", currentId);
|
|
||||||
info.put("bufferOneStart", current()[0]);
|
|
||||||
info.put("bufferOneEnd", current()[1]);
|
|
||||||
info.put("bufferTwoStart", another()[0]);
|
|
||||||
info.put("bufferTwoEnd", another()[1]);
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long[] current() {
|
|
||||||
return bufferIndex ? bufferOne : bufferTwo;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long[] another() {
|
|
||||||
return bufferIndex ? bufferTwo : bufferOne;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void swap() {
|
|
||||||
bufferIndex = !bufferIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean needToAcquire(long currentId, long[] bufferUse) {
|
|
||||||
return (currentId * 1.0D - bufferUse[0] + 1) / (bufferUse[1] * 1.0D - bufferUse[0] + 1) > SWAP_CRITICAL_VALUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void update(long[] newBuffer) {
|
|
||||||
if (bufferIndex) {
|
|
||||||
bufferTwo = newBuffer;
|
|
||||||
} else {
|
|
||||||
bufferOne = newBuffer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void doAcquire() {
|
|
||||||
GlobalExecutor.executeByCommon(() -> {
|
|
||||||
idStore.acquireNewIdSequence(resource, Integer.MAX_VALUE, this, bufferIndex);
|
|
||||||
inAcquire = false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,311 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
package com.alibaba.nacos.core.distributed.id;
|
|
||||||
|
|
||||||
import com.alibaba.nacos.consistency.Config;
|
|
||||||
import com.alibaba.nacos.consistency.ConsistencyProtocol;
|
|
||||||
import com.alibaba.nacos.consistency.LogFuture;
|
|
||||||
import com.alibaba.nacos.consistency.SerializeFactory;
|
|
||||||
import com.alibaba.nacos.consistency.Serializer;
|
|
||||||
import com.alibaba.nacos.consistency.cp.CPProtocol;
|
|
||||||
import com.alibaba.nacos.consistency.cp.Constants;
|
|
||||||
import com.alibaba.nacos.consistency.cp.LogProcessor4CP;
|
|
||||||
import com.alibaba.nacos.consistency.entity.GetRequest;
|
|
||||||
import com.alibaba.nacos.consistency.entity.GetResponse;
|
|
||||||
import com.alibaba.nacos.consistency.entity.Log;
|
|
||||||
import com.alibaba.nacos.consistency.snapshot.CallFinally;
|
|
||||||
import com.alibaba.nacos.consistency.snapshot.Reader;
|
|
||||||
import com.alibaba.nacos.consistency.snapshot.SnapshotOperation;
|
|
||||||
import com.alibaba.nacos.consistency.snapshot.Writer;
|
|
||||||
import com.alibaba.nacos.core.distributed.raft.utils.RaftExecutor;
|
|
||||||
import com.alibaba.nacos.core.utils.ByteUtils;
|
|
||||||
import com.alibaba.nacos.core.utils.ConvertUtils;
|
|
||||||
import com.alibaba.nacos.core.utils.DiskUtils;
|
|
||||||
import com.alibaba.nacos.core.utils.GlobalExecutor;
|
|
||||||
import com.alibaba.nacos.core.utils.Loggers;
|
|
||||||
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
|
||||||
import com.alibaba.nacos.core.utils.ThreadUtils;
|
|
||||||
import com.alibaba.nacos.core.utils.TimerContext;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.NotDirectoryException;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
import java.util.Observable;
|
|
||||||
import java.util.Observer;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.zip.CRC32;
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
|
|
||||||
import com.google.protobuf.ByteString;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.context.annotation.DependsOn;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Simple implementation of LeafID based on Raft+File
|
|
||||||
*
|
|
||||||
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("all")
|
|
||||||
@ConditionalOnProperty(value = "nacos.core.id-generator.type", havingValue = "default")
|
|
||||||
@Component
|
|
||||||
@DependsOn("serverMemberManager")
|
|
||||||
public class DefaultIdStore extends LogProcessor4CP {
|
|
||||||
|
|
||||||
private final File[] EMPTY = new File[0];
|
|
||||||
private final String SNAPSHOT_DIR = "id_generator";
|
|
||||||
private final String SNAPSHOT_ARCHIVE = "id_generator.zip";
|
|
||||||
private final String FILE_PATH = Paths.get(ApplicationUtils.getNacosHome(), "data", "id_generator").toString();
|
|
||||||
private long ACQUIRE_STEP;
|
|
||||||
private CPProtocol cpProtocol;
|
|
||||||
private Map<String, IdStoreFile> storeFileMap = new ConcurrentHashMap<>(4);
|
|
||||||
private Serializer serializer;
|
|
||||||
private List<SnapshotOperation> snapshotOperations = Collections.singletonList(new IdSnapshotOperation());
|
|
||||||
|
|
||||||
private volatile boolean hasLeader = false;
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
protected void init() throws Exception {
|
|
||||||
Loggers.ID_GENERATOR.info("The Leaf-ID start");
|
|
||||||
this.serializer = SerializeFactory.getDefault();
|
|
||||||
ACQUIRE_STEP =
|
|
||||||
ConvertUtils.toLong(ApplicationUtils.getProperty("nacos.core.id-generator.default.acquire.step"), 100);
|
|
||||||
|
|
||||||
// Delete existing data, relying on raft's snapshot and log
|
|
||||||
// playback to reply to the data is the correct behavior.
|
|
||||||
|
|
||||||
DiskUtils.deleteDirectory(FILE_PATH);
|
|
||||||
DiskUtils.forceMkdir(FILE_PATH);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void afterInject(ConsistencyProtocol<? extends Config> protocol) {
|
|
||||||
super.afterInject(protocol);
|
|
||||||
this.cpProtocol = (CPProtocol) protocol;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void firstAcquire(String resource, int maxRetryCnt, DefaultIdGenerator generator, boolean bufferIndex) {
|
|
||||||
this.cpProtocol.protocolMetaData()
|
|
||||||
.subscribe(group(), Constants.LEADER_META_DATA, (o, arg) -> {
|
|
||||||
GlobalExecutor.executeByCommon(
|
|
||||||
() -> acquireNewIdSequence(resource, maxRetryCnt,
|
|
||||||
generator, bufferIndex));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void acquireNewIdSequence(String resource, int maxRetryCnt, DefaultIdGenerator generator, boolean bufferIndex) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!cpProtocol.isLeader(group())) {
|
|
||||||
Loggers.ID_GENERATOR.warn("The current is not a Leader node and the number cannot be issued");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
Loggers.ID_GENERATOR.error("An exception occurred while applying for a new segment sequence : {}", e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
storeFileMap.computeIfAbsent(resource, s -> new IdStoreFile(resource));
|
|
||||||
for (int i = 0; i < maxRetryCnt; i++) {
|
|
||||||
// need read maxId from raft-leader
|
|
||||||
try {
|
|
||||||
long currentMaxId = Long.parseLong(protocol.getData(GetRequest.newBuilder()
|
|
||||||
.setGroup(group())
|
|
||||||
.setData(ByteString.copyFromUtf8(resource))
|
|
||||||
.build()).getData().toStringUtf8());
|
|
||||||
|
|
||||||
final long minId = currentMaxId + 1;
|
|
||||||
final long maxId = minId + ACQUIRE_STEP;
|
|
||||||
final AcquireId acquireId = AcquireId.builder()
|
|
||||||
.minId(minId)
|
|
||||||
.maxId(maxId)
|
|
||||||
.applicant(resource)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Log gLog = Log.newBuilder()
|
|
||||||
.setData(ByteString.copyFrom(serializer.serialize(acquireId)))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
LogFuture future = commitAutoSetGroup(gLog);
|
|
||||||
if (future.isOk()) {
|
|
||||||
generator.update(new long[]{minId, maxId});
|
|
||||||
Loggers.ID_GENERATOR.info("[{}] ID application successful, bufferIndex : {}, startId : {}, endId : {}",
|
|
||||||
resource, bufferIndex ? "bufferTwo" : "bufferOne", minId, maxId);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
Loggers.ID_GENERATOR.error("[{}] An error occurred while applying for ID, error : {}", resource, future.getError());
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
Loggers.ID_GENERATOR.error("[{}] An error occurred while applying for ID, error : {}", resource, e);
|
|
||||||
}
|
|
||||||
ThreadUtils.sleep(100);
|
|
||||||
}
|
|
||||||
throw new AcquireIdException("[" + resource + "] The maximum number of retries exceeded");
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("all")
|
|
||||||
@Override
|
|
||||||
public GetResponse getData(GetRequest request) {
|
|
||||||
String resources = request.getData().toStringUtf8();
|
|
||||||
IdStoreFile file = storeFileMap.get(resources);
|
|
||||||
return GetResponse.newBuilder()
|
|
||||||
.setData(ByteString.copyFromUtf8(file.getCurrentMaxId().toString()))
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("all")
|
|
||||||
@Override
|
|
||||||
public LogFuture onApply(Log log) {
|
|
||||||
final AcquireId acquireId = serializer.deserialize(log.getData().toByteArray(), AcquireId.class);
|
|
||||||
final String resource = acquireId.getApplicant();
|
|
||||||
final long minId = acquireId.getMinId();
|
|
||||||
final long maxId = acquireId.getMaxId();
|
|
||||||
storeFileMap.computeIfAbsent(resource, s -> new IdStoreFile(resource));
|
|
||||||
IdStoreFile storeFile = storeFileMap.get(resource);
|
|
||||||
if (storeFile == null) {
|
|
||||||
return LogFuture.fail(new NoSuchElementException("The resource does not exist"));
|
|
||||||
}
|
|
||||||
return LogFuture.success(storeFile.canAccept(maxId));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(Throwable throwable) {
|
|
||||||
Loggers.ID_GENERATOR.error("An error occurred while onApply for ID, error : {}", throwable);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String group() {
|
|
||||||
return "default_id_generator";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<SnapshotOperation> loadSnapshotOperate() {
|
|
||||||
return snapshotOperations;
|
|
||||||
}
|
|
||||||
|
|
||||||
void loadFromFile(File parentFile) throws IOException {
|
|
||||||
if (parentFile == null) {
|
|
||||||
throw new NullPointerException();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!parentFile.isDirectory()) {
|
|
||||||
throw new NotDirectoryException(parentFile.getPath() + " not a file directory");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (File file : Optional.ofNullable(parentFile.listFiles()).orElse(EMPTY)) {
|
|
||||||
String resourceName = file.getName();
|
|
||||||
storeFileMap.computeIfAbsent(resourceName, s -> new IdStoreFile(resourceName));
|
|
||||||
IdStoreFile storeFile = storeFileMap.get(resourceName);
|
|
||||||
storeFile.forceWrite(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class IdStoreFile {
|
|
||||||
|
|
||||||
private final File file;
|
|
||||||
|
|
||||||
public IdStoreFile(String resourceName) {
|
|
||||||
try {
|
|
||||||
file = new File(Paths.get(FILE_PATH, resourceName).toUri());
|
|
||||||
DiskUtils.touch(file);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean canAccept(long maxId) {
|
|
||||||
long currentMaxId = getCurrentMaxId();
|
|
||||||
if (maxId > currentMaxId) {
|
|
||||||
DiskUtils.writeFile(file, ByteUtils.toBytes(maxId), false);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void forceWrite(File remoteFile) throws IOException {
|
|
||||||
DiskUtils.copyFile(file, remoteFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getCurrentMaxId() {
|
|
||||||
String data = DiskUtils.readFile(file);
|
|
||||||
if (StringUtils.isBlank(data)) {
|
|
||||||
return -1L;
|
|
||||||
}
|
|
||||||
return Long.parseLong(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class IdSnapshotOperation implements SnapshotOperation {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSnapshotSave(Writer writer, CallFinally callFinally) {
|
|
||||||
RaftExecutor.doSnapshot(() -> {
|
|
||||||
|
|
||||||
boolean result = false;
|
|
||||||
Throwable throwable = null;
|
|
||||||
final String writePath = writer.getPath();
|
|
||||||
TimerContext.start("[DefaultIdStore] snapshot save job");
|
|
||||||
try {
|
|
||||||
final String parentPath = Paths.get(writePath, SNAPSHOT_DIR).toString();
|
|
||||||
DiskUtils.deleteDirThenMkdir(parentPath);
|
|
||||||
DiskUtils.copyDirectory(new File(FILE_PATH), new File(parentPath));
|
|
||||||
final String outputFile = Paths.get(writePath, SNAPSHOT_ARCHIVE).toString();
|
|
||||||
DiskUtils.compress(writePath, SNAPSHOT_DIR, outputFile, new CRC32());
|
|
||||||
DiskUtils.deleteDirectory(parentPath);
|
|
||||||
writer.addFile(SNAPSHOT_ARCHIVE);
|
|
||||||
result = true;
|
|
||||||
} catch (Exception e) {
|
|
||||||
Loggers.ID_GENERATOR.error("path : {}, snapshot execution error : {}", writePath, e);
|
|
||||||
throwable = e;
|
|
||||||
} finally {
|
|
||||||
TimerContext.end(Loggers.ID_GENERATOR);
|
|
||||||
}
|
|
||||||
|
|
||||||
callFinally.run(result, throwable);
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onSnapshotLoad(Reader reader) {
|
|
||||||
final String readerPath = reader.getPath();
|
|
||||||
final String sourceFile = Paths.get(readerPath, SNAPSHOT_ARCHIVE).toString();
|
|
||||||
TimerContext.start("[DefaultIdStore] snapshot load job");
|
|
||||||
try {
|
|
||||||
DiskUtils.decompress(sourceFile, readerPath, new CRC32());
|
|
||||||
final String loadPath = Paths.get(readerPath, SNAPSHOT_DIR).toString();
|
|
||||||
Loggers.ID_GENERATOR.info("snapshot load from : {}", loadPath);
|
|
||||||
DefaultIdStore.this.loadFromFile(new File(loadPath));
|
|
||||||
DiskUtils.deleteDirectory(loadPath);
|
|
||||||
return true;
|
|
||||||
} catch (final Throwable t) {
|
|
||||||
Loggers.ID_GENERATOR.error("Fail to load snapshot, path={}, file list={}, error : {}.", readerPath,
|
|
||||||
reader.listFiles(), t);
|
|
||||||
return false;
|
|
||||||
} finally {
|
|
||||||
TimerContext.end(Loggers.ID_GENERATOR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -16,17 +16,34 @@
|
|||||||
|
|
||||||
package com.alibaba.nacos.core.distributed.id;
|
package com.alibaba.nacos.core.distributed.id;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.consistency.Config;
|
||||||
|
import com.alibaba.nacos.consistency.ConsistencyProtocol;
|
||||||
import com.alibaba.nacos.consistency.IdGenerator;
|
import com.alibaba.nacos.consistency.IdGenerator;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Observable;
|
||||||
|
import java.util.Observer;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.consistency.LogFuture;
|
||||||
|
import com.alibaba.nacos.consistency.ProtocolMetaData;
|
||||||
|
import com.alibaba.nacos.consistency.cp.CPProtocol;
|
||||||
|
import com.alibaba.nacos.consistency.cp.Constants;
|
||||||
|
import com.alibaba.nacos.consistency.cp.LogProcessor4CP;
|
||||||
|
import com.alibaba.nacos.consistency.entity.GetRequest;
|
||||||
|
import com.alibaba.nacos.consistency.entity.GetResponse;
|
||||||
|
import com.alibaba.nacos.consistency.entity.Log;
|
||||||
|
import com.alibaba.nacos.consistency.snapshot.SnapshotOperation;
|
||||||
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
|
import com.alibaba.nacos.core.utils.Loggers;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,19 +51,15 @@ import org.springframework.stereotype.Component;
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("PMD.UndefineMagicConstantRule")
|
@SuppressWarnings("PMD.UndefineMagicConstantRule")
|
||||||
@Component
|
@Component
|
||||||
public class IdGeneratorManager {
|
public class IdGeneratorManager extends LogProcessor4CP {
|
||||||
|
|
||||||
private static final String ID_GENERATOR_TYPE = "nacos.core.id-generator.type";
|
|
||||||
|
|
||||||
private final Map<String, IdGenerator> generatorMap = new ConcurrentHashMap<>();
|
private final Map<String, IdGenerator> generatorMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private final Function<String, IdGenerator> supplier;
|
private final Function<String, IdGenerator> supplier;
|
||||||
|
private CPProtocol cpProtocol;
|
||||||
private final String idType;
|
private GetResponse emptyResponse = GetResponse.newBuilder().build();
|
||||||
|
private LogFuture emptyFuture = LogFuture.success(null);
|
||||||
|
|
||||||
public IdGeneratorManager() {
|
public IdGeneratorManager() {
|
||||||
this.idType = ApplicationUtils
|
|
||||||
.getProperty(ID_GENERATOR_TYPE, "default");
|
|
||||||
this.supplier = s -> {
|
this.supplier = s -> {
|
||||||
IdGenerator generator;
|
IdGenerator generator;
|
||||||
ServiceLoader<IdGenerator> loader = ServiceLoader.load(IdGenerator.class);
|
ServiceLoader<IdGenerator> loader = ServiceLoader.load(IdGenerator.class);
|
||||||
@ -54,11 +67,7 @@ public class IdGeneratorManager {
|
|||||||
if (iterator.hasNext()) {
|
if (iterator.hasNext()) {
|
||||||
generator = iterator.next();
|
generator = iterator.next();
|
||||||
} else {
|
} else {
|
||||||
if (Objects.equals(idType, "snakeflower")) {
|
generator = new SnakeFlowerIdGenerator();
|
||||||
generator = new SnakeFlowerIdGenerator();
|
|
||||||
} else {
|
|
||||||
generator = new DefaultIdGenerator(s);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
generator.init();
|
generator.init();
|
||||||
return generator;
|
return generator;
|
||||||
@ -88,4 +97,48 @@ public class IdGeneratorManager {
|
|||||||
"ID resource for the time being.");
|
"ID resource for the time being.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void afterInject(ConsistencyProtocol<? extends Config> protocol) {
|
||||||
|
super.afterInject(protocol);
|
||||||
|
this.cpProtocol = (CPProtocol) protocol;
|
||||||
|
this.cpProtocol.protocolMetaData()
|
||||||
|
.subscribe(group(), Constants.TERM_META_DATA, new Observer() {
|
||||||
|
@Override
|
||||||
|
public void update(Observable o, Object arg) {
|
||||||
|
long term;
|
||||||
|
if (arg == null) {
|
||||||
|
term = 0l;
|
||||||
|
} else {
|
||||||
|
term = Long.parseLong(String.valueOf(arg));
|
||||||
|
}
|
||||||
|
long dataCenterId = term % SnakeFlowerIdGenerator.MAX_DATA_CENTER_ID;
|
||||||
|
SnakeFlowerIdGenerator.setDataCenterId(dataCenterId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GetResponse getData(GetRequest request) {
|
||||||
|
return emptyResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LogFuture onApply(Log log) {
|
||||||
|
return emptyFuture;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(Throwable throwable) {
|
||||||
|
Loggers.ID_GENERATOR.error("An error occurred while onApply for ID, error : {}", throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String group() {
|
||||||
|
return "id_generator";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<SnapshotOperation> loadSnapshotOperate() {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,9 @@ package com.alibaba.nacos.core.distributed.id;
|
|||||||
|
|
||||||
import com.alibaba.nacos.consistency.IdGenerator;
|
import com.alibaba.nacos.consistency.IdGenerator;
|
||||||
import com.alibaba.nacos.core.exception.SnakflowerException;
|
import com.alibaba.nacos.core.exception.SnakflowerException;
|
||||||
import com.alibaba.nacos.core.utils.ConvertUtils;
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -28,123 +30,136 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class SnakeFlowerIdGenerator implements IdGenerator {
|
public class SnakeFlowerIdGenerator implements IdGenerator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start time intercept (2018-08-05 08:34)
|
* Start time intercept (2018-08-05 08:34)
|
||||||
*/
|
*/
|
||||||
private final long twepoch = 1533429269000L;
|
private static final long TWEPOCH = 1533429269000L;
|
||||||
private final long workerIdBits = 5L;
|
private static final long WORKER_ID_BITS = 5L;
|
||||||
private final long datacenterIdBits = 5L;
|
private static final long DATA_CENTER_ID_BITS = 5L;
|
||||||
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
|
public static final long MAX_WORKER_ID = ~(-1L << WORKER_ID_BITS);
|
||||||
private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
|
public static final long MAX_DATA_CENTER_ID = ~(-1L << DATA_CENTER_ID_BITS);
|
||||||
|
|
||||||
private final long sequenceBits = 12L;
|
private static final long SEQUENCE_BITS = 12L;
|
||||||
private final long workerIdShift = sequenceBits;
|
private static final long SEQUENCE_BITS1 = SEQUENCE_BITS;
|
||||||
private final long datacenterIdShift = sequenceBits + workerIdBits;
|
private static final long DATA_CENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS;
|
||||||
private final long timestampLeftShift = sequenceBits + workerIdBits
|
private static final long TIMESTAMP_LEFT_SHIFT =
|
||||||
+ datacenterIdBits;
|
SEQUENCE_BITS + WORKER_ID_BITS + DATA_CENTER_ID_BITS;
|
||||||
private final long sequenceMask = -1L ^ (-1L << sequenceBits);
|
private static final long SEQUENCE_MASK = ~(-1L << SEQUENCE_BITS);
|
||||||
private volatile long currentId;
|
|
||||||
private long workerId;
|
|
||||||
private long datacenterId;
|
|
||||||
private long sequence = 0L;
|
|
||||||
private long lastTimestamp = -1L;
|
|
||||||
|
|
||||||
@Override
|
private static long workerId;
|
||||||
|
private static volatile long dataCenterId;
|
||||||
|
|
||||||
|
private volatile long currentId;
|
||||||
|
private long sequence = 0L;
|
||||||
|
private long lastTimestamp = -1L;
|
||||||
|
|
||||||
|
public static void setDataCenterId(long dataCenterId) {
|
||||||
|
SnakeFlowerIdGenerator.dataCenterId = dataCenterId;
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
InetAddress address;
|
||||||
|
try {
|
||||||
|
address = InetAddress.getLocalHost();
|
||||||
|
}
|
||||||
|
catch (final UnknownHostException e) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Cannot get LocalHost InetAddress, please check your network!");
|
||||||
|
}
|
||||||
|
byte[] ipAddressByteArray = address.getAddress();
|
||||||
|
workerId = (
|
||||||
|
((ipAddressByteArray[ipAddressByteArray.length - 2] & 0B11) << Byte.SIZE)
|
||||||
|
+ (ipAddressByteArray[ipAddressByteArray.length - 1] & 0xFF));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
|
initialize(workerId, dataCenterId);
|
||||||
// Snowflake algorithm default parameter information
|
|
||||||
|
|
||||||
int dataCenterId = ConvertUtils.toInt(System.getProperty("nacos.core.snowflake.data-center"), 1);
|
|
||||||
int workerId = ConvertUtils.toInt(System.getProperty("nacos.core.snowflake.worker-id"), 1);
|
|
||||||
|
|
||||||
initialize(dataCenterId, workerId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long currentId() {
|
public long currentId() {
|
||||||
return currentId;
|
return currentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized long nextId() {
|
public synchronized long nextId() {
|
||||||
long timestamp = timeGen();
|
long timestamp = timeGen();
|
||||||
|
|
||||||
if (timestamp < lastTimestamp) {
|
if (timestamp < lastTimestamp) {
|
||||||
throw new SnakflowerException(String.format(
|
throw new SnakflowerException(String.format(
|
||||||
"Clock moved backwards. Refusing to generate id for %d milliseconds",
|
"Clock moved backwards. Refusing to generate id for %d milliseconds",
|
||||||
lastTimestamp - timestamp));
|
lastTimestamp - timestamp));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastTimestamp == timestamp) {
|
if (lastTimestamp == timestamp) {
|
||||||
sequence = (sequence + 1) & sequenceMask;
|
sequence = (sequence + 1) & SEQUENCE_MASK;
|
||||||
if (sequence == 0) {
|
if (sequence == 0) {
|
||||||
timestamp = tilNextMillis(lastTimestamp);
|
timestamp = tilNextMillis(lastTimestamp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
sequence = 0L;
|
sequence = 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
lastTimestamp = timestamp;
|
lastTimestamp = timestamp;
|
||||||
|
currentId = ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) | (dataCenterId
|
||||||
|
<< DATA_CENTER_ID_SHIFT) | (workerId << SEQUENCE_BITS1) | sequence;
|
||||||
|
return currentId;
|
||||||
|
}
|
||||||
|
|
||||||
currentId = ((timestamp - twepoch) << timestampLeftShift)
|
@Override
|
||||||
| (datacenterId << datacenterIdShift)
|
|
||||||
| (workerId << workerIdShift)
|
|
||||||
| sequence;
|
|
||||||
return currentId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<Object, Object> info() {
|
public Map<Object, Object> info() {
|
||||||
Map<Object, Object> info = new HashMap<>(4);
|
Map<Object, Object> info = new HashMap<>(4);
|
||||||
info.put("currentId", currentId);
|
info.put("currentId", currentId);
|
||||||
info.put("dataCenterId", datacenterId);
|
info.put("dataCenterId", dataCenterId);
|
||||||
info.put("workerId", workerId);
|
info.put("workerId", workerId);
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==============================Constructors=====================================
|
// ==============================Constructors=====================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* init
|
* init
|
||||||
*
|
*
|
||||||
* @param workerId worker id (0~31)
|
* @param workerId worker id (0~31)
|
||||||
* @param datacenterId data center id (0~31)
|
* @param datacenterId data center id (0~31)
|
||||||
*/
|
*/
|
||||||
public void initialize(long workerId, long datacenterId) {
|
public void initialize(long workerId, long datacenterId) {
|
||||||
if (workerId > maxWorkerId || workerId < 0) {
|
if (workerId > MAX_WORKER_ID || workerId < 0) {
|
||||||
throw new IllegalArgumentException(String.format(
|
throw new IllegalArgumentException(
|
||||||
"worker Id can't be greater than %d or less than 0", maxWorkerId));
|
String.format("worker Id can't be greater than %d or less than 0",
|
||||||
}
|
MAX_WORKER_ID));
|
||||||
if (datacenterId > maxDatacenterId || datacenterId < 0) {
|
}
|
||||||
throw new IllegalArgumentException(
|
if (datacenterId > MAX_DATA_CENTER_ID || datacenterId < 0) {
|
||||||
String.format("datacenter Id can't be greater than %d or less than 0",
|
throw new IllegalArgumentException(
|
||||||
maxDatacenterId));
|
String.format("datacenter Id can't be greater than %d or less than 0",
|
||||||
}
|
MAX_DATA_CENTER_ID));
|
||||||
this.workerId = workerId;
|
}
|
||||||
this.datacenterId = datacenterId;
|
SnakeFlowerIdGenerator.workerId = workerId;
|
||||||
}
|
SnakeFlowerIdGenerator.dataCenterId = datacenterId;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Block to the next millisecond until a new timestamp is obtained
|
* Block to the next millisecond until a new timestamp is obtained
|
||||||
*
|
*
|
||||||
* @param lastTimestamp 上次生成ID的时间截
|
* @param lastTimestamp 上次生成ID的时间截
|
||||||
* @return 当前时间戳
|
* @return 当前时间戳
|
||||||
*/
|
*/
|
||||||
protected long tilNextMillis(long lastTimestamp) {
|
protected long tilNextMillis(long lastTimestamp) {
|
||||||
long timestamp = timeGen();
|
long timestamp = timeGen();
|
||||||
while (timestamp <= lastTimestamp) {
|
while (timestamp <= lastTimestamp) {
|
||||||
timestamp = timeGen();
|
timestamp = timeGen();
|
||||||
}
|
}
|
||||||
return timestamp;
|
return timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current time in milliseconds
|
* Returns the current time in milliseconds
|
||||||
*
|
*
|
||||||
* @return 当前时间(毫秒)
|
* @return 当前时间(毫秒)
|
||||||
*/
|
*/
|
||||||
protected long timeGen() {
|
protected long timeGen() {
|
||||||
return System.currentTimeMillis();
|
return System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -30,6 +30,7 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import com.alibaba.nacos.common.utils.IoUtils;
|
import com.alibaba.nacos.common.utils.IoUtils;
|
||||||
import com.sun.management.OperatingSystemMXBean;
|
import com.sun.management.OperatingSystemMXBean;
|
||||||
@ -320,8 +321,8 @@ public class ApplicationUtils implements ApplicationContextInitializer<Configura
|
|||||||
* Standalone mode or not
|
* Standalone mode or not
|
||||||
*/
|
*/
|
||||||
public static boolean getStandaloneMode() {
|
public static boolean getStandaloneMode() {
|
||||||
if (isStandalone == null) {
|
if (Objects.isNull(isStandalone)) {
|
||||||
isStandalone = getProperty(STANDALONE_MODE_PROPERTY_NAME, Boolean.class, false);
|
isStandalone = Boolean.getBoolean(STANDALONE_MODE_PROPERTY_NAME);
|
||||||
}
|
}
|
||||||
return isStandalone;
|
return isStandalone;
|
||||||
}
|
}
|
||||||
@ -331,15 +332,15 @@ public class ApplicationUtils implements ApplicationContextInitializer<Configura
|
|||||||
*/
|
*/
|
||||||
public static String getFunctionMode() {
|
public static String getFunctionMode() {
|
||||||
if (StringUtils.isEmpty(functionModeType)) {
|
if (StringUtils.isEmpty(functionModeType)) {
|
||||||
functionModeType = getProperty(FUNCTION_MODE_PROPERTY_NAME);
|
functionModeType = System.getProperty(FUNCTION_MODE_PROPERTY_NAME);
|
||||||
}
|
}
|
||||||
return functionModeType;
|
return functionModeType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getNacosHome() {
|
public static String getNacosHome() {
|
||||||
String nacosHome = getProperty(NACOS_HOME_KEY);
|
String nacosHome = System.getProperty(NACOS_HOME_KEY);
|
||||||
if (StringUtils.isBlank(nacosHome)) {
|
if (StringUtils.isBlank(nacosHome)) {
|
||||||
nacosHome = System.getProperty("user.home") + File.separator + "nacos";
|
nacosHome = Paths.get(System.getProperty("user.home"), "nacos").toString();
|
||||||
}
|
}
|
||||||
return nacosHome;
|
return nacosHome;
|
||||||
}
|
}
|
||||||
|
@ -1,156 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.alibaba.nacos.core.utils;
|
|
||||||
|
|
||||||
import com.alibaba.nacos.common.utils.IoUtils;
|
|
||||||
import com.sun.management.OperatingSystemMXBean;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.lang.management.ManagementFactory;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.Constants.FUNCTION_MODE_PROPERTY_NAME;
|
|
||||||
import static com.alibaba.nacos.core.utils.Constants.STANDALONE_MODE_PROPERTY_NAME;
|
|
||||||
import static org.apache.commons.lang3.CharEncoding.UTF_8;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author nacos
|
|
||||||
*/
|
|
||||||
public class SystemUtils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Standalone mode or not
|
|
||||||
*/
|
|
||||||
public static final boolean STANDALONE_MODE = Boolean.getBoolean(STANDALONE_MODE_PROPERTY_NAME);
|
|
||||||
|
|
||||||
public static final String STANDALONE_MODE_ALONE = "standalone";
|
|
||||||
public static final String STANDALONE_MODE_CLUSTER = "cluster";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* server
|
|
||||||
*/
|
|
||||||
public static final String FUNCTION_MODE = System.getProperty(FUNCTION_MODE_PROPERTY_NAME);
|
|
||||||
|
|
||||||
public static final String FUNCTION_MODE_CONFIG = "config";
|
|
||||||
public static final String FUNCTION_MODE_NAMING = "naming";
|
|
||||||
|
|
||||||
|
|
||||||
private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory
|
|
||||||
.getOperatingSystemMXBean();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* nacos local ip
|
|
||||||
*/
|
|
||||||
public static final String LOCAL_IP = InetUtils.getSelfIp();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The key of nacos home.
|
|
||||||
*/
|
|
||||||
public static final String NACOS_HOME_KEY = "nacos.home";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The home of nacos.
|
|
||||||
*/
|
|
||||||
public static final String NACOS_HOME = getNacosHome();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The file path of cluster conf.
|
|
||||||
*/
|
|
||||||
public static final String CLUSTER_CONF_FILE_PATH = getClusterConfFilePath();
|
|
||||||
|
|
||||||
public static List<String> getIPsBySystemEnv(String key) {
|
|
||||||
String env = getSystemEnv(key);
|
|
||||||
List<String> ips = new ArrayList<>();
|
|
||||||
if (StringUtils.isNotEmpty(env)) {
|
|
||||||
ips = Arrays.asList(env.split(","));
|
|
||||||
}
|
|
||||||
return ips;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getSystemEnv(String key) {
|
|
||||||
return System.getenv(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float getLoad() {
|
|
||||||
return (float) operatingSystemMXBean.getSystemLoadAverage();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float getCPU() {
|
|
||||||
return (float) operatingSystemMXBean.getSystemCpuLoad();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float getMem() {
|
|
||||||
return (float) (1 - (double) operatingSystemMXBean.getFreePhysicalMemorySize() / (double) operatingSystemMXBean
|
|
||||||
.getTotalPhysicalMemorySize());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getNacosHome() {
|
|
||||||
String nacosHome = System.getProperty(NACOS_HOME_KEY);
|
|
||||||
if (StringUtils.isBlank(nacosHome)) {
|
|
||||||
nacosHome = System.getProperty("user.home") + File.separator + "nacos";
|
|
||||||
}
|
|
||||||
return nacosHome;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getConfFilePath() {
|
|
||||||
return NACOS_HOME + File.separator + "conf" + File.separator;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getClusterConfFilePath() {
|
|
||||||
return NACOS_HOME + File.separator + "conf" + File.separator + "cluster.conf";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<String> readClusterConf() throws IOException {
|
|
||||||
List<String> instanceList = new ArrayList<String>();
|
|
||||||
try(Reader reader = new InputStreamReader(new FileInputStream(new File(CLUSTER_CONF_FILE_PATH)),
|
|
||||||
StandardCharsets.UTF_8)) {
|
|
||||||
List<String> lines = IoUtils.readLines(reader);
|
|
||||||
String comment = "#";
|
|
||||||
for (String line : lines) {
|
|
||||||
String instance = line.trim();
|
|
||||||
if (instance.startsWith(comment)) {
|
|
||||||
// # it is ip
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (instance.contains(comment)) {
|
|
||||||
// 192.168.71.52:8848 # Instance A
|
|
||||||
instance = instance.substring(0, instance.indexOf(comment));
|
|
||||||
instance = instance.trim();
|
|
||||||
}
|
|
||||||
int multiIndex = instance.indexOf(Constants.COMMA_DIVISION);
|
|
||||||
if (multiIndex > 0) {
|
|
||||||
// support the format: ip1:port,ip2:port # multi inline
|
|
||||||
instanceList.addAll(Arrays.asList(instance.split(Constants.COMMA_DIVISION)));
|
|
||||||
} else {
|
|
||||||
//support the format: 192.168.71.52:8848
|
|
||||||
instanceList.add(instance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return instanceList;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void writeClusterConf(String content) throws IOException {
|
|
||||||
IoUtils.writeStringToFile(new File(CLUSTER_CONF_FILE_PATH), content, UTF_8);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -16,7 +16,7 @@
|
|||||||
package com.alibaba.nacos.core.util;
|
package com.alibaba.nacos.core.util;
|
||||||
|
|
||||||
|
|
||||||
import com.alibaba.nacos.core.utils.SystemUtils;
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
@ -31,7 +31,7 @@ import static com.alibaba.nacos.core.utils.Constants.PREFER_HOSTNAME_OVER_IP_PRO
|
|||||||
import static com.alibaba.nacos.core.utils.Constants.STANDALONE_MODE_PROPERTY_NAME;
|
import static com.alibaba.nacos.core.utils.Constants.STANDALONE_MODE_PROPERTY_NAME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link SystemUtils} Test
|
* {@link ApplicationUtils} Test
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
* @since 0.2.2
|
* @since 0.2.2
|
||||||
@ -81,7 +81,7 @@ public class SystemUtilsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReadClusterConf() throws IOException {
|
public void testReadClusterConf() throws IOException {
|
||||||
FileUtils.forceMkdir(new File(SystemUtils.getConfFilePath()));
|
FileUtils.forceMkdir(new File(ApplicationUtils.getConfFilePath()));
|
||||||
|
|
||||||
String lineSeparator = System.getProperty("line.separator");
|
String lineSeparator = System.getProperty("line.separator");
|
||||||
|
|
||||||
@ -90,8 +90,8 @@ public class SystemUtilsTest {
|
|||||||
* #example
|
* #example
|
||||||
* 192.168.1.1:8848
|
* 192.168.1.1:8848
|
||||||
*/
|
*/
|
||||||
SystemUtils.writeClusterConf("#it is ip" + lineSeparator + "#example" + lineSeparator + "192.168.1.1:8848");
|
ApplicationUtils.writeClusterConf("#it is ip" + lineSeparator + "#example" + lineSeparator + "192.168.1.1:8848");
|
||||||
Assert.assertEquals(SystemUtils.readClusterConf().get(0), "192.168.1.1:8848");
|
Assert.assertEquals(ApplicationUtils.readClusterConf().get(0), "192.168.1.1:8848");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* #it is ip
|
* #it is ip
|
||||||
@ -99,10 +99,10 @@ public class SystemUtilsTest {
|
|||||||
* # 192.168.1.1:8848
|
* # 192.168.1.1:8848
|
||||||
* 192.168.1.2:8848 # Instance A
|
* 192.168.1.2:8848 # Instance A
|
||||||
*/
|
*/
|
||||||
SystemUtils.writeClusterConf(
|
ApplicationUtils.writeClusterConf(
|
||||||
"#it is ip" + lineSeparator + " #example" + lineSeparator + " # 192.168.1.1:8848" + lineSeparator
|
"#it is ip" + lineSeparator + " #example" + lineSeparator + " # 192.168.1.1:8848" + lineSeparator
|
||||||
+ " 192.168.1.2:8848 # Instance A " + lineSeparator + "192.168.1.3#:8848");
|
+ " 192.168.1.2:8848 # Instance A " + lineSeparator + "192.168.1.3#:8848");
|
||||||
List<String> instanceList = SystemUtils.readClusterConf();
|
List<String> instanceList = ApplicationUtils.readClusterConf();
|
||||||
Assert.assertEquals(instanceList.get(0), "192.168.1.2:8848");
|
Assert.assertEquals(instanceList.get(0), "192.168.1.2:8848");
|
||||||
Assert.assertEquals(instanceList.get(1), "192.168.1.3");
|
Assert.assertEquals(instanceList.get(1), "192.168.1.3");
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ import com.alibaba.nacos.api.common.Constants;
|
|||||||
import com.alibaba.nacos.api.exception.NacosException;
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
import com.alibaba.nacos.core.cluster.Member;
|
import com.alibaba.nacos.core.cluster.Member;
|
||||||
import com.alibaba.nacos.core.cluster.ServerMemberManager;
|
import com.alibaba.nacos.core.cluster.ServerMemberManager;
|
||||||
import com.alibaba.nacos.core.utils.SystemUtils;
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
import com.alibaba.nacos.naming.cluster.ServerStatus;
|
import com.alibaba.nacos.naming.cluster.ServerStatus;
|
||||||
import com.alibaba.nacos.naming.cluster.transport.Serializer;
|
import com.alibaba.nacos.naming.cluster.transport.Serializer;
|
||||||
import com.alibaba.nacos.naming.consistency.ApplyAction;
|
import com.alibaba.nacos.naming.consistency.ApplyAction;
|
||||||
@ -117,7 +117,7 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void load() throws Exception {
|
public void load() throws Exception {
|
||||||
if (SystemUtils.STANDALONE_MODE) {
|
if (ApplicationUtils.getStandaloneMode()) {
|
||||||
initialized = true;
|
initialized = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import com.alibaba.fastjson.JSON;
|
|||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.alibaba.fastjson.TypeReference;
|
import com.alibaba.fastjson.TypeReference;
|
||||||
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
import com.alibaba.nacos.naming.boot.RunningConfig;
|
import com.alibaba.nacos.naming.boot.RunningConfig;
|
||||||
import com.alibaba.nacos.naming.consistency.ApplyAction;
|
import com.alibaba.nacos.naming.consistency.ApplyAction;
|
||||||
import com.alibaba.nacos.naming.consistency.Datum;
|
import com.alibaba.nacos.naming.consistency.Datum;
|
||||||
@ -53,8 +54,6 @@ import java.util.concurrent.locks.Lock;
|
|||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.zip.GZIPOutputStream;
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author nacos
|
* @author nacos
|
||||||
*/
|
*/
|
||||||
@ -483,7 +482,7 @@ public class RaftCore {
|
|||||||
|
|
||||||
public void sendBeat() throws IOException, InterruptedException {
|
public void sendBeat() throws IOException, InterruptedException {
|
||||||
RaftPeer local = peers.local();
|
RaftPeer local = peers.local();
|
||||||
if (local.state != RaftPeer.State.LEADER && !STANDALONE_MODE) {
|
if (local.state != RaftPeer.State.LEADER && !ApplicationUtils.getStandaloneMode()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,8 +21,6 @@ import com.alibaba.nacos.core.cluster.MemberChangeListener;
|
|||||||
import com.alibaba.nacos.core.cluster.NodeChangeEvent;
|
import com.alibaba.nacos.core.cluster.NodeChangeEvent;
|
||||||
import com.alibaba.nacos.core.cluster.ServerMemberManager;
|
import com.alibaba.nacos.core.cluster.ServerMemberManager;
|
||||||
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
import com.alibaba.nacos.core.utils.SystemUtils;
|
|
||||||
import com.alibaba.nacos.naming.boot.RunningConfig;
|
|
||||||
import com.alibaba.nacos.naming.misc.HttpClient;
|
import com.alibaba.nacos.naming.misc.HttpClient;
|
||||||
import com.alibaba.nacos.naming.misc.Loggers;
|
import com.alibaba.nacos.naming.misc.Loggers;
|
||||||
import com.alibaba.nacos.naming.misc.NetUtils;
|
import com.alibaba.nacos.naming.misc.NetUtils;
|
||||||
@ -47,8 +45,6 @@ import java.util.Objects;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author nacos
|
* @author nacos
|
||||||
*/
|
*/
|
||||||
@ -78,7 +74,7 @@ public class RaftPeerSet implements MemberChangeListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public RaftPeer getLeader() {
|
public RaftPeer getLeader() {
|
||||||
if (STANDALONE_MODE) {
|
if (ApplicationUtils.getStandaloneMode()) {
|
||||||
return local();
|
return local();
|
||||||
}
|
}
|
||||||
return leader;
|
return leader;
|
||||||
@ -104,7 +100,7 @@ public class RaftPeerSet implements MemberChangeListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLeader(String ip) {
|
public boolean isLeader(String ip) {
|
||||||
if (STANDALONE_MODE) {
|
if (ApplicationUtils.getStandaloneMode()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,7 +205,7 @@ public class RaftPeerSet implements MemberChangeListener {
|
|||||||
|
|
||||||
public RaftPeer local() {
|
public RaftPeer local() {
|
||||||
RaftPeer peer = peers.get(NetUtils.localServer());
|
RaftPeer peer = peers.get(NetUtils.localServer());
|
||||||
if (peer == null && SystemUtils.STANDALONE_MODE) {
|
if (peer == null && ApplicationUtils.getStandaloneMode()) {
|
||||||
RaftPeer localPeer = new RaftPeer();
|
RaftPeer localPeer = new RaftPeer();
|
||||||
localPeer.ip = NetUtils.localServer();
|
localPeer.ip = NetUtils.localServer();
|
||||||
localPeer.term.set(localTerm.get());
|
localPeer.term.set(localTerm.get());
|
||||||
|
@ -23,7 +23,7 @@ import com.alibaba.nacos.core.auth.ActionTypes;
|
|||||||
import com.alibaba.nacos.core.auth.Secured;
|
import com.alibaba.nacos.core.auth.Secured;
|
||||||
import com.alibaba.nacos.core.cluster.NodeState;
|
import com.alibaba.nacos.core.cluster.NodeState;
|
||||||
import com.alibaba.nacos.core.cluster.ServerMemberManager;
|
import com.alibaba.nacos.core.cluster.ServerMemberManager;
|
||||||
import com.alibaba.nacos.core.utils.SystemUtils;
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
import com.alibaba.nacos.naming.cluster.ServerStatusManager;
|
import com.alibaba.nacos.naming.cluster.ServerStatusManager;
|
||||||
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftCore;
|
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftCore;
|
||||||
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer;
|
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer;
|
||||||
@ -144,9 +144,9 @@ public class OperatorController {
|
|||||||
result.put("raftNotifyTaskCount", raftCore.getNotifyTaskCount());
|
result.put("raftNotifyTaskCount", raftCore.getNotifyTaskCount());
|
||||||
result.put("responsibleServiceCount", responsibleDomCount);
|
result.put("responsibleServiceCount", responsibleDomCount);
|
||||||
result.put("responsibleInstanceCount", responsibleIPCount);
|
result.put("responsibleInstanceCount", responsibleIPCount);
|
||||||
result.put("cpu", SystemUtils.getCPU());
|
result.put("cpu", ApplicationUtils.getCPU());
|
||||||
result.put("load", SystemUtils.getLoad());
|
result.put("load", ApplicationUtils.getLoad());
|
||||||
result.put("mem", SystemUtils.getMem());
|
result.put("mem", ApplicationUtils.getMem());
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ import com.alibaba.nacos.core.cluster.Member;
|
|||||||
import com.alibaba.nacos.core.cluster.MemberChangeListener;
|
import com.alibaba.nacos.core.cluster.MemberChangeListener;
|
||||||
import com.alibaba.nacos.core.cluster.NodeChangeEvent;
|
import com.alibaba.nacos.core.cluster.NodeChangeEvent;
|
||||||
import com.alibaba.nacos.core.cluster.ServerMemberManager;
|
import com.alibaba.nacos.core.cluster.ServerMemberManager;
|
||||||
import com.alibaba.nacos.core.utils.SystemUtils;
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
import com.alibaba.nacos.naming.misc.Loggers;
|
import com.alibaba.nacos.naming.misc.Loggers;
|
||||||
import com.alibaba.nacos.naming.misc.NetUtils;
|
import com.alibaba.nacos.naming.misc.NetUtils;
|
||||||
import com.alibaba.nacos.naming.misc.SwitchDomain;
|
import com.alibaba.nacos.naming.misc.SwitchDomain;
|
||||||
@ -65,7 +65,7 @@ public class DistroMapper implements MemberChangeListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean responsible(String serviceName) {
|
public boolean responsible(String serviceName) {
|
||||||
if (!switchDomain.isDistroEnabled() || SystemUtils.STANDALONE_MODE) {
|
if (!switchDomain.isDistroEnabled() || ApplicationUtils.getStandaloneMode()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ import com.alibaba.fastjson.serializer.SerializerFeature;
|
|||||||
import com.alibaba.nacos.api.exception.NacosException;
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker;
|
import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker;
|
||||||
import com.alibaba.nacos.common.utils.VersionUtils;
|
import com.alibaba.nacos.common.utils.VersionUtils;
|
||||||
|
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||||
import com.alibaba.nacos.naming.healthcheck.JsonAdapter;
|
import com.alibaba.nacos.naming.healthcheck.JsonAdapter;
|
||||||
import com.alibaba.nacos.naming.selector.Selector;
|
import com.alibaba.nacos.naming.selector.Selector;
|
||||||
import com.alibaba.nacos.naming.selector.SelectorJsonAdapter;
|
import com.alibaba.nacos.naming.selector.SelectorJsonAdapter;
|
||||||
@ -33,8 +34,6 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author nacos
|
* @author nacos
|
||||||
* @author jifengnan
|
* @author jifengnan
|
||||||
@ -117,7 +116,7 @@ public class UtilsAndCommons {
|
|||||||
|
|
||||||
public static final String UPDATE_INSTANCE_ACTION_REMOVE = "remove";
|
public static final String UPDATE_INSTANCE_ACTION_REMOVE = "remove";
|
||||||
|
|
||||||
public static final String DATA_BASE_DIR = NACOS_HOME + File.separator + "data" + File.separator + "naming";
|
public static final String DATA_BASE_DIR = ApplicationUtils.getNacosHome() + File.separator + "data" + File.separator + "naming";
|
||||||
|
|
||||||
public static final String NUMBER_PATTERN = "^\\d+$";
|
public static final String NUMBER_PATTERN = "^\\d+$";
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user