From 9f8c19cafb607e001695bd581bccb2212dcdde48 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Thu, 2 Apr 2020 19:24:11 +0800 Subject: [PATCH] refactor: Change the distributed ID logic --- .../NacosConfigConfiguration.java | 6 +- .../server/controller/ConfigController.java | 2 +- .../controller/ConfigOpsController.java | 3 +- .../server/controller/ConfigServletInner.java | 13 +- .../server/controller/HealthController.java | 2 +- .../server/model/app/ApplicationInfo.java | 2 +- .../service/BasicDataSourceServiceImpl.java | 4 +- .../config/server/service/ConfigService.java | 21 +- .../nacos/config/server/service/DiskUtil.java | 29 +- .../server/service/DynamicDataSource.java | 23 +- .../service/LocalDataSourceServiceImpl.java | 31 +- .../capacity/GroupCapacityPersistService.java | 4 +- .../TenantCapacityPersistService.java | 4 +- .../server/service/dump/DumpService.java | 9 +- .../service/merge/MergeDatumService.java | 4 +- .../service/merge/MergeTaskProcessor.java | 2 +- .../service/notify/AsyncNotifyService.java | 2 +- .../service/notify/NotifyTaskProcessor.java | 2 +- .../service/trace/ConfigTraceService.java | 2 +- ...Type.java => ConditionOnDefaultStore.java} | 9 +- ....java => ConditionOnDistributedStore.java} | 12 +- .../DistributedDatabaseOperateImpl.java | 2 +- .../transaction/LocalDatabaseOperateImpl.java | 2 +- .../config/server/utils/PaginationHelper.java | 4 +- .../config/server/utils/PropertyUtil.java | 50 ++- .../controller/ServerStateController.java | 8 +- .../src/main/resources/application.properties | 5 +- .../nacos/core/distributed/id/AcquireId.java | 93 ------ .../distributed/id/AcquireIdException.java | 43 --- .../distributed/id/DefaultIdGenerator.java | 140 -------- .../core/distributed/id/DefaultIdStore.java | 311 ------------------ .../distributed/id/IdGeneratorManager.java | 79 ++++- .../id/SnakeFlowerIdGenerator.java | 221 +++++++------ .../nacos/core/utils/ApplicationUtils.java | 11 +- .../alibaba/nacos/core/utils/SystemUtils.java | 156 --------- .../nacos/core/util/SystemUtilsTest.java | 14 +- .../distro/DistroConsistencyServiceImpl.java | 4 +- .../consistency/persistent/raft/RaftCore.java | 5 +- .../persistent/raft/RaftPeerSet.java | 10 +- .../controllers/OperatorController.java | 8 +- .../nacos/naming/core/DistroMapper.java | 4 +- .../nacos/naming/misc/UtilsAndCommons.java | 5 +- 42 files changed, 329 insertions(+), 1032 deletions(-) rename config/src/main/java/com/alibaba/nacos/config/server/service/transaction/{ConditionOnDefaultStoreType.java => ConditionOnDefaultStore.java} (77%) rename config/src/main/java/com/alibaba/nacos/config/server/service/transaction/{ConditionOnEmbedStoreType.java => ConditionOnDistributedStore.java} (73%) delete mode 100644 core/src/main/java/com/alibaba/nacos/core/distributed/id/AcquireId.java delete mode 100644 core/src/main/java/com/alibaba/nacos/core/distributed/id/AcquireIdException.java delete mode 100644 core/src/main/java/com/alibaba/nacos/core/distributed/id/DefaultIdGenerator.java delete mode 100644 core/src/main/java/com/alibaba/nacos/core/distributed/id/DefaultIdStore.java delete mode 100644 core/src/main/java/com/alibaba/nacos/core/utils/SystemUtils.java diff --git a/config/src/main/java/com/alibaba/nacos/config/server/configuration/NacosConfigConfiguration.java b/config/src/main/java/com/alibaba/nacos/config/server/configuration/NacosConfigConfiguration.java index d2211f39f..968ba433b 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/configuration/NacosConfigConfiguration.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/configuration/NacosConfigConfiguration.java @@ -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 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(); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java index fab8c6dba..a148cfab6 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java @@ -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; /** * 软负载客户端发布数据专用控制器 diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java index 48cef9c14..27d055239 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java @@ -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 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)) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java index 4b0bfcc36..13e8852b4 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java @@ -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(); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/HealthController.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/HealthController.java index 336f8d4c6..938188e11 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/HealthController.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/HealthController.java @@ -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 diff --git a/config/src/main/java/com/alibaba/nacos/config/server/model/app/ApplicationInfo.java b/config/src/main/java/com/alibaba/nacos/config/server/model/app/ApplicationInfo.java index 50d1759fb..acaede85a 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/model/app/ApplicationInfo.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/model/app/ApplicationInfo.java @@ -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 diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/BasicDataSourceServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/BasicDataSourceServiceImpl.java index 5cc012cfe..3d4c79f00 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/BasicDataSourceServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/BasicDataSourceServiceImpl.java @@ -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) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java index 422503b8c..17eddb12e 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java @@ -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); } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/DiskUtil.java b/config/src/main/java/com/alibaba/nacos/config/server/service/DiskUtil.java index 5d0450f0e..a44d99406 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/DiskUtil.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/DiskUtil.java @@ -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; - /** * 磁盘操作工具类。 *

@@ -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 { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java b/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java index bc42aa9bb..6c755f390 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java @@ -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(); - } - } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java index e1244cc16..dad919ced 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java @@ -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 diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java index ef1b33054..401aaa756 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java @@ -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 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 { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java index 2c6eca6b3..4c389c254 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java @@ -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 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"; } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java index 2625e7fba..40d55bbbd 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java @@ -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 { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java index b0afff780..01d58ca59 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java @@ -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 { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeTaskProcessor.java b/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeTaskProcessor.java index d151a5a17..f3ae46383 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeTaskProcessor.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeTaskProcessor.java @@ -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 diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/notify/AsyncNotifyService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/notify/AsyncNotifyService.java index c453ee23a..1640bf38d 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/notify/AsyncNotifyService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/notify/AsyncNotifyService.java @@ -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 diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/notify/NotifyTaskProcessor.java b/config/src/main/java/com/alibaba/nacos/config/server/service/notify/NotifyTaskProcessor.java index c32ae4c6d..84412df63 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/notify/NotifyTaskProcessor.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/notify/NotifyTaskProcessor.java @@ -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,包括自己,加载新数据。 diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/trace/ConfigTraceService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/trace/ConfigTraceService.java index 1775cf893..f1df31e73 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/trace/ConfigTraceService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/trace/ConfigTraceService.java @@ -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 diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnDefaultStoreType.java b/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnDefaultStore.java similarity index 77% rename from config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnDefaultStoreType.java rename to config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnDefaultStore.java index c7a4fd458..023f0b9fb 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnDefaultStoreType.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnDefaultStore.java @@ -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 liaochuntao */ -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()); } } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnEmbedStoreType.java b/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnDistributedStore.java similarity index 73% rename from config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnEmbedStoreType.java rename to config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnDistributedStore.java index 2d6ce7c5d..1d9df42a8 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnEmbedStoreType.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/ConditionOnDistributedStore.java @@ -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 liaochuntao */ -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(); } } \ No newline at end of file diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/DistributedDatabaseOperateImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/DistributedDatabaseOperateImpl.java index 228140d53..6a4c7df71 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/DistributedDatabaseOperateImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/DistributedDatabaseOperateImpl.java @@ -64,7 +64,7 @@ import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog; * @author liaochuntao */ @SuppressWarnings("all") -@Conditional(ConditionOnEmbedStoreType.class) +@Conditional(ConditionOnDistributedStore.class) @Component public class DistributedDatabaseOperateImpl extends LogProcessor4CP implements BaseDatabaseOperate, DatabaseOperate { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/LocalDatabaseOperateImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/LocalDatabaseOperateImpl.java index 594b2f51c..2afb6a486 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/LocalDatabaseOperateImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/transaction/LocalDatabaseOperateImpl.java @@ -35,7 +35,7 @@ import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog; * @author liaochuntao */ @Primary -@Conditional(ConditionOnDefaultStoreType.class) +@Conditional(ConditionOnDefaultStore.class) @Component public class LocalDatabaseOperateImpl implements BaseDatabaseOperate, DatabaseOperate { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/PaginationHelper.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/PaginationHelper.java index cb250a072..41fab460b 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/utils/PaginationHelper.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/PaginationHelper.java @@ -215,7 +215,7 @@ public class PaginationHelper { } private boolean isDerby() { - return (ApplicationUtils.getStandaloneMode() && !PropertyUtil.isUseMysql()) || - PropertyUtil.isEmbeddedDistributedStorage(); + return (ApplicationUtils.getStandaloneMode() && !PropertyUtil.isUseExternalDB()) || + PropertyUtil.isEmbeddedStorage(); } } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/PropertyUtil.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/PropertyUtil.java index fd1743c3d..016c02b31 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/utils/PropertyUtil.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/PropertyUtil.java @@ -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 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); diff --git a/console/src/main/resources/application.properties b/console/src/main/resources/application.properties index b4f078979..f17cbe508 100644 --- a/console/src/main/resources/application.properties +++ b/console/src/main/resources/application.properties @@ -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 ***************# diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/id/AcquireId.java b/core/src/main/java/com/alibaba/nacos/core/distributed/id/AcquireId.java deleted file mode 100644 index 58c70801c..000000000 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/id/AcquireId.java +++ /dev/null @@ -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 liaochuntao - */ -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; - } - } -} diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/id/AcquireIdException.java b/core/src/main/java/com/alibaba/nacos/core/distributed/id/AcquireIdException.java deleted file mode 100644 index 08a738aa6..000000000 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/id/AcquireIdException.java +++ /dev/null @@ -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 liaochuntao - */ -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); - } -} diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/id/DefaultIdGenerator.java b/core/src/main/java/com/alibaba/nacos/core/distributed/id/DefaultIdGenerator.java deleted file mode 100644 index 73e8f92a6..000000000 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/id/DefaultIdGenerator.java +++ /dev/null @@ -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 liaochuntao - */ -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 info() { - Map 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; - }); - } -} diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/id/DefaultIdStore.java b/core/src/main/java/com/alibaba/nacos/core/distributed/id/DefaultIdStore.java deleted file mode 100644 index bdf6c7c05..000000000 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/id/DefaultIdStore.java +++ /dev/null @@ -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 liaochuntao - */ -@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 storeFileMap = new ConcurrentHashMap<>(4); - private Serializer serializer; - private List 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 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 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); - } - } - } -} diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/id/IdGeneratorManager.java b/core/src/main/java/com/alibaba/nacos/core/distributed/id/IdGeneratorManager.java index 47946519a..2632e29b7 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/id/IdGeneratorManager.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/id/IdGeneratorManager.java @@ -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 generatorMap = new ConcurrentHashMap<>(); - private final Function 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 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 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 loadSnapshotOperate() { + return Collections.emptyList(); + } } diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/id/SnakeFlowerIdGenerator.java b/core/src/main/java/com/alibaba/nacos/core/distributed/id/SnakeFlowerIdGenerator.java index e132bc36a..49c05ada1 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/id/SnakeFlowerIdGenerator.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/id/SnakeFlowerIdGenerator.java @@ -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 info() { - Map info = new HashMap<>(4); - info.put("currentId", currentId); - info.put("dataCenterId", datacenterId); - info.put("workerId", workerId); - return info; - } + Map 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(); + } } \ No newline at end of file diff --git a/core/src/main/java/com/alibaba/nacos/core/utils/ApplicationUtils.java b/core/src/main/java/com/alibaba/nacos/core/utils/ApplicationUtils.java index 64459e423..5d133be6d 100644 --- a/core/src/main/java/com/alibaba/nacos/core/utils/ApplicationUtils.java +++ b/core/src/main/java/com/alibaba/nacos/core/utils/ApplicationUtils.java @@ -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 getIPsBySystemEnv(String key) { - String env = getSystemEnv(key); - List 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 readClusterConf() throws IOException { - List instanceList = new ArrayList(); - try(Reader reader = new InputStreamReader(new FileInputStream(new File(CLUSTER_CONF_FILE_PATH)), - StandardCharsets.UTF_8)) { - List 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); - } - -} diff --git a/core/src/test/java/com/alibaba/nacos/core/util/SystemUtilsTest.java b/core/src/test/java/com/alibaba/nacos/core/util/SystemUtilsTest.java index a7285f479..85747a86e 100644 --- a/core/src/test/java/com/alibaba/nacos/core/util/SystemUtilsTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/util/SystemUtilsTest.java @@ -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 Mercy * @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 instanceList = SystemUtils.readClusterConf(); + List instanceList = ApplicationUtils.readClusterConf(); Assert.assertEquals(instanceList.get(0), "192.168.1.2:8848"); Assert.assertEquals(instanceList.get(1), "192.168.1.3"); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java index 1f544e328..586940032 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java @@ -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; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java index c77239d52..024ccc292 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java @@ -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; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java index 863363ea1..a937effc7 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java @@ -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()); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java index 7b3e17740..80a4a8e0e 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java @@ -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; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/DistroMapper.java b/naming/src/main/java/com/alibaba/nacos/naming/core/DistroMapper.java index 8d7206420..df6f4756b 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/DistroMapper.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/DistroMapper.java @@ -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; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java index f4f07539f..48d37d113 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java @@ -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+$";