From 09ba9ec545b1ebe931f0e22513b4a37bd255bd71 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Wed, 8 Jan 2025 02:24:54 +0000 Subject: [PATCH 01/32] fix --- core/common/MachineInfoUtil.cpp | 2 +- core/common/MachineInfoUtil.h | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 0efa6dcee0..5c233493f6 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -504,13 +504,13 @@ size_t FetchECSMetaCallback(char* buffer, size_t size, size_t nmemb, std::string HostIdentifier::HostIdentifier() { #ifdef __ENTERPRISE__ getECSMetaFromFile(); - updateHostId(); #else ECSMeta ecsMeta; if (FetchECSMeta(ecsMeta)) { UpdateECSMetaAndHostid(ecsMeta); } #endif + updateHostId(); } bool HostIdentifier::UpdateECSMetaAndHostid(const ECSMeta& meta) { diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index 4264360f44..b494348b1a 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -81,10 +81,11 @@ class HostIdentifier { } bool UpdateECSMetaAndHostid(const ECSMeta& meta); - bool FetchECSMeta(ECSMeta& metaObj); void DumpECSMeta(); private: + bool FetchECSMeta(ECSMeta& metaObj); + void getECSMetaFromFile(); // 从云助手获取序列号 void getSerialNumberFromEcsAssist(); @@ -110,6 +111,9 @@ class HostIdentifier { std::string mMetadataStr; std::string mLocalHostId; +#ifdef __ENTERPRISE__ + friend class EnterpriseConfigProvider; +#endif }; } // namespace logtail From 72a55cb5677058891849d436d3c5384ac35409cd Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Wed, 8 Jan 2025 02:26:35 +0000 Subject: [PATCH 02/32] fix --- core/common/MachineInfoUtil.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 5c233493f6..ced7868f40 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -505,10 +505,7 @@ HostIdentifier::HostIdentifier() { #ifdef __ENTERPRISE__ getECSMetaFromFile(); #else - ECSMeta ecsMeta; - if (FetchECSMeta(ecsMeta)) { - UpdateECSMetaAndHostid(ecsMeta); - } + FetchECSMeta(mMetadata); #endif updateHostId(); } From 98808d4a3dfda96694c1ea280433de223d0b21c6 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 01:50:42 +0000 Subject: [PATCH 03/32] fix --- core/app_config/AppConfig.cpp | 30 +++++++++++++++++-- core/app_config/AppConfig.h | 2 ++ .../event_handler/HistoryFileImporter.cpp | 2 +- core/plugin/flusher/sls/DiskBufferWriter.cpp | 4 +-- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/core/app_config/AppConfig.cpp b/core/app_config/AppConfig.cpp index c3cb0130ca..5167cbda66 100644 --- a/core/app_config/AppConfig.cpp +++ b/core/app_config/AppConfig.cpp @@ -318,6 +318,32 @@ std::string GetAgentGoLogConfDir() { return dir; } +std::string GetBufferFileDir() { + static std::string dir; + if (!dir.empty()) { + return dir; + } + if (BOOL_FLAG(logtail_mode)) { + dir = GetProcessExecutionDir(); + } else { + dir = GetAgentDataDir(); + } + return dir; +} + +std::string GetHistoryDataPath() { + static std::string dir; + if (!dir.empty()) { + return dir; + } + if (BOOL_FLAG(logtail_mode)) { + dir = GetProcessExecutionDir(); + } else { + dir = GetAgentDataDir(); + } + return dir; +} + std::string GetAgentGoCheckpointDir() { static std::string dir; if (!dir.empty()) { @@ -344,7 +370,7 @@ std::string GetAgentDataDir() { dir = GetProcessExecutionDir(); #else if (BOOL_FLAG(logtail_mode)) { - dir = GetProcessExecutionDir(); + dir = AppConfig::GetInstance()->GetLoongcollectorConfDir() + PATH_SEPARATOR + "checkpoint"; } else { dir = STRING_FLAG(loongcollector_data_dir) + PATH_SEPARATOR; } @@ -392,7 +418,7 @@ std::string GetAgentDockerPathConfig() { return file_path; } if (BOOL_FLAG(logtail_mode)) { - file_path = GetAgentDataDir() + STRING_FLAG(ilogtail_docker_file_path_config); + file_path = GetProcessExecutionDir() + STRING_FLAG(ilogtail_docker_file_path_config); } else { file_path = GetAgentDataDir() + "docker_path_config.json"; } diff --git a/core/app_config/AppConfig.h b/core/app_config/AppConfig.h index 2e8d59613d..35e9b991e1 100644 --- a/core/app_config/AppConfig.h +++ b/core/app_config/AppConfig.h @@ -47,6 +47,8 @@ std::string GetAgentConfDir(); std::string GetAgentRunDir(); std::string GetAgentThirdPartyDir(); +std::string GetBufferFileDir(); +std::string GetHistoryDataPath(); std::string GetAgentGoCheckpointDir(); std::string GetAgentGoLogConfDir(); std::string GetAgentPrometheusAuthorizationPath(); diff --git a/core/file_server/event_handler/HistoryFileImporter.cpp b/core/file_server/event_handler/HistoryFileImporter.cpp index 24c8bbfbae..aa1bcbce98 100644 --- a/core/file_server/event_handler/HistoryFileImporter.cpp +++ b/core/file_server/event_handler/HistoryFileImporter.cpp @@ -51,7 +51,7 @@ void HistoryFileImporter::Run() { } void HistoryFileImporter::LoadCheckPoint() { - std::string historyDataPath = GetAgentDataDir() + "history_file_checkpoint"; + std::string historyDataPath = GetHistoryDataPath() + "history_file_checkpoint"; FILE* readPtr = fopen(historyDataPath.c_str(), "r"); if (readPtr != NULL) { fclose(readPtr); diff --git a/core/plugin/flusher/sls/DiskBufferWriter.cpp b/core/plugin/flusher/sls/DiskBufferWriter.cpp index fd4c8117d9..ff94990e6d 100644 --- a/core/plugin/flusher/sls/DiskBufferWriter.cpp +++ b/core/plugin/flusher/sls/DiskBufferWriter.cpp @@ -258,7 +258,7 @@ void DiskBufferWriter::BufferSenderThread() { void DiskBufferWriter::SetBufferFilePath(const std::string& bufferfilepath) { lock_guard lock(mBufferFileLock); if (bufferfilepath == "") { - mBufferFilePath = GetAgentDataDir(); + mBufferFilePath = GetBufferFileDir(); } else mBufferFilePath = bufferfilepath; @@ -285,7 +285,7 @@ std::string DiskBufferWriter::GetBufferFileName() { bool DiskBufferWriter::LoadFileToSend(time_t timeLine, std::vector& filesToSend) { string bufferFilePath = GetBufferFilePath(); if (!CheckExistance(bufferFilePath)) { - if (GetAgentDataDir().find(bufferFilePath) != 0) { + if (GetBufferFileDir().find(bufferFilePath) != 0) { LOG_WARNING(sLogger, ("buffer file path not exist", bufferFilePath)("logtail will not recreate external path", "local secondary does not work")); From 57c67539abb63e62646b7ea83026c6230e8a8815 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 03:14:22 +0000 Subject: [PATCH 04/32] fix --- core/app_config/AppConfig.cpp | 7 ++ core/application/Application.cpp | 11 +- core/common/MachineInfoUtil.cpp | 198 ++++++++++++++++--------------- core/common/MachineInfoUtil.h | 64 +++++----- 4 files changed, 151 insertions(+), 129 deletions(-) diff --git a/core/app_config/AppConfig.cpp b/core/app_config/AppConfig.cpp index 5167cbda66..a90aa9f199 100644 --- a/core/app_config/AppConfig.cpp +++ b/core/app_config/AppConfig.cpp @@ -375,6 +375,13 @@ std::string GetAgentDataDir() { dir = STRING_FLAG(loongcollector_data_dir) + PATH_SEPARATOR; } #endif + if (!CheckExistance(dir)) { + if (Mkdirs(dir)) { + LOG_INFO(sLogger, ("create data dir success", dir)); + } else { + LOG_ERROR(sLogger, ("create data dir failed", dir)); + } + } return dir; } diff --git a/core/application/Application.cpp b/core/application/Application.cpp index 9804cdf2fd..0d7d4a4e87 100644 --- a/core/application/Application.cpp +++ b/core/application/Application.cpp @@ -106,6 +106,15 @@ void Application::Init() { } AppConfig::GetInstance()->LoadAppConfig(GetAgentConfigFile()); +#ifdef __ENTERPRISE__ + if (!HostIdentifier::Instance()->GetInstanceIdentity().isReady) { + // not ready, fetch ecs meta + ECSMeta ecsMeta; + if (FetchECSMeta(ecsMeta)) { + HostIdentifier::Instance()->UpdateInstanceIdentity(ecsMeta); + } + } +#endif // Initialize basic information: IP, hostname, etc. LoongCollectorMonitor::GetInstance(); @@ -168,7 +177,7 @@ void Application::Init() { appInfoJson["UUID"] = Json::Value(Application::GetInstance()->GetUUID()); appInfoJson["instance_id"] = Json::Value(Application::GetInstance()->GetInstanceId()); #ifdef __ENTERPRISE__ - appInfoJson["host_id"] = Json::Value(HostIdentifier::Instance()->GetHostId().id); + appInfoJson["host_id"] = Json::Value(HostIdentifier::Instance()->GetInstanceIdentity().hostid.id); appInfoJson[GetVersionTag()] = Json::Value(ILOGTAIL_VERSION); #else appInfoJson[GetVersionTag()] = Json::Value(string(ILOGTAIL_VERSION) + " Community Edition"); diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index ced7868f40..0fff293560 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -26,6 +26,7 @@ #include "FileSystemUtil.h" #include "StringTools.h" #include "common/FileSystemUtil.h" +#include "common/JsonUtil.h" #include "common/UUIDUtil.h" #include "logger/Logger.h" #if defined(__linux__) @@ -503,108 +504,141 @@ size_t FetchECSMetaCallback(char* buffer, size_t size, size_t nmemb, std::string HostIdentifier::HostIdentifier() { #ifdef __ENTERPRISE__ - getECSMetaFromFile(); + mInstanceIdentityFile = GetAgentDataDir() + PATH_SEPARATOR + "instance_identity"; + getInstanceIdentityFromFile(); + updateHostId(); + mInstanceIdentity.getWriteBuffer().hostid = mHostid; + mInstanceIdentity.swap(); + if (mHasGeneratedLocalHostId) { + mInstanceIdentityJson["random-hostid"] = mLocalHostId; + DumpInstanceIdentity(); + } #else - FetchECSMeta(mMetadata); + mInstanceIdentity.getWriteBuffer().hostid = {STRING_FLAG(agent_host_id), Type::CUSTOM}; + mInstanceIdentity.getWriteBuffer().isReady = true; + mInstanceIdentity.swap(); #endif - updateHostId(); } -bool HostIdentifier::UpdateECSMetaAndHostid(const ECSMeta& meta) { +void HostIdentifier::getInstanceIdentityFromFile() { + if (!CheckExistance(mInstanceIdentityFile)) { + return; + } + std::string instanceIdentityStr; + if (ReadFileContent(mInstanceIdentityFile, instanceIdentityStr)) { + Json::Value doc; + std::string errMsg; + if (!ParseJsonTable(instanceIdentityStr, doc, errMsg)) { + LOG_WARNING(sLogger, + ("parse instanceIdentity from file fail", + errMsg)("instanceIdentity", instanceIdentityStr)("file", mInstanceIdentityFile)); + return; + } + mInstanceIdentityJson = std::move(doc); + // 文件存在且不为非法json,则认为instanceIdentity是ready的 + mInstanceIdentity.getWriteBuffer().isReady = true; + if (ParseECSMeta(instanceIdentityStr, mMetadata)) { + mInstanceIdentity.getWriteBuffer().ecsMeta = mMetadata; + } else { + // 不存在ecs meta信息, 则尝试读取下 random-hostid + if (mInstanceIdentityJson.isMember("random-hostid") && mInstanceIdentityJson["random-hostid"].isString()) { + mLocalHostId = mInstanceIdentityJson["random-hostid"].asString(); + } else { + LOG_ERROR(sLogger, + ("instanceIdentity is ready, but no random-hostid and ecs meta found, file", + mInstanceIdentityFile)("instanceIdentity", instanceIdentityStr)); + } + } + } else { + LOG_ERROR(sLogger, + ("read instanceIdentity from file fail, file", mInstanceIdentityFile)("instanceIdentity", + instanceIdentityStr)); + } +} + +bool HostIdentifier::UpdateInstanceIdentity(const ECSMeta& meta) { // 如果 instanceID 发生变化,则更新ecs元数据 if (mMetadata.instanceID != meta.instanceID) { LOG_INFO(sLogger, ("ecs instanceID changed, old instanceID", mMetadata.instanceID)("new instanceID", meta.instanceID)); - { - std::unique_lock lock(mMutex); // 写锁 - mMetadata = meta; - } + mMetadata = meta; updateHostId(); + mInstanceIdentity.getWriteBuffer().isReady = true; + mInstanceIdentity.getWriteBuffer().ecsMeta = mMetadata; + mInstanceIdentity.getWriteBuffer().hostid = mHostid; + mInstanceIdentity.swap(); + // 存在ecs meta信息, 只要dump ecs meta信息即可,无需dump random-hostid + mInstanceIdentityJson.clear(); + mInstanceIdentityJson["instance-id"] = meta.instanceID; + mInstanceIdentityJson["owner-account-id"] = meta.userID; + mInstanceIdentityJson["region-id"] = meta.regionID; + DumpInstanceIdentity(); return true; } return false; } -void HostIdentifier::DumpECSMeta() { - std::string fileName = AppConfig::GetInstance()->GetLoongcollectorConfDir() + PATH_SEPARATOR + "instance_identity"; +void HostIdentifier::DumpInstanceIdentity() { std::string errMsg; - if (!WriteFile(fileName, mMetadataStr, errMsg)) { - LOG_WARNING(sLogger, ("failed to write ecs meta to file", fileName)("error", errMsg)); + if (!WriteFile(mInstanceIdentityFile, mInstanceIdentityJson.toStyledString(), errMsg)) { + LOG_ERROR(sLogger, ("failed to write instanceIdentity to file", mInstanceIdentityFile)("error", errMsg)); } else { - LOG_INFO(sLogger, ("write ecs meta to file success, fileName", fileName)("mMetadataStr", mMetadataStr)); + LOG_INFO(sLogger, + ("write instanceIdentity to file success, fileName", + mInstanceIdentityFile)("instanceIdentity", mInstanceIdentityJson.toStyledString())); } } void HostIdentifier::updateHostId() { - if (!STRING_FLAG(agent_host_id).empty()) { - setHostId(Hostid{STRING_FLAG(agent_host_id), Type::CUSTOM}); - return; - } + Hostid newId; if (mMetadata.isValid && !mMetadata.instanceID.empty()) { - setHostId(Hostid{mMetadata.instanceID, Type::ECS}); - return; - } - getSerialNumberFromEcsAssist(); - if (!mSerialNumber.empty()) { - setHostId(Hostid{mSerialNumber, Type::ECS_ASSIST}); - return; + newId = {mMetadata.instanceID, Type::ECS}; + } else { + getSerialNumberFromEcsAssist(); + if (!mSerialNumber.empty()) { + newId = {mSerialNumber, Type::ECS_ASSIST}; + } else if (!STRING_FLAG(agent_host_id).empty()) { + newId = {STRING_FLAG(agent_host_id), Type::CUSTOM}; + } else { + getLocalHostId(); + newId = {mLocalHostId, Type::LOCAL}; + } } - getLocalHostId(); - setHostId(Hostid{mLocalHostId, Type::LOCAL}); -} - -void HostIdentifier::setHostId(const Hostid& hostid) { - if (mHostid.id == hostid.id && mHostid.type == hostid.type) { - return; + // 只在ID发生变化时更新并记录日志 + if (mHostid.id != newId.id || mHostid.type != newId.type) { + LOG_INFO(sLogger, + ("change hostId, id from", mHostid.id)("to", newId.id)("type from", mHostid.type)("to", newId.type)); + mHostid = newId; } - LOG_INFO(sLogger, - ("change hostId, id from", mHostid.id)("to", hostid.id)("type from", mHostid.type)("to", hostid.type)); - std::unique_lock lock(mMutex); // 写锁 - mHostid = hostid; } bool ParseECSMeta(const std::string& meta, ECSMeta& metaObj) { - rapidjson::Document doc; - doc.Parse(meta.c_str()); - if (doc.HasParseError() || !doc.IsObject()) { - LOG_WARNING(sLogger, ("fetch ecs meta fail", meta)); + Json::Value doc; + std::string errMsg; + if (!ParseJsonTable(meta, doc, errMsg)) { + LOG_WARNING(sLogger, ("parse ecs meta fail, errMsg", errMsg)("meta", meta)); return false; } - if (const auto instanceItr = doc.FindMember("instance-id"); - instanceItr != doc.MemberEnd() && instanceItr->value.IsString()) { - metaObj.instanceID = instanceItr->value.GetString(); + if (doc.isMember("instance-id") && doc["instance-id"].isString()) { + metaObj.instanceID = doc["instance-id"].asString(); } - if (const auto userItr = doc.FindMember("owner-account-id"); - userItr != doc.MemberEnd() && userItr->value.IsString()) { - metaObj.userID = userItr->value.GetString(); + if (doc.isMember("owner-account-id") && doc["owner-account-id"].isString()) { + metaObj.userID = doc["owner-account-id"].asString(); } - if (const auto regionItr = doc.FindMember("region-id"); - regionItr != doc.MemberEnd() && regionItr->value.IsString()) { - metaObj.regionID = regionItr->value.GetString(); + if (doc.isMember("region-id") && doc["region-id"].isString()) { + metaObj.regionID = doc["region-id"].asString(); } if (!metaObj.instanceID.empty() && !metaObj.userID.empty() && !metaObj.regionID.empty()) { metaObj.isValid = true; + return true; } - return true; -} - -void HostIdentifier::getECSMetaFromFile() { - std::string fileName = AppConfig::GetInstance()->GetLoongcollectorConfDir() + PATH_SEPARATOR + "instance_identity"; - if (!CheckExistance(fileName)) { - return; - } - std::string ecsMetaStr; - if (ReadFileContent(fileName, ecsMetaStr) && ParseECSMeta(ecsMetaStr, mMetadata)) { - return; - } else { - LOG_ERROR(sLogger, ("read ecs meta from file fail", fileName)("ecs meta", ecsMetaStr)); - } + return false; } -bool HostIdentifier::FetchECSMeta(ECSMeta& metaObj) { +bool FetchECSMeta(ECSMeta& metaObj) { metaObj.isValid = false; CURL* curl = nullptr; for (size_t retryTimes = 1; retryTimes <= 5; retryTimes++) { @@ -626,7 +660,8 @@ bool HostIdentifier::FetchECSMeta(ECSMeta& metaObj) { curl_easy_setopt(curl, CURLOPT_HTTPHEADER, tokenHeaders); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); + // 超时1秒 + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &token); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, FetchECSMetaCallback); @@ -652,7 +687,8 @@ bool HostIdentifier::FetchECSMeta(ECSMeta& metaObj) { curl_easy_setopt(curl, CURLOPT_HTTPHEADER, metaHeaders); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); + // 超时1秒 + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &meta); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, FetchECSMetaCallback); @@ -668,7 +704,6 @@ bool HostIdentifier::FetchECSMeta(ECSMeta& metaObj) { curl_easy_cleanup(curl); return false; } - mMetadataStr = meta; curl_easy_cleanup(curl); return metaObj.isValid; } @@ -692,38 +727,11 @@ void HostIdentifier::getSerialNumberFromEcsAssist() { } void HostIdentifier::getLocalHostId() { - std::string fileName = AppConfig::GetInstance()->GetLoongcollectorConfDir() + PATH_SEPARATOR + "host_id"; if (!mLocalHostId.empty()) { return; } - if (CheckExistance(fileName)) { - if (!ReadFileContent(fileName, mLocalHostId)) { - mLocalHostId = ""; - } - } - if (mLocalHostId.empty()) { - // 随机生成hostid - mLocalHostId = CalculateRandomUUID(); - - LOG_INFO(sLogger, ("save hostId file to local file system, hostId", mLocalHostId)); - int fd = open(fileName.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0755); - if (fd == -1) { - int savedErrno = errno; - if (savedErrno != EEXIST) { - LOG_ERROR(sLogger, ("save hostId file fail", fileName)("errno", strerror(savedErrno))); - } - } else { - // 文件成功创建,现在写入hostId - ssize_t written = write(fd, mLocalHostId.c_str(), mLocalHostId.length()); - if (written == static_cast(mLocalHostId.length())) { - LOG_INFO(sLogger, ("hostId saved successfully to", fileName)); - } else { - int writeErrno = errno; - LOG_ERROR(sLogger, ("Failed to write hostId to file", fileName)("errno", strerror(writeErrno))); - } - close(fd); - } - } + mHasGeneratedLocalHostId = true; + mLocalHostId = CalculateRandomUUID(); } } // namespace logtail diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index b494348b1a..91c78d545f 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -16,12 +16,13 @@ #pragma once -#include - -#include #include #include +#include "json/value.h" + +#include "AppConfig.h" + namespace logtail { struct ECSMeta { @@ -30,6 +31,22 @@ struct ECSMeta { std::string userID; std::string regionID; }; +enum Type { + CUSTOM, + ECS, + ECS_ASSIST, + LOCAL, +}; +struct Hostid { + std::string id; + Type type; +}; +struct InstanceIdentity { + bool isReady = false; + ECSMeta ecsMeta; + Hostid hostid; +}; + std::string GetOsDetail(); std::string GetUsername(); std::string GetHostName(); @@ -49,53 +66,32 @@ bool IsDigitsDotsHostname(const char* hostname); // NOTE: logger must be initialized before calling this. std::string GetAnyAvailableIP(); +bool FetchECSMeta(ECSMeta& metaObj); + class HostIdentifier { public: - enum Type { - CUSTOM, - ECS, - ECS_ASSIST, - LOCAL, - }; - struct Hostid { - std::string id; - Type type; - }; HostIdentifier(); static HostIdentifier* Instance() { static HostIdentifier sInstance; return &sInstance; } - // 注意: 不要在类初始化时调用并缓存结果,因为此时ECS元数据可能尚未就绪 - // 建议在实际使用时再调用此方法 - HostIdentifier::Hostid GetHostId() { - std::shared_lock lock(mMutex); // 获取读锁 - return mHostid; - } // 注意: 不要在类初始化时调用并缓存结果,因为此时ECS元数据可能尚未就绪 // 建议在实际使用时再调用此方法 - ECSMeta GetECSMeta() { - std::shared_lock lock(mMutex); // 获取读锁 - return mMetadata; - } + InstanceIdentity GetInstanceIdentity() { return mInstanceIdentity.getReadBuffer(); } - bool UpdateECSMetaAndHostid(const ECSMeta& meta); - void DumpECSMeta(); + bool UpdateInstanceIdentity(const ECSMeta& meta); + void DumpInstanceIdentity(); private: - bool FetchECSMeta(ECSMeta& metaObj); - - void getECSMetaFromFile(); + // 从文件获取ecs元数据 + void getInstanceIdentityFromFile(); // 从云助手获取序列号 void getSerialNumberFromEcsAssist(); // 从本地文件获取hostid void getLocalHostId(); void updateHostId(); - void setHostId(const Hostid& hostid); - - std::shared_mutex mMutex; #if defined(_MSC_VER) std::string mEcsAssistMachineIdFile = "C:\\ProgramData\\aliyun\\assist\\hybrid\\machine-id"; @@ -105,11 +101,13 @@ class HostIdentifier { bool mHasTriedToGetSerialNumber = false; std::string mSerialNumber; + DoubleBuffer mInstanceIdentity; Hostid mHostid; ECSMeta mMetadata; - std::string mMetadataStr; - + Json::Value mInstanceIdentityJson; + std::string mInstanceIdentityFile; + bool mHasGeneratedLocalHostId = false; std::string mLocalHostId; #ifdef __ENTERPRISE__ friend class EnterpriseConfigProvider; From 38665c3a469913ef8cf83acbc092ce4df25c00ab Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 03:16:18 +0000 Subject: [PATCH 05/32] fix --- core/common/MachineInfoUtil.cpp | 52 ++++++++++++++++----------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 0fff293560..dd4c152e54 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -502,6 +502,32 @@ size_t FetchECSMetaCallback(char* buffer, size_t size, size_t nmemb, std::string return sizes; } +bool ParseECSMeta(const std::string& meta, ECSMeta& metaObj) { + Json::Value doc; + std::string errMsg; + if (!ParseJsonTable(meta, doc, errMsg)) { + LOG_WARNING(sLogger, ("parse ecs meta fail, errMsg", errMsg)("meta", meta)); + return false; + } + + if (doc.isMember("instance-id") && doc["instance-id"].isString()) { + metaObj.instanceID = doc["instance-id"].asString(); + } + + if (doc.isMember("owner-account-id") && doc["owner-account-id"].isString()) { + metaObj.userID = doc["owner-account-id"].asString(); + } + + if (doc.isMember("region-id") && doc["region-id"].isString()) { + metaObj.regionID = doc["region-id"].asString(); + } + if (!metaObj.instanceID.empty() && !metaObj.userID.empty() && !metaObj.regionID.empty()) { + metaObj.isValid = true; + return true; + } + return false; +} + HostIdentifier::HostIdentifier() { #ifdef __ENTERPRISE__ mInstanceIdentityFile = GetAgentDataDir() + PATH_SEPARATOR + "instance_identity"; @@ -612,32 +638,6 @@ void HostIdentifier::updateHostId() { } } -bool ParseECSMeta(const std::string& meta, ECSMeta& metaObj) { - Json::Value doc; - std::string errMsg; - if (!ParseJsonTable(meta, doc, errMsg)) { - LOG_WARNING(sLogger, ("parse ecs meta fail, errMsg", errMsg)("meta", meta)); - return false; - } - - if (doc.isMember("instance-id") && doc["instance-id"].isString()) { - metaObj.instanceID = doc["instance-id"].asString(); - } - - if (doc.isMember("owner-account-id") && doc["owner-account-id"].isString()) { - metaObj.userID = doc["owner-account-id"].asString(); - } - - if (doc.isMember("region-id") && doc["region-id"].isString()) { - metaObj.regionID = doc["region-id"].asString(); - } - if (!metaObj.instanceID.empty() && !metaObj.userID.empty() && !metaObj.regionID.empty()) { - metaObj.isValid = true; - return true; - } - return false; -} - bool FetchECSMeta(ECSMeta& metaObj) { metaObj.isValid = false; CURL* curl = nullptr; From f4eb25a4754f745f4cf897585cc737c4836831b4 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 03:26:11 +0000 Subject: [PATCH 06/32] fix --- core/application/Application.cpp | 2 ++ core/common/MachineInfoUtil.h | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/core/application/Application.cpp b/core/application/Application.cpp index 0d7d4a4e87..2906e4cb0a 100644 --- a/core/application/Application.cpp +++ b/core/application/Application.cpp @@ -113,6 +113,8 @@ void Application::Init() { if (FetchECSMeta(ecsMeta)) { HostIdentifier::Instance()->UpdateInstanceIdentity(ecsMeta); } + // 不管ecs meta是否获取成功,都设置instanceIdentity为ready + HostIdentifier::Instance()->SetInstanceIdentityReady(); } #endif diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index 91c78d545f..36c25c0c81 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -82,6 +82,11 @@ class HostIdentifier { bool UpdateInstanceIdentity(const ECSMeta& meta); void DumpInstanceIdentity(); + bool SetInstanceIdentityReady() { + mInstanceIdentity.getWriteBuffer() = mInstanceIdentity.getReadBuffer(); + mInstanceIdentity.getWriteBuffer().isReady = true; + mInstanceIdentity.swap(); + }; private: // 从文件获取ecs元数据 From f8450e0ff02dc1b8494d6b80e7a9c5c2025e9d0e Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 03:27:52 +0000 Subject: [PATCH 07/32] fix --- core/common/MachineInfoUtil.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index 36c25c0c81..cf2204d637 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -82,7 +82,7 @@ class HostIdentifier { bool UpdateInstanceIdentity(const ECSMeta& meta); void DumpInstanceIdentity(); - bool SetInstanceIdentityReady() { + void SetInstanceIdentityReady() { mInstanceIdentity.getWriteBuffer() = mInstanceIdentity.getReadBuffer(); mInstanceIdentity.getWriteBuffer().isReady = true; mInstanceIdentity.swap(); From 64b099ce5d7c46a62388412a2186a9443a867479 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 03:37:52 +0000 Subject: [PATCH 08/32] fix --- core/common/MachineInfoUtil.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index dd4c152e54..86ecdb5d6e 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -49,6 +49,11 @@ DEFINE_FLAG_STRING(agent_host_id, "", ""); +const std::string sInstanceIdKey = "instance-id"; +const std::string sOwnerAccountIdKey = "owner-account-id"; +const std::string sRegionIdKey = "region-id"; +const std::string sRandomHostIdKey = "random-hostid"; + #if defined(_MSC_VER) typedef LONG NTSTATUS, *PNTSTATUS; #define STATUS_SUCCESS (0x00000000) @@ -510,16 +515,16 @@ bool ParseECSMeta(const std::string& meta, ECSMeta& metaObj) { return false; } - if (doc.isMember("instance-id") && doc["instance-id"].isString()) { - metaObj.instanceID = doc["instance-id"].asString(); + if (doc.isMember(sInstanceIdKey) && doc[sInstanceIdKey].isString()) { + metaObj.instanceID = doc[sInstanceIdKey].asString(); } - if (doc.isMember("owner-account-id") && doc["owner-account-id"].isString()) { - metaObj.userID = doc["owner-account-id"].asString(); + if (doc.isMember(sOwnerAccountIdKey) && doc[sOwnerAccountIdKey].isString()) { + metaObj.userID = doc[sOwnerAccountIdKey].asString(); } - if (doc.isMember("region-id") && doc["region-id"].isString()) { - metaObj.regionID = doc["region-id"].asString(); + if (doc.isMember(sRegionIdKey) && doc[sRegionIdKey].isString()) { + metaObj.regionID = doc[sRegionIdKey].asString(); } if (!metaObj.instanceID.empty() && !metaObj.userID.empty() && !metaObj.regionID.empty()) { metaObj.isValid = true; @@ -536,7 +541,7 @@ HostIdentifier::HostIdentifier() { mInstanceIdentity.getWriteBuffer().hostid = mHostid; mInstanceIdentity.swap(); if (mHasGeneratedLocalHostId) { - mInstanceIdentityJson["random-hostid"] = mLocalHostId; + mInstanceIdentityJson[sRandomHostIdKey] = mLocalHostId; DumpInstanceIdentity(); } #else @@ -567,8 +572,9 @@ void HostIdentifier::getInstanceIdentityFromFile() { mInstanceIdentity.getWriteBuffer().ecsMeta = mMetadata; } else { // 不存在ecs meta信息, 则尝试读取下 random-hostid - if (mInstanceIdentityJson.isMember("random-hostid") && mInstanceIdentityJson["random-hostid"].isString()) { - mLocalHostId = mInstanceIdentityJson["random-hostid"].asString(); + if (mInstanceIdentityJson.isMember(sRandomHostIdKey) + && mInstanceIdentityJson[sRandomHostIdKey].isString()) { + mLocalHostId = mInstanceIdentityJson[sRandomHostIdKey].asString(); } else { LOG_ERROR(sLogger, ("instanceIdentity is ready, but no random-hostid and ecs meta found, file", @@ -595,9 +601,9 @@ bool HostIdentifier::UpdateInstanceIdentity(const ECSMeta& meta) { mInstanceIdentity.swap(); // 存在ecs meta信息, 只要dump ecs meta信息即可,无需dump random-hostid mInstanceIdentityJson.clear(); - mInstanceIdentityJson["instance-id"] = meta.instanceID; - mInstanceIdentityJson["owner-account-id"] = meta.userID; - mInstanceIdentityJson["region-id"] = meta.regionID; + mInstanceIdentityJson[sInstanceIdKey] = meta.instanceID; + mInstanceIdentityJson[sOwnerAccountIdKey] = meta.userID; + mInstanceIdentityJson[sRegionIdKey] = meta.regionID; DumpInstanceIdentity(); return true; } From 7a2d48dc0e08dae7ee1d023c13d39f3d90c43936 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 03:46:36 +0000 Subject: [PATCH 09/32] fix --- core/plugin/flusher/sls/SLSClientManager.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/plugin/flusher/sls/SLSClientManager.cpp b/core/plugin/flusher/sls/SLSClientManager.cpp index 42a3b7f2f4..983c2e5213 100644 --- a/core/plugin/flusher/sls/SLSClientManager.cpp +++ b/core/plugin/flusher/sls/SLSClientManager.cpp @@ -14,6 +14,8 @@ #include "plugin/flusher/sls/SLSClientManager.h" +#include "MachineInfoUtil.h" + #ifdef __linux__ #include #endif @@ -108,7 +110,7 @@ string SLSClientManager::GetRunningEnvironment() { // containers in K8S will possess the above env if (AppConfig::GetInstance()->IsPurageContainerMode()) { env = "K8S-Daemonset"; - } else if (PingEndpoint("100.100.100.200", "/latest/meta-data")) { + } else if (HostIdentifier::Instance()->GetInstanceIdentity().ecsMeta.isValid) { // containers in ACK can be connected to the above address, see // https://help.aliyun.com/document_detail/108460.html#section-akf-lwh-1gb. // Note: we can not distinguish ACK from K8S built on ECS @@ -118,7 +120,7 @@ string SLSClientManager::GetRunningEnvironment() { } } else if (AppConfig::GetInstance()->IsPurageContainerMode() || getenv("ALIYUN_LOGTAIL_CONFIG")) { env = "Docker"; - } else if (PingEndpoint("100.100.100.200", "/latest/meta-data")) { + } else if (HostIdentifier::Instance()->GetInstanceIdentity().ecsMeta.isValid) { env = "ECS"; } else { env = "Others"; From e983c8b626fd6d267cc18285227494476b570741 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 03:48:13 +0000 Subject: [PATCH 10/32] fix --- core/plugin/flusher/sls/SLSClientManager.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/plugin/flusher/sls/SLSClientManager.cpp b/core/plugin/flusher/sls/SLSClientManager.cpp index 983c2e5213..84f2b998d8 100644 --- a/core/plugin/flusher/sls/SLSClientManager.cpp +++ b/core/plugin/flusher/sls/SLSClientManager.cpp @@ -14,8 +14,6 @@ #include "plugin/flusher/sls/SLSClientManager.h" -#include "MachineInfoUtil.h" - #ifdef __linux__ #include #endif @@ -23,6 +21,7 @@ #include "app_config/AppConfig.h" #include "common/Flags.h" #include "common/HashUtil.h" +#include "common/MachineInfoUtil.h" #include "common/StringTools.h" #include "common/http/Constant.h" #include "common/http/Curl.h" From 11dd424b5a46ce9bd9c8f272c1abc234bc6ed691 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 06:08:52 +0000 Subject: [PATCH 11/32] =?UTF-8?q?=E6=8C=87=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/common/MachineInfoUtil.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index cf2204d637..030a651969 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -78,7 +78,7 @@ class HostIdentifier { // 注意: 不要在类初始化时调用并缓存结果,因为此时ECS元数据可能尚未就绪 // 建议在实际使用时再调用此方法 - InstanceIdentity GetInstanceIdentity() { return mInstanceIdentity.getReadBuffer(); } + const InstanceIdentity* GetInstanceIdentity() { return &mInstanceIdentity.getReadBuffer(); } bool UpdateInstanceIdentity(const ECSMeta& meta); void DumpInstanceIdentity(); From e775601d4750a61ed3f847c2ec3b28145b8f0b12 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 07:14:46 +0000 Subject: [PATCH 12/32] fix --- core/common/MachineInfoUtil.cpp | 43 +++++++------- core/common/MachineInfoUtil.h | 56 +++++++++++++++++-- core/plugin/flusher/sls/SLSClientManager.cpp | 4 +- core/unittest/common/CMakeLists.txt | 3 + .../common/InstanceIdentityUnittest.cpp | 47 ++++++++++++++++ 5 files changed, 123 insertions(+), 30 deletions(-) create mode 100644 core/unittest/common/InstanceIdentityUnittest.cpp diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 86ecdb5d6e..951955fe0a 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -516,18 +516,17 @@ bool ParseECSMeta(const std::string& meta, ECSMeta& metaObj) { } if (doc.isMember(sInstanceIdKey) && doc[sInstanceIdKey].isString()) { - metaObj.instanceID = doc[sInstanceIdKey].asString(); + metaObj.SetInstanceID(doc[sInstanceIdKey].asString()); } if (doc.isMember(sOwnerAccountIdKey) && doc[sOwnerAccountIdKey].isString()) { - metaObj.userID = doc[sOwnerAccountIdKey].asString(); + metaObj.SetUserID(doc[sOwnerAccountIdKey].asString()); } if (doc.isMember(sRegionIdKey) && doc[sRegionIdKey].isString()) { - metaObj.regionID = doc[sRegionIdKey].asString(); + metaObj.SetRegionID(doc[sRegionIdKey].asString()); } - if (!metaObj.instanceID.empty() && !metaObj.userID.empty() && !metaObj.regionID.empty()) { - metaObj.isValid = true; + if (!metaObj.GetInstanceID().empty() && !metaObj.GetUserID().empty() && !metaObj.GetRegionID().empty()) { return true; } return false; @@ -538,15 +537,15 @@ HostIdentifier::HostIdentifier() { mInstanceIdentityFile = GetAgentDataDir() + PATH_SEPARATOR + "instance_identity"; getInstanceIdentityFromFile(); updateHostId(); - mInstanceIdentity.getWriteBuffer().hostid = mHostid; + mInstanceIdentity.getWriteBuffer().SetHostID(mHostid); mInstanceIdentity.swap(); if (mHasGeneratedLocalHostId) { mInstanceIdentityJson[sRandomHostIdKey] = mLocalHostId; DumpInstanceIdentity(); } #else - mInstanceIdentity.getWriteBuffer().hostid = {STRING_FLAG(agent_host_id), Type::CUSTOM}; - mInstanceIdentity.getWriteBuffer().isReady = true; + mInstanceIdentity.getWriteBuffer().SetHostID({STRING_FLAG(agent_host_id), Type::CUSTOM}); + mInstanceIdentity.getWriteBuffer().SetReady(true); mInstanceIdentity.swap(); #endif } @@ -567,9 +566,9 @@ void HostIdentifier::getInstanceIdentityFromFile() { } mInstanceIdentityJson = std::move(doc); // 文件存在且不为非法json,则认为instanceIdentity是ready的 - mInstanceIdentity.getWriteBuffer().isReady = true; + mInstanceIdentity.getWriteBuffer().SetReady(true); if (ParseECSMeta(instanceIdentityStr, mMetadata)) { - mInstanceIdentity.getWriteBuffer().ecsMeta = mMetadata; + mInstanceIdentity.getWriteBuffer().SetECSMeta(mMetadata); } else { // 不存在ecs meta信息, 则尝试读取下 random-hostid if (mInstanceIdentityJson.isMember(sRandomHostIdKey) @@ -590,20 +589,21 @@ void HostIdentifier::getInstanceIdentityFromFile() { bool HostIdentifier::UpdateInstanceIdentity(const ECSMeta& meta) { // 如果 instanceID 发生变化,则更新ecs元数据 - if (mMetadata.instanceID != meta.instanceID) { + if (mMetadata.GetInstanceID() != meta.GetInstanceID()) { LOG_INFO(sLogger, - ("ecs instanceID changed, old instanceID", mMetadata.instanceID)("new instanceID", meta.instanceID)); + ("ecs instanceID changed, old instanceID", + mMetadata.GetInstanceID().to_string())("new instanceID", meta.GetInstanceID().to_string())); mMetadata = meta; updateHostId(); - mInstanceIdentity.getWriteBuffer().isReady = true; - mInstanceIdentity.getWriteBuffer().ecsMeta = mMetadata; - mInstanceIdentity.getWriteBuffer().hostid = mHostid; + mInstanceIdentity.getWriteBuffer().SetReady(true); + mInstanceIdentity.getWriteBuffer().SetECSMeta(mMetadata); + mInstanceIdentity.getWriteBuffer().SetHostID(mHostid); mInstanceIdentity.swap(); // 存在ecs meta信息, 只要dump ecs meta信息即可,无需dump random-hostid mInstanceIdentityJson.clear(); - mInstanceIdentityJson[sInstanceIdKey] = meta.instanceID; - mInstanceIdentityJson[sOwnerAccountIdKey] = meta.userID; - mInstanceIdentityJson[sRegionIdKey] = meta.regionID; + mInstanceIdentityJson[sInstanceIdKey] = meta.GetInstanceID().to_string(); + mInstanceIdentityJson[sOwnerAccountIdKey] = meta.GetUserID().to_string(); + mInstanceIdentityJson[sRegionIdKey] = meta.GetRegionID().to_string(); DumpInstanceIdentity(); return true; } @@ -623,8 +623,8 @@ void HostIdentifier::DumpInstanceIdentity() { void HostIdentifier::updateHostId() { Hostid newId; - if (mMetadata.isValid && !mMetadata.instanceID.empty()) { - newId = {mMetadata.instanceID, Type::ECS}; + if (mMetadata.IsValid() && !mMetadata.GetInstanceID().empty()) { + newId = {mMetadata.GetInstanceID().to_string(), Type::ECS}; } else { getSerialNumberFromEcsAssist(); if (!mSerialNumber.empty()) { @@ -645,7 +645,6 @@ void HostIdentifier::updateHostId() { } bool FetchECSMeta(ECSMeta& metaObj) { - metaObj.isValid = false; CURL* curl = nullptr; for (size_t retryTimes = 1; retryTimes <= 5; retryTimes++) { curl = curl_easy_init(); @@ -711,7 +710,7 @@ bool FetchECSMeta(ECSMeta& metaObj) { return false; } curl_easy_cleanup(curl); - return metaObj.isValid; + return metaObj.IsValid(); } LOG_WARNING( sLogger, diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index 030a651969..8b6f464f10 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -16,20 +16,50 @@ #pragma once +#include #include #include #include "json/value.h" #include "AppConfig.h" +#include "models/StringView.h" namespace logtail { struct ECSMeta { - bool isValid = false; - std::string instanceID; - std::string userID; - std::string regionID; + ECSMeta() = default; + + void SetInstanceID(const std::string& id) { SetID(id, instanceID, instanceIDLen); } + + void SetUserID(const std::string& id) { SetID(id, userID, userIDLen); } + + void SetRegionID(const std::string& id) { SetID(id, regionID, regionIDLen); } + + [[nodiscard]] StringView GetInstanceID() const { return StringView(instanceID.data(), instanceIDLen); } + [[nodiscard]] StringView GetUserID() const { return StringView(userID.data(), userIDLen); } + [[nodiscard]] StringView GetRegionID() const { return StringView(regionID.data(), regionIDLen); } + + [[nodiscard]] bool IsValid() const { return instanceIDLen > 0 && userIDLen > 0 && regionIDLen > 0; } + +private: + static const size_t ID_MAX_LENGTH = 128; + + std::array instanceID{}; + size_t instanceIDLen = 0UL; + + std::array userID{}; + size_t userIDLen = 0UL; + + std::array regionID{}; + size_t regionIDLen = 0UL; + + template + void SetID(const std::string& id, std::array& target, size_t& targetLen) { + targetLen = std::min(id.size(), N - 1); + std::copy_n(id.begin(), targetLen, target.begin()); + target[targetLen] = '\0'; + } }; enum Type { CUSTOM, @@ -41,7 +71,21 @@ struct Hostid { std::string id; Type type; }; -struct InstanceIdentity { +class InstanceIdentity { +public: + bool IsReady() const { return isReady; } + bool IsECSValid() const { return ecsMeta.IsValid(); } + StringView GetEcsInstanceID() const { return ecsMeta.GetInstanceID(); } + StringView GetEcsUserID() const { return ecsMeta.GetUserID(); } + StringView GetEcsRegionID() const { return ecsMeta.GetRegionID(); } + StringView GetHostID() const { return hostid.id; } + Type GetHostIdType() const { return hostid.type; } + + void SetReady(bool ready) { isReady = ready; } + void SetECSMeta(const ECSMeta& meta) { ecsMeta = meta; } + void SetHostID(const Hostid& hostid) { this->hostid = hostid; } + +private: bool isReady = false; ECSMeta ecsMeta; Hostid hostid; @@ -84,7 +128,7 @@ class HostIdentifier { void DumpInstanceIdentity(); void SetInstanceIdentityReady() { mInstanceIdentity.getWriteBuffer() = mInstanceIdentity.getReadBuffer(); - mInstanceIdentity.getWriteBuffer().isReady = true; + mInstanceIdentity.getWriteBuffer().SetReady(true); mInstanceIdentity.swap(); }; diff --git a/core/plugin/flusher/sls/SLSClientManager.cpp b/core/plugin/flusher/sls/SLSClientManager.cpp index 84f2b998d8..1d4f68b3e8 100644 --- a/core/plugin/flusher/sls/SLSClientManager.cpp +++ b/core/plugin/flusher/sls/SLSClientManager.cpp @@ -109,7 +109,7 @@ string SLSClientManager::GetRunningEnvironment() { // containers in K8S will possess the above env if (AppConfig::GetInstance()->IsPurageContainerMode()) { env = "K8S-Daemonset"; - } else if (HostIdentifier::Instance()->GetInstanceIdentity().ecsMeta.isValid) { + } else if (HostIdentifier::Instance()->GetInstanceIdentity()->IsECSValid()) { // containers in ACK can be connected to the above address, see // https://help.aliyun.com/document_detail/108460.html#section-akf-lwh-1gb. // Note: we can not distinguish ACK from K8S built on ECS @@ -119,7 +119,7 @@ string SLSClientManager::GetRunningEnvironment() { } } else if (AppConfig::GetInstance()->IsPurageContainerMode() || getenv("ALIYUN_LOGTAIL_CONFIG")) { env = "Docker"; - } else if (HostIdentifier::Instance()->GetInstanceIdentity().ecsMeta.isValid) { + } else if (HostIdentifier::Instance()->GetInstanceIdentity()->IsECSValid()) { env = "ECS"; } else { env = "Others"; diff --git a/core/unittest/common/CMakeLists.txt b/core/unittest/common/CMakeLists.txt index 80de5c490b..288129372f 100644 --- a/core/unittest/common/CMakeLists.txt +++ b/core/unittest/common/CMakeLists.txt @@ -24,6 +24,9 @@ target_link_libraries(common_simple_utils_unittest ${UT_BASE_TARGET}) add_executable(common_logfileoperator_unittest LogFileOperatorUnittest.cpp) target_link_libraries(common_logfileoperator_unittest ${UT_BASE_TARGET}) +add_executable(instance_identity_unittest InstanceIdentityUnittest.cpp) +target_link_libraries(instance_identity_unittest ${UT_BASE_TARGET}) + add_executable(common_sliding_window_counter_unittest SlidingWindowCounterUnittest.cpp) target_link_libraries(common_sliding_window_counter_unittest ${UT_BASE_TARGET}) diff --git a/core/unittest/common/InstanceIdentityUnittest.cpp b/core/unittest/common/InstanceIdentityUnittest.cpp new file mode 100644 index 0000000000..cc65dba9d4 --- /dev/null +++ b/core/unittest/common/InstanceIdentityUnittest.cpp @@ -0,0 +1,47 @@ +// Copyright 2023 iLogtail Authors +// +// 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. + + +#include "MachineInfoUtil.h" +#include "unittest/Unittest.h" + +namespace logtail { + +class InstanceIdentityUnittest : public ::testing::Test { +public: + void TestECSMeta(); +}; +UNIT_TEST_CASE(InstanceIdentityUnittest, TestECSMeta); + +void InstanceIdentityUnittest::TestECSMeta() { + { + ECSMeta meta; + meta.SetInstanceID("i-1234567890"); + meta.SetUserID("1234567890"); + meta.SetRegionID("cn-hangzhou"); + APSARA_TEST_TRUE(meta.IsValid()); + APSARA_TEST_EQUAL(meta.GetInstanceID().to_string(), "i-1234567890"); + APSARA_TEST_EQUAL(meta.GetUserID().to_string(), "1234567890"); + APSARA_TEST_EQUAL(meta.GetRegionID().to_string(), "cn-hangzhou"); + } + { + ECSMeta meta; + meta.SetInstanceID(""); + meta.SetUserID("1234567890"); + meta.SetRegionID("cn-hangzhou"); + APSARA_TEST_FALSE(meta.IsValid()); + } +} + +} // namespace logtail From f0e555fb04c693fa5ec57aec8595cf16f0c75a0d Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 07:28:08 +0000 Subject: [PATCH 13/32] fix --- core/common/MachineInfoUtil.h | 1 + .../common/InstanceIdentityUnittest.cpp | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index 8b6f464f10..df3711378e 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -60,6 +60,7 @@ struct ECSMeta { std::copy_n(id.begin(), targetLen, target.begin()); target[targetLen] = '\0'; } + friend class InstanceIdentityUnittest; }; enum Type { CUSTOM, diff --git a/core/unittest/common/InstanceIdentityUnittest.cpp b/core/unittest/common/InstanceIdentityUnittest.cpp index cc65dba9d4..032bc667d7 100644 --- a/core/unittest/common/InstanceIdentityUnittest.cpp +++ b/core/unittest/common/InstanceIdentityUnittest.cpp @@ -13,15 +13,20 @@ // limitations under the License. +#include + #include "MachineInfoUtil.h" #include "unittest/Unittest.h" namespace logtail { +static const size_t ID_MAX_LENGTH = ECSMeta::ID_MAX_LENGTH; + class InstanceIdentityUnittest : public ::testing::Test { public: void TestECSMeta(); }; + UNIT_TEST_CASE(InstanceIdentityUnittest, TestECSMeta); void InstanceIdentityUnittest::TestECSMeta() { @@ -42,6 +47,34 @@ void InstanceIdentityUnittest::TestECSMeta() { meta.SetRegionID("cn-hangzhou"); APSARA_TEST_FALSE(meta.IsValid()); } + { + ECSMeta meta; + for (size_t i = 0; i < ID_MAX_LENGTH; ++i) { + APSARA_TEST_EQUAL(meta.instanceID[i], '\0'); + APSARA_TEST_EQUAL(meta.userID[i], '\0'); + APSARA_TEST_EQUAL(meta.regionID[i], '\0'); + } + } + { + ECSMeta meta; + std::array testString{}; + for (size_t i = 0; i < testString.size(); ++i) { + testString[i] = 'a'; + } + meta.SetInstanceID(testString.data()); + meta.SetUserID(testString.data()); + meta.SetRegionID(testString.data()); + APSARA_TEST_TRUE(meta.IsValid()); + APSARA_TEST_EQUAL(meta.GetInstanceID().to_string(), StringView(testString.data(), ID_MAX_LENGTH - 1)); + APSARA_TEST_EQUAL(meta.GetUserID().to_string(), StringView(testString.data(), ID_MAX_LENGTH - 1)); + APSARA_TEST_EQUAL(meta.GetRegionID().to_string(), StringView(testString.data(), ID_MAX_LENGTH - 1)); + + APSARA_TEST_EQUAL(meta.GetInstanceID().size(), ID_MAX_LENGTH - 1); + APSARA_TEST_EQUAL(meta.GetUserID().size(), ID_MAX_LENGTH - 1); + APSARA_TEST_EQUAL(meta.GetRegionID().size(), ID_MAX_LENGTH - 1); + } } } // namespace logtail + +UNIT_TEST_MAIN From f7ff4343027f84283a03be7d582e4096fc01ef07 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 07:33:31 +0000 Subject: [PATCH 14/32] fix --- core/application/Application.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/application/Application.cpp b/core/application/Application.cpp index 2906e4cb0a..c988dd7fab 100644 --- a/core/application/Application.cpp +++ b/core/application/Application.cpp @@ -107,7 +107,7 @@ void Application::Init() { AppConfig::GetInstance()->LoadAppConfig(GetAgentConfigFile()); #ifdef __ENTERPRISE__ - if (!HostIdentifier::Instance()->GetInstanceIdentity().isReady) { + if (!HostIdentifier::Instance()->GetInstanceIdentity()->IsReady()) { // not ready, fetch ecs meta ECSMeta ecsMeta; if (FetchECSMeta(ecsMeta)) { @@ -179,7 +179,7 @@ void Application::Init() { appInfoJson["UUID"] = Json::Value(Application::GetInstance()->GetUUID()); appInfoJson["instance_id"] = Json::Value(Application::GetInstance()->GetInstanceId()); #ifdef __ENTERPRISE__ - appInfoJson["host_id"] = Json::Value(HostIdentifier::Instance()->GetInstanceIdentity().hostid.id); + appInfoJson["host_id"] = Json::Value(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostID().to_string()); appInfoJson[GetVersionTag()] = Json::Value(ILOGTAIL_VERSION); #else appInfoJson[GetVersionTag()] = Json::Value(string(ILOGTAIL_VERSION) + " Community Edition"); From 725918ee89531f4bffa69e6290f27d6dfde04e00 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 07:58:27 +0000 Subject: [PATCH 15/32] fix --- core/common/MachineInfoUtil.cpp | 11 ++++------- core/unittest/common/InstanceIdentityUnittest.cpp | 3 +++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 951955fe0a..3dacfa21cb 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -526,10 +526,7 @@ bool ParseECSMeta(const std::string& meta, ECSMeta& metaObj) { if (doc.isMember(sRegionIdKey) && doc[sRegionIdKey].isString()) { metaObj.SetRegionID(doc[sRegionIdKey].asString()); } - if (!metaObj.GetInstanceID().empty() && !metaObj.GetUserID().empty() && !metaObj.GetRegionID().empty()) { - return true; - } - return false; + return metaObj.IsValid(); } HostIdentifier::HostIdentifier() { @@ -591,8 +588,8 @@ bool HostIdentifier::UpdateInstanceIdentity(const ECSMeta& meta) { // 如果 instanceID 发生变化,则更新ecs元数据 if (mMetadata.GetInstanceID() != meta.GetInstanceID()) { LOG_INFO(sLogger, - ("ecs instanceID changed, old instanceID", - mMetadata.GetInstanceID().to_string())("new instanceID", meta.GetInstanceID().to_string())); + ("ecs instanceID changed, old instanceID", mMetadata.GetInstanceID())("new instanceID", + meta.GetInstanceID())); mMetadata = meta; updateHostId(); mInstanceIdentity.getWriteBuffer().SetReady(true); @@ -623,7 +620,7 @@ void HostIdentifier::DumpInstanceIdentity() { void HostIdentifier::updateHostId() { Hostid newId; - if (mMetadata.IsValid() && !mMetadata.GetInstanceID().empty()) { + if (mMetadata.IsValid()) { newId = {mMetadata.GetInstanceID().to_string(), Type::ECS}; } else { getSerialNumberFromEcsAssist(); diff --git a/core/unittest/common/InstanceIdentityUnittest.cpp b/core/unittest/common/InstanceIdentityUnittest.cpp index 032bc667d7..65677440ad 100644 --- a/core/unittest/common/InstanceIdentityUnittest.cpp +++ b/core/unittest/common/InstanceIdentityUnittest.cpp @@ -37,8 +37,11 @@ void InstanceIdentityUnittest::TestECSMeta() { meta.SetRegionID("cn-hangzhou"); APSARA_TEST_TRUE(meta.IsValid()); APSARA_TEST_EQUAL(meta.GetInstanceID().to_string(), "i-1234567890"); + APSARA_TEST_EQUAL(meta.instanceIDLen, 12); APSARA_TEST_EQUAL(meta.GetUserID().to_string(), "1234567890"); + APSARA_TEST_EQUAL(meta.userIDLen, 10); APSARA_TEST_EQUAL(meta.GetRegionID().to_string(), "cn-hangzhou"); + APSARA_TEST_EQUAL(meta.regionIDLen, 11); } { ECSMeta meta; From 657caa4d07500bd4af0a5af95cf5451972f1dc30 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 08:15:48 +0000 Subject: [PATCH 16/32] fix --- core/common/MachineInfoUtil.cpp | 18 +++-- core/common/MachineInfoUtil.h | 74 ++++++++++++++----- .../common/InstanceIdentityUnittest.cpp | 2 - 3 files changed, 66 insertions(+), 28 deletions(-) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 3dacfa21cb..7cc0bc7521 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -541,7 +541,7 @@ HostIdentifier::HostIdentifier() { DumpInstanceIdentity(); } #else - mInstanceIdentity.getWriteBuffer().SetHostID({STRING_FLAG(agent_host_id), Type::CUSTOM}); + mInstanceIdentity.getWriteBuffer().SetHostID({STRING_FLAG(agent_host_id), Hostid::Type::CUSTOM}); mInstanceIdentity.getWriteBuffer().SetReady(true); mInstanceIdentity.swap(); #endif @@ -621,22 +621,24 @@ void HostIdentifier::DumpInstanceIdentity() { void HostIdentifier::updateHostId() { Hostid newId; if (mMetadata.IsValid()) { - newId = {mMetadata.GetInstanceID().to_string(), Type::ECS}; + newId = {mMetadata.GetInstanceID().to_string(), Hostid::Type::ECS}; } else { getSerialNumberFromEcsAssist(); if (!mSerialNumber.empty()) { - newId = {mSerialNumber, Type::ECS_ASSIST}; + newId = {mSerialNumber, Hostid::Type::ECS_ASSIST}; } else if (!STRING_FLAG(agent_host_id).empty()) { - newId = {STRING_FLAG(agent_host_id), Type::CUSTOM}; + newId = {STRING_FLAG(agent_host_id), Hostid::Type::CUSTOM}; } else { getLocalHostId(); - newId = {mLocalHostId, Type::LOCAL}; + newId = {mLocalHostId, Hostid::Type::LOCAL}; } } // 只在ID发生变化时更新并记录日志 - if (mHostid.id != newId.id || mHostid.type != newId.type) { - LOG_INFO(sLogger, - ("change hostId, id from", mHostid.id)("to", newId.id)("type from", mHostid.type)("to", newId.type)); + if (mHostid.GetHostID() != newId.GetHostID() || mHostid.GetType() != newId.GetType()) { + LOG_INFO( + sLogger, + ("change hostId, id from", mHostid.GetHostID())("to", newId.GetHostID())( + "type from", Hostid::TypeToString(mHostid.GetType()))("to", Hostid::TypeToString(newId.GetType()))); mHostid = newId; } } diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index df3711378e..d9ded35687 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -27,6 +27,18 @@ namespace logtail { +static const size_t ID_MAX_LENGTH = 128; +template +inline void SetID(const std::string& id, std::array& target, size_t& targetLen) { + if (id.empty()) { + target[0] = '\0'; + targetLen = 0; + return; + } + targetLen = std::min(id.size(), N - 1); + std::memcpy(target.data(), id.data(), targetLen); + target[targetLen] = '\0'; +} struct ECSMeta { ECSMeta() = default; @@ -40,11 +52,11 @@ struct ECSMeta { [[nodiscard]] StringView GetUserID() const { return StringView(userID.data(), userIDLen); } [[nodiscard]] StringView GetRegionID() const { return StringView(regionID.data(), regionIDLen); } - [[nodiscard]] bool IsValid() const { return instanceIDLen > 0 && userIDLen > 0 && regionIDLen > 0; } + [[nodiscard]] bool IsValid() const { + return !GetInstanceID().empty() && !GetUserID().empty() && !GetRegionID().empty(); + } private: - static const size_t ID_MAX_LENGTH = 128; - std::array instanceID{}; size_t instanceIDLen = 0UL; @@ -54,23 +66,49 @@ struct ECSMeta { std::array regionID{}; size_t regionIDLen = 0UL; - template - void SetID(const std::string& id, std::array& target, size_t& targetLen) { - targetLen = std::min(id.size(), N - 1); - std::copy_n(id.begin(), targetLen, target.begin()); - target[targetLen] = '\0'; - } friend class InstanceIdentityUnittest; }; -enum Type { - CUSTOM, - ECS, - ECS_ASSIST, - LOCAL, -}; struct Hostid { - std::string id; + enum Type { + CUSTOM, + ECS, + ECS_ASSIST, + LOCAL, + }; + static std::string_view TypeToString(Type type) { + switch (type) { + case Type::CUSTOM: + return "CUSTOM"; + case Type::ECS: + return "ECS"; + case Type::ECS_ASSIST: + return "ECS_ASSIST"; + case Type::LOCAL: + return "LOCAL"; + default: + return "UNKNOWN"; + } + } + + Hostid() = default; + Hostid(const std::string& id, const Type& type) { SetHostID(id, type); } + + void SetHostID(const std::string& id, const Type& type) { + SetID(id, this->id, idLen); + this->type = type; + } + + [[nodiscard]] StringView GetHostID() const { return StringView(id.data(), idLen); } + + [[nodiscard]] Type GetType() const { return type; } + +private: + std::array id{}; + size_t idLen = 0UL; + Type type; + + friend class InstanceIdentityUnittest; }; class InstanceIdentity { public: @@ -79,8 +117,8 @@ class InstanceIdentity { StringView GetEcsInstanceID() const { return ecsMeta.GetInstanceID(); } StringView GetEcsUserID() const { return ecsMeta.GetUserID(); } StringView GetEcsRegionID() const { return ecsMeta.GetRegionID(); } - StringView GetHostID() const { return hostid.id; } - Type GetHostIdType() const { return hostid.type; } + StringView GetHostID() const { return hostid.GetHostID(); } + Hostid::Type GetHostIdType() const { return hostid.GetType(); } void SetReady(bool ready) { isReady = ready; } void SetECSMeta(const ECSMeta& meta) { ecsMeta = meta; } diff --git a/core/unittest/common/InstanceIdentityUnittest.cpp b/core/unittest/common/InstanceIdentityUnittest.cpp index 65677440ad..9067de1fed 100644 --- a/core/unittest/common/InstanceIdentityUnittest.cpp +++ b/core/unittest/common/InstanceIdentityUnittest.cpp @@ -20,8 +20,6 @@ namespace logtail { -static const size_t ID_MAX_LENGTH = ECSMeta::ID_MAX_LENGTH; - class InstanceIdentityUnittest : public ::testing::Test { public: void TestECSMeta(); From a516554d11e58da69ef65ddce4f8090bfec48192 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 08:26:54 +0000 Subject: [PATCH 17/32] std::string --- core/common/MachineInfoUtil.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index d9ded35687..8739a779ff 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -75,7 +75,7 @@ struct Hostid { ECS_ASSIST, LOCAL, }; - static std::string_view TypeToString(Type type) { + static std::string TypeToString(Type type) { switch (type) { case Type::CUSTOM: return "CUSTOM"; From 5bb0bf3cb720599157cca512c46e00559b403f40 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 09:58:00 +0000 Subject: [PATCH 18/32] fix --- core/common/MachineInfoUtil.cpp | 38 ++++++++++++++++----------------- core/common/MachineInfoUtil.h | 4 +--- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 7cc0bc7521..06ad2f17fd 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -533,7 +533,7 @@ HostIdentifier::HostIdentifier() { #ifdef __ENTERPRISE__ mInstanceIdentityFile = GetAgentDataDir() + PATH_SEPARATOR + "instance_identity"; getInstanceIdentityFromFile(); - updateHostId(); + updateHostId(mInstanceIdentity.getWriteBuffer().GetECSMeta()); mInstanceIdentity.getWriteBuffer().SetHostID(mHostid); mInstanceIdentity.swap(); if (mHasGeneratedLocalHostId) { @@ -564,8 +564,9 @@ void HostIdentifier::getInstanceIdentityFromFile() { mInstanceIdentityJson = std::move(doc); // 文件存在且不为非法json,则认为instanceIdentity是ready的 mInstanceIdentity.getWriteBuffer().SetReady(true); - if (ParseECSMeta(instanceIdentityStr, mMetadata)) { - mInstanceIdentity.getWriteBuffer().SetECSMeta(mMetadata); + ECSMeta meta; + if (ParseECSMeta(instanceIdentityStr, meta)) { + mInstanceIdentity.getWriteBuffer().SetECSMeta(meta); } else { // 不存在ecs meta信息, 则尝试读取下 random-hostid if (mInstanceIdentityJson.isMember(sRandomHostIdKey) @@ -586,15 +587,13 @@ void HostIdentifier::getInstanceIdentityFromFile() { bool HostIdentifier::UpdateInstanceIdentity(const ECSMeta& meta) { // 如果 instanceID 发生变化,则更新ecs元数据 - if (mMetadata.GetInstanceID() != meta.GetInstanceID()) { + if (mInstanceIdentity.getReadBuffer().GetEcsInstanceID() != meta.GetInstanceID()) { LOG_INFO(sLogger, - ("ecs instanceID changed, old instanceID", mMetadata.GetInstanceID())("new instanceID", - meta.GetInstanceID())); - mMetadata = meta; - updateHostId(); + ("ecs instanceID changed, old instanceID", + mInstanceIdentity.getReadBuffer().GetEcsInstanceID())("new instanceID", meta.GetInstanceID())); + updateHostId(meta); mInstanceIdentity.getWriteBuffer().SetReady(true); - mInstanceIdentity.getWriteBuffer().SetECSMeta(mMetadata); - mInstanceIdentity.getWriteBuffer().SetHostID(mHostid); + mInstanceIdentity.getWriteBuffer().SetECSMeta(meta); mInstanceIdentity.swap(); // 存在ecs meta信息, 只要dump ecs meta信息即可,无需dump random-hostid mInstanceIdentityJson.clear(); @@ -618,10 +617,10 @@ void HostIdentifier::DumpInstanceIdentity() { } } -void HostIdentifier::updateHostId() { +void HostIdentifier::updateHostId(const ECSMeta& meta) { Hostid newId; - if (mMetadata.IsValid()) { - newId = {mMetadata.GetInstanceID().to_string(), Hostid::Type::ECS}; + if (meta.IsValid()) { + newId = {meta.GetInstanceID().to_string(), Hostid::Type::ECS}; } else { getSerialNumberFromEcsAssist(); if (!mSerialNumber.empty()) { @@ -634,12 +633,13 @@ void HostIdentifier::updateHostId() { } } // 只在ID发生变化时更新并记录日志 - if (mHostid.GetHostID() != newId.GetHostID() || mHostid.GetType() != newId.GetType()) { - LOG_INFO( - sLogger, - ("change hostId, id from", mHostid.GetHostID())("to", newId.GetHostID())( - "type from", Hostid::TypeToString(mHostid.GetType()))("to", Hostid::TypeToString(newId.GetType()))); - mHostid = newId; + if (mInstanceIdentity.getReadBuffer().GetHostID() != newId.GetHostID() + || mInstanceIdentity.getReadBuffer().GetHostIdType() != newId.GetType()) { + LOG_INFO(sLogger, + ("change hostId, id from", mInstanceIdentity.getReadBuffer().GetHostID())("to", newId.GetHostID())( + "type from", Hostid::TypeToString(mInstanceIdentity.getReadBuffer().GetHostIdType()))( + "to", Hostid::TypeToString(newId.GetType()))); + mInstanceIdentity.getWriteBuffer().SetHostID(newId); } } diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index 8739a779ff..187e704d89 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -179,7 +179,7 @@ class HostIdentifier { // 从本地文件获取hostid void getLocalHostId(); - void updateHostId(); + void updateHostId(const ECSMeta& meta); #if defined(_MSC_VER) std::string mEcsAssistMachineIdFile = "C:\\ProgramData\\aliyun\\assist\\hybrid\\machine-id"; @@ -190,9 +190,7 @@ class HostIdentifier { std::string mSerialNumber; DoubleBuffer mInstanceIdentity; - Hostid mHostid; - ECSMeta mMetadata; Json::Value mInstanceIdentityJson; std::string mInstanceIdentityFile; bool mHasGeneratedLocalHostId = false; From bc91129016c361bc2f46d847ab64de5559a93c8d Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 10:01:58 +0000 Subject: [PATCH 19/32] fix --- core/common/MachineInfoUtil.cpp | 1 - core/common/MachineInfoUtil.h | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 06ad2f17fd..b67d2a0703 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -534,7 +534,6 @@ HostIdentifier::HostIdentifier() { mInstanceIdentityFile = GetAgentDataDir() + PATH_SEPARATOR + "instance_identity"; getInstanceIdentityFromFile(); updateHostId(mInstanceIdentity.getWriteBuffer().GetECSMeta()); - mInstanceIdentity.getWriteBuffer().SetHostID(mHostid); mInstanceIdentity.swap(); if (mHasGeneratedLocalHostId) { mInstanceIdentityJson[sRandomHostIdKey] = mLocalHostId; diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index 187e704d89..c241163f16 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -119,6 +119,7 @@ class InstanceIdentity { StringView GetEcsRegionID() const { return ecsMeta.GetRegionID(); } StringView GetHostID() const { return hostid.GetHostID(); } Hostid::Type GetHostIdType() const { return hostid.GetType(); } + [[nodiscard]] const ECSMeta& GetECSMeta() const { return ecsMeta; } void SetReady(bool ready) { isReady = ready; } void SetECSMeta(const ECSMeta& meta) { ecsMeta = meta; } From b883fe6fd3b1f0fd47da1e73880c109e7ace3962 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 10:06:51 +0000 Subject: [PATCH 20/32] fix --- core/common/MachineInfoUtil.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index c241163f16..8c5fe29f65 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -112,23 +112,23 @@ struct Hostid { }; class InstanceIdentity { public: - bool IsReady() const { return isReady; } - bool IsECSValid() const { return ecsMeta.IsValid(); } - StringView GetEcsInstanceID() const { return ecsMeta.GetInstanceID(); } - StringView GetEcsUserID() const { return ecsMeta.GetUserID(); } - StringView GetEcsRegionID() const { return ecsMeta.GetRegionID(); } - StringView GetHostID() const { return hostid.GetHostID(); } - Hostid::Type GetHostIdType() const { return hostid.GetType(); } - [[nodiscard]] const ECSMeta& GetECSMeta() const { return ecsMeta; } - - void SetReady(bool ready) { isReady = ready; } - void SetECSMeta(const ECSMeta& meta) { ecsMeta = meta; } - void SetHostID(const Hostid& hostid) { this->hostid = hostid; } + bool IsReady() const { return mIsReady; } + bool IsECSValid() const { return mECSMeta.IsValid(); } + StringView GetEcsInstanceID() const { return mECSMeta.GetInstanceID(); } + StringView GetEcsUserID() const { return mECSMeta.GetUserID(); } + StringView GetEcsRegionID() const { return mECSMeta.GetRegionID(); } + StringView GetHostID() const { return mHostid.GetHostID(); } + Hostid::Type GetHostIdType() const { return mHostid.GetType(); } + [[nodiscard]] const ECSMeta& GetECSMeta() const { return mECSMeta; } + + void SetReady(bool ready) { mIsReady = ready; } + void SetECSMeta(const ECSMeta& meta) { mECSMeta = meta; } + void SetHostID(const Hostid& hostid) { mHostid = hostid; } private: - bool isReady = false; - ECSMeta ecsMeta; - Hostid hostid; + bool mIsReady = false; + ECSMeta mECSMeta; + Hostid mHostid; }; std::string GetOsDetail(); From 003485dfe71f411ef02844356fbae04054879e91 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 10:24:58 +0000 Subject: [PATCH 21/32] fix --- core/common/MachineInfoUtil.cpp | 4 +- .../common/InstanceIdentityUnittest.cpp | 51 ++++++++++++++++++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index b67d2a0703..75e9be3bbc 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -585,8 +585,8 @@ void HostIdentifier::getInstanceIdentityFromFile() { } bool HostIdentifier::UpdateInstanceIdentity(const ECSMeta& meta) { - // 如果 instanceID 发生变化,则更新ecs元数据 - if (mInstanceIdentity.getReadBuffer().GetEcsInstanceID() != meta.GetInstanceID()) { + // 如果 meta合法 且 instanceID 发生变化,则更新ecs元数据 + if (meta.IsValid() && mInstanceIdentity.getReadBuffer().GetEcsInstanceID() != meta.GetInstanceID()) { LOG_INFO(sLogger, ("ecs instanceID changed, old instanceID", mInstanceIdentity.getReadBuffer().GetEcsInstanceID())("new instanceID", meta.GetInstanceID())); diff --git a/core/unittest/common/InstanceIdentityUnittest.cpp b/core/unittest/common/InstanceIdentityUnittest.cpp index 9067de1fed..49d40e2413 100644 --- a/core/unittest/common/InstanceIdentityUnittest.cpp +++ b/core/unittest/common/InstanceIdentityUnittest.cpp @@ -15,18 +15,22 @@ #include +#include "Flags.h" #include "MachineInfoUtil.h" #include "unittest/Unittest.h" +DECLARE_FLAG_STRING(agent_host_id); + namespace logtail { class InstanceIdentityUnittest : public ::testing::Test { public: void TestECSMeta(); + void TestUpdateECSMeta(); }; UNIT_TEST_CASE(InstanceIdentityUnittest, TestECSMeta); - +UNIT_TEST_CASE(InstanceIdentityUnittest, TestUpdateECSMeta); void InstanceIdentityUnittest::TestECSMeta() { { ECSMeta meta; @@ -76,6 +80,51 @@ void InstanceIdentityUnittest::TestECSMeta() { } } +void InstanceIdentityUnittest::TestUpdateECSMeta() { + STRING_FLAG(agent_host_id) = "test_host_id"; + { + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostID().to_string(), + BOOL_FLAG(agent_host_id)); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostIdType(), Hostid::Type::CUSTOM); + APSARA_TEST_TRUE(HostIdentifier::Instance()->GetInstanceIdentity()->IsReady()); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsInstanceID().to_string(), ""); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsUserID().to_string(), ""); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsRegionID().to_string(), ""); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->IsECSValid(), false); + } + { + // 更新合法的ecs meta + ECSMeta meta; + meta.SetInstanceID("i-1234567890"); + meta.SetUserID("1234567890"); + meta.SetRegionID("cn-hangzhou"); + HostIdentifier::Instance()->UpdateInstanceIdentity(meta); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostID().to_string(), "i-1234567890"); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostIdType(), Hostid::Type::ECS); + APSARA_TEST_TRUE(HostIdentifier::Instance()->GetInstanceIdentity()->IsReady()); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsInstanceID().to_string(), + "i-1234567890"); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsUserID().to_string(), "1234567890"); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsRegionID().to_string(), + "cn-hangzhou"); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->IsECSValid(), true); + } + { + // 更新不合法的ecs meta时,instanceIdentity不更新 + ECSMeta meta; + HostIdentifier::Instance()->UpdateInstanceIdentity(meta); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostID().to_string(), "i-1234567890"); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostIdType(), Hostid::Type::ECS); + APSARA_TEST_TRUE(HostIdentifier::Instance()->GetInstanceIdentity()->IsReady()); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsInstanceID().to_string(), + "i-1234567890"); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsUserID().to_string(), "1234567890"); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsRegionID().to_string(), + "cn-hangzhou"); + APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->IsECSValid(), true); + } +} // namespace logtail + } // namespace logtail UNIT_TEST_MAIN From 4d73e04f1124be6cdd21fe384c229460cfe34436 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 10:26:46 +0000 Subject: [PATCH 22/32] fix --- core/common/MachineInfoUtil.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 75e9be3bbc..f88e297ba9 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -590,6 +590,7 @@ bool HostIdentifier::UpdateInstanceIdentity(const ECSMeta& meta) { LOG_INFO(sLogger, ("ecs instanceID changed, old instanceID", mInstanceIdentity.getReadBuffer().GetEcsInstanceID())("new instanceID", meta.GetInstanceID())); + mInstanceIdentity.getWriteBuffer() = mInstanceIdentity.getWriteBuffer(); updateHostId(meta); mInstanceIdentity.getWriteBuffer().SetReady(true); mInstanceIdentity.getWriteBuffer().SetECSMeta(meta); From 28314a1913d9848a0235601c52d585af7645fde3 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 12:10:50 +0000 Subject: [PATCH 23/32] fix --- core/common/MachineInfoUtil.cpp | 72 ++++++++++++++++----------------- core/common/MachineInfoUtil.h | 11 ++--- 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index f88e297ba9..41b99683ae 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -533,55 +533,56 @@ HostIdentifier::HostIdentifier() { #ifdef __ENTERPRISE__ mInstanceIdentityFile = GetAgentDataDir() + PATH_SEPARATOR + "instance_identity"; getInstanceIdentityFromFile(); - updateHostId(mInstanceIdentity.getWriteBuffer().GetECSMeta()); - mInstanceIdentity.swap(); if (mHasGeneratedLocalHostId) { mInstanceIdentityJson[sRandomHostIdKey] = mLocalHostId; DumpInstanceIdentity(); } #else mInstanceIdentity.getWriteBuffer().SetHostID({STRING_FLAG(agent_host_id), Hostid::Type::CUSTOM}); - mInstanceIdentity.getWriteBuffer().SetReady(true); + mIsReady = true; mInstanceIdentity.swap(); #endif } void HostIdentifier::getInstanceIdentityFromFile() { - if (!CheckExistance(mInstanceIdentityFile)) { - return; - } - std::string instanceIdentityStr; - if (ReadFileContent(mInstanceIdentityFile, instanceIdentityStr)) { - Json::Value doc; - std::string errMsg; - if (!ParseJsonTable(instanceIdentityStr, doc, errMsg)) { - LOG_WARNING(sLogger, - ("parse instanceIdentity from file fail", - errMsg)("instanceIdentity", instanceIdentityStr)("file", mInstanceIdentityFile)); - return; - } - mInstanceIdentityJson = std::move(doc); - // 文件存在且不为非法json,则认为instanceIdentity是ready的 - mInstanceIdentity.getWriteBuffer().SetReady(true); - ECSMeta meta; - if (ParseECSMeta(instanceIdentityStr, meta)) { - mInstanceIdentity.getWriteBuffer().SetECSMeta(meta); - } else { - // 不存在ecs meta信息, 则尝试读取下 random-hostid - if (mInstanceIdentityJson.isMember(sRandomHostIdKey) - && mInstanceIdentityJson[sRandomHostIdKey].isString()) { - mLocalHostId = mInstanceIdentityJson[sRandomHostIdKey].asString(); + if (CheckExistance(mInstanceIdentityFile)) { + std::string instanceIdentityStr; + if (ReadFileContent(mInstanceIdentityFile, instanceIdentityStr)) { + Json::Value doc; + std::string errMsg; + if (!ParseJsonTable(instanceIdentityStr, doc, errMsg)) { + LOG_WARNING(sLogger, + ("parse instanceIdentity from file fail", + errMsg)("instanceIdentity", instanceIdentityStr)("file", mInstanceIdentityFile)); + return; } else { - LOG_ERROR(sLogger, - ("instanceIdentity is ready, but no random-hostid and ecs meta found, file", - mInstanceIdentityFile)("instanceIdentity", instanceIdentityStr)); + mInstanceIdentityJson = std::move(doc); + // 文件存在且不为非法json,则认为instanceIdentity是ready的 + ECSMeta meta; + if (ParseECSMeta(instanceIdentityStr, meta)) { + mInstanceIdentity.getWriteBuffer().SetECSMeta(meta); + mIsReady = true; + } else { + // 不存在ecs meta信息, 则尝试读取下 random-hostid + if (mInstanceIdentityJson.isMember(sRandomHostIdKey) + && mInstanceIdentityJson[sRandomHostIdKey].isString()) { + mLocalHostId = mInstanceIdentityJson[sRandomHostIdKey].asString(); + mIsReady = true; + } else { + LOG_ERROR(sLogger, + ("instanceIdentity is ready, but no random-hostid and ecs meta found, file", + mInstanceIdentityFile)("instanceIdentity", instanceIdentityStr)); + } + } } + } else { + LOG_ERROR(sLogger, + ("read instanceIdentity from file fail, file", mInstanceIdentityFile)("instanceIdentity", + instanceIdentityStr)); } - } else { - LOG_ERROR(sLogger, - ("read instanceIdentity from file fail, file", mInstanceIdentityFile)("instanceIdentity", - instanceIdentityStr)); } + updateHostId(mInstanceIdentity.getWriteBuffer().GetECSMeta()); + mInstanceIdentity.swap(); } bool HostIdentifier::UpdateInstanceIdentity(const ECSMeta& meta) { @@ -590,9 +591,8 @@ bool HostIdentifier::UpdateInstanceIdentity(const ECSMeta& meta) { LOG_INFO(sLogger, ("ecs instanceID changed, old instanceID", mInstanceIdentity.getReadBuffer().GetEcsInstanceID())("new instanceID", meta.GetInstanceID())); - mInstanceIdentity.getWriteBuffer() = mInstanceIdentity.getWriteBuffer(); + mInstanceIdentity.getWriteBuffer() = mInstanceIdentity.getReadBuffer(); updateHostId(meta); - mInstanceIdentity.getWriteBuffer().SetReady(true); mInstanceIdentity.getWriteBuffer().SetECSMeta(meta); mInstanceIdentity.swap(); // 存在ecs meta信息, 只要dump ecs meta信息即可,无需dump random-hostid diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index 8c5fe29f65..769660b058 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -112,7 +112,6 @@ struct Hostid { }; class InstanceIdentity { public: - bool IsReady() const { return mIsReady; } bool IsECSValid() const { return mECSMeta.IsValid(); } StringView GetEcsInstanceID() const { return mECSMeta.GetInstanceID(); } StringView GetEcsUserID() const { return mECSMeta.GetUserID(); } @@ -121,12 +120,10 @@ class InstanceIdentity { Hostid::Type GetHostIdType() const { return mHostid.GetType(); } [[nodiscard]] const ECSMeta& GetECSMeta() const { return mECSMeta; } - void SetReady(bool ready) { mIsReady = ready; } void SetECSMeta(const ECSMeta& meta) { mECSMeta = meta; } void SetHostID(const Hostid& hostid) { mHostid = hostid; } private: - bool mIsReady = false; ECSMeta mECSMeta; Hostid mHostid; }; @@ -166,11 +163,7 @@ class HostIdentifier { bool UpdateInstanceIdentity(const ECSMeta& meta); void DumpInstanceIdentity(); - void SetInstanceIdentityReady() { - mInstanceIdentity.getWriteBuffer() = mInstanceIdentity.getReadBuffer(); - mInstanceIdentity.getWriteBuffer().SetReady(true); - mInstanceIdentity.swap(); - }; + void SetInstanceIdentityReady() { mIsReady = true; }; private: // 从文件获取ecs元数据 @@ -187,6 +180,8 @@ class HostIdentifier { #else std::string mEcsAssistMachineIdFile = "/usr/local/share/aliyun-assist/hybrid/machine-id"; #endif + bool mIsReady = false; + bool mHasTriedToGetSerialNumber = false; std::string mSerialNumber; From 9873272b0b870b13b6d96d45b9c33e72b759ae3e Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 12:24:58 +0000 Subject: [PATCH 24/32] fix --- core/application/Application.cpp | 8 +-- core/common/MachineInfoUtil.cpp | 52 ++++++++++--------- core/common/MachineInfoUtil.h | 17 +++--- core/plugin/flusher/sls/SLSClientManager.cpp | 4 +- .../common/InstanceIdentityUnittest.cpp | 51 ++++++++---------- 5 files changed, 66 insertions(+), 66 deletions(-) diff --git a/core/application/Application.cpp b/core/application/Application.cpp index c988dd7fab..e044196cc9 100644 --- a/core/application/Application.cpp +++ b/core/application/Application.cpp @@ -107,14 +107,14 @@ void Application::Init() { AppConfig::GetInstance()->LoadAppConfig(GetAgentConfigFile()); #ifdef __ENTERPRISE__ - if (!HostIdentifier::Instance()->GetInstanceIdentity()->IsReady()) { + if (!InstanceIdentity::Instance()->GetEntity()->IsReady()) { // not ready, fetch ecs meta ECSMeta ecsMeta; if (FetchECSMeta(ecsMeta)) { - HostIdentifier::Instance()->UpdateInstanceIdentity(ecsMeta); + InstanceIdentity::Instance()->UpdateInstanceIdentity(ecsMeta); } // 不管ecs meta是否获取成功,都设置instanceIdentity为ready - HostIdentifier::Instance()->SetInstanceIdentityReady(); + InstanceIdentity::Instance()->SetReady(); } #endif @@ -179,7 +179,7 @@ void Application::Init() { appInfoJson["UUID"] = Json::Value(Application::GetInstance()->GetUUID()); appInfoJson["instance_id"] = Json::Value(Application::GetInstance()->GetInstanceId()); #ifdef __ENTERPRISE__ - appInfoJson["host_id"] = Json::Value(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostID().to_string()); + appInfoJson["host_id"] = Json::Value(InstanceIdentity::Instance()->GetEntity()->GetHostID().to_string()); appInfoJson[GetVersionTag()] = Json::Value(ILOGTAIL_VERSION); #else appInfoJson[GetVersionTag()] = Json::Value(string(ILOGTAIL_VERSION) + " Community Edition"); diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 41b99683ae..7312cf31ad 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -529,7 +529,7 @@ bool ParseECSMeta(const std::string& meta, ECSMeta& metaObj) { return metaObj.IsValid(); } -HostIdentifier::HostIdentifier() { +InstanceIdentity::InstanceIdentity() { #ifdef __ENTERPRISE__ mInstanceIdentityFile = GetAgentDataDir() + PATH_SEPARATOR + "instance_identity"; getInstanceIdentityFromFile(); @@ -538,13 +538,13 @@ HostIdentifier::HostIdentifier() { DumpInstanceIdentity(); } #else - mInstanceIdentity.getWriteBuffer().SetHostID({STRING_FLAG(agent_host_id), Hostid::Type::CUSTOM}); + mEntity.getWriteBuffer().SetHostID({STRING_FLAG(agent_host_id), Hostid::Type::CUSTOM}); mIsReady = true; - mInstanceIdentity.swap(); + mEntity.swap(); #endif } -void HostIdentifier::getInstanceIdentityFromFile() { +void InstanceIdentity::getInstanceIdentityFromFile() { if (CheckExistance(mInstanceIdentityFile)) { std::string instanceIdentityStr; if (ReadFileContent(mInstanceIdentityFile, instanceIdentityStr)) { @@ -554,19 +554,19 @@ void HostIdentifier::getInstanceIdentityFromFile() { LOG_WARNING(sLogger, ("parse instanceIdentity from file fail", errMsg)("instanceIdentity", instanceIdentityStr)("file", mInstanceIdentityFile)); - return; } else { mInstanceIdentityJson = std::move(doc); - // 文件存在且不为非法json,则认为instanceIdentity是ready的 ECSMeta meta; if (ParseECSMeta(instanceIdentityStr, meta)) { - mInstanceIdentity.getWriteBuffer().SetECSMeta(meta); + mEntity.getWriteBuffer().SetECSMeta(meta); + // 存在 ecs meta信息,则认为instanceIdentity是ready的 mIsReady = true; } else { // 不存在ecs meta信息, 则尝试读取下 random-hostid if (mInstanceIdentityJson.isMember(sRandomHostIdKey) && mInstanceIdentityJson[sRandomHostIdKey].isString()) { mLocalHostId = mInstanceIdentityJson[sRandomHostIdKey].asString(); + // 存在 random-hostid,则认为instanceIdentity是ready的 mIsReady = true; } else { LOG_ERROR(sLogger, @@ -581,20 +581,20 @@ void HostIdentifier::getInstanceIdentityFromFile() { instanceIdentityStr)); } } - updateHostId(mInstanceIdentity.getWriteBuffer().GetECSMeta()); - mInstanceIdentity.swap(); + // 计算hostid + updateHostId(mEntity.getWriteBuffer().GetECSMeta()); + mEntity.swap(); } -bool HostIdentifier::UpdateInstanceIdentity(const ECSMeta& meta) { +bool InstanceIdentity::UpdateInstanceIdentity(const ECSMeta& meta) { // 如果 meta合法 且 instanceID 发生变化,则更新ecs元数据 - if (meta.IsValid() && mInstanceIdentity.getReadBuffer().GetEcsInstanceID() != meta.GetInstanceID()) { + if (meta.IsValid() && mEntity.getReadBuffer().GetEcsInstanceID() != meta.GetInstanceID()) { LOG_INFO(sLogger, ("ecs instanceID changed, old instanceID", - mInstanceIdentity.getReadBuffer().GetEcsInstanceID())("new instanceID", meta.GetInstanceID())); - mInstanceIdentity.getWriteBuffer() = mInstanceIdentity.getReadBuffer(); + mEntity.getReadBuffer().GetEcsInstanceID())("new instanceID", meta.GetInstanceID())); updateHostId(meta); - mInstanceIdentity.getWriteBuffer().SetECSMeta(meta); - mInstanceIdentity.swap(); + mEntity.getWriteBuffer().SetECSMeta(meta); + mEntity.swap(); // 存在ecs meta信息, 只要dump ecs meta信息即可,无需dump random-hostid mInstanceIdentityJson.clear(); mInstanceIdentityJson[sInstanceIdKey] = meta.GetInstanceID().to_string(); @@ -606,7 +606,7 @@ bool HostIdentifier::UpdateInstanceIdentity(const ECSMeta& meta) { return false; } -void HostIdentifier::DumpInstanceIdentity() { +void InstanceIdentity::DumpInstanceIdentity() { std::string errMsg; if (!WriteFile(mInstanceIdentityFile, mInstanceIdentityJson.toStyledString(), errMsg)) { LOG_ERROR(sLogger, ("failed to write instanceIdentity to file", mInstanceIdentityFile)("error", errMsg)); @@ -617,7 +617,7 @@ void HostIdentifier::DumpInstanceIdentity() { } } -void HostIdentifier::updateHostId(const ECSMeta& meta) { +void InstanceIdentity::updateHostId(const ECSMeta& meta) { Hostid newId; if (meta.IsValid()) { newId = {meta.GetInstanceID().to_string(), Hostid::Type::ECS}; @@ -633,13 +633,17 @@ void HostIdentifier::updateHostId(const ECSMeta& meta) { } } // 只在ID发生变化时更新并记录日志 - if (mInstanceIdentity.getReadBuffer().GetHostID() != newId.GetHostID() - || mInstanceIdentity.getReadBuffer().GetHostIdType() != newId.GetType()) { + if (mEntity.getReadBuffer().GetHostID() != newId.GetHostID() + || mEntity.getReadBuffer().GetHostIdType() != newId.GetType()) { LOG_INFO(sLogger, - ("change hostId, id from", mInstanceIdentity.getReadBuffer().GetHostID())("to", newId.GetHostID())( - "type from", Hostid::TypeToString(mInstanceIdentity.getReadBuffer().GetHostIdType()))( + ("change hostId, id from", mEntity.getReadBuffer().GetHostID())("to", newId.GetHostID())( + "type from", Hostid::TypeToString(mEntity.getReadBuffer().GetHostIdType()))( "to", Hostid::TypeToString(newId.GetType()))); - mInstanceIdentity.getWriteBuffer().SetHostID(newId); + mEntity.getWriteBuffer().SetHostID(newId); + } else { + // 没有变化时,WriteBuffer的host id维持原状 + Hostid hostId(mEntity.getReadBuffer().GetHostID().to_string(), mEntity.getReadBuffer().GetHostIdType()); + mEntity.getWriteBuffer().SetHostID(hostId); } } @@ -718,7 +722,7 @@ bool FetchECSMeta(ECSMeta& metaObj) { } // 从云助手获取序列号 -void HostIdentifier::getSerialNumberFromEcsAssist() { +void InstanceIdentity::getSerialNumberFromEcsAssist() { if (mHasTriedToGetSerialNumber) { return; } @@ -730,7 +734,7 @@ void HostIdentifier::getSerialNumberFromEcsAssist() { mHasTriedToGetSerialNumber = true; } -void HostIdentifier::getLocalHostId() { +void InstanceIdentity::getLocalHostId() { if (!mLocalHostId.empty()) { return; } diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index 769660b058..5dfbba29a5 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -110,7 +110,7 @@ struct Hostid { friend class InstanceIdentityUnittest; }; -class InstanceIdentity { +class Entity { public: bool IsECSValid() const { return mECSMeta.IsValid(); } StringView GetEcsInstanceID() const { return mECSMeta.GetInstanceID(); } @@ -149,21 +149,22 @@ std::string GetAnyAvailableIP(); bool FetchECSMeta(ECSMeta& metaObj); -class HostIdentifier { +class InstanceIdentity { public: - HostIdentifier(); - static HostIdentifier* Instance() { - static HostIdentifier sInstance; + InstanceIdentity(); + static InstanceIdentity* Instance() { + static InstanceIdentity sInstance; return &sInstance; } // 注意: 不要在类初始化时调用并缓存结果,因为此时ECS元数据可能尚未就绪 // 建议在实际使用时再调用此方法 - const InstanceIdentity* GetInstanceIdentity() { return &mInstanceIdentity.getReadBuffer(); } + const Entity* GetEntity() { return &mEntity.getReadBuffer(); } bool UpdateInstanceIdentity(const ECSMeta& meta); void DumpInstanceIdentity(); - void SetInstanceIdentityReady() { mIsReady = true; }; + void SetReady() { mIsReady = true; }; + [[nodiscard]] bool IsReady() const { return mIsReady; } private: // 从文件获取ecs元数据 @@ -185,7 +186,7 @@ class HostIdentifier { bool mHasTriedToGetSerialNumber = false; std::string mSerialNumber; - DoubleBuffer mInstanceIdentity; + DoubleBuffer mEntity; Json::Value mInstanceIdentityJson; std::string mInstanceIdentityFile; diff --git a/core/plugin/flusher/sls/SLSClientManager.cpp b/core/plugin/flusher/sls/SLSClientManager.cpp index 1d4f68b3e8..9989d1991e 100644 --- a/core/plugin/flusher/sls/SLSClientManager.cpp +++ b/core/plugin/flusher/sls/SLSClientManager.cpp @@ -109,7 +109,7 @@ string SLSClientManager::GetRunningEnvironment() { // containers in K8S will possess the above env if (AppConfig::GetInstance()->IsPurageContainerMode()) { env = "K8S-Daemonset"; - } else if (HostIdentifier::Instance()->GetInstanceIdentity()->IsECSValid()) { + } else if (InstanceIdentity::Instance()->GetEntity()->IsECSValid()) { // containers in ACK can be connected to the above address, see // https://help.aliyun.com/document_detail/108460.html#section-akf-lwh-1gb. // Note: we can not distinguish ACK from K8S built on ECS @@ -119,7 +119,7 @@ string SLSClientManager::GetRunningEnvironment() { } } else if (AppConfig::GetInstance()->IsPurageContainerMode() || getenv("ALIYUN_LOGTAIL_CONFIG")) { env = "Docker"; - } else if (HostIdentifier::Instance()->GetInstanceIdentity()->IsECSValid()) { + } else if (InstanceIdentity::Instance()->GetEntity()->IsECSValid()) { env = "ECS"; } else { env = "Others"; diff --git a/core/unittest/common/InstanceIdentityUnittest.cpp b/core/unittest/common/InstanceIdentityUnittest.cpp index 49d40e2413..1200e28a75 100644 --- a/core/unittest/common/InstanceIdentityUnittest.cpp +++ b/core/unittest/common/InstanceIdentityUnittest.cpp @@ -83,14 +83,13 @@ void InstanceIdentityUnittest::TestECSMeta() { void InstanceIdentityUnittest::TestUpdateECSMeta() { STRING_FLAG(agent_host_id) = "test_host_id"; { - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostID().to_string(), - BOOL_FLAG(agent_host_id)); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostIdType(), Hostid::Type::CUSTOM); - APSARA_TEST_TRUE(HostIdentifier::Instance()->GetInstanceIdentity()->IsReady()); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsInstanceID().to_string(), ""); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsUserID().to_string(), ""); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsRegionID().to_string(), ""); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->IsECSValid(), false); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostID().to_string(), BOOL_FLAG(agent_host_id)); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostIdType(), Hostid::Type::CUSTOM); + APSARA_TEST_TRUE(InstanceIdentity::Instance()->IsReady()); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsInstanceID().to_string(), ""); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsUserID().to_string(), ""); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsRegionID().to_string(), ""); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->IsECSValid(), false); } { // 更新合法的ecs meta @@ -98,30 +97,26 @@ void InstanceIdentityUnittest::TestUpdateECSMeta() { meta.SetInstanceID("i-1234567890"); meta.SetUserID("1234567890"); meta.SetRegionID("cn-hangzhou"); - HostIdentifier::Instance()->UpdateInstanceIdentity(meta); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostID().to_string(), "i-1234567890"); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostIdType(), Hostid::Type::ECS); - APSARA_TEST_TRUE(HostIdentifier::Instance()->GetInstanceIdentity()->IsReady()); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsInstanceID().to_string(), - "i-1234567890"); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsUserID().to_string(), "1234567890"); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsRegionID().to_string(), - "cn-hangzhou"); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->IsECSValid(), true); + InstanceIdentity::Instance()->UpdateInstanceIdentity(meta); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostID().to_string(), "i-1234567890"); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostIdType(), Hostid::Type::ECS); + APSARA_TEST_TRUE(InstanceIdentity::Instance()->IsReady()); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsInstanceID().to_string(), "i-1234567890"); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsUserID().to_string(), "1234567890"); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsRegionID().to_string(), "cn-hangzhou"); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->IsECSValid(), true); } { // 更新不合法的ecs meta时,instanceIdentity不更新 ECSMeta meta; - HostIdentifier::Instance()->UpdateInstanceIdentity(meta); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostID().to_string(), "i-1234567890"); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetHostIdType(), Hostid::Type::ECS); - APSARA_TEST_TRUE(HostIdentifier::Instance()->GetInstanceIdentity()->IsReady()); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsInstanceID().to_string(), - "i-1234567890"); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsUserID().to_string(), "1234567890"); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->GetEcsRegionID().to_string(), - "cn-hangzhou"); - APSARA_TEST_EQUAL(HostIdentifier::Instance()->GetInstanceIdentity()->IsECSValid(), true); + InstanceIdentity::Instance()->UpdateInstanceIdentity(meta); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostID().to_string(), "i-1234567890"); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostIdType(), Hostid::Type::ECS); + APSARA_TEST_TRUE(InstanceIdentity::Instance()->IsReady()); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsInstanceID().to_string(), "i-1234567890"); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsUserID().to_string(), "1234567890"); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsRegionID().to_string(), "cn-hangzhou"); + APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->IsECSValid(), true); } } // namespace logtail From efddef11197f5075f7bb2a2a02bb7b1e5b52ff21 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 12:27:12 +0000 Subject: [PATCH 25/32] fix --- core/application/Application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/application/Application.cpp b/core/application/Application.cpp index e044196cc9..0f51ea1d59 100644 --- a/core/application/Application.cpp +++ b/core/application/Application.cpp @@ -107,7 +107,7 @@ void Application::Init() { AppConfig::GetInstance()->LoadAppConfig(GetAgentConfigFile()); #ifdef __ENTERPRISE__ - if (!InstanceIdentity::Instance()->GetEntity()->IsReady()) { + if (!InstanceIdentity::Instance()->IsReady()) { // not ready, fetch ecs meta ECSMeta ecsMeta; if (FetchECSMeta(ecsMeta)) { From dd65c17b6156f48f7100e38ec0200efd5ec4ad22 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 12:56:46 +0000 Subject: [PATCH 26/32] fix --- core/application/Application.cpp | 13 +++---- core/common/MachineInfoUtil.cpp | 58 ++++++++++++++++++++------------ core/common/MachineInfoUtil.h | 8 +++-- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/core/application/Application.cpp b/core/application/Application.cpp index 0f51ea1d59..08e02d114a 100644 --- a/core/application/Application.cpp +++ b/core/application/Application.cpp @@ -107,17 +107,14 @@ void Application::Init() { AppConfig::GetInstance()->LoadAppConfig(GetAgentConfigFile()); #ifdef __ENTERPRISE__ + InstanceIdentity::Instance()->InitFromFile(); if (!InstanceIdentity::Instance()->IsReady()) { - // not ready, fetch ecs meta - ECSMeta ecsMeta; - if (FetchECSMeta(ecsMeta)) { - InstanceIdentity::Instance()->UpdateInstanceIdentity(ecsMeta); - } - // 不管ecs meta是否获取成功,都设置instanceIdentity为ready - InstanceIdentity::Instance()->SetReady(); + InstanceIdentity::Instance()->InitFromNetwork(); } #endif - + if (!InstanceIdentity::Instance()->IsReady()) { + InstanceIdentity::Instance()->SetReady(); + } // Initialize basic information: IP, hostname, etc. LoongCollectorMonitor::GetInstance(); #ifdef __ENTERPRISE__ diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 7312cf31ad..8d10f9ad11 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -53,6 +53,8 @@ const std::string sInstanceIdKey = "instance-id"; const std::string sOwnerAccountIdKey = "owner-account-id"; const std::string sRegionIdKey = "region-id"; const std::string sRandomHostIdKey = "random-hostid"; +const std::string sECSAssistMachineIdKey = "ecs-assist-machine-id"; + #if defined(_MSC_VER) typedef LONG NTSTATUS, *PNTSTATUS; @@ -530,21 +532,33 @@ bool ParseECSMeta(const std::string& meta, ECSMeta& metaObj) { } InstanceIdentity::InstanceIdentity() { -#ifdef __ENTERPRISE__ - mInstanceIdentityFile = GetAgentDataDir() + PATH_SEPARATOR + "instance_identity"; - getInstanceIdentityFromFile(); - if (mHasGeneratedLocalHostId) { + mEntity.getWriteBuffer().SetHostID({STRING_FLAG(agent_host_id), Hostid::Type::CUSTOM}); + mEntity.swap(); +} + +void InstanceIdentity::SetReady() { + mIsReady = true; + // hostid为本地hostid时,且为首次生成出来的,则需要dump到文件 + if (mEntity.getReadBuffer().GetHostIdType() == Hostid::Type::LOCAL && mHasGeneratedLocalHostId) { + mInstanceIdentityJson.clear(); mInstanceIdentityJson[sRandomHostIdKey] = mLocalHostId; DumpInstanceIdentity(); + } else if (mEntity.getReadBuffer().GetHostIdType() == Hostid::Type::ECS_ASSIST && !mSerialNumber.empty()) { + mInstanceIdentityJson.clear(); + mInstanceIdentityJson[sECSAssistMachineIdKey] = mSerialNumber; + DumpInstanceIdentity(); + } +}; + +void InstanceIdentity::InitFromNetwork() { + ECSMeta ecsMeta; + if (FetchECSMeta(ecsMeta)) { + InstanceIdentity::Instance()->UpdateInstanceIdentity(ecsMeta); } -#else - mEntity.getWriteBuffer().SetHostID({STRING_FLAG(agent_host_id), Hostid::Type::CUSTOM}); - mIsReady = true; - mEntity.swap(); -#endif } -void InstanceIdentity::getInstanceIdentityFromFile() { +void InstanceIdentity::InitFromFile() { + mInstanceIdentityFile = GetAgentDataDir() + PATH_SEPARATOR + "instance_identity"; if (CheckExistance(mInstanceIdentityFile)) { std::string instanceIdentityStr; if (ReadFileContent(mInstanceIdentityFile, instanceIdentityStr)) { @@ -561,18 +575,20 @@ void InstanceIdentity::getInstanceIdentityFromFile() { mEntity.getWriteBuffer().SetECSMeta(meta); // 存在 ecs meta信息,则认为instanceIdentity是ready的 mIsReady = true; - } else { + } else if (mInstanceIdentityJson.isMember(sRandomHostIdKey) + && mInstanceIdentityJson[sRandomHostIdKey].isString()) { // 不存在ecs meta信息, 则尝试读取下 random-hostid - if (mInstanceIdentityJson.isMember(sRandomHostIdKey) - && mInstanceIdentityJson[sRandomHostIdKey].isString()) { - mLocalHostId = mInstanceIdentityJson[sRandomHostIdKey].asString(); - // 存在 random-hostid,则认为instanceIdentity是ready的 - mIsReady = true; - } else { - LOG_ERROR(sLogger, - ("instanceIdentity is ready, but no random-hostid and ecs meta found, file", - mInstanceIdentityFile)("instanceIdentity", instanceIdentityStr)); - } + mLocalHostId = mInstanceIdentityJson[sRandomHostIdKey].asString(); + // 存在 random-hostid,则认为instanceIdentity是ready的 + mIsReady = true; + } else if (mInstanceIdentityJson.isMember(sECSAssistMachineIdKey) + && mInstanceIdentityJson[sECSAssistMachineIdKey].isString()) { + // 存在 ecs-assist-machine-id,则认为instanceIdentity是ready的 + mIsReady = true; + } else { + LOG_ERROR(sLogger, + ("instanceIdentity is ready, but no random-hostid and ecs meta found, file", + mInstanceIdentityFile)("instanceIdentity", instanceIdentityStr)); } } } else { diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index 5dfbba29a5..41cba398d7 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -163,12 +163,14 @@ class InstanceIdentity { bool UpdateInstanceIdentity(const ECSMeta& meta); void DumpInstanceIdentity(); - void SetReady() { mIsReady = true; }; + void SetReady(); [[nodiscard]] bool IsReady() const { return mIsReady; } + void InitFromFile(); + + void InitFromNetwork(); + private: - // 从文件获取ecs元数据 - void getInstanceIdentityFromFile(); // 从云助手获取序列号 void getSerialNumberFromEcsAssist(); // 从本地文件获取hostid From 17dd36e478ce465796ec153ba3372a32dee04cd8 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 13:01:35 +0000 Subject: [PATCH 27/32] fix test --- core/unittest/common/InstanceIdentityUnittest.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/unittest/common/InstanceIdentityUnittest.cpp b/core/unittest/common/InstanceIdentityUnittest.cpp index 1200e28a75..8416ed043d 100644 --- a/core/unittest/common/InstanceIdentityUnittest.cpp +++ b/core/unittest/common/InstanceIdentityUnittest.cpp @@ -85,7 +85,6 @@ void InstanceIdentityUnittest::TestUpdateECSMeta() { { APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostID().to_string(), BOOL_FLAG(agent_host_id)); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostIdType(), Hostid::Type::CUSTOM); - APSARA_TEST_TRUE(InstanceIdentity::Instance()->IsReady()); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsInstanceID().to_string(), ""); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsUserID().to_string(), ""); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsRegionID().to_string(), ""); @@ -100,7 +99,6 @@ void InstanceIdentityUnittest::TestUpdateECSMeta() { InstanceIdentity::Instance()->UpdateInstanceIdentity(meta); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostID().to_string(), "i-1234567890"); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostIdType(), Hostid::Type::ECS); - APSARA_TEST_TRUE(InstanceIdentity::Instance()->IsReady()); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsInstanceID().to_string(), "i-1234567890"); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsUserID().to_string(), "1234567890"); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsRegionID().to_string(), "cn-hangzhou"); @@ -112,7 +110,6 @@ void InstanceIdentityUnittest::TestUpdateECSMeta() { InstanceIdentity::Instance()->UpdateInstanceIdentity(meta); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostID().to_string(), "i-1234567890"); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetHostIdType(), Hostid::Type::ECS); - APSARA_TEST_TRUE(InstanceIdentity::Instance()->IsReady()); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsInstanceID().to_string(), "i-1234567890"); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsUserID().to_string(), "1234567890"); APSARA_TEST_EQUAL(InstanceIdentity::Instance()->GetEntity()->GetEcsRegionID().to_string(), "cn-hangzhou"); From 3301b258716c331a2264d95f9ddff882727300c3 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 13:14:19 +0000 Subject: [PATCH 28/32] mIsReady --- core/common/MachineInfoUtil.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 8d10f9ad11..a5c58a9deb 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -617,6 +617,7 @@ bool InstanceIdentity::UpdateInstanceIdentity(const ECSMeta& meta) { mInstanceIdentityJson[sOwnerAccountIdKey] = meta.GetUserID().to_string(); mInstanceIdentityJson[sRegionIdKey] = meta.GetRegionID().to_string(); DumpInstanceIdentity(); + mIsReady = true; return true; } return false; From bd4ccc8469441b3dbbd7ee94a98229046413034c Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 13:37:28 +0000 Subject: [PATCH 29/32] fix --- core/application/Application.cpp | 6 ++---- core/common/MachineInfoUtil.cpp | 29 +++++++++++++---------------- core/common/MachineInfoUtil.h | 5 +++-- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/core/application/Application.cpp b/core/application/Application.cpp index 08e02d114a..586baf4bea 100644 --- a/core/application/Application.cpp +++ b/core/application/Application.cpp @@ -108,13 +108,11 @@ void Application::Init() { AppConfig::GetInstance()->LoadAppConfig(GetAgentConfigFile()); #ifdef __ENTERPRISE__ InstanceIdentity::Instance()->InitFromFile(); - if (!InstanceIdentity::Instance()->IsReady()) { + if (InstanceIdentity::Instance()->NeedFetchECSMeta()) { InstanceIdentity::Instance()->InitFromNetwork(); + InstanceIdentity::Instance()->DumpInstanceIdentity(); } #endif - if (!InstanceIdentity::Instance()->IsReady()) { - InstanceIdentity::Instance()->SetReady(); - } // Initialize basic information: IP, hostname, etc. LoongCollectorMonitor::GetInstance(); #ifdef __ENTERPRISE__ diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index a5c58a9deb..679f806961 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -536,10 +536,14 @@ InstanceIdentity::InstanceIdentity() { mEntity.swap(); } -void InstanceIdentity::SetReady() { - mIsReady = true; - // hostid为本地hostid时,且为首次生成出来的,则需要dump到文件 - if (mEntity.getReadBuffer().GetHostIdType() == Hostid::Type::LOCAL && mHasGeneratedLocalHostId) { +void InstanceIdentity::DumpInstanceIdentity() { + if (mEntity.getReadBuffer().GetHostIdType() == Hostid::Type::ECS) { + mInstanceIdentityJson.clear(); + mInstanceIdentityJson[sInstanceIdKey] = mEntity.getReadBuffer().GetEcsInstanceID().to_string(); + mInstanceIdentityJson[sOwnerAccountIdKey] = mEntity.getReadBuffer().GetEcsUserID().to_string(); + mInstanceIdentityJson[sRegionIdKey] = mEntity.getReadBuffer().GetEcsRegionID().to_string(); + DumpInstanceIdentity(); + } else if (mEntity.getReadBuffer().GetHostIdType() == Hostid::Type::LOCAL && mHasGeneratedLocalHostId) { mInstanceIdentityJson.clear(); mInstanceIdentityJson[sRandomHostIdKey] = mLocalHostId; DumpInstanceIdentity(); @@ -548,7 +552,7 @@ void InstanceIdentity::SetReady() { mInstanceIdentityJson[sECSAssistMachineIdKey] = mSerialNumber; DumpInstanceIdentity(); } -}; +} void InstanceIdentity::InitFromNetwork() { ECSMeta ecsMeta; @@ -574,17 +578,17 @@ void InstanceIdentity::InitFromFile() { if (ParseECSMeta(instanceIdentityStr, meta)) { mEntity.getWriteBuffer().SetECSMeta(meta); // 存在 ecs meta信息,则认为instanceIdentity是ready的 - mIsReady = true; + mNeedFetchECSMeta = false; } else if (mInstanceIdentityJson.isMember(sRandomHostIdKey) && mInstanceIdentityJson[sRandomHostIdKey].isString()) { // 不存在ecs meta信息, 则尝试读取下 random-hostid mLocalHostId = mInstanceIdentityJson[sRandomHostIdKey].asString(); // 存在 random-hostid,则认为instanceIdentity是ready的 - mIsReady = true; + mNeedFetchECSMeta = false; } else if (mInstanceIdentityJson.isMember(sECSAssistMachineIdKey) && mInstanceIdentityJson[sECSAssistMachineIdKey].isString()) { // 存在 ecs-assist-machine-id,则认为instanceIdentity是ready的 - mIsReady = true; + mNeedFetchECSMeta = false; } else { LOG_ERROR(sLogger, ("instanceIdentity is ready, but no random-hostid and ecs meta found, file", @@ -611,19 +615,12 @@ bool InstanceIdentity::UpdateInstanceIdentity(const ECSMeta& meta) { updateHostId(meta); mEntity.getWriteBuffer().SetECSMeta(meta); mEntity.swap(); - // 存在ecs meta信息, 只要dump ecs meta信息即可,无需dump random-hostid - mInstanceIdentityJson.clear(); - mInstanceIdentityJson[sInstanceIdKey] = meta.GetInstanceID().to_string(); - mInstanceIdentityJson[sOwnerAccountIdKey] = meta.GetUserID().to_string(); - mInstanceIdentityJson[sRegionIdKey] = meta.GetRegionID().to_string(); - DumpInstanceIdentity(); - mIsReady = true; return true; } return false; } -void InstanceIdentity::DumpInstanceIdentity() { +void InstanceIdentity::dumpInstanceIdentityToFile() { std::string errMsg; if (!WriteFile(mInstanceIdentityFile, mInstanceIdentityJson.toStyledString(), errMsg)) { LOG_ERROR(sLogger, ("failed to write instanceIdentity to file", mInstanceIdentityFile)("error", errMsg)); diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index 41cba398d7..b0ce6e9175 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -164,7 +164,7 @@ class InstanceIdentity { bool UpdateInstanceIdentity(const ECSMeta& meta); void DumpInstanceIdentity(); void SetReady(); - [[nodiscard]] bool IsReady() const { return mIsReady; } + [[nodiscard]] bool NeedFetchECSMeta() const { return mNeedFetchECSMeta; } void InitFromFile(); @@ -177,13 +177,14 @@ class InstanceIdentity { void getLocalHostId(); void updateHostId(const ECSMeta& meta); + void dumpInstanceIdentityToFile(); #if defined(_MSC_VER) std::string mEcsAssistMachineIdFile = "C:\\ProgramData\\aliyun\\assist\\hybrid\\machine-id"; #else std::string mEcsAssistMachineIdFile = "/usr/local/share/aliyun-assist/hybrid/machine-id"; #endif - bool mIsReady = false; + bool mNeedFetchECSMeta = false; bool mHasTriedToGetSerialNumber = false; std::string mSerialNumber; From 5027e39dafff095472781701c13454c4783a46d8 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 13:48:14 +0000 Subject: [PATCH 30/32] fix --- core/application/Application.cpp | 3 +- core/common/MachineInfoUtil.cpp | 16 ++++--- core/common/MachineInfoUtil.h | 43 +++++++++---------- .../common/InstanceIdentityUnittest.cpp | 15 ++++--- 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/core/application/Application.cpp b/core/application/Application.cpp index 586baf4bea..f0c955a3a7 100644 --- a/core/application/Application.cpp +++ b/core/application/Application.cpp @@ -107,8 +107,7 @@ void Application::Init() { AppConfig::GetInstance()->LoadAppConfig(GetAgentConfigFile()); #ifdef __ENTERPRISE__ - InstanceIdentity::Instance()->InitFromFile(); - if (InstanceIdentity::Instance()->NeedFetchECSMeta()) { + if (!InstanceIdentity::Instance()->InitFromFile()) { InstanceIdentity::Instance()->InitFromNetwork(); InstanceIdentity::Instance()->DumpInstanceIdentity(); } diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 679f806961..8f0aa29cf0 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -561,8 +561,9 @@ void InstanceIdentity::InitFromNetwork() { } } -void InstanceIdentity::InitFromFile() { +bool InstanceIdentity::InitFromFile() { mInstanceIdentityFile = GetAgentDataDir() + PATH_SEPARATOR + "instance_identity"; + bool initSuccess = false; if (CheckExistance(mInstanceIdentityFile)) { std::string instanceIdentityStr; if (ReadFileContent(mInstanceIdentityFile, instanceIdentityStr)) { @@ -578,17 +579,17 @@ void InstanceIdentity::InitFromFile() { if (ParseECSMeta(instanceIdentityStr, meta)) { mEntity.getWriteBuffer().SetECSMeta(meta); // 存在 ecs meta信息,则认为instanceIdentity是ready的 - mNeedFetchECSMeta = false; + initSuccess = true; } else if (mInstanceIdentityJson.isMember(sRandomHostIdKey) && mInstanceIdentityJson[sRandomHostIdKey].isString()) { // 不存在ecs meta信息, 则尝试读取下 random-hostid mLocalHostId = mInstanceIdentityJson[sRandomHostIdKey].asString(); // 存在 random-hostid,则认为instanceIdentity是ready的 - mNeedFetchECSMeta = false; + initSuccess = true; } else if (mInstanceIdentityJson.isMember(sECSAssistMachineIdKey) && mInstanceIdentityJson[sECSAssistMachineIdKey].isString()) { // 存在 ecs-assist-machine-id,则认为instanceIdentity是ready的 - mNeedFetchECSMeta = false; + initSuccess = true; } else { LOG_ERROR(sLogger, ("instanceIdentity is ready, but no random-hostid and ecs meta found, file", @@ -604,14 +605,15 @@ void InstanceIdentity::InitFromFile() { // 计算hostid updateHostId(mEntity.getWriteBuffer().GetECSMeta()); mEntity.swap(); + return initSuccess; } bool InstanceIdentity::UpdateInstanceIdentity(const ECSMeta& meta) { - // 如果 meta合法 且 instanceID 发生变化,则更新ecs元数据 + // 如果 meta合法 且 mInstanceID 发生变化,则更新ecs元数据 if (meta.IsValid() && mEntity.getReadBuffer().GetEcsInstanceID() != meta.GetInstanceID()) { LOG_INFO(sLogger, - ("ecs instanceID changed, old instanceID", - mEntity.getReadBuffer().GetEcsInstanceID())("new instanceID", meta.GetInstanceID())); + ("ecs mInstanceID changed, old mInstanceID", + mEntity.getReadBuffer().GetEcsInstanceID())("new mInstanceID", meta.GetInstanceID())); updateHostId(meta); mEntity.getWriteBuffer().SetECSMeta(meta); mEntity.swap(); diff --git a/core/common/MachineInfoUtil.h b/core/common/MachineInfoUtil.h index b0ce6e9175..eae5d2fdac 100644 --- a/core/common/MachineInfoUtil.h +++ b/core/common/MachineInfoUtil.h @@ -42,29 +42,29 @@ inline void SetID(const std::string& id, std::array& target, size_t& ta struct ECSMeta { ECSMeta() = default; - void SetInstanceID(const std::string& id) { SetID(id, instanceID, instanceIDLen); } + void SetInstanceID(const std::string& id) { SetID(id, mInstanceID, mInstanceIDLen); } - void SetUserID(const std::string& id) { SetID(id, userID, userIDLen); } + void SetUserID(const std::string& id) { SetID(id, mUserID, mUserIDLen); } - void SetRegionID(const std::string& id) { SetID(id, regionID, regionIDLen); } + void SetRegionID(const std::string& id) { SetID(id, mRegionID, mRegionIDLen); } - [[nodiscard]] StringView GetInstanceID() const { return StringView(instanceID.data(), instanceIDLen); } - [[nodiscard]] StringView GetUserID() const { return StringView(userID.data(), userIDLen); } - [[nodiscard]] StringView GetRegionID() const { return StringView(regionID.data(), regionIDLen); } + [[nodiscard]] StringView GetInstanceID() const { return StringView(mInstanceID.data(), mInstanceIDLen); } + [[nodiscard]] StringView GetUserID() const { return StringView(mUserID.data(), mUserIDLen); } + [[nodiscard]] StringView GetRegionID() const { return StringView(mRegionID.data(), mRegionIDLen); } [[nodiscard]] bool IsValid() const { return !GetInstanceID().empty() && !GetUserID().empty() && !GetRegionID().empty(); } private: - std::array instanceID{}; - size_t instanceIDLen = 0UL; + std::array mInstanceID{}; + size_t mInstanceIDLen = 0UL; - std::array userID{}; - size_t userIDLen = 0UL; + std::array mUserID{}; + size_t mUserIDLen = 0UL; - std::array regionID{}; - size_t regionIDLen = 0UL; + std::array mRegionID{}; + size_t mRegionIDLen = 0UL; friend class InstanceIdentityUnittest; }; @@ -94,19 +94,19 @@ struct Hostid { Hostid(const std::string& id, const Type& type) { SetHostID(id, type); } void SetHostID(const std::string& id, const Type& type) { - SetID(id, this->id, idLen); - this->type = type; + SetID(id, mId, mIdLen); + mType = type; } - [[nodiscard]] StringView GetHostID() const { return StringView(id.data(), idLen); } + [[nodiscard]] StringView GetHostID() const { return StringView(mId.data(), mIdLen); } - [[nodiscard]] Type GetType() const { return type; } + [[nodiscard]] Type GetType() const { return mType; } private: - std::array id{}; - size_t idLen = 0UL; + std::array mId{}; + size_t mIdLen = 0UL; - Type type; + Type mType; friend class InstanceIdentityUnittest; }; @@ -163,10 +163,8 @@ class InstanceIdentity { bool UpdateInstanceIdentity(const ECSMeta& meta); void DumpInstanceIdentity(); - void SetReady(); - [[nodiscard]] bool NeedFetchECSMeta() const { return mNeedFetchECSMeta; } - void InitFromFile(); + bool InitFromFile(); void InitFromNetwork(); @@ -184,7 +182,6 @@ class InstanceIdentity { #else std::string mEcsAssistMachineIdFile = "/usr/local/share/aliyun-assist/hybrid/machine-id"; #endif - bool mNeedFetchECSMeta = false; bool mHasTriedToGetSerialNumber = false; std::string mSerialNumber; diff --git a/core/unittest/common/InstanceIdentityUnittest.cpp b/core/unittest/common/InstanceIdentityUnittest.cpp index 8416ed043d..303425ea97 100644 --- a/core/unittest/common/InstanceIdentityUnittest.cpp +++ b/core/unittest/common/InstanceIdentityUnittest.cpp @@ -39,11 +39,11 @@ void InstanceIdentityUnittest::TestECSMeta() { meta.SetRegionID("cn-hangzhou"); APSARA_TEST_TRUE(meta.IsValid()); APSARA_TEST_EQUAL(meta.GetInstanceID().to_string(), "i-1234567890"); - APSARA_TEST_EQUAL(meta.instanceIDLen, 12); + APSARA_TEST_EQUAL(meta.mInstanceIDLen, 12); APSARA_TEST_EQUAL(meta.GetUserID().to_string(), "1234567890"); - APSARA_TEST_EQUAL(meta.userIDLen, 10); + APSARA_TEST_EQUAL(meta.mUserIDLen, 10); APSARA_TEST_EQUAL(meta.GetRegionID().to_string(), "cn-hangzhou"); - APSARA_TEST_EQUAL(meta.regionIDLen, 11); + APSARA_TEST_EQUAL(meta.mRegionIDLen, 11); } { ECSMeta meta; @@ -55,14 +55,15 @@ void InstanceIdentityUnittest::TestECSMeta() { { ECSMeta meta; for (size_t i = 0; i < ID_MAX_LENGTH; ++i) { - APSARA_TEST_EQUAL(meta.instanceID[i], '\0'); - APSARA_TEST_EQUAL(meta.userID[i], '\0'); - APSARA_TEST_EQUAL(meta.regionID[i], '\0'); + APSARA_TEST_EQUAL(meta.mInstanceID[i], '\0'); + APSARA_TEST_EQUAL(meta.mUserID[i], '\0'); + APSARA_TEST_EQUAL(meta.mRegionID[i], '\0'); } } { + // 测试设置字符串长度超过ID_MAX_LENGTH的情况 ECSMeta meta; - std::array testString{}; + std::array testString{}; for (size_t i = 0; i < testString.size(); ++i) { testString[i] = 'a'; } From 37a0e045a87e1605c815f7fcabaca1846997e49e Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 13:59:54 +0000 Subject: [PATCH 31/32] fix --- core/common/MachineInfoUtil.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/core/common/MachineInfoUtil.cpp b/core/common/MachineInfoUtil.cpp index 8f0aa29cf0..e307de040b 100644 --- a/core/common/MachineInfoUtil.cpp +++ b/core/common/MachineInfoUtil.cpp @@ -542,15 +542,15 @@ void InstanceIdentity::DumpInstanceIdentity() { mInstanceIdentityJson[sInstanceIdKey] = mEntity.getReadBuffer().GetEcsInstanceID().to_string(); mInstanceIdentityJson[sOwnerAccountIdKey] = mEntity.getReadBuffer().GetEcsUserID().to_string(); mInstanceIdentityJson[sRegionIdKey] = mEntity.getReadBuffer().GetEcsRegionID().to_string(); - DumpInstanceIdentity(); + dumpInstanceIdentityToFile(); } else if (mEntity.getReadBuffer().GetHostIdType() == Hostid::Type::LOCAL && mHasGeneratedLocalHostId) { mInstanceIdentityJson.clear(); mInstanceIdentityJson[sRandomHostIdKey] = mLocalHostId; - DumpInstanceIdentity(); + dumpInstanceIdentityToFile(); } else if (mEntity.getReadBuffer().GetHostIdType() == Hostid::Type::ECS_ASSIST && !mSerialNumber.empty()) { mInstanceIdentityJson.clear(); mInstanceIdentityJson[sECSAssistMachineIdKey] = mSerialNumber; - DumpInstanceIdentity(); + dumpInstanceIdentityToFile(); } } @@ -564,6 +564,7 @@ void InstanceIdentity::InitFromNetwork() { bool InstanceIdentity::InitFromFile() { mInstanceIdentityFile = GetAgentDataDir() + PATH_SEPARATOR + "instance_identity"; bool initSuccess = false; + ECSMeta meta; if (CheckExistance(mInstanceIdentityFile)) { std::string instanceIdentityStr; if (ReadFileContent(mInstanceIdentityFile, instanceIdentityStr)) { @@ -575,9 +576,7 @@ bool InstanceIdentity::InitFromFile() { errMsg)("instanceIdentity", instanceIdentityStr)("file", mInstanceIdentityFile)); } else { mInstanceIdentityJson = std::move(doc); - ECSMeta meta; if (ParseECSMeta(instanceIdentityStr, meta)) { - mEntity.getWriteBuffer().SetECSMeta(meta); // 存在 ecs meta信息,则认为instanceIdentity是ready的 initSuccess = true; } else if (mInstanceIdentityJson.isMember(sRandomHostIdKey) @@ -603,7 +602,10 @@ bool InstanceIdentity::InitFromFile() { } } // 计算hostid - updateHostId(mEntity.getWriteBuffer().GetECSMeta()); + if (meta.IsValid()) { + mEntity.getWriteBuffer().SetECSMeta(meta); + } + updateHostId(meta); mEntity.swap(); return initSuccess; } @@ -614,8 +616,8 @@ bool InstanceIdentity::UpdateInstanceIdentity(const ECSMeta& meta) { LOG_INFO(sLogger, ("ecs mInstanceID changed, old mInstanceID", mEntity.getReadBuffer().GetEcsInstanceID())("new mInstanceID", meta.GetInstanceID())); - updateHostId(meta); mEntity.getWriteBuffer().SetECSMeta(meta); + updateHostId(meta); mEntity.swap(); return true; } From e66dddc6a4c70f69f877f1846373f2083c4f074b Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 9 Jan 2025 14:25:36 +0000 Subject: [PATCH 32/32] fix test --- core/unittest/common/InstanceIdentityUnittest.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/core/unittest/common/InstanceIdentityUnittest.cpp b/core/unittest/common/InstanceIdentityUnittest.cpp index 303425ea97..c1f878a54a 100644 --- a/core/unittest/common/InstanceIdentityUnittest.cpp +++ b/core/unittest/common/InstanceIdentityUnittest.cpp @@ -67,6 +67,7 @@ void InstanceIdentityUnittest::TestECSMeta() { for (size_t i = 0; i < testString.size(); ++i) { testString[i] = 'a'; } + testString[testString.size() - 1] = '\0'; meta.SetInstanceID(testString.data()); meta.SetUserID(testString.data()); meta.SetRegionID(testString.data());