Skip to content
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: collect files in PV when another container restart or stop #2010

Merged
merged 11 commits into from
Feb 18, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix
Abingcbc committed Jan 7, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit a190069e639247efcd20109bf6b787e30a7d8658
3 changes: 2 additions & 1 deletion core/file_server/FileDiscoveryOptions.cpp
Original file line number Diff line number Diff line change
@@ -645,7 +645,8 @@ ContainerInfo* FileDiscoveryOptions::GetContainerPathByLogPath(const string& log
if (!mContainerInfos) {
return NULL;
}
for (size_t i = 0; i < mContainerInfos->size(); ++i) {
// reverse order to find the latest container
for (size_t i = mContainerInfos->size() - 1; i >= 0; --i) {
if (_IsSubPath((*mContainerInfos)[i].mRealBaseDir, logPath)) {
return &(*mContainerInfos)[i];
}
31 changes: 21 additions & 10 deletions core/file_server/event_handler/EventHandler.cpp
Original file line number Diff line number Diff line change
@@ -792,16 +792,27 @@ void ModifyHandler::Handle(const Event& event) {
"file size", reader->GetFileSize()));
reader->CloseFilePtr();
} else if (reader->IsContainerStopped()) {
// release fd as quick as possible
LOG_INFO(
sLogger,
("close the file", "current file has been read, and the relative container has been stopped")(
"project", reader->GetProject())("logstore", reader->GetLogstore())("config", mConfigName)(
"log reader queue name", reader->GetHostLogPath())("file device",
reader->GetDevInode().dev)(
"file inode", reader->GetDevInode().inode)("file size", reader->GetFileSize()));
ForceReadLogAndPush(reader);
reader->CloseFilePtr();
// update container info one more time, ensure file is hold by same cotnainer
if (reader->UpdateContainerInfo()) {
LOG_INFO(
sLogger,
("close the file", "but file is reused by a new container")(
"project", reader->GetProject())("logstore", reader->GetLogstore())(
"config", mConfigName)("log reader queue name", reader->GetHostLogPath())(
"file device", reader->GetDevInode().dev)("file inode", reader->GetDevInode().inode)(
"file size", reader->GetFileSize())("container id", reader->GetContainerID()));
} else {
// release fd as quick as possible
LOG_INFO(sLogger,
("close the file",
"current file has been read, and the relative container has been stopped")(
"project", reader->GetProject())("logstore", reader->GetLogstore())(
"config", mConfigName)("log reader queue name", reader->GetHostLogPath())(
"file device", reader->GetDevInode().dev)(
"file inode", reader->GetDevInode().inode)("file size", reader->GetFileSize()));
ForceReadLogAndPush(reader);
reader->CloseFilePtr();
}
}
break;
}
17 changes: 17 additions & 0 deletions core/file_server/reader/LogFileReader.cpp
Original file line number Diff line number Diff line change
@@ -2530,6 +2530,23 @@ const std::string& LogFileReader::GetConvertedPath() const {
#endif
}

bool LogFileReader::UpdateContainerInfo() {
FileDiscoveryConfig discoveryConfig = FileServer::GetInstance()->GetFileDiscoveryConfig(mConfigName);
ContainerInfo* containerPath = discoveryConfig.first->GetContainerPathByLogPath(mHostLogPathDir);
if (containerPath && containerPath->mID != mContainerID) {
// if config have wildcard path, use mWildcardPaths[0] as base path
SetDockerPath(!discoveryConfig.first->GetWildcardPaths().empty() ? discoveryConfig.first->GetWildcardPaths()[0]
: discoveryConfig.first->GetBasePath(),
containerPath->mRealBaseDir.size());
SetContainerID(containerPath->mID);
mExtraTags.clear();
AddExtraTags(containerPath->mMetadatas);
AddExtraTags(containerPath->mTags);
return true;
}
return false;
}

#ifdef APSARA_UNIT_TEST_MAIN
void LogFileReader::UpdateReaderManual() {
if (mLogFileOp.IsOpen()) {
2 changes: 2 additions & 0 deletions core/file_server/reader/LogFileReader.h
Original file line number Diff line number Diff line change
@@ -296,6 +296,8 @@ class LogFileReader {

void SetContainerID(const std::string& containerID) { mContainerID = containerID; }

bool UpdateContainerInfo();

bool IsFileOpened() const { return mLogFileOp.IsOpen(); }

bool ShouldForceReleaseDeletedFileFd();