-
Notifications
You must be signed in to change notification settings - Fork 396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: fix PreservedDirDepth not working with polling and wildcard path #1866
Merged
yyuuttaaoo
merged 9 commits into
alibaba:main
from
yyuuttaaoo:fix/preserved_dir_depth_polling
Nov 25, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
48f1354
fix PreservedDirDepth not working with polling and wildcard path
yyuuttaaoo 3be6377
Add unittest and fix mem leaks
yyuuttaaoo 45efdd6
Merge branch 'main' into fix/preserved_dir_depth_polling
yyuuttaaoo bc4ba21
fix compile
yyuuttaaoo fcdfc67
fix register dir
yyuuttaaoo 92642c2
fix CMakeLists.txt
yyuuttaaoo 250d6d3
remove unwanted logs
yyuuttaaoo df6c7ad
Merge upstream/main into fix/preserved_dir_depth_polling
yyuuttaaoo 642f50c
fix UT
yyuuttaaoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,10 +110,15 @@ ParseConfResult ParseConfig(const std::string& configName, Json::Value& jsonRoot | |
|
||
ifstream is; | ||
is.open(fullPath.c_str()); | ||
if (!is.good()) { | ||
if (!is) { // https://horstmann.com/cpp/pitfalls.html | ||
return CONFIG_NOT_EXIST; | ||
} | ||
std::string buffer; | ||
try { | ||
buffer.assign(std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>()); | ||
} catch (const std::ios_base::failure& e) { | ||
return CONFIG_NOT_EXIST; | ||
} | ||
std::string buffer((std::istreambuf_iterator<char>(is)), (std::istreambuf_iterator<char>())); | ||
if (!IsValidJson(buffer.c_str(), buffer.length())) { | ||
return CONFIG_INVALID_FORMAT; | ||
} | ||
|
@@ -144,7 +149,7 @@ bool ConfigManager::RegisterHandlersRecursively(const std::string& path, | |
return result; | ||
|
||
if (!config.first->IsDirectoryInBlacklist(path)) | ||
result = EventDispatcher::GetInstance()->RegisterEventHandler(path.c_str(), config, mSharedHandler); | ||
result = EventDispatcher::GetInstance()->RegisterEventHandler(path, config, mSharedHandler); | ||
|
||
if (!result) | ||
return result; | ||
|
@@ -305,9 +310,16 @@ void ConfigManager::RegisterWildcardPath(const FileDiscoveryConfig& config, cons | |
if (registerStatus == GET_REGISTER_STATUS_ERROR) { | ||
return; | ||
} | ||
if (EventDispatcher::GetInstance()->RegisterEventHandler(item.c_str(), config, mSharedHandler)) { | ||
if (config.first->mPreservedDirDepth < 0) | ||
RegisterDescendants( | ||
item, config, config.first->mMaxDirSearchDepth < 0 ? 100 : config.first->mMaxDirSearchDepth); | ||
else { | ||
// preserve_depth register | ||
RegisterHandlersWithinDepth(item, | ||
config, | ||
config.first->mPreservedDirDepth, | ||
config.first->mMaxDirSearchDepth < 0 ? 100 | ||
: config.first->mMaxDirSearchDepth); | ||
} | ||
} else { | ||
RegisterWildcardPath(config, item, depth + 1); | ||
|
@@ -381,9 +393,16 @@ void ConfigManager::RegisterWildcardPath(const FileDiscoveryConfig& config, cons | |
if (registerStatus == GET_REGISTER_STATUS_ERROR) { | ||
return; | ||
} | ||
if (EventDispatcher::GetInstance()->RegisterEventHandler(item.c_str(), config, mSharedHandler)) { | ||
if (config.first->mPreservedDirDepth < 0) | ||
RegisterDescendants( | ||
item, config, config.first->mMaxDirSearchDepth < 0 ? 100 : config.first->mMaxDirSearchDepth); | ||
else { | ||
// preserve_depth register | ||
RegisterHandlersWithinDepth( | ||
item, | ||
config, | ||
config.first->mPreservedDirDepth, | ||
config.first->mMaxDirSearchDepth < 0 ? 100 : config.first->mMaxDirSearchDepth); | ||
} | ||
} else { | ||
RegisterWildcardPath(config, item, depth + 1); | ||
|
@@ -420,52 +439,57 @@ bool ConfigManager::RegisterHandlers(const string& basePath, const FileDiscovery | |
DirRegisterStatus registerStatus = EventDispatcher::GetInstance()->IsDirRegistered(basePath); | ||
if (registerStatus == GET_REGISTER_STATUS_ERROR) | ||
return result; | ||
// dir in config is valid by default, do not call pathValidator | ||
result = EventDispatcher::GetInstance()->RegisterEventHandler(basePath.c_str(), config, mSharedHandler); | ||
// if we come into a failure, do not try to register others, there must be something wrong! | ||
if (!result) | ||
return result; | ||
|
||
if (config.first->mPreservedDirDepth < 0) | ||
result = RegisterDescendants( | ||
basePath, config, config.first->mMaxDirSearchDepth < 0 ? 100 : config.first->mMaxDirSearchDepth); | ||
else { | ||
// preserve_depth register | ||
int depth = config.first->mPreservedDirDepth; | ||
result = RegisterHandlersWithinDepth(basePath, config, depth); | ||
result = RegisterHandlersWithinDepth(basePath, | ||
config, | ||
config.first->mPreservedDirDepth, | ||
config.first->mMaxDirSearchDepth < 0 ? 100 | ||
: config.first->mMaxDirSearchDepth); | ||
} | ||
return result; | ||
} | ||
|
||
bool ConfigManager::RegisterDirectory(const std::string& source, const std::string& object) { | ||
// TODO��A potential bug: FindBestMatch will test @object with filePattern, which has very | ||
// TODO: A potential bug: FindBestMatch will test @object with filePattern, which has very | ||
// low possibility to match a sub directory name, so here will return false in most cases. | ||
// e.g.: source: /path/to/monitor, file pattern: *.log, object: subdir. | ||
// Match(subdir, *.log) = false. | ||
FileDiscoveryConfig config = FindBestMatch(source, object); | ||
if (config.first && !config.first->IsDirectoryInBlacklist(source)) | ||
return EventDispatcher::GetInstance()->RegisterEventHandler(source.c_str(), config, mSharedHandler); | ||
if (config.first && !config.first->IsDirectoryInBlacklist(source)) { | ||
return EventDispatcher::GetInstance()->RegisterEventHandler(source, config, mSharedHandler); | ||
} | ||
return false; | ||
} | ||
|
||
bool ConfigManager::RegisterHandlersWithinDepth(const std::string& path, const FileDiscoveryConfig& config, int depth) { | ||
bool ConfigManager::RegisterHandlersWithinDepth(const std::string& path, | ||
const FileDiscoveryConfig& config, | ||
int preservedDirDepth, | ||
int maxDepth) { | ||
if (maxDepth < 0) { | ||
return true; | ||
} | ||
if (AppConfig::GetInstance()->IsHostPathMatchBlacklist(path)) { | ||
LOG_INFO(sLogger, ("ignore path matching host path blacklist", path)); | ||
return false; | ||
} | ||
if (depth <= 0) { | ||
DirCheckPointPtr dirCheckPoint; | ||
if (CheckPointManager::Instance()->GetDirCheckPoint(path, dirCheckPoint) == false) | ||
if (preservedDirDepth < 0) { | ||
fsutil::PathStat statBuf; | ||
if (!fsutil::PathStat::stat(path, statBuf)) { | ||
return true; | ||
} | ||
int64_t sec = 0; | ||
int64_t nsec = 0; | ||
statBuf.GetLastWriteTime(sec, nsec); | ||
auto curTime = time(nullptr); | ||
if (curTime - sec > INT32_FLAG(timeout_interval)) { | ||
return true; | ||
// path had dircheckpoint means it was watched before, so it is valid | ||
const set<string>& subdir = dirCheckPoint.get()->mSubDir; | ||
for (set<string>::iterator it = subdir.begin(); it != subdir.end(); it++) { | ||
if (EventDispatcher::GetInstance()->RegisterEventHandler((*it).c_str(), config, mSharedHandler)) | ||
RegisterHandlersWithinDepth(*it, config, depth - 1); | ||
} | ||
return true; | ||
} | ||
bool result = true; | ||
|
||
fsutil::Dir dir(path); | ||
if (!dir.Open()) { | ||
|
@@ -479,30 +503,44 @@ bool ConfigManager::RegisterHandlersWithinDepth(const std::string& path, const F | |
LOG_ERROR(sLogger, ("Open dir error: ", path.c_str())("errno", err)); | ||
return false; | ||
} | ||
if (!(EventDispatcher::GetInstance()->RegisterEventHandler(path, config, mSharedHandler))) { | ||
// break;// fail early, do not try to register others | ||
return false; | ||
} | ||
if (maxDepth == 0) { | ||
return true; | ||
} | ||
|
||
if (preservedDirDepth == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. =0时就必须进入checkpoint恢复的注册方式,在此方式下保持PreservedDirDepth=0确保注册成功 |
||
DirCheckPointPtr dirCheckPoint; | ||
if (CheckPointManager::Instance()->GetDirCheckPoint(path, dirCheckPoint)) { | ||
// path had dircheckpoint means it was watched before, so it is valid | ||
const set<string>& subdir = dirCheckPoint.get()->mSubDir; | ||
for (const auto& it : subdir) { | ||
RegisterHandlersWithinDepth(it, config, 0, maxDepth - 1); | ||
} | ||
return true; | ||
} | ||
} | ||
fsutil::Entry ent; | ||
while ((ent = dir.ReadNext())) { | ||
string item = PathJoin(path, ent.Name()); | ||
if (ent.IsDir() && !config.first->IsDirectoryInBlacklist(item)) { | ||
if (!(EventDispatcher::GetInstance()->RegisterEventHandler(item.c_str(), config, mSharedHandler))) { | ||
// break;// fail early, do not try to register others | ||
result = false; | ||
} else // sub dir will not be registered if parent dir fails | ||
RegisterHandlersWithinDepth(item, config, depth - 1); | ||
RegisterHandlersWithinDepth(item, config, preservedDirDepth - 1, maxDepth - 1); | ||
} | ||
} | ||
|
||
return result; | ||
return true; | ||
} | ||
|
||
// path not terminated by '/', path already registered | ||
bool ConfigManager::RegisterDescendants(const string& path, const FileDiscoveryConfig& config, int withinDepth) { | ||
if (withinDepth < 0) { | ||
return true; | ||
} | ||
if (AppConfig::GetInstance()->IsHostPathMatchBlacklist(path)) { | ||
LOG_INFO(sLogger, ("ignore path matching host path blacklist", path)); | ||
return false; | ||
} | ||
if (withinDepth <= 0) { | ||
return true; | ||
} | ||
|
||
fsutil::Dir dir(path); | ||
if (!dir.Open()) { | ||
|
@@ -515,14 +553,20 @@ bool ConfigManager::RegisterDescendants(const string& path, const FileDiscoveryC | |
LOG_ERROR(sLogger, ("Open dir error: ", path.c_str())("errno", err)); | ||
return false; | ||
} | ||
if (!EventDispatcher::GetInstance()->RegisterEventHandler(path, config, mSharedHandler)) { | ||
// break;// fail early, do not try to register others | ||
return false; | ||
} | ||
if (withinDepth == 0) { | ||
return true; | ||
} | ||
|
||
fsutil::Entry ent; | ||
bool result = true; | ||
while ((ent = dir.ReadNext())) { | ||
string item = PathJoin(path, ent.Name()); | ||
if (ent.IsDir() && !config.first->IsDirectoryInBlacklist(item)) { | ||
result = EventDispatcher::GetInstance()->RegisterEventHandler(item.c_str(), config, mSharedHandler); | ||
if (result) | ||
RegisterDescendants(item, config, withinDepth - 1); | ||
RegisterDescendants(item, config, withinDepth - 1); | ||
} | ||
} | ||
return result; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
=0和<0需要区分,=0时总是注册baseDir,<0时必须符合不超时的条件