From 29a6edc8c5b97ed9f77ab4a6072a780ec19f7be8 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Fri, 8 May 2020 14:53:29 +0800 Subject: [PATCH] refactor: fix bugs --- .../nacos/api/exception/NacosException.java | 13 +- .../nacos/common/file/WatchFileCenter.java | 8 +- .../nacos/common/utils/LoggerUtils.java | 32 ++++ .../exception/GlobalExceptionHandler.java | 6 +- .../server/filter/TransferToLeaderFilter.java | 8 +- .../server/service/DynamicDataSource.java | 4 +- .../service/LocalDataSourceServiceImpl.java | 4 +- .../server/service/dump/DumpService.java | 2 +- .../repository/BaseDatabaseOperate.java | 19 +-- .../DistributedDatabaseOperateImpl.java | 57 +++---- .../com/alibaba/nacos/consistency/Config.java | 14 -- .../consistency/ConsistencyProtocol.java | 10 +- .../nacos/consistency/ProtocolMetaData.java | 10 +- .../nacos/consistency/ap/APProtocol.java | 2 +- .../nacos/consistency/cp/CPProtocol.java | 2 +- .../exception/ConsoleExceptionHandler.java | 4 +- .../src/main/resources/application.properties | 4 +- .../alibaba/nacos/core/cluster/Member.java | 3 +- .../nacos/core/cluster/MemberUtils.java | 37 +++-- .../core/cluster/ServerMemberManager.java | 27 ++-- .../lookup/AddressServerMemberLookup.java | 7 +- .../cluster/lookup/DiscoveryMemberLookup.java | 12 +- .../lookup/FileConfigMemberLookup.java | 4 +- .../StartingSpringApplicationRunListener.java | 23 ++- .../controller/NacosClusterController.java | 5 +- .../AbstractConsistencyProtocol.java | 2 +- .../core/distributed/ProtocolManager.java | 21 +-- .../nacos/core/distributed/raft/JRaftOps.java | 2 +- .../core/distributed/raft/JRaftProtocol.java | 16 +- .../core/distributed/raft/JRaftServer.java | 12 +- .../distributed/raft/NacosStateMachine.java | 9 +- .../core/distributed/raft/RaftConfig.java | 11 -- .../nacos/core/notify/NotifyCenter.java | 2 +- .../nacos/core/utils/ApplicationUtils.java | 4 + .../nacos/core/utils/TimerContext.java | 3 +- .../main/resources/META-INF/logback/nacos.xml | 7 +- distribution/conf/nacos-logback.xml | 2 +- .../exception/ResponseExceptionHandler.java | 10 +- .../test/common/WatchFileCenter_ITCase.java | 5 +- .../test/config/ConfigDerbyRaft_DITCase.java | 8 +- .../ConfigLongPollReturnChanges_ITCase.java | 6 +- .../test/config/DerbyRaftError_ITCase.java | 144 ------------------ .../nacos/test/core/InetUtils_ITCase.java | 3 +- .../cluster/ServerMemberManager_ITCase.java | 3 +- .../test/core/notify/NotifyCenter_ITCase.java | 4 +- .../src/test/resources/application.properties | 8 +- 46 files changed, 256 insertions(+), 343 deletions(-) create mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/LoggerUtils.java delete mode 100644 test/src/test/java/com/alibaba/nacos/test/config/DerbyRaftError_ITCase.java diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java index 68c7cf536..ac3f7ec3a 100644 --- a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java +++ b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java @@ -15,8 +15,8 @@ */ package com.alibaba.nacos.api.exception; +import com.alibaba.nacos.api.common.Constants; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.exception.ExceptionUtils; /** * Nacos Exception @@ -63,16 +63,13 @@ public class NacosException extends Exception { } public String getErrMsg() { - StringBuilder builder = new StringBuilder(); if (!StringUtils.isBlank(this.errMsg)) { - builder.append("errMsg:").append(errMsg); + return errMsg; } if (this.causeThrowable != null) { - builder.append("causeThrowable:[").append(causeThrowable.getClass().getName()) - .append("]") - .append(causeThrowable.getMessage()); + return causeThrowable.getMessage(); } - return builder.toString(); + return Constants.NULL; } public void setErrCode(int errCode) { @@ -127,7 +124,7 @@ public class NacosException extends Exception { */ public static final int NO_RIGHT = 403; /** - * not found + * not found */ public static final int NOT_FOUND = 404; /** diff --git a/common/src/main/java/com/alibaba/nacos/common/file/WatchFileCenter.java b/common/src/main/java/com/alibaba/nacos/common/file/WatchFileCenter.java index 7fc0cfa48..b056ac4d5 100644 --- a/common/src/main/java/com/alibaba/nacos/common/file/WatchFileCenter.java +++ b/common/src/main/java/com/alibaba/nacos/common/file/WatchFileCenter.java @@ -111,17 +111,17 @@ public class WatchFileCenter { if (!CLOSED.compareAndSet(false, true)) { return; } - LOGGER.warn("WatchFileCenter start close"); + LOGGER.warn("[WatchFileCenter] start close"); for (Map.Entry entry : MANAGER.entrySet()) { - LOGGER.warn("start to shutdown this watcher which is watch : " + entry.getKey()); + LOGGER.warn("[WatchFileCenter] start to shutdown this watcher which is watch : " + entry.getKey()); try { entry.getValue().shutdown(); } catch (Throwable e) { - LOGGER.error("WatchFileCenter shutdown has error : {}", e); + LOGGER.error("[WatchFileCenter] shutdown has error : {}", e); } } MANAGER.clear(); - LOGGER.warn("WatchFileCenter already closed"); + LOGGER.warn("[WatchFileCenter] already closed"); } public synchronized static boolean deregisterWatcher(final String path, final FileWatcher watcher) { diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/LoggerUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/LoggerUtils.java new file mode 100644 index 000000000..1546cb62f --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/utils/LoggerUtils.java @@ -0,0 +1,32 @@ +/* + * 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.common.utils; + +import org.slf4j.Logger; + +/** + * @author liaochuntao + */ +public final class LoggerUtils { + + public static void printIfDebugEnabled(Logger logger, String s, Object... args) { + if (logger.isDebugEnabled()) { + logger.debug(s, args); + } + } + +} diff --git a/config/src/main/java/com/alibaba/nacos/config/server/exception/GlobalExceptionHandler.java b/config/src/main/java/com/alibaba/nacos/config/server/exception/GlobalExceptionHandler.java index 0b8c6f1f0..33eb71990 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/exception/GlobalExceptionHandler.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/exception/GlobalExceptionHandler.java @@ -42,7 +42,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(IllegalArgumentException.class) public ResponseEntity handleIllegalArgumentException(Exception ex) throws IOException { MetricsMonitor.getIllegalArgumentException().increment(); - return ResponseEntity.status(400).body(ExceptionUtil.getStackTrace(ex)); + return ResponseEntity.status(400).body(ExceptionUtil.getAllExceptionMsg(ex)); } /** @@ -53,7 +53,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(NacosException.class) public ResponseEntity handleNacosException(NacosException ex) throws IOException { MetricsMonitor.getNacosException().increment(); - return ResponseEntity.status(ex.getErrCode()).body(ExceptionUtil.getStackTrace(ex)); + return ResponseEntity.status(ex.getErrCode()).body(ExceptionUtil.getAllExceptionMsg(ex)); } /** @@ -64,7 +64,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(DataAccessException.class) public ResponseEntity handleDataAccessException(DataAccessException ex) throws DataAccessException { MetricsMonitor.getDbException().increment(); - return ResponseEntity.status(500).body(ExceptionUtil.getStackTrace(ex)); + return ResponseEntity.status(500).body(ExceptionUtil.getAllExceptionMsg(ex)); } } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/filter/TransferToLeaderFilter.java b/config/src/main/java/com/alibaba/nacos/config/server/filter/TransferToLeaderFilter.java index f38c5df6a..41fcc2994 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/filter/TransferToLeaderFilter.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/filter/TransferToLeaderFilter.java @@ -43,7 +43,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; @@ -58,7 +57,6 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Method; import java.net.URI; -import java.nio.charset.StandardCharsets; import java.security.AccessControlException; import java.util.Enumeration; import java.util.Map; @@ -137,7 +135,7 @@ public class TransferToLeaderFilter implements Filter { boolean isLeader = protocol.isLeader(Constants.CONFIG_MODEL_RAFT_GROUP); if (downgrading && isLeader) { - resp.sendError(HttpStatus.LOCKED.value(), + resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Unable to process the request at this time: System triggered degradation"); return; } @@ -145,7 +143,7 @@ public class TransferToLeaderFilter implements Filter { if (downgrading || (method.isAnnotationPresent(ToLeader.class) && !isLeader)) { if (StringUtils.isBlank(leaderServer)) { - resp.sendError(HttpStatus.LOCKED.value(), + resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Unable to process the request at this time: no Leader"); return; } @@ -187,7 +185,7 @@ public class TransferToLeaderFilter implements Filter { } catch (AccessControlException e) { resp.sendError(HttpServletResponse.SC_FORBIDDEN, - "access denied: " + ExceptionUtil.getStackTrace(e)); + "access denied: " + ExceptionUtil.getAllExceptionMsg(e)); return; } catch (NoSuchMethodException e) { 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 6c755f390..7bb21282e 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 @@ -24,8 +24,8 @@ import com.alibaba.nacos.config.server.utils.PropertyUtil; */ public class DynamicDataSource { - private volatile DataSourceService localDataSourceService = null; - private volatile DataSourceService basicDataSourceService = null; + private DataSourceService localDataSourceService = null; + private DataSourceService basicDataSourceService = null; private static final DynamicDataSource INSTANCE = new DynamicDataSource(); 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 0edbdaec4..a31e33fe2 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 @@ -109,10 +109,8 @@ public class LocalDataSourceServiceImpl implements DataSourceService { try { DriverManager.getConnection("jdbc:derby:;shutdown=true"); } catch (Exception e) { - // An error is thrown when the Derby shutdown is executed, which should be ignored - - if (!StringUtils.contains(e.getMessage().toLowerCase(), derbyShutdownErrMsg.toLowerCase())) { + if (!StringUtils.containsIgnoreCase(e.getMessage(), derbyShutdownErrMsg)) { throw e; } } 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 7e31bffb2..75e758da6 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 @@ -115,7 +115,7 @@ public class DumpService { dumpOperate(processor, dumpAllProcessor, dumpAllBetaProcessor, dumpAllTagProcessor); }); protocol.protocolMetaData() - .ubSubscribe(Constants.CONFIG_MODEL_RAFT_GROUP, + .unSubscribe(Constants.CONFIG_MODEL_RAFT_GROUP, com.alibaba.nacos.consistency.cp.Constants.LEADER_META_DATA, this); } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/BaseDatabaseOperate.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/BaseDatabaseOperate.java index 552b7be4b..e4f3d2dbd 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/BaseDatabaseOperate.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/BaseDatabaseOperate.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.config.server.service.repository; +import com.alibaba.nacos.common.utils.LoggerUtils; import com.alibaba.nacos.config.server.service.sql.ModifyRequest; import com.alibaba.nacos.config.server.utils.LogUtil; import com.alibaba.nacos.common.utils.ExceptionUtil; @@ -58,7 +59,7 @@ public interface BaseDatabaseOperate { } catch (IncorrectResultSizeDataAccessException e) { return null; } catch (CannotGetJdbcConnectionException e) { - fatalLog.error("[db-error] " + e.toString(), e); + fatalLog.error("[db-error] {}", e.toString()); throw e; } catch (DataAccessException e) { fatalLog.error("[db-error] DataAccessException sql : {}, args : {}, error : {}", @@ -76,7 +77,7 @@ public interface BaseDatabaseOperate { } catch (IncorrectResultSizeDataAccessException e) { return null; } catch (CannotGetJdbcConnectionException e) { - fatalLog.error("[db-error] " + e.toString(), e); + fatalLog.error("[db-error] {}", e.toString()); throw e; } catch (DataAccessException e) { fatalLog.error("[db-error] DataAccessException sql : {}, args : {}, error : {}", @@ -92,7 +93,7 @@ public interface BaseDatabaseOperate { try { return jdbcTemplate.query(sql, args, mapper); } catch (CannotGetJdbcConnectionException e) { - fatalLog.error("[db-error] " + e.toString(), e); + fatalLog.error("[db-error] {}", e.toString()); throw e; } catch (DataAccessException e) { fatalLog.error("[db-error] DataAccessException sql : {}, args : {}, error : {}", @@ -112,7 +113,7 @@ public interface BaseDatabaseOperate { return null; } catch (CannotGetJdbcConnectionException e) { - fatalLog.error("[db-error] " + e.toString(), e); + fatalLog.error("[db-error] {}", e.toString()); throw e; } catch (DataAccessException e) { fatalLog.error("[db-error] DataAccessException sql : {}, args : {}, error : {}", @@ -128,7 +129,7 @@ public interface BaseDatabaseOperate { try { return jdbcTemplate.queryForList(sql, args); } catch (CannotGetJdbcConnectionException e) { - fatalLog.error("[db-error] " + e.toString(), e); + fatalLog.error("[db-error] {}", e.toString()); throw e; } catch (DataAccessException e) { fatalLog.error("[db-error] DataAccessException sql : {}, args : {}, error : {}", @@ -147,18 +148,18 @@ public interface BaseDatabaseOperate { contexts.forEach(pair -> { errSql[0] = pair.getSql(); args[0] = pair.getArgs(); - LogUtil.defaultLog.debug("current sql : {}", errSql[0]); - LogUtil.defaultLog.debug("current args : {}", args[0]); + LoggerUtils.printIfDebugEnabled(LogUtil.defaultLog, "current sql : {}", errSql[0]); + LoggerUtils.printIfDebugEnabled(LogUtil.defaultLog, "current args : {}", args[0]); jdbcTemplate.update(pair.getSql(), pair.getArgs()); }); return Boolean.TRUE; } catch (BadSqlGrammarException | DataIntegrityViolationException e) { - fatalLog.error("[db-error] sql : {}, args : {}, error : {}", errSql[0], args[0], e); + fatalLog.error("[db-error] sql : {}, args : {}, error : {}", errSql[0], args[0], e.toString()); return false; } catch (CannotGetJdbcConnectionException e) { - fatalLog.error("[db-error] sql : {}, args : {}, error : {}", errSql[0], args[0], e); + fatalLog.error("[db-error] sql : {}, args : {}, error : {}", errSql[0], args[0], e.toString()); throw e; } catch (DataAccessException e) { fatalLog.error("[db-error] DataAccessException sql : {}, args : {}, error : {}", diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/DistributedDatabaseOperateImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/DistributedDatabaseOperateImpl.java index 4ff042f27..ad9b6640c 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/DistributedDatabaseOperateImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/DistributedDatabaseOperateImpl.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.config.server.service.repository; import com.alibaba.nacos.common.JustForTest; +import com.alibaba.nacos.common.utils.LoggerUtils; import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.common.utils.Observable; import com.alibaba.nacos.common.utils.Observer; @@ -47,6 +48,7 @@ import com.alibaba.nacos.core.distributed.id.SnakeFlowerIdGenerator; import com.alibaba.nacos.core.notify.Event; import com.alibaba.nacos.core.notify.NotifyCenter; import com.alibaba.nacos.core.notify.listener.Subscribe; +import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.core.utils.ClassUtils; import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; @@ -128,12 +130,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; public class DistributedDatabaseOperateImpl extends LogProcessor4CP implements BaseDatabaseOperate, DatabaseOperate { - @Autowired private ServerMemberManager memberManager; - - @Autowired - private ProtocolManager protocolManager; - private CPProtocol protocol; private LocalDataSourceServiceImpl dataSourceService; @@ -144,9 +141,18 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP private ReentrantReadWriteLock.ReadLock readLock = lock.readLock(); private ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock(); - @PostConstruct - protected void init() throws Exception { + public DistributedDatabaseOperateImpl(ServerMemberManager memberManager, + ProtocolManager protocolManager) throws Exception { + this.memberManager = memberManager; this.protocol = protocolManager.getCpProtocol(); + + init(); + + this.protocol.addLogProcessors(Collections.singletonList(this)); + } + + protected void init() throws Exception { + this.dataSourceService = (LocalDataSourceServiceImpl) DynamicDataSource.getInstance() .getDataSource(); @@ -183,7 +189,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP @Override public R queryOne(String sql, Class cls) { try { - LogUtil.defaultLog.debug("queryOne info : sql : {}", sql); + LoggerUtils.printIfDebugEnabled(LogUtil.defaultLog, "queryOne info : sql : {}", sql); byte[] data = serializer.serialize(SelectRequest.builder() .queryType(QueryType.QUERY_ONE_NO_MAPPER_NO_ARGS).sql(sql) @@ -199,7 +205,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP } catch (Exception e) { LogUtil.fatalLog - .error("An exception occurred during the query operation : {}", e); + .error("An exception occurred during the query operation : {}", e.toString()); throw new NJdbcException(e); } } @@ -207,7 +213,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP @Override public R queryOne(String sql, Object[] args, Class cls) { try { - LogUtil.defaultLog.debug("queryOne info : sql : {}, args : {}", sql, args); + LoggerUtils.printIfDebugEnabled(LogUtil.defaultLog, "queryOne info : sql : {}, args : {}", sql, args); byte[] data = serializer.serialize(SelectRequest.builder() .queryType(QueryType.QUERY_ONE_NO_MAPPER_WITH_ARGS).sql(sql) @@ -223,7 +229,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP } catch (Exception e) { LogUtil.fatalLog - .error("An exception occurred during the query operation : {}", e); + .error("An exception occurred during the query operation : {}", e.toString()); throw new NJdbcException(e); } } @@ -231,8 +237,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP @Override public R queryOne(String sql, Object[] args, RowMapper mapper) { try { - - LogUtil.defaultLog.debug("queryOne info : sql : {}, args : {}", sql, args); + LoggerUtils.printIfDebugEnabled(LogUtil.defaultLog, "queryOne info : sql : {}, args : {}", sql, args); byte[] data = serializer.serialize(SelectRequest.builder() .queryType(QueryType.QUERY_ONE_WITH_MAPPER_WITH_ARGS).sql(sql) @@ -249,7 +254,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP } catch (Exception e) { LogUtil.fatalLog - .error("An exception occurred during the query operation : {}", e); + .error("An exception occurred during the query operation : {}", e.toString()); throw new NJdbcException(e); } } @@ -257,7 +262,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP @Override public List queryMany(String sql, Object[] args, RowMapper mapper) { try { - LogUtil.defaultLog.debug("queryMany info : sql : {}, args : {}", sql, args); + LoggerUtils.printIfDebugEnabled(LogUtil.defaultLog, "queryMany info : sql : {}, args : {}", sql, args); byte[] data = serializer.serialize(SelectRequest.builder() .queryType(QueryType.QUERY_MANY_WITH_MAPPER_WITH_ARGS).sql(sql) @@ -274,7 +279,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP } catch (Exception e) { LogUtil.fatalLog - .error("An exception occurred during the query operation : {}", e); + .error("An exception occurred during the query operation : {}", e.toString()); throw new NJdbcException(e); } } @@ -282,7 +287,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP @Override public List queryMany(String sql, Object[] args, Class rClass) { try { - LogUtil.defaultLog.debug("queryMany info : sql : {}, args : {}", sql, args); + LoggerUtils.printIfDebugEnabled(LogUtil.defaultLog, "queryMany info : sql : {}, args : {}", sql, args); byte[] data = serializer.serialize(SelectRequest.builder() .queryType(QueryType.QUERY_MANY_NO_MAPPER_WITH_ARGS).sql(sql) @@ -298,7 +303,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP } catch (Exception e) { LogUtil.fatalLog - .error("An exception occurred during the query operation : {}", e); + .error("An exception occurred during the query operation : {}", e.toString()); throw new NJdbcException(e); } } @@ -306,8 +311,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP @Override public List> queryMany(String sql, Object[] args) { try { - - LogUtil.defaultLog.debug("queryMany info : sql : {}, args : {}", sql, args); + LoggerUtils.printIfDebugEnabled(LogUtil.defaultLog, "queryMany info : sql : {}, args : {}", sql, args); byte[] data = serializer.serialize(SelectRequest.builder() .queryType(QueryType.QUERY_MANY_WITH_LIST_WITH_ARGS).sql(sql) @@ -324,7 +328,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP } catch (Exception e) { LogUtil.fatalLog - .error("An exception occurred during the query operation : {}", e); + .error("An exception occurred during the query operation : {}", e.toString()); throw new NJdbcException(e); } } @@ -337,7 +341,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP // array elements are not lost, the serialization here is done using the java-specific // serialization framework, rather than continuing with the protobuff - LogUtil.defaultLog.debug("modifyRequests info : {}", sqlContext); + LoggerUtils.printIfDebugEnabled(LogUtil.defaultLog, "modifyRequests info : {}", sqlContext); // {timestamp}-{group}-{ip:port}-{signature} @@ -358,7 +362,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP throw (ConsistencyException) e; } LogUtil.fatalLog - .error("An exception occurred during the update operation : {}", e); + .error("An exception occurred during the update operation : {}", e.toString()); throw new NJdbcException(e); } } @@ -374,7 +378,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP final SelectRequest selectRequest = serializer .deserialize(request.getData().toByteArray(), SelectRequest.class); - LogUtil.defaultLog.debug("getData info : selectRequest : {}", selectRequest); + LoggerUtils.printIfDebugEnabled(LogUtil.defaultLog, "getData info : selectRequest : {}", selectRequest); final RowMapper mapper = RowMapperManager .getRowMapper(selectRequest.getClassName()); @@ -417,7 +421,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP catch (Exception e) { LogUtil.fatalLog .error("There was an error querying the data, request : {}, error : {}", - selectRequest, e); + selectRequest, e.toString()); return GetResponse.newBuilder() .setErrMsg(e.getClass().getSimpleName() + ":" + e.getMessage()) .build(); @@ -429,8 +433,7 @@ public class DistributedDatabaseOperateImpl extends LogProcessor4CP @Override public LogFuture onApply(Log log) { - - LogUtil.defaultLog.debug("onApply info : log : {}", log); + LoggerUtils.printIfDebugEnabled(LogUtil.defaultLog, "onApply info : log : {}", log); final ByteString byteString = log.getData(); Preconditions.checkArgument(byteString != null, "Log.getData() must not null"); diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/Config.java b/consistency/src/main/java/com/alibaba/nacos/consistency/Config.java index 984557bff..ca48615ae 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/Config.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/Config.java @@ -96,18 +96,4 @@ public interface Config extends Serializable { */ String getValOfDefault(String key, String defaultVal); - /** - * get LogProcessors - * - * @return {@link List} - */ - List listLogProcessor(); - - /** - * add {@link LogProcessor} processor - * - * @param processors {@link LogProcessor} array - */ - void addLogProcessors(Collection processors); - } diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/ConsistencyProtocol.java b/consistency/src/main/java/com/alibaba/nacos/consistency/ConsistencyProtocol.java index a199c9637..f26f270ca 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/ConsistencyProtocol.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/ConsistencyProtocol.java @@ -21,6 +21,7 @@ import com.alibaba.nacos.consistency.entity.GetRequest; import com.alibaba.nacos.consistency.entity.GetResponse; import com.alibaba.nacos.consistency.entity.Log; +import java.util.Collection; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -38,7 +39,7 @@ import java.util.concurrent.CompletableFuture; * * @author liaochuntao */ -public interface ConsistencyProtocol extends CommandOperations { +public interface ConsistencyProtocol extends CommandOperations { /** * Consistency protocol initialization: perform initialization operations based on the incoming Config @@ -48,6 +49,13 @@ public interface ConsistencyProtocol extends CommandOperations */ void init(T config); + /** + * Add a log handler + * + * @param processors {@link LogProcessor} + */ + void addLogProcessors(Collection

processors); + /** * Copy of metadata information for this consensus protocol * 该一致性协议的元数据信息 diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/ProtocolMetaData.java b/consistency/src/main/java/com/alibaba/nacos/consistency/ProtocolMetaData.java index 4f57fef5f..aa928cfc9 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/ProtocolMetaData.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/ProtocolMetaData.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.consistency; import com.alibaba.nacos.common.utils.Observable; import com.alibaba.nacos.common.utils.Observer; +import com.alibaba.nacos.common.utils.StringUtils; import org.javatuples.Pair; import java.util.Map; @@ -64,13 +65,12 @@ public final class ProtocolMetaData { }); } - public Object get(String group, String... subKey) { - if (subKey == null || subKey.length == 0) { + public Object get(String group, String subKey) { + if (StringUtils.isBlank(subKey)) { return metaDataMap.get(group); } else { - final String key = subKey[0]; if (metaDataMap.containsKey(group)) { - return metaDataMap.get(group).get(key); + return metaDataMap.get(group).get(subKey); } return null; } @@ -84,7 +84,7 @@ public final class ProtocolMetaData { .subscribe(key, observer); } - public void ubSubscribe(final String group, final String key, final Observer observer) { + public void unSubscribe(final String group, final String key, final Observer observer) { metaDataMap.computeIfAbsent(group, s -> new MetaData(group)); metaDataMap.get(group) .unSubscribe(key, observer); diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/ap/APProtocol.java b/consistency/src/main/java/com/alibaba/nacos/consistency/ap/APProtocol.java index fa7f440d4..336df6a6b 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/ap/APProtocol.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/ap/APProtocol.java @@ -23,6 +23,6 @@ import com.alibaba.nacos.consistency.ConsistencyProtocol; * @author liaochuntao */ @SuppressWarnings("all") -public interface APProtocol extends ConsistencyProtocol { +public interface APProtocol extends ConsistencyProtocol { } diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/cp/CPProtocol.java b/consistency/src/main/java/com/alibaba/nacos/consistency/cp/CPProtocol.java index b088a92aa..9d7a351df 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/cp/CPProtocol.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/cp/CPProtocol.java @@ -23,7 +23,7 @@ import com.alibaba.nacos.consistency.ConsistencyProtocol; * @author liaochuntao */ @SuppressWarnings("all") -public interface CPProtocol extends ConsistencyProtocol { +public interface CPProtocol extends ConsistencyProtocol { /** * Returns whether this node is a leader node diff --git a/console/src/main/java/com/alibaba/nacos/console/exception/ConsoleExceptionHandler.java b/console/src/main/java/com/alibaba/nacos/console/exception/ConsoleExceptionHandler.java index 6d6901213..4a8b86abc 100644 --- a/console/src/main/java/com/alibaba/nacos/console/exception/ConsoleExceptionHandler.java +++ b/console/src/main/java/com/alibaba/nacos/console/exception/ConsoleExceptionHandler.java @@ -42,12 +42,12 @@ public class ConsoleExceptionHandler { @ExceptionHandler(IllegalArgumentException.class) private ResponseEntity handleIllegalArgumentException(IllegalArgumentException e) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ExceptionUtil.getStackTrace(e)); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ExceptionUtil.getAllExceptionMsg(e)); } @ExceptionHandler(Exception.class) private ResponseEntity handleException(Exception e) { logger.error("CONSOLE", e); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ExceptionUtil.getStackTrace(e)); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ExceptionUtil.getAllExceptionMsg(e)); } } diff --git a/console/src/main/resources/application.properties b/console/src/main/resources/application.properties index 8f09474f0..caff96a01 100644 --- a/console/src/main/resources/application.properties +++ b/console/src/main/resources/application.properties @@ -21,8 +21,8 @@ server.port=8848 ### Connect URL of DB: # db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC -# db.user=nacos -# db.password=nacos +# db.user=root +# db.password=1017 #*************** Naming Module Related Configurations ***************# diff --git a/core/src/main/java/com/alibaba/nacos/core/cluster/Member.java b/core/src/main/java/com/alibaba/nacos/core/cluster/Member.java index fb26d35ef..a0d9760a7 100644 --- a/core/src/main/java/com/alibaba/nacos/core/cluster/Member.java +++ b/core/src/main/java/com/alibaba/nacos/core/cluster/Member.java @@ -135,8 +135,7 @@ public class Member implements Comparable, Cloneable { @Override public String toString() { - return "Member{" + "ip='" + ip + '\'' + ", port=" + port + ", state=" + state - + ", extendInfo=" + new TreeMap<>(extendInfo) + '}'; + return "Member{" + "address='" + getAddress() + '\'' + '}'; } @Override diff --git a/core/src/main/java/com/alibaba/nacos/core/cluster/MemberUtils.java b/core/src/main/java/com/alibaba/nacos/core/cluster/MemberUtils.java index b9f6c140b..a9aa1933e 100644 --- a/core/src/main/java/com/alibaba/nacos/core/cluster/MemberUtils.java +++ b/core/src/main/java/com/alibaba/nacos/core/cluster/MemberUtils.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.core.cluster; +import com.alibaba.nacos.common.utils.ExceptionUtil; import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.core.utils.Loggers; import org.apache.commons.lang3.StringUtils; @@ -36,6 +37,7 @@ import java.util.function.Predicate; public class MemberUtils { private static final String SEMICOLON = ":"; + private static final String TARGET_MEMBER_CONNECT_REFUSE_ERRMSG = "Connection refused"; private static ServerMemberManager manager; @@ -65,13 +67,20 @@ public class MemberUtils { port = Integer.parseInt(info[1]); } - int raftPort = port - 1000; + Member target = Member.builder().ip(address).port(port) + .state(NodeState.UP).build(); - Map extendInfo = new HashMap<>(4); + int raftPort = calculateRaftPort(target); + + Map extendInfo = new HashMap<>(4); // The Raft Port information needs to be set by default extendInfo.put(MemberMetaDataConstants.RAFT_PORT, String.valueOf(raftPort)); - return Member.builder().ip(address).port(port).extendInfo(extendInfo) - .state(NodeState.UP).build(); + target.setExtendInfo(extendInfo); + return target; + } + + public static int calculateRaftPort(Member member) { + return member.getPort() - 1000; } public static Collection multiParse(Collection addresses) { @@ -91,12 +100,19 @@ public class MemberUtils { } public static void onFail(Member member) { + onFail(member, null); + } + + public static void onFail(Member member, Throwable ex) { manager.getMemberAddressInfos().remove(member.getAddress()); member.setState(NodeState.SUSPICIOUS); member.setFailAccessCnt(member.getFailAccessCnt() + 1); int maxFailAccessCnt = ApplicationUtils .getProperty("nacos.core.member.fail-access-cnt", Integer.class, 3); - if (member.getFailAccessCnt() > maxFailAccessCnt) { + + // If the number of consecutive failures to access the target node reaches + // a maximum, or the link request is rejected, the state is directly down + if (member.getFailAccessCnt() > maxFailAccessCnt || StringUtils.containsIgnoreCase(ex.getMessage(), TARGET_MEMBER_CONNECT_REFUSE_ERRMSG)) { member.setState(NodeState.DOWN); } manager.update(member); @@ -112,21 +128,22 @@ public class MemberUtils { ApplicationUtils.writeClusterConf(builder.toString()); } catch (Throwable ex) { - Loggers.CLUSTER.error("cluster member node persistence failed : {}", ex); + Loggers.CLUSTER.error("cluster member node persistence failed : {}", + ExceptionUtil.getAllExceptionMsg(ex)); } } @SuppressWarnings("PMD.UndefineMagicConstantRule") - public static List kRandom(Collection members, + public static Collection kRandom(Collection members, Predicate filter) { int k = ApplicationUtils .getProperty("nacos.core.member.report.random-num", Integer.class, 3); - List tmp = new ArrayList<>(); - // Here thinking similar consul gossip protocols random k node + Set tmp = new HashSet<>(); + // Here thinking similar consul gossip protocols random k node int totalSize = members.size(); - for (int i = 0; i < 3 * totalSize && members.size() <= k; i++) { + for (int i = 0; i < 3 * totalSize && tmp.size() <= k; i++) { for (Member member : members) { if (filter.test(member)) { tmp.add(member); diff --git a/core/src/main/java/com/alibaba/nacos/core/cluster/ServerMemberManager.java b/core/src/main/java/com/alibaba/nacos/core/cluster/ServerMemberManager.java index 25126e15b..3dae704cd 100644 --- a/core/src/main/java/com/alibaba/nacos/core/cluster/ServerMemberManager.java +++ b/core/src/main/java/com/alibaba/nacos/core/cluster/ServerMemberManager.java @@ -26,6 +26,7 @@ import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.ConcurrentHashSet; +import com.alibaba.nacos.common.utils.ExceptionUtil; import com.alibaba.nacos.core.cluster.lookup.LookupFactory; import com.alibaba.nacos.core.notify.Event; import com.alibaba.nacos.core.notify.NotifyCenter; @@ -47,7 +48,6 @@ import javax.servlet.ServletContext; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -111,7 +111,7 @@ public class ServerMemberManager /** * self member obj */ - private Member self; + private volatile Member self; /** * here is always the node information of the "UP" state @@ -230,9 +230,9 @@ public class ServerMemberManager public Collection allMembers() { // We need to do a copy to avoid affecting the real data - List list = new ArrayList<>(serverList.values()); - Collections.sort(list); - return list; + HashSet set = new HashSet<>(serverList.values()); + set.add(self); + return set; } public List allMembersWithoutSelf() { @@ -257,7 +257,7 @@ public class ServerMemberManager isInIpList = false; members.add(this.self); Loggers.CLUSTER - .error("[serverlist] self ip {} not in serverlist {}", self, members); + .warn("[serverlist] self ip {} not in serverlist {}", self, members); } boolean hasChange = false; @@ -281,6 +281,10 @@ public class ServerMemberManager serverList = tmpMap; memberAddressInfos = tmpAddressInfo; + Collection finalMembers = allMembers(); + + Loggers.CLUSTER.warn("[serverlist] updated to : {}", finalMembers); + oldList.clear(); oldSet.clear(); @@ -288,9 +292,8 @@ public class ServerMemberManager // need to put the event publication into a synchronized block to ensure // that the event publication is sequential if (hasChange) { - MemberUtils.syncToFile(members); - Loggers.CLUSTER.warn("member has changed : {}", members); - Event event = MemberChangeEvent.builder().members(allMembers()).build(); + MemberUtils.syncToFile(finalMembers); + Event event = MemberChangeEvent.builder().members(finalMembers).build(); NotifyCenter.publishEvent(event); } @@ -411,15 +414,15 @@ public class ServerMemberManager public void onError(Throwable throwable) { Loggers.CLUSTER .error("failed to report new info to target node : {}, error : {}", - target.getAddress(), throwable); - MemberUtils.onFail(target); + target.getAddress(), ExceptionUtil.getAllExceptionMsg(throwable)); + MemberUtils.onFail(target, throwable); } }); } catch (Throwable ex) { Loggers.CLUSTER .error("failed to report new info to target node : {}, error : {}", - target.getAddress(), ex); + target.getAddress(), ExceptionUtil.getAllExceptionMsg(ex)); } } diff --git a/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/AddressServerMemberLookup.java b/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/AddressServerMemberLookup.java index 7cb08eef5..37ac43667 100644 --- a/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/AddressServerMemberLookup.java +++ b/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/AddressServerMemberLookup.java @@ -22,6 +22,7 @@ import com.alibaba.nacos.common.http.NSyncHttpClient; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RestResult; +import com.alibaba.nacos.common.utils.ExceptionUtil; import com.alibaba.nacos.core.cluster.AbstractMemberLookup; import com.alibaba.nacos.core.cluster.MemberUtils; import com.alibaba.nacos.core.utils.ApplicationUtils; @@ -102,7 +103,7 @@ public class AddressServerMemberLookup extends AbstractMemberLookup { break; } catch (Throwable e) { ex = e; - Loggers.CLUSTER.error("[serverlist] exception, error : {}", ex); + Loggers.CLUSTER.error("[serverlist] exception, error : {}", ExceptionUtil.getAllExceptionMsg(ex)); } } if (!success) { @@ -142,7 +143,7 @@ public class AddressServerMemberLookup extends AbstractMemberLookup { if (addressServerFailCount >= maxFailCount) { isAddressServerHealth = false; } - Loggers.CLUSTER.error("[serverlist] exception, error : {}", ex); + Loggers.CLUSTER.error("[serverlist] exception, error : {}", ExceptionUtil.getAllExceptionMsg(ex)); } finally { GlobalExecutor.scheduleByCommon(this, 5_000L); } @@ -162,7 +163,7 @@ public class AddressServerMemberLookup extends AbstractMemberLookup { catch (Throwable e) { Loggers.CLUSTER .error("[serverlist] exception for analyzeClusterConf, error : {}", - e); + ExceptionUtil.getAllExceptionMsg(e)); } addressServerFailCount = 0; isAddressServerHealth = false; diff --git a/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/DiscoveryMemberLookup.java b/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/DiscoveryMemberLookup.java index 4eb56bbff..16764cd81 100644 --- a/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/DiscoveryMemberLookup.java +++ b/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/DiscoveryMemberLookup.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.common.http.NAsyncHttpClient; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RestResult; +import com.alibaba.nacos.common.utils.LoggerUtils; import com.alibaba.nacos.core.utils.TimerContext; import com.alibaba.nacos.core.cluster.AbstractMemberLookup; import com.alibaba.nacos.core.cluster.Member; @@ -147,9 +148,8 @@ public class DiscoveryMemberLookup extends AbstractMemberLookup { @Override public void onReceive(RestResult> result) { if (result.ok()) { - Loggers.CLUSTER - .debug("success ping to node : {}, result : {}", - member, result); + LoggerUtils.printIfDebugEnabled(Loggers.CLUSTER, "success ping to node : {}, result : {}", + member, result); final Collection data = result.getData(); if (CollectionUtils.isNotEmpty(data)) { @@ -172,14 +172,14 @@ public class DiscoveryMemberLookup extends AbstractMemberLookup { Loggers.CLUSTER .error("An exception occurred while reporting their " + "information to the node : {}, error : {}", - member.getAddress(), e); - MemberUtils.onFail(member); + member.getAddress(), e.getMessage()); + MemberUtils.onFail(member, e); } }); } } catch (Exception e) { - Loggers.CLUSTER.error("node state report task has error : {}", e); + Loggers.CLUSTER.error("node state report task has error : {}", e.getMessage()); } finally { TimerContext.end(Loggers.CLUSTER); diff --git a/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/FileConfigMemberLookup.java b/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/FileConfigMemberLookup.java index d12944167..bb1f1b3bf 100644 --- a/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/FileConfigMemberLookup.java +++ b/core/src/main/java/com/alibaba/nacos/core/cluster/lookup/FileConfigMemberLookup.java @@ -62,7 +62,7 @@ public class FileConfigMemberLookup extends AbstractMemberLookup { } catch (Throwable e) { Loggers.CLUSTER - .error("An exception occurred in the launch file monitor : {}", e); + .error("An exception occurred in the launch file monitor : {}", e.getMessage()); } } } @@ -81,7 +81,7 @@ public class FileConfigMemberLookup extends AbstractMemberLookup { catch (Throwable e) { Loggers.CLUSTER .error("nacos-XXXX [serverlist] failed to get serverlist from disk!, error : {}", - e); + e.getMessage()); } afterLookup(tmpMembers); diff --git a/core/src/main/java/com/alibaba/nacos/core/code/StartingSpringApplicationRunListener.java b/core/src/main/java/com/alibaba/nacos/core/code/StartingSpringApplicationRunListener.java index 45ad8735f..11b53f59d 100644 --- a/core/src/main/java/com/alibaba/nacos/core/code/StartingSpringApplicationRunListener.java +++ b/core/src/main/java/com/alibaba/nacos/core/code/StartingSpringApplicationRunListener.java @@ -112,14 +112,33 @@ public class StartingSpringApplicationRunListener public void started(ConfigurableApplicationContext context) { starting = false; + ConfigurableEnvironment env = context.getEnvironment(); + if (scheduledExecutorService != null) { scheduledExecutorService.shutdownNow(); } logFilePath(); - LOGGER.info("Nacos started successfully in {} mode.", - System.getProperty(MODE_PROPERTY_KEY_STAND_MODE)); + // External data sources are used by default in cluster mode + boolean useExternalStorage = ("mysql".equalsIgnoreCase(env.getProperty("spring.datasource.platform", ""))); + + // must initialize after setUseExternalDB + // 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 + // default value is depend on ${nacos.standalone} + + if (!useExternalStorage) { + boolean embeddedStorage = ApplicationUtils.getStandaloneMode() || Boolean.getBoolean("embeddedStorage"); + // If the embedded data source storage is not turned on, it is automatically + // upgraded to the external data source storage, as before + if (!embeddedStorage) { + useExternalStorage = true; + } + } + + LOGGER.info("Nacos started successfully in {} mode. use {} storage", + System.getProperty(MODE_PROPERTY_KEY_STAND_MODE), useExternalStorage ? "external" : "embedded"); } @Override diff --git a/core/src/main/java/com/alibaba/nacos/core/controller/NacosClusterController.java b/core/src/main/java/com/alibaba/nacos/core/controller/NacosClusterController.java index 60c95764b..fbd59380b 100644 --- a/core/src/main/java/com/alibaba/nacos/core/controller/NacosClusterController.java +++ b/core/src/main/java/com/alibaba/nacos/core/controller/NacosClusterController.java @@ -24,6 +24,7 @@ import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.model.RestResultUtils; +import com.alibaba.nacos.common.utils.LoggerUtils; import com.alibaba.nacos.core.cluster.Member; import com.alibaba.nacos.core.cluster.MemberUtils; import com.alibaba.nacos.core.cluster.NodeState; @@ -101,7 +102,7 @@ public class NacosClusterController { if (!node.check()) { return RestResultUtils.failedWithMsg(400, "Node information is illegal"); } - Loggers.CLUSTER.debug("node state report, receive info : {}", node); + LoggerUtils.printIfDebugEnabled(Loggers.CLUSTER, "node state report, receive info : {}", node); node.setState(NodeState.UP); node.setFailAccessCnt(0); memberManager.update(node); @@ -138,7 +139,7 @@ public class NacosClusterController { public void onReceive(RestResult result) { try { if (result.ok()) { - Loggers.CLUSTER.debug("The node : [{}] success to process the request", + LoggerUtils.printIfDebugEnabled(Loggers.CLUSTER, "The node : [{}] success to process the request", member); MemberUtils.onSuccess(member); } diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/AbstractConsistencyProtocol.java b/core/src/main/java/com/alibaba/nacos/core/distributed/AbstractConsistencyProtocol.java index 83b11d21e..e4f7785ab 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/AbstractConsistencyProtocol.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/AbstractConsistencyProtocol.java @@ -28,7 +28,7 @@ import java.util.Map; /** * @author liaochuntao */ -public abstract class AbstractConsistencyProtocol implements ConsistencyProtocol { +public abstract class AbstractConsistencyProtocol implements ConsistencyProtocol { protected final ProtocolMetaData metaData = new ProtocolMetaData(); diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java b/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java index 9a55e4aff..e749054a2 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java @@ -17,16 +17,13 @@ package com.alibaba.nacos.core.distributed; import com.alibaba.nacos.consistency.Config; -import com.alibaba.nacos.consistency.ConsistencyProtocol; -import com.alibaba.nacos.consistency.LogProcessor; import com.alibaba.nacos.consistency.ap.APProtocol; -import com.alibaba.nacos.consistency.ap.LogProcessor4AP; import com.alibaba.nacos.consistency.cp.CPProtocol; -import com.alibaba.nacos.consistency.cp.LogProcessor4CP; import com.alibaba.nacos.core.cluster.Member; import com.alibaba.nacos.core.cluster.MemberChangeEvent; import com.alibaba.nacos.core.cluster.MemberChangeListener; import com.alibaba.nacos.core.cluster.MemberMetaDataConstants; +import com.alibaba.nacos.core.cluster.MemberUtils; import com.alibaba.nacos.core.cluster.ServerMemberManager; import com.alibaba.nacos.core.notify.NotifyCenter; import com.alibaba.nacos.core.utils.ApplicationUtils; @@ -39,11 +36,8 @@ import org.springframework.context.event.ContextStartedEvent; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; -import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; -import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.Set; @@ -125,7 +119,6 @@ public class ProtocolManager Class configType = ClassUtils.resolveGenericType(protocol.getClass()); Config config = (Config) ApplicationUtils.getBean(configType); injectMembers4AP(config); - config.addLogProcessors(loadProcessor(LogProcessor4AP.class, protocol)); protocol.init((config)); ProtocolManager.this.apProtocol = protocol; }); @@ -136,7 +129,6 @@ public class ProtocolManager Class configType = ClassUtils.resolveGenericType(protocol.getClass()); Config config = (Config) ApplicationUtils.getBean(configType); injectMembers4CP(config); - config.addLogProcessors(loadProcessor(LogProcessor4CP.class, protocol)); protocol.init((config)); ProtocolManager.this.cpProtocol = protocol; }); @@ -156,14 +148,6 @@ public class ProtocolManager config.setMembers(self, others); } - @SuppressWarnings("all") - private List loadProcessor(Class cls, ConsistencyProtocol protocol) { - final Map beans = (Map) ApplicationUtils - .getBeansOfType(cls); - final List result = new ArrayList<>(beans.values()); - return result; - } - @Override public void onEvent(MemberChangeEvent event) { // Here, the sequence of node change events is very important. For example, @@ -192,8 +176,7 @@ public class ProtocolManager Set nodes = new HashSet<>(); members.forEach(member -> { final String ip = member.getIp(); - final int port = member.getPort(); - final int raftPort = port + 1000 >= 65535 ? port + 1 : port + 1000; + final int raftPort = MemberUtils.calculateRaftPort(member); nodes.add(ip + ":" + raftPort); }); return nodes; diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftOps.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftOps.java index 3fb56924b..d2ef0e48a 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftOps.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftOps.java @@ -43,7 +43,7 @@ public class JRaftOps { } public RestResult execute(String[] args) { - return RestResultUtils.failed("not support yeah"); + return RestResultUtils.failed("not support yet"); } public RestResult execute(Map args) { diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftProtocol.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftProtocol.java index 01a8a5bdc..1ee1c0739 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftProtocol.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftProtocol.java @@ -20,6 +20,7 @@ import com.alibaba.nacos.common.JustForTest; import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.consistency.LogFuture; +import com.alibaba.nacos.consistency.LogProcessor; import com.alibaba.nacos.consistency.ProtocolMetaData; import com.alibaba.nacos.consistency.cp.CPProtocol; import com.alibaba.nacos.consistency.cp.Constants; @@ -43,6 +44,7 @@ import com.alipay.sofa.jraft.Node; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -100,7 +102,7 @@ import java.util.function.BiConsumer; @SuppressWarnings("all") public class JRaftProtocol extends AbstractConsistencyProtocol - implements CPProtocol { + implements CPProtocol { private final AtomicBoolean initialized = new AtomicBoolean(false); private final AtomicBoolean shutdowned = new AtomicBoolean(false); @@ -131,17 +133,13 @@ public class JRaftProtocol public void init(RaftConfig config) { if (initialized.compareAndSet(false, true)) { this.raftConfig = config; - - // Load all LogProcessor information in advance - loadLogProcessor(config.listLogProcessor()); - this.selfAddress = memberManager.getSelf().getAddress(); NotifyCenter.registerToSharePublisher(RaftEvent.class); this.failoverRetries = ConvertUtils .toInt(config.getVal(RaftSysConstants.REQUEST_FAILOVER_RETRIES), 1); this.failoverRetriesStr = String.valueOf(failoverRetries); this.raftServer.setFailoverRetries(failoverRetries); - this.raftServer.init(this.raftConfig, this.raftConfig.listLogProcessor()); + this.raftServer.init(this.raftConfig); this.raftServer.start(); // There is only one consumer to ensure that the internal consumption @@ -184,6 +182,11 @@ public class JRaftProtocol } } + @Override + public void addLogProcessors(Collection processors) { + raftServer.createMultiRaftGroup(processors); + } + @Override public GetResponse getData(GetRequest request) throws Exception { int retryCnt = Integer.parseInt( @@ -247,6 +250,7 @@ public class JRaftProtocol private void injectProtocolMetaData(ProtocolMetaData metaData) { Member member = memberManager.getSelf(); member.setExtendVal("raft_meta_data", metaData); + memberManager.update(member); } @Override diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java index 24e7a5064..743e374bd 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java @@ -20,6 +20,7 @@ import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.ByteUtils; import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.DiskUtils; +import com.alibaba.nacos.common.utils.LoggerUtils; import com.alibaba.nacos.common.utils.ThreadUtils; import com.alibaba.nacos.consistency.LogProcessor; import com.alibaba.nacos.consistency.SerializeFactory; @@ -143,7 +144,7 @@ public class JRaftServer { this.failoverRetries = failoverRetries; } - void init(RaftConfig config, Collection processors) { + void init(RaftConfig config) { this.raftConfig = config; this.serializer = SerializeFactory.getDefault(); Loggers.RAFT.info("Initializes the Raft protocol, raft-config info : {}", config); @@ -175,8 +176,6 @@ public class JRaftServer { this.cliClientService = new BoltCliClientService(); this.cliClientService.init(cliOptions); this.cliService = RaftServiceFactory.createAndInitCliService(cliOptions); - - this.processors.addAll(processors); } synchronized void start() { @@ -376,7 +375,7 @@ public class JRaftServer { public CompletableFuture commit(Log data, final CompletableFuture future, final int retryLeft) { - Loggers.RAFT.debug("data requested this time : {}", data); + LoggerUtils.printIfDebugEnabled(Loggers.RAFT, "data requested this time : {}", data); final String group = data.getGroup(); final RaftGroupTuple tuple = findTupleByGroup(group); if (tuple == null) { @@ -409,6 +408,11 @@ public class JRaftServer { for (String address : addresses) { newConf.addPeer(PeerId.parsePeer(address)); } + + if (Objects.equals(oldConf, newConf)) { + return; + } + for (int i = 0; i < 3; i++) { try { Status status = cliService.changePeers(groupId, oldConf, newConf); diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosStateMachine.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosStateMachine.java index afc265903..1accdf381 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosStateMachine.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosStateMachine.java @@ -17,6 +17,8 @@ package com.alibaba.nacos.core.distributed.raft; import com.alibaba.fastjson.JSON; +import com.alibaba.nacos.common.utils.ExceptionUtil; +import com.alibaba.nacos.common.utils.LoggerUtils; import com.alibaba.nacos.consistency.LogFuture; import com.alibaba.nacos.consistency.LogProcessor; import com.alibaba.nacos.consistency.cp.LogProcessor4CP; @@ -100,8 +102,7 @@ class NacosStateMachine extends StateMachineAdapter { final ByteBuffer data = iter.getData(); log = Log.parseFrom(data.array()); } - - Loggers.RAFT.debug("receive log : {}", log); + LoggerUtils.printIfDebugEnabled(Loggers.RAFT, "receive log : {}", log); final String type = log .getExtendInfoOrDefault(JRaftConstants.JRAFT_EXTEND_INFO_KEY, @@ -122,7 +123,7 @@ class NacosStateMachine extends StateMachineAdapter { } catch (Throwable e) { index++; - status.setError(RaftError.UNKNOWN, e.getMessage()); + status.setError(RaftError.UNKNOWN, e.toString()); Optional.ofNullable(closure) .ifPresent(closure1 -> closure1.setThrowable(e)); throw e; @@ -142,7 +143,7 @@ class NacosStateMachine extends StateMachineAdapter { Loggers.RAFT.error("processor : {}, stateMachine meet critical error: {}.", processor, t); iter.setErrorAndRollback(index - applied, new Status(RaftError.ESTATEMACHINE, - "StateMachine meet critical error: %s.", t.getMessage())); + "StateMachine meet critical error: %s.", ExceptionUtil.getStackTrace(t))); } } diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/RaftConfig.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/RaftConfig.java index 620de9b15..a5374b197 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/RaftConfig.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/RaftConfig.java @@ -41,7 +41,6 @@ public class RaftConfig implements Config { private static final long serialVersionUID = 9174789390266064002L; private Map data = Collections.synchronizedMap(new HashMap<>()); - private List processors = Collections.synchronizedList(new ArrayList<>()); private String selfAddress; private Set members = Collections.synchronizedSet(new HashSet<>()); @@ -95,16 +94,6 @@ public class RaftConfig implements Config { return data.getOrDefault(key, defaultVal); } - @Override - public List listLogProcessor() { - return processors; - } - - @Override - public void addLogProcessors(Collection processors) { - this.processors.addAll(processors); - } - @Override public String toString() { try { diff --git a/core/src/main/java/com/alibaba/nacos/core/notify/NotifyCenter.java b/core/src/main/java/com/alibaba/nacos/core/notify/NotifyCenter.java index e43882995..b3abc315a 100644 --- a/core/src/main/java/com/alibaba/nacos/core/notify/NotifyCenter.java +++ b/core/src/main/java/com/alibaba/nacos/core/notify/NotifyCenter.java @@ -232,7 +232,7 @@ public class NotifyCenter { return publisher.publish(event); } throw new NoSuchElementException( - "There are no event publishers for this event, please register"); + "There are no [" + topic + "] publishers for this event, please register"); } /** 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 75d6b3cd5..b4f613a2e 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 @@ -532,5 +532,9 @@ public class ApplicationUtils ApplicationUtils.environment = environment; } + public static void injectContext(ConfigurableApplicationContext context) { + ApplicationUtils.applicationContext = context; + } + } diff --git a/core/src/main/java/com/alibaba/nacos/core/utils/TimerContext.java b/core/src/main/java/com/alibaba/nacos/core/utils/TimerContext.java index 1363e5f98..f7580250b 100644 --- a/core/src/main/java/com/alibaba/nacos/core/utils/TimerContext.java +++ b/core/src/main/java/com/alibaba/nacos/core/utils/TimerContext.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.core.utils; +import com.alibaba.nacos.common.utils.LoggerUtils; import com.alibaba.nacos.common.utils.Pair; import org.slf4j.Logger; @@ -38,7 +39,7 @@ public class TimerContext { public static void end(final Logger logger) { long endTime = System.currentTimeMillis(); Pair record = TIME_RECORD.get(); - logger.debug("{} cost time : {} ms", record.getFirst(), (endTime - record.getSecond())); + LoggerUtils.printIfDebugEnabled(logger, "{} cost time : {} ms", record.getFirst(), (endTime - record.getSecond())); TIME_RECORD.remove(); } diff --git a/core/src/main/resources/META-INF/logback/nacos.xml b/core/src/main/resources/META-INF/logback/nacos.xml index 8313602b7..e0faee0c6 100644 --- a/core/src/main/resources/META-INF/logback/nacos.xml +++ b/core/src/main/resources/META-INF/logback/nacos.xml @@ -211,7 +211,12 @@ - + + + + + + diff --git a/distribution/conf/nacos-logback.xml b/distribution/conf/nacos-logback.xml index 2c4b356b8..149a85101 100644 --- a/distribution/conf/nacos-logback.xml +++ b/distribution/conf/nacos-logback.xml @@ -630,7 +630,7 @@ - + diff --git a/naming/src/main/java/com/alibaba/nacos/naming/exception/ResponseExceptionHandler.java b/naming/src/main/java/com/alibaba/nacos/naming/exception/ResponseExceptionHandler.java index a9f6fe54a..5ad15bf7f 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/exception/ResponseExceptionHandler.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/exception/ResponseExceptionHandler.java @@ -32,26 +32,26 @@ public class ResponseExceptionHandler { @ExceptionHandler(NacosException.class) public ResponseEntity handleNacosException(NacosException e) { - Loggers.SRV_LOG.error("got exception. {}", e.getErrMsg(), ExceptionUtil.getStackTrace(e)); + Loggers.SRV_LOG.error("got exception. {}", e.getErrMsg(), ExceptionUtil.getAllExceptionMsg(e)); return ResponseEntity.status(e.getErrCode()).body(e.getMessage()); } @ExceptionHandler(IllegalArgumentException.class) public ResponseEntity handleParameterError(IllegalArgumentException ex) { - Loggers.SRV_LOG.error("got exception. {}", ex.getMessage(), ExceptionUtil.getStackTrace(ex)); + Loggers.SRV_LOG.error("got exception. {}", ex.getMessage(), ExceptionUtil.getAllExceptionMsg(ex)); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage()); } @ExceptionHandler(MissingServletRequestParameterException.class) public ResponseEntity handleMissingParams(MissingServletRequestParameterException ex) { - Loggers.SRV_LOG.error("got exception.", ExceptionUtil.getStackTrace(ex)); + Loggers.SRV_LOG.error("got exception.", ExceptionUtil.getAllExceptionMsg(ex)); String name = ex.getParameterName(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Parameter '" + name + "' is missing"); } @ExceptionHandler(Exception.class) public ResponseEntity handleException(Exception e) { - Loggers.SRV_LOG.error("got exception.", ExceptionUtil.getStackTrace(e)); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ExceptionUtil.getStackTrace(e)); + Loggers.SRV_LOG.error("got exception.", ExceptionUtil.getAllExceptionMsg(e)); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ExceptionUtil.getAllExceptionMsg(e)); } } diff --git a/test/src/test/java/com/alibaba/nacos/test/common/WatchFileCenter_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/WatchFileCenter_ITCase.java index 648f5b157..3c9348417 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/WatchFileCenter_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/WatchFileCenter_ITCase.java @@ -36,6 +36,7 @@ import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; @@ -111,7 +112,7 @@ public class WatchFileCenter_ITCase { ThreadUtils.sleep(10_000L); } - latch.await(); + latch.await(10_000L, TimeUnit.MILLISECONDS); Assert.assertEquals(3, count.get()); } @@ -139,7 +140,7 @@ public class WatchFileCenter_ITCase { } }); } - latch.await(); + latch.await(10_000L, TimeUnit.MILLISECONDS); ThreadUtils.sleep(5_000L); } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java index e71ab9a76..e73553951 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java @@ -250,7 +250,7 @@ public class ConfigDerbyRaft_DITCase iconfig7.publishConfig(dataId, group, content); ThreadUtils.sleep(10_000L); - latch[0].await(); + latch[0].await(10_000L, TimeUnit.MILLISECONDS); Assert.assertEquals(content, r.get()); Assert.assertEquals(content, iconfig7.getConfig(dataId, group, 2_000L)); @@ -258,7 +258,7 @@ public class ConfigDerbyRaft_DITCase iconfig7.publishConfig(dataId, group, content); ThreadUtils.sleep(10_000L); - latch[1].await(); + latch[1].await(10_000L, TimeUnit.MILLISECONDS); Assert.assertEquals(content, r.get()); Assert.assertEquals(content, iconfig7.getConfig(dataId, group, 2_000L)); } @@ -311,7 +311,7 @@ public class ConfigDerbyRaft_DITCase } }); NotifyCenter.publishEvent(new RaftDBErrorEvent()); - latch1.await(); + latch1.await(10_000L, TimeUnit.MILLISECONDS); result = iconfig7.publishConfig("raft_test_raft_error", "cluster_test_1", "this.is.raft_cluster=lessspring_7"); @@ -331,7 +331,7 @@ public class ConfigDerbyRaft_DITCase } }); NotifyCenter.publishEvent(new RaftDBErrorRecoverEvent()); - latch2.await(); + latch2.await(10_000L, TimeUnit.MILLISECONDS); result = iconfig7.publishConfig("raft_test_raft_error", "cluster_test_1", "this.is.raft_cluster=lessspring_7"); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java index 7841d4f21..9815b3e8e 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java @@ -84,7 +84,7 @@ public class ConfigLongPollReturnChanges_ITCase { configService.getConfig(dataId, group, 50); - latch.await(); + latch.await(10_000L, TimeUnit.MILLISECONDS); } @Test @@ -124,7 +124,7 @@ public class ConfigLongPollReturnChanges_ITCase { }); configService.publishConfig(dataId, group, newData); - latch.await(); + latch.await(10_000L, TimeUnit.MILLISECONDS); } @Test @@ -162,7 +162,7 @@ public class ConfigLongPollReturnChanges_ITCase { }); configService.removeConfig(dataId, group); - latch.await(); + latch.await(10_000L, TimeUnit.MILLISECONDS); } } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/DerbyRaftError_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/DerbyRaftError_ITCase.java deleted file mode 100644 index e12c40764..000000000 --- a/test/src/test/java/com/alibaba/nacos/test/config/DerbyRaftError_ITCase.java +++ /dev/null @@ -1,144 +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.test.config; - -import com.alibaba.nacos.config.server.service.repository.DistributedDatabaseOperateImpl; -import com.alibaba.nacos.config.server.service.sql.ModifyRequest; -import com.alibaba.nacos.consistency.LogFuture; -import com.alibaba.nacos.consistency.SerializeFactory; -import com.alibaba.nacos.consistency.Serializer; -import com.alibaba.nacos.consistency.entity.Log; -import com.alibaba.nacos.consistency.exception.ConsistencyException; -import com.alibaba.nacos.core.utils.ApplicationUtils; -import com.google.common.collect.Lists; -import com.google.protobuf.ByteString; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.mockito.Spy; -import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.dao.DeadlockLoserDataAccessException; -import org.springframework.dao.DuplicateKeyException; -import org.springframework.jdbc.BadSqlGrammarException; -import org.springframework.jdbc.CannotGetJdbcConnectionException; - -import java.sql.SQLException; -import java.util.List; - -/** - * If the SQL logic is wrong or the constraint is violated, the exception should not be - * thrown, but the return should be carried in the LogFuture, otherwise it will be thrown - * to the upper level - * - * @author liaochuntao - */ -@RunWith(MockitoJUnitRunner.class) -public class DerbyRaftError_ITCase { - - @Spy - private DistributedDatabaseOperateImpl databaseOperate; - - private Serializer serializer = SerializeFactory.getDefault(); - - @Before - public void before() { - MockitoAnnotations.initMocks(this); - } - - @Test - public void test_logic_DuplicateKeyException() { - - Mockito.doThrow(new DuplicateKeyException("DuplicateKeyException")) - .when(databaseOperate).onUpdate(Mockito.anyList()); - - List list = Lists.newArrayList(new ModifyRequest()); - - LogFuture future = databaseOperate.onApply( - Log.newBuilder().setData(ByteString.copyFrom(serializer.serialize(list))) - .build()); - - Assert.assertEquals(future.getError().getMessage(), "DuplicateKeyException"); - } - - @Test - public void test_logic_BadSqlGrammarException() { - - Mockito.doThrow(new BadSqlGrammarException("BadSqlGrammarException", - "BadSqlGrammarException", new SQLException())).when(databaseOperate) - .onUpdate(Mockito.anyList()); - - List list = Lists.newArrayList(new ModifyRequest()); - - LogFuture future = databaseOperate.onApply( - Log.newBuilder().setData(ByteString.copyFrom(serializer.serialize(list))) - .build()); - - Assert.assertEquals(future.getError().getMessage(), - "BadSqlGrammarException; bad SQL grammar [BadSqlGrammarException]; nested exception is java.sql.SQLException"); - } - - @Test - public void test_logic_DataIntegrityViolationException() { - - Mockito.doThrow( - new DataIntegrityViolationException("DataIntegrityViolationException")) - .when(databaseOperate).onUpdate(Mockito.anyList()); - - List list = Lists.newArrayList(new ModifyRequest()); - - LogFuture future = databaseOperate.onApply( - Log.newBuilder().setData(ByteString.copyFrom(serializer.serialize(list))) - .build()); - - Assert.assertEquals(future.getError().getMessage(), - "DataIntegrityViolationException"); - } - - @Test(expected = ConsistencyException.class) - public void test_error_CannotGetJdbcConnectionException() { - - Mockito.doThrow( - new CannotGetJdbcConnectionException("CannotGetJdbcConnectionException")) - .when(databaseOperate).onUpdate(Mockito.anyList()); - - List list = Lists.newArrayList(new ModifyRequest()); - - databaseOperate.onApply( - Log.newBuilder().setData(ByteString.copyFrom(serializer.serialize(list))) - .build()); - } - - @Test(expected = ConsistencyException.class) - public void test_error_DataAccessException() { - - Mockito.doThrow( - new DeadlockLoserDataAccessException("DeadlockLoserDataAccessException", - new SQLException())).when(databaseOperate) - .onUpdate(Mockito.anyList()); - - List list = Lists.newArrayList(new ModifyRequest()); - - databaseOperate.onApply( - Log.newBuilder().setData(ByteString.copyFrom(serializer.serialize(list))) - .build()); - } - -} diff --git a/test/src/test/java/com/alibaba/nacos/test/core/InetUtils_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/core/InetUtils_ITCase.java index e3559b39e..308200a5e 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/InetUtils_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/InetUtils_ITCase.java @@ -25,6 +25,7 @@ import org.junit.Test; import java.util.Objects; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import static com.alibaba.nacos.core.utils.Constants.NACOS_SERVER_IP; @@ -69,7 +70,7 @@ public class InetUtils_ITCase { }; NotifyCenter.registerSubscribe(subscribe); - latch.await(); + latch.await(10_000L, TimeUnit.MILLISECONDS); Assert.assertEquals(testIp, reference.get()); Assert.assertEquals(testIp, InetUtils.getSelfIp()); diff --git a/test/src/test/java/com/alibaba/nacos/test/core/cluster/ServerMemberManager_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/core/cluster/ServerMemberManager_ITCase.java index 325c283d9..aa6bae3b9 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/cluster/ServerMemberManager_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/cluster/ServerMemberManager_ITCase.java @@ -39,6 +39,7 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; /** @@ -119,7 +120,7 @@ public class ServerMemberManager_ITCase { boolean changed = memberManager.memberJoin(members); Assert.assertTrue(changed); - latch.await(); + latch.await(10_000L, TimeUnit.MILLISECONDS); Assert.assertEquals(1, integer.get()); } diff --git a/test/src/test/java/com/alibaba/nacos/test/core/notify/NotifyCenter_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/core/notify/NotifyCenter_ITCase.java index d0bcfba60..20499862b 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/notify/NotifyCenter_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/notify/NotifyCenter_ITCase.java @@ -252,8 +252,8 @@ public class NotifyCenter_ITCase { NotifyCenter.publishEvent(new SlowE1()); NotifyCenter.publishEvent(new SlowE2()); - latch1.await(); - latch2.await(); + latch1.await(10_000L, TimeUnit.MILLISECONDS); + latch2.await(10_000L, TimeUnit.MILLISECONDS); Assert.assertEquals("SlowE1", values[0]); Assert.assertEquals("SlowE2", values[1]); diff --git a/test/src/test/resources/application.properties b/test/src/test/resources/application.properties index 88ee10835..e0fe74863 100644 --- a/test/src/test/resources/application.properties +++ b/test/src/test/resources/application.properties @@ -17,12 +17,12 @@ server.port=8848 # spring.datasource.platform=mysql ### Count of DB: -db.num=1 +# db.num=1 ### Connect URL of DB: -db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC -db.user=root -db.password=1017 +# db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC +# db.user=root +# db.password=1017 #*************** Naming Module Related Configurations ***************#