refactor: Change the distributed ID logic

This commit is contained in:
chuntaojun 2020-04-02 19:24:11 +08:00
parent 66db5207d3
commit 9f8c19cafb
42 changed files with 329 additions and 1032 deletions

View File

@ -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.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.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
@ -42,7 +42,7 @@ public class NacosConfigConfiguration {
return registration;
}
@Conditional(ConditionOnEmbedStoreType.class)
@Conditional(ConditionOnDistributedStore.class)
@Bean
public FilterRegistrationBean transferToLeaderRegistration() {
FilterRegistrationBean<TransferToLeaderFilter> registration = new FilterRegistrationBean<>();
@ -58,7 +58,7 @@ public class NacosConfigConfiguration {
return new NacosWebFilter();
}
@Conditional(ConditionOnEmbedStoreType.class)
@Conditional(ConditionOnDistributedStore.class)
@Bean
public TransferToLeaderFilter transferToLeader() {
return new TransferToLeaderFilter();

View File

@ -54,7 +54,7 @@ import java.sql.Timestamp;
import java.util.*;
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;
/**
* 软负载客户端发布数据专用控制器

View File

@ -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.dump.DumpService;
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 org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@ -82,7 +81,7 @@ public class ConfigOpsController {
@GetMapping(value = "/derby")
public RestResult<Object> derbyOps(@RequestParam(value = "sql") String sql) {
String selectSign = "select";
if (!PropertyUtil.isUseMysql()) {
if (!PropertyUtil.isUseExternalDB()) {
LocalDataSourceServiceImpl dataSourceService = (LocalDataSourceServiceImpl) DynamicDataSource
.getInstance().getDataSource();
if (StringUtils.startsWithIgnoreCase(sql, selectSign)) {

View File

@ -43,7 +43,6 @@ import java.util.List;
import java.util.Map;
import static com.alibaba.nacos.config.server.utils.LogUtil.pullLog;
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
/**
* ConfigServlet inner for aop
@ -142,7 +141,7 @@ public class ConfigServletInner {
if (isBeta) {
md5 = cacheItem.getMd54Beta();
lastModified = cacheItem.getLastModifiedTs4Beta();
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
if (PropertyUtil.isEmbeddedStorage()) {
configInfoBase = persistService.findConfigInfo4Beta(dataId, group, tenant);
} else {
file = DiskUtil.targetBetaFile(dataId, group, tenant);
@ -159,7 +158,7 @@ public class ConfigServletInner {
lastModified = cacheItem.tagLastModifiedTs.get(autoTag);
}
}
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
if (PropertyUtil.isEmbeddedStorage()) {
configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, autoTag);
} else {
file = DiskUtil.targetTagFile(dataId, group, tenant, autoTag);
@ -170,7 +169,7 @@ public class ConfigServletInner {
} else {
md5 = cacheItem.getMd5();
lastModified = cacheItem.getLastModifiedTs();
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
if (PropertyUtil.isEmbeddedStorage()) {
configInfoBase = persistService.findConfigInfo(dataId, group, tenant);
} else {
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);
} else {
file = DiskUtil.targetTagFile(dataId, group, tenant, tag);
@ -232,14 +231,14 @@ public class ConfigServletInner {
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache,no-store");
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
if (PropertyUtil.isEmbeddedStorage()) {
response.setDateHeader("Last-Modified", lastModified);
} else {
fis = new FileInputStream(file);
response.setDateHeader("Last-Modified", file.lastModified());
}
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
if (PropertyUtil.isEmbeddedStorage()) {
out = response.getWriter();
out.print(configInfoBase.getContent());
out.flush();

View File

@ -26,7 +26,7 @@ import org.springframework.web.bind.annotation.RestController;
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

View File

@ -15,7 +15,7 @@
*/
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

View File

@ -28,7 +28,6 @@ import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
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.utils.LogUtil.defaultLog;
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
/**
* Base data source
@ -125,7 +123,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
* 事务的超时时间需要与普通操作区分开
*/
tjt.setTimeout(TRANSACTION_QUERY_TIMEOUT);
if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
if (PropertyUtil.isUseExternalDB()) {
try {
reload();
} catch (IOException e) {

View File

@ -33,7 +33,6 @@ import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
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={}, "
+ "lastModifiedNew={}",
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
} else if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
} else if (PropertyUtil.isUseExternalDB()) {
DiskUtil.saveToDisk(dataId, group, tenant, content);
}
updateMd5(groupKey, md5, lastModifiedTs);
@ -122,7 +121,7 @@ public class ConfigService {
"[dump-beta-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
+ "lastModifiedNew={}",
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
} else if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
} else if (PropertyUtil.isUseExternalDB()) {
DiskUtil.saveBetaToDisk(dataId, group, tenant, content);
}
String[] betaIpsArr = betaIps.split(",");
@ -161,7 +160,7 @@ public class ConfigService {
"[dump-tag-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
+ "lastModifiedNew={}",
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
} else if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
} else if (PropertyUtil.isUseExternalDB()) {
DiskUtil.saveTagToDisk(dataId, group, tenant, tag, content);
}
@ -193,7 +192,7 @@ public class ConfigService {
try {
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE);
if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
if (PropertyUtil.isUseExternalDB()) {
String loacalMd5 = DiskUtil.getLocalConfigMd5(dataId, group, tenant);
if (md5.equals(loacalMd5)) {
dumpLog.warn(
@ -218,7 +217,7 @@ public class ConfigService {
static public void reloadConfig() {
String aggreds = null;
try {
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
if (PropertyUtil.isEmbeddedStorage()) {
ConfigInfoBase config = persistService.findConfigInfoBase(AggrWhitelist.AGGRIDS_METADATA,
"DEFAULT_GROUP");
if (config != null) {
@ -237,7 +236,7 @@ public class ConfigService {
String clientIpWhitelist = null;
try {
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
if (PropertyUtil.isEmbeddedStorage()) {
ConfigInfoBase config = persistService.findConfigInfoBase(
ClientIpWhiteList.CLIENT_IP_WHITELIST_METADATA, "DEFAULT_GROUP");
if (config != null) {
@ -257,7 +256,7 @@ public class ConfigService {
String switchContent = null;
try {
if (STANDALONE_MODE && !PropertyUtil.isUseMysql()) {
if (PropertyUtil.isEmbeddedStorage()) {
ConfigInfoBase config = persistService.findConfigInfoBase(SwitchService.SWITCH_META_DATAID,
"DEFAULT_GROUP");
if (config != null) {
@ -325,7 +324,7 @@ public class ConfigService {
}
try {
if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
if (PropertyUtil.isUseExternalDB()) {
DiskUtil.removeConfigInfo(dataId, group, tenant);
}
CACHE.remove(groupKey);
@ -359,7 +358,7 @@ public class ConfigService {
}
try {
if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
if (PropertyUtil.isUseExternalDB()) {
DiskUtil.removeConfigInfo4Beta(dataId, group, tenant);
}
EventDispatcher.fireEvent(new LocalDataChangeEvent(groupKey, true, CACHE.get(groupKey).getIps4Beta()));
@ -394,7 +393,7 @@ public class ConfigService {
}
try {
if (!STANDALONE_MODE || PropertyUtil.isUseMysql()) {
if (PropertyUtil.isUseExternalDB()) {
DiskUtil.removeConfigInfo4Tag(dataId, group, tenant, tag);
}

View File

@ -19,6 +19,7 @@ import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.Md5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
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.lang3.StringUtils;
import org.slf4j.Logger;
@ -29,8 +30,6 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME;
/**
* 磁盘操作工具类
* <p>
@ -112,9 +111,9 @@ public class DiskUtil {
static public File targetFile(String dataId, String group, String tenant) {
File file = null;
if (StringUtils.isBlank(tenant)) {
file = new File(NACOS_HOME, BASE_DIR);
file = new File(ApplicationUtils.getNacosHome(), BASE_DIR);
} 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, group);
@ -128,9 +127,9 @@ public class DiskUtil {
static public File targetBetaFile(String dataId, String group, String tenant) {
File file = null;
if (StringUtils.isBlank(tenant)) {
file = new File(NACOS_HOME, BETA_DIR);
file = new File(ApplicationUtils.getNacosHome(), BETA_DIR);
} 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, group);
@ -144,9 +143,9 @@ public class DiskUtil {
static public File targetTagFile(String dataId, String group, String tenant, String tag) {
File file = null;
if (StringUtils.isBlank(tenant)) {
file = new File(NACOS_HOME, TAG_DIR);
file = new File(ApplicationUtils.getNacosHome(), TAG_DIR);
} 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, group);
@ -176,7 +175,7 @@ public class DiskUtil {
}
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) {
@ -184,13 +183,13 @@ public class DiskUtil {
}
static public void clearAll() {
File file = new File(NACOS_HOME, BASE_DIR);
File file = new File(ApplicationUtils.getNacosHome(), BASE_DIR);
if (FileUtils.deleteQuietly(file)) {
LogUtil.defaultLog.info("clear all config-info success.");
} else {
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)) {
LogUtil.defaultLog.info("clear all config-info-tenant success.");
} else {
@ -199,13 +198,13 @@ public class DiskUtil {
}
static public void clearAllBeta() {
File file = new File(NACOS_HOME, BETA_DIR);
File file = new File(ApplicationUtils.getNacosHome(), BETA_DIR);
if (FileUtils.deleteQuietly(file)) {
LogUtil.defaultLog.info("clear all config-info-beta success.");
} else {
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)) {
LogUtil.defaultLog.info("clear all config-info-beta-tenant success.");
} else {
@ -214,13 +213,13 @@ public class DiskUtil {
}
static public void clearAllTag() {
File file = new File(NACOS_HOME, TAG_DIR);
File file = new File(ApplicationUtils.getNacosHome(), TAG_DIR);
if (FileUtils.deleteQuietly(file)) {
LogUtil.defaultLog.info("clear all config-info-tag success.");
} else {
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)) {
LogUtil.defaultLog.info("clear all config-info-tag-tenant success.");
} else {

View File

@ -16,9 +16,6 @@
package com.alibaba.nacos.config.server.service;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import org.springframework.stereotype.Component;
/**
* datasource adapter
@ -38,7 +35,11 @@ public class DynamicDataSource {
public synchronized DataSourceService getDataSource() {
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) {
localDataSourceService = new LocalDataSourceServiceImpl();
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();
}
}

View File

@ -47,11 +47,11 @@ import org.springframework.transaction.support.TransactionTemplate;
*/
public class LocalDataSourceServiceImpl implements DataSourceService {
private final String JDBC_DRIVER_NAME = "org.apache.derby.jdbc.EmbeddedDriver";
private final String USER_NAME = "nacos";
private final String PASSWORD = "nacos";
private final String DERBY_BASE_DIR = "data" + File.separator + "derby-data";
private final String DERBY_SHUTDOWN_ERR_MSG = "Derby system shutdown.";
private final String jdbcDriverName = "org.apache.derby.jdbc.EmbeddedDriver";
private final String userName = "nacos";
private final String password = "nacos";
private final String derbyBaseDir = "data" + File.separator + "derby-data";
private final String derbyShutdownErrMsg = "Derby system shutdown.";
private volatile JdbcTemplate jt;
private volatile TransactionTemplate tjt;
@ -62,10 +62,11 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
@PostConstruct
@Override
public synchronized void init() throws Exception {
if (!PropertyUtil.isUseMysql()) {
if (!PropertyUtil.isUseExternalDB()) {
if (!initialize) {
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 = true;
}
@ -90,7 +91,8 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
public void cleanAndReopenDerby() throws Exception {
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);
}
@ -108,19 +110,19 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
// 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;
}
}
DiskUtils.deleteDirectory(Paths.get(ApplicationUtils.getNacosHome(), DERBY_BASE_DIR).toString());
DiskUtils.deleteDirectory(Paths.get(ApplicationUtils.getNacosHome(), derbyBaseDir).toString());
}
private synchronized void initialize(String jdbcUrl) {
HikariDataSource ds = new HikariDataSource();
ds.setDriverClassName(JDBC_DRIVER_NAME);
ds.setDriverClassName(jdbcDriverName);
ds.setJdbcUrl(jdbcUrl);
ds.setUsername(USER_NAME);
ds.setPassword(PASSWORD);
ds.setUsername(userName);
ds.setPassword(password);
ds.setMaximumPoolSize(80);
ds.setConnectionTimeout(10000L);
DataSourceTransactionManager tm = new DataSourceTransactionManager();
@ -157,7 +159,8 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
@Override
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

View File

@ -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.TimeUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
@ -34,7 +33,6 @@ import javax.annotation.PostConstruct;
import java.sql.*;
import java.util.List;
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
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) {
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";
}
try {

View File

@ -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.TimeUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
@ -33,7 +32,6 @@ import javax.annotation.PostConstruct;
import java.sql.*;
import java.util.List;
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
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) {
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";
}

View File

@ -54,8 +54,7 @@ import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
import static com.alibaba.nacos.core.utils.ApplicationUtils.LOCAL_IP;
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
// underlying master to complete the selection
if (PropertyUtil.isEmbeddedDistributedStorage()) {
if (PropertyUtil.isEmbeddedStorage()) {
LogUtil.dumpLog.info("With embedded distributed storage, you need to wait for " +
"the underlying master to complete before you can perform the dump operation.");
@ -179,7 +178,7 @@ public class DumpService {
throw new RuntimeException(
"Nacos Server did not start because dumpservice bean construction failure :\n" + e.getMessage());
}
if (!STANDALONE_MODE) {
if (!ApplicationUtils.getStandaloneMode()) {
Runnable heartbeat = () -> {
String heartBeatTime = TimeUtils.getCurrentTime().toString();
// write disk
@ -417,7 +416,7 @@ public class DumpService {
}
private boolean canExecute() {
if (!PropertyUtil.isEmbeddedDistributedStorage()) {
if (!PropertyUtil.isEmbeddedStorage()) {
return true;
}
try {

View File

@ -37,7 +37,7 @@ import java.util.ArrayList;
import java.util.List;
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() {
if (!PropertyUtil.isEmbeddedDistributedStorage()) {
if (!PropertyUtil.isEmbeddedStorage()) {
return true;
}
try {

View File

@ -35,7 +35,7 @@ import java.sql.Timestamp;
import java.util.ArrayList;
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

View File

@ -50,7 +50,7 @@ import java.util.List;
import java.util.Queue;
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

View File

@ -33,7 +33,7 @@ import java.util.Arrays;
import java.util.List;
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包括自己加载新数据

View File

@ -24,7 +24,7 @@ import org.springframework.stereotype.Service;
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

View File

@ -16,20 +16,21 @@
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.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* 没有开启 embeddedDistributedStorage 以及 spring.datasource.platform 设置了参数
* when embeddedStorage==false
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public class ConditionOnDefaultStoreType implements Condition {
public class ConditionOnDefaultStore implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return !Objects.equals(context.getEnvironment().getProperty("embeddedDistributedStorage"), "true");
return !(PropertyUtil.isEmbeddedStorage() && !ApplicationUtils.getStandaloneMode());
}
}

View File

@ -16,21 +16,21 @@
package com.alibaba.nacos.config.server.service.transaction;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
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.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* when embeddedStorage==true and nacos.standalone=false
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public class ConditionOnEmbedStoreType implements Condition {
public class ConditionOnDistributedStore implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
boolean result = Objects.equals(context.getEnvironment().getProperty("embeddedDistributedStorage"), "true") &&
StringUtils.isBlank(context.getEnvironment().getProperty("spring.datasource.platform"));
return result;
return PropertyUtil.isEmbeddedStorage() && !ApplicationUtils.getStandaloneMode();
}
}

View File

@ -64,7 +64,7 @@ import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog;
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
@SuppressWarnings("all")
@Conditional(ConditionOnEmbedStoreType.class)
@Conditional(ConditionOnDistributedStore.class)
@Component
public class DistributedDatabaseOperateImpl extends LogProcessor4CP implements BaseDatabaseOperate, DatabaseOperate {

View File

@ -35,7 +35,7 @@ import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog;
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
@Primary
@Conditional(ConditionOnDefaultStoreType.class)
@Conditional(ConditionOnDefaultStore.class)
@Component
public class LocalDatabaseOperateImpl implements BaseDatabaseOperate, DatabaseOperate {

View File

@ -215,7 +215,7 @@ public class PaginationHelper<E> {
}
private boolean isDerby() {
return (ApplicationUtils.getStandaloneMode() && !PropertyUtil.isUseMysql()) ||
PropertyUtil.isEmbeddedDistributedStorage();
return (ApplicationUtils.getStandaloneMode() && !PropertyUtil.isUseExternalDB()) ||
PropertyUtil.isEmbeddedStorage();
}
}

View File

@ -19,8 +19,6 @@ import com.alibaba.nacos.core.utils.ApplicationUtils;
import org.slf4j.Logger;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
/**
* properties utils
@ -80,13 +78,11 @@ public class PropertyUtil implements ApplicationContextInitializer<ConfigurableA
/**
* 单机模式使用db
*/
private static boolean useMysql = false;
private static boolean useExternalDB = false;
/**
* 内嵌分布式存储
* 内嵌存储 value = ${nacos.standalone}
*/
private static boolean embeddedDistributedStorage = false;
private Environment env;
private static boolean embeddedStorage = ApplicationUtils.getStandaloneMode();
public static int getNotifyConnectTimeout() {
return notifyConnectTimeout;
@ -212,37 +208,37 @@ public class PropertyUtil implements ApplicationContextInitializer<ConfigurableA
return ApplicationUtils.getStandaloneMode();
}
public static boolean isUseMysql() {
return useMysql;
public static boolean isUseExternalDB() {
return useExternalDB;
}
public static void setUseMysql(boolean useMysql) {
PropertyUtil.useMysql = useMysql;
public static void setUseExternalDB(boolean useExternalDB) {
PropertyUtil.useExternalDB = useExternalDB;
}
public static boolean isEmbeddedDistributedStorage() {
return embeddedDistributedStorage;
public static boolean isEmbeddedStorage() {
return embeddedStorage;
}
public static void setEmbeddedDistributedStorage(boolean embeddedDistributedStorage) {
PropertyUtil.embeddedDistributedStorage = embeddedDistributedStorage && !isUseMysql();
public static void setEmbeddedStorage(boolean embeddedStorage) {
PropertyUtil.embeddedStorage = embeddedStorage && !isUseExternalDB();
}
public static boolean isEnableDistributedID() {
return !ApplicationUtils.getStandaloneMode() && isEmbeddedDistributedStorage();
return !ApplicationUtils.getStandaloneMode() && isEmbeddedStorage();
}
private void loadSetting() {
try {
setNotifyConnectTimeout(Integer.parseInt(env.getProperty("notifyConnectTimeout", "100")));
setNotifyConnectTimeout(Integer.parseInt(ApplicationUtils.getProperty("notifyConnectTimeout", "100")));
logger.info("notifyConnectTimeout:{}", notifyConnectTimeout);
setNotifySocketTimeout(Integer.parseInt(env.getProperty("notifySocketTimeout", "200")));
setNotifySocketTimeout(Integer.parseInt(ApplicationUtils.getProperty("notifySocketTimeout", "200")));
logger.info("notifySocketTimeout:{}", notifySocketTimeout);
setHealthCheck(Boolean.parseBoolean(env.getProperty("isHealthCheck", "true")));
setHealthCheck(Boolean.parseBoolean(ApplicationUtils.getProperty("isHealthCheck", "true")));
logger.info("isHealthCheck:{}", isHealthCheck);
setMaxHealthCheckFailCount(Integer.parseInt(env.getProperty("maxHealthCheckFailCount", "12")));
setMaxHealthCheckFailCount(Integer.parseInt(ApplicationUtils.getProperty("maxHealthCheckFailCount", "12")));
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);
// 容量管理
setManageCapacity(getBoolean("isManageCapacity", isManageCapacity));
@ -255,8 +251,8 @@ public class PropertyUtil implements ApplicationContextInitializer<ConfigurableA
setDefaultMaxAggrSize(getInt("defaultMaxAggrSize", defaultMaxAggrSize));
setCorrectUsageDelay(getInt("correctUsageDelay", correctUsageDelay));
setInitialExpansionPercent(getInt("initialExpansionPercent", initialExpansionPercent));
setUseMysql(getString("spring.datasource.platform", "").equals("mysql"));
setEmbeddedDistributedStorage(getBoolean("embeddedDistributedStorage", embeddedDistributedStorage));
setUseExternalDB(getString("spring.datasource.platform", "").equals("mysql"));
setEmbeddedStorage(getBoolean("embeddedStorage", embeddedStorage));
} catch (Exception 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) {
String value = env.getProperty(key);
String value = getProperty(key);
if (value == null) {
return defaultValue;
}
@ -280,16 +276,16 @@ public class PropertyUtil implements ApplicationContextInitializer<ConfigurableA
}
public String getProperty(String key) {
return env.getProperty(key);
return ApplicationUtils.getProperty(key);
}
public String getProperty(String key, String defaultValue) {
return env.getProperty(key, defaultValue);
return ApplicationUtils.getProperty(key, defaultValue);
}
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
env = configurableApplicationContext.getEnvironment();
ApplicationUtils.injectEnvironment(configurableApplicationContext.getEnvironment());
loadSetting();
}
}

View File

@ -17,7 +17,7 @@ package com.alibaba.nacos.console.controller;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -37,10 +37,10 @@ public class ServerStateController {
@GetMapping("/state")
public ResponseEntity serverState() {
Map<String,String> serverState = new HashMap<>(3);
serverState.put("standalone_mode",SystemUtils.STANDALONE_MODE ?
SystemUtils.STANDALONE_MODE_ALONE : SystemUtils.STANDALONE_MODE_CLUSTER);
serverState.put("standalone_mode", ApplicationUtils.getStandaloneMode() ?
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);
return ResponseEntity.ok().body(serverState);

View File

@ -133,8 +133,9 @@ nacos.core.auth.caching.enabled=true
nacos.istio.mcp.server.enabled=false
#*************** Embed Storage Related Configurations ***************#
### Whether to open embedded distributed storage in nacos cluster mode
embeddedDistributedStorage=true
### This value is true in stand-alone mode and false in cluster mode
### If this value is set to true in cluster mode, nacos's distributed storage engine is turned on
embeddedStorage=true
#*************** Consistency Related Configurations ***************#

View File

@ -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;
}
}
}

View File

@ -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);
}
}

View File

@ -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;
});
}
}

View File

@ -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);
}
}
}
}

View File

@ -16,17 +16,34 @@
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 java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Observable;
import java.util.Observer;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
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.Loggers;
import org.springframework.stereotype.Component;
/**
@ -34,19 +51,15 @@ import org.springframework.stereotype.Component;
*/
@SuppressWarnings("PMD.UndefineMagicConstantRule")
@Component
public class IdGeneratorManager {
private static final String ID_GENERATOR_TYPE = "nacos.core.id-generator.type";
public class IdGeneratorManager extends LogProcessor4CP {
private final Map<String, IdGenerator> generatorMap = new ConcurrentHashMap<>();
private final Function<String, IdGenerator> supplier;
private final String idType;
private CPProtocol cpProtocol;
private GetResponse emptyResponse = GetResponse.newBuilder().build();
private LogFuture emptyFuture = LogFuture.success(null);
public IdGeneratorManager() {
this.idType = ApplicationUtils
.getProperty(ID_GENERATOR_TYPE, "default");
this.supplier = s -> {
IdGenerator generator;
ServiceLoader<IdGenerator> loader = ServiceLoader.load(IdGenerator.class);
@ -54,11 +67,7 @@ public class IdGeneratorManager {
if (iterator.hasNext()) {
generator = iterator.next();
} else {
if (Objects.equals(idType, "snakeflower")) {
generator = new SnakeFlowerIdGenerator();
} else {
generator = new DefaultIdGenerator(s);
}
generator = new SnakeFlowerIdGenerator();
}
generator.init();
return generator;
@ -88,4 +97,48 @@ public class IdGeneratorManager {
"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();
}
}

View File

@ -17,7 +17,9 @@ package com.alibaba.nacos.core.distributed.id;
import com.alibaba.nacos.consistency.IdGenerator;
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.Map;
@ -28,123 +30,136 @@ import java.util.Map;
*/
public class SnakeFlowerIdGenerator implements IdGenerator {
/**
* Start time intercept (2018-08-05 08:34)
*/
private final long twepoch = 1533429269000L;
private final long workerIdBits = 5L;
private final long datacenterIdBits = 5L;
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
/**
* Start time intercept (2018-08-05 08:34)
*/
private static final long TWEPOCH = 1533429269000L;
private static final long WORKER_ID_BITS = 5L;
private static final long DATA_CENTER_ID_BITS = 5L;
public static final long MAX_WORKER_ID = ~(-1L << WORKER_ID_BITS);
public static final long MAX_DATA_CENTER_ID = ~(-1L << DATA_CENTER_ID_BITS);
private final long sequenceBits = 12L;
private final long workerIdShift = sequenceBits;
private final long datacenterIdShift = sequenceBits + workerIdBits;
private final long timestampLeftShift = sequenceBits + workerIdBits
+ datacenterIdBits;
private final long sequenceMask = -1L ^ (-1L << sequenceBits);
private volatile long currentId;
private long workerId;
private long datacenterId;
private long sequence = 0L;
private long lastTimestamp = -1L;
private static final long SEQUENCE_BITS = 12L;
private static final long SEQUENCE_BITS1 = SEQUENCE_BITS;
private static final long DATA_CENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS;
private static final long TIMESTAMP_LEFT_SHIFT =
SEQUENCE_BITS + WORKER_ID_BITS + DATA_CENTER_ID_BITS;
private static final long SEQUENCE_MASK = ~(-1L << SEQUENCE_BITS);
@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() {
// 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);
initialize(workerId, dataCenterId);
}
@Override
@Override
public long currentId() {
return currentId;
}
return currentId;
}
@Override
@Override
public synchronized long nextId() {
long timestamp = timeGen();
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new SnakflowerException(String.format(
"Clock moved backwards. Refusing to generate id for %d milliseconds",
lastTimestamp - timestamp));
}
if (timestamp < lastTimestamp) {
throw new SnakflowerException(String.format(
"Clock moved backwards. Refusing to generate id for %d milliseconds",
lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
}
else {
sequence = 0L;
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & SEQUENCE_MASK;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
}
else {
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)
| (datacenterId << datacenterIdShift)
| (workerId << workerIdShift)
| sequence;
return currentId;
}
@Override
@Override
public Map<Object, Object> info() {
Map<Object, Object> info = new HashMap<>(4);
info.put("currentId", currentId);
info.put("dataCenterId", datacenterId);
info.put("workerId", workerId);
return info;
}
Map<Object, Object> info = new HashMap<>(4);
info.put("currentId", currentId);
info.put("dataCenterId", dataCenterId);
info.put("workerId", workerId);
return info;
}
// ==============================Constructors=====================================
// ==============================Constructors=====================================
/**
* init
*
* @param workerId worker id (0~31)
* @param datacenterId data center id (0~31)
*/
public void initialize(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format(
"worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(
String.format("datacenter Id can't be greater than %d or less than 0",
maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
/**
* init
*
* @param workerId worker id (0~31)
* @param datacenterId data center id (0~31)
*/
public void initialize(long workerId, long datacenterId) {
if (workerId > MAX_WORKER_ID || workerId < 0) {
throw new IllegalArgumentException(
String.format("worker Id can't be greater than %d or less than 0",
MAX_WORKER_ID));
}
if (datacenterId > MAX_DATA_CENTER_ID || datacenterId < 0) {
throw new IllegalArgumentException(
String.format("datacenter Id can't be greater than %d or less than 0",
MAX_DATA_CENTER_ID));
}
SnakeFlowerIdGenerator.workerId = workerId;
SnakeFlowerIdGenerator.dataCenterId = datacenterId;
}
/**
* Block to the next millisecond until a new timestamp is obtained
*
* @param lastTimestamp 上次生成ID的时间截
* @return 当前时间戳
*/
protected long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
/**
* Block to the next millisecond until a new timestamp is obtained
*
* @param lastTimestamp 上次生成ID的时间截
* @return 当前时间戳
*/
protected long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
/**
* Returns the current time in milliseconds
*
* @return 当前时间(毫秒)
*/
protected long timeGen() {
return System.currentTimeMillis();
}
/**
* Returns the current time in milliseconds
*
* @return 当前时间(毫秒)
*/
protected long timeGen() {
return System.currentTimeMillis();
}
}

View File

@ -30,6 +30,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import com.alibaba.nacos.common.utils.IoUtils;
import com.sun.management.OperatingSystemMXBean;
@ -320,8 +321,8 @@ public class ApplicationUtils implements ApplicationContextInitializer<Configura
* Standalone mode or not
*/
public static boolean getStandaloneMode() {
if (isStandalone == null) {
isStandalone = getProperty(STANDALONE_MODE_PROPERTY_NAME, Boolean.class, false);
if (Objects.isNull(isStandalone)) {
isStandalone = Boolean.getBoolean(STANDALONE_MODE_PROPERTY_NAME);
}
return isStandalone;
}
@ -331,15 +332,15 @@ public class ApplicationUtils implements ApplicationContextInitializer<Configura
*/
public static String getFunctionMode() {
if (StringUtils.isEmpty(functionModeType)) {
functionModeType = getProperty(FUNCTION_MODE_PROPERTY_NAME);
functionModeType = System.getProperty(FUNCTION_MODE_PROPERTY_NAME);
}
return functionModeType;
}
public static String getNacosHome() {
String nacosHome = getProperty(NACOS_HOME_KEY);
String nacosHome = System.getProperty(NACOS_HOME_KEY);
if (StringUtils.isBlank(nacosHome)) {
nacosHome = System.getProperty("user.home") + File.separator + "nacos";
nacosHome = Paths.get(System.getProperty("user.home"), "nacos").toString();
}
return nacosHome;
}

View File

@ -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);
}
}

View File

@ -16,7 +16,7 @@
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.junit.Assert;
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;
/**
* {@link SystemUtils} Test
* {@link ApplicationUtils} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @since 0.2.2
@ -81,7 +81,7 @@ public class SystemUtilsTest {
@Test
public void testReadClusterConf() throws IOException {
FileUtils.forceMkdir(new File(SystemUtils.getConfFilePath()));
FileUtils.forceMkdir(new File(ApplicationUtils.getConfFilePath()));
String lineSeparator = System.getProperty("line.separator");
@ -90,8 +90,8 @@ public class SystemUtilsTest {
* #example
* 192.168.1.1:8848
*/
SystemUtils.writeClusterConf("#it is ip" + lineSeparator + "#example" + lineSeparator + "192.168.1.1:8848");
Assert.assertEquals(SystemUtils.readClusterConf().get(0), "192.168.1.1:8848");
ApplicationUtils.writeClusterConf("#it is ip" + lineSeparator + "#example" + lineSeparator + "192.168.1.1:8848");
Assert.assertEquals(ApplicationUtils.readClusterConf().get(0), "192.168.1.1:8848");
/*
* #it is ip
@ -99,10 +99,10 @@ public class SystemUtilsTest {
* # 192.168.1.1:8848
* 192.168.1.2:8848 # Instance A
*/
SystemUtils.writeClusterConf(
ApplicationUtils.writeClusterConf(
"#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");
List<String> instanceList = SystemUtils.readClusterConf();
List<String> instanceList = ApplicationUtils.readClusterConf();
Assert.assertEquals(instanceList.get(0), "192.168.1.2:8848");
Assert.assertEquals(instanceList.get(1), "192.168.1.3");
}

View File

@ -19,7 +19,7 @@ import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.core.cluster.Member;
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.transport.Serializer;
import com.alibaba.nacos.naming.consistency.ApplyAction;
@ -117,7 +117,7 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
}
public void load() throws Exception {
if (SystemUtils.STANDALONE_MODE) {
if (ApplicationUtils.getStandaloneMode()) {
initialized = true;
return;
}

View File

@ -19,6 +19,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.alibaba.nacos.naming.boot.RunningConfig;
import com.alibaba.nacos.naming.consistency.ApplyAction;
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.zip.GZIPOutputStream;
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
/**
* @author nacos
*/
@ -483,7 +482,7 @@ public class RaftCore {
public void sendBeat() throws IOException, InterruptedException {
RaftPeer local = peers.local();
if (local.state != RaftPeer.State.LEADER && !STANDALONE_MODE) {
if (local.state != RaftPeer.State.LEADER && !ApplicationUtils.getStandaloneMode()) {
return;
}

View File

@ -21,8 +21,6 @@ import com.alibaba.nacos.core.cluster.MemberChangeListener;
import com.alibaba.nacos.core.cluster.NodeChangeEvent;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
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.Loggers;
import com.alibaba.nacos.naming.misc.NetUtils;
@ -47,8 +45,6 @@ import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
/**
* @author nacos
*/
@ -78,7 +74,7 @@ public class RaftPeerSet implements MemberChangeListener {
}
public RaftPeer getLeader() {
if (STANDALONE_MODE) {
if (ApplicationUtils.getStandaloneMode()) {
return local();
}
return leader;
@ -104,7 +100,7 @@ public class RaftPeerSet implements MemberChangeListener {
}
public boolean isLeader(String ip) {
if (STANDALONE_MODE) {
if (ApplicationUtils.getStandaloneMode()) {
return true;
}
@ -209,7 +205,7 @@ public class RaftPeerSet implements MemberChangeListener {
public RaftPeer local() {
RaftPeer peer = peers.get(NetUtils.localServer());
if (peer == null && SystemUtils.STANDALONE_MODE) {
if (peer == null && ApplicationUtils.getStandaloneMode()) {
RaftPeer localPeer = new RaftPeer();
localPeer.ip = NetUtils.localServer();
localPeer.term.set(localTerm.get());

View File

@ -23,7 +23,7 @@ import com.alibaba.nacos.core.auth.ActionTypes;
import com.alibaba.nacos.core.auth.Secured;
import com.alibaba.nacos.core.cluster.NodeState;
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.consistency.persistent.raft.RaftCore;
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer;
@ -144,9 +144,9 @@ public class OperatorController {
result.put("raftNotifyTaskCount", raftCore.getNotifyTaskCount());
result.put("responsibleServiceCount", responsibleDomCount);
result.put("responsibleInstanceCount", responsibleIPCount);
result.put("cpu", SystemUtils.getCPU());
result.put("load", SystemUtils.getLoad());
result.put("mem", SystemUtils.getMem());
result.put("cpu", ApplicationUtils.getCPU());
result.put("load", ApplicationUtils.getLoad());
result.put("mem", ApplicationUtils.getMem());
return result;
}

View File

@ -19,7 +19,7 @@ import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.MemberChangeListener;
import com.alibaba.nacos.core.cluster.NodeChangeEvent;
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.NetUtils;
import com.alibaba.nacos.naming.misc.SwitchDomain;
@ -65,7 +65,7 @@ public class DistroMapper implements MemberChangeListener {
}
public boolean responsible(String serviceName) {
if (!switchDomain.isDistroEnabled() || SystemUtils.STANDALONE_MODE) {
if (!switchDomain.isDistroEnabled() || ApplicationUtils.getStandaloneMode()) {
return true;
}

View File

@ -23,6 +23,7 @@ import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker;
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.selector.Selector;
import com.alibaba.nacos.naming.selector.SelectorJsonAdapter;
@ -33,8 +34,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME;
/**
* @author nacos
* @author jifengnan
@ -117,7 +116,7 @@ public class UtilsAndCommons {
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+$";