Skip to content

Commit

Permalink
Merge pull request #111 from nacos-group/develop
Browse files Browse the repository at this point in the history
issue #85 and NacosConfigService::getConfig behavior change
  • Loading branch information
TTTTTAAAAAKKKKEEEENNNN authored Apr 10, 2022
2 parents 72d0cd2 + 707be61 commit 60dc0fb
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/listenToKeys.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include "Nacos.h"
#include <stdio.h>

using namespace std;
using namespace nacos;
Expand Down
1 change: 1 addition & 0 deletions examples/subscribeServices.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include "Nacos.h"
#include <stdio.h>

using namespace std;
using namespace nacos;
Expand Down
7 changes: 5 additions & 2 deletions src/config/NacosConfigService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ NacosString NacosConfigService::getConfigInner
result = _objectConfigData->_clientWorker->getServerConfig(tenant, dataId, group, timeoutMs);
} catch (NacosException &e) {
if (e.errorcode() == NacosException::NO_RIGHT) {
throw e;
log_error("Invalid credential, e: %d = %s\n", e.errorcode(), e.what());
}

const NacosString &clientName = _appConfigManager->get(PropertyKeyConst::CLIENT_NAME);
result = _localSnapshotManager->getSnapshot(clientName, dataId, group, tenant);
if (e.errorcode() == NacosException::NO_RIGHT && NacosStringOps::isNullStr(result)) {
//permission denied and no failback, let user decide what to do
throw e;
}
}
return result;
}
Expand Down
1 change: 1 addition & 0 deletions src/factory/ObjectConfigData.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "config/ConfigService.h"
#include "NacosExceptions.h"
#include "Compatibility.h"
#include <stdint.h>

namespace nacos{
class HttpDelegate;
Expand Down
21 changes: 18 additions & 3 deletions src/security/SecurityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ SecurityManager::SecurityManager(ObjectConfigData *objectConfigData) {
}
void SecurityManager::doLogin(const NacosString &serverAddr) NACOS_THROW(NacosException, NetworkException) {
//TODO:refactor string constants
NacosString url = serverAddr + "/" + ConfigConstant::DEFAULT_CONTEXT_PATH + "/v1/auth/users/login";
NacosString url = serverAddr + "/" + _objectConfigData->_appConfigManager->getContextPath() + "/v1/auth/users/login";
list <NacosString> headers;
list <NacosString> paramValues;

Expand Down Expand Up @@ -62,8 +62,17 @@ void SecurityManager::login() NACOS_THROW (NacosException) {
//for some cases, e.g.:invalid username/password,
//we should throw exception directly since retry on another node will not correct this problem
if (e.errorcode() == NacosException::INVALID_LOGIN_CREDENTIAL) {
/**
* Here we don't need to keep log for it, because there are 3 situations where we will call this login() routine:
* 1. Initialization stage of NamingService
* 2. Initialization stage of ConfigService
* 3. tokenRefreshThreadFunc's invocation of this routine to refresh the credentials
* In case 1 and case 2, the program will crash, because these situations are considered as a config error of the program, so let it crash
* In case 3, the log is printed by tokenRefreshThreadFunc to remind the dev-ops to correct the config
*/
throw e;
}
log_error("Unknown error while login to server, e:%d = %s\n", e.errorcode(), e.what());
continue;
}
//login succeeded
Expand Down Expand Up @@ -116,8 +125,14 @@ void *SecurityManager::tokenRefreshThreadFunc(void *param) {
thisObj->login();
} catch (NacosException &e) {
if (e.errorcode() == NacosException::INVALID_LOGIN_CREDENTIAL) {
log_error("Invalid credential!\n");
throw e;//Invalid login credential, let it crash
/**
* invalid credential while the application is running, wait for a moment and retry
* since the existing data in the nacos client is still usable for the application
* we should keep the application alive for the Availability, but the Consistency, in this case, is NOT guaranteed
* the error log reminds the dev-ops to check the config
*/
log_error("Invalid credential, please check your server settings!\n");
sleep(30);
} else if (e.errorcode() == NacosException::ALL_SERVERS_TRIED_AND_FAILED) {
log_warn("Network down, sleep for 30 secs and retry\n");
sleep(30);//network down, wait for a moment
Expand Down
2 changes: 1 addition & 1 deletion src/server/ServerListManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ list <NacosServerInfo> ServerListManager::tryPullServerListFromNacosServer() NAC
log_debug("Trying to access server:%s\n", server.getCompleteAddress().c_str());
try {
HttpResult serverRes = _objectConfigData->_httpDelegate->httpGet(
server.getCompleteAddress() + "/" + ConfigConstant::DEFAULT_CONTEXT_PATH + "/"
server.getCompleteAddress() + "/" + _objectConfigData->_appConfigManager->getContextPath() + "/"
+ ConfigConstant::PROTOCOL_VERSION + "/" + ConfigConstant::GET_SERVERS_PATH,
headers, paramValues, NULLSTR, _read_timeout);
return JSON::Json2NacosServerInfo(serverRes.content);
Expand Down

0 comments on commit 60dc0fb

Please sign in to comment.