Skip to content

refactor logic of last matched line #1426

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

Merged
merged 7 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
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
73 changes: 34 additions & 39 deletions core/reader/LogFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@
#include <fcntl.h>
#include <io.h>
#endif
#include <cityhash/city.h>
#include <time.h>

#include <algorithm>
#include <boost/filesystem.hpp>
#include <boost/regex.hpp>
#include <limits>
#include <numeric>
#include <random>

#include <boost/filesystem.hpp>
#include <boost/regex.hpp>
#include <cityhash/city.h>

#include "GloablFileDescriptorManager.h"
#include "app_config/AppConfig.h"
#include "checkpoint/CheckPointManager.h"
Expand Down Expand Up @@ -2004,52 +2003,48 @@ int32_t LogFileReader::LastMatchedLine(char* buffer, int32_t size, int32_t& roll
return 0;
}
// Multiline rollback
int begPs = size - 2;
std::string exception;
while (begPs >= 0) {
if (buffer[begPs] == '\n' || begPs == 0) {
int lineBegin = begPs == 0 ? 0 : begPs + 1;
if (mMultilineConfig.first->GetContinuePatternReg()
&& BoostRegexMatch(buffer + lineBegin,
endPs - lineBegin,
*mMultilineConfig.first->GetContinuePatternReg(),
exception)) {
++rollbackLineFeedCount;
endPs = begPs;
} else if (mMultilineConfig.first->GetEndPatternReg()
&& BoostRegexMatch(buffer + lineBegin,
endPs - lineBegin,
*mMultilineConfig.first->GetEndPatternReg(),
exception)) {
while (endPs >= 0) {
size_t begPs = GetNextLine(buffer, endPs);
if (mMultilineConfig.first->GetEndPatternReg()) {
// start + end, continue + end, end
if (BoostRegexMatch(
buffer + begPs, endPs - begPs, *mMultilineConfig.first->GetEndPatternReg(), exception)) {
// Ensure the end line is complete
if (buffer[endPs] == '\n') {
return endPs + 1;
} else {
++rollbackLineFeedCount;
endPs = begPs;
}
} else if (mMultilineConfig.first->GetStartPatternReg()
&& BoostRegexMatch(buffer + lineBegin,
endPs - lineBegin,
*mMultilineConfig.first->GetStartPatternReg(),
exception)) {
++rollbackLineFeedCount;
// Keep all the buffer if rollback all
return lineBegin;
} else if (mMultilineConfig.first->GetContinuePatternReg()) {
// We can confirm the logs before are complete if continue is configured but no regex pattern can match.
if (buffer[endPs] == '\n') {
return endPs + 1;
} else {
// Keep all the buffer if rollback all
return lineBegin;
endPs = begPs - 1;
}
} else {
++rollbackLineFeedCount;
endPs = begPs;
endPs = begPs - 1;
}
} else if (mMultilineConfig.first->GetStartPatternReg()
&& BoostRegexMatch(
buffer + begPs, endPs - begPs, *mMultilineConfig.first->GetStartPatternReg(), exception)) {
// start + continue, start
++rollbackLineFeedCount;
// Keep all the buffer if rollback all
return begPs;
} else {
++rollbackLineFeedCount;
endPs = begPs - 1;
}
}
return 0;
}

size_t LogFileReader::GetNextLine(const char* buffer, size_t end) {
if (end <= 0) {
return 0;
}

for (size_t begin = end; begin > 0; --begin) {
if (buffer[begin - 1] == '\n') {
return begin;
}
begPs--;
}
return 0;
}
Expand Down
5 changes: 3 additions & 2 deletions core/reader/LogFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ class LogFileReader {
// @param fromCpt: if the read size is recoveried from checkpoint, set it to true.
size_t getNextReadSize(int64_t fileEnd, bool& fromCpt);

size_t GetNextLine(const char* buffer, size_t begin);

// Update current checkpoint's read offset and length after success read.
void setExactlyOnceCheckpointAfterRead(size_t readSize);

Expand Down Expand Up @@ -589,8 +591,7 @@ class LogFileReader {
friend class LogSplitUnittest;
friend class LogSplitDiscardUnmatchUnittest;
friend class LogSplitNoDiscardUnmatchUnittest;
friend class LastMatchedLineDiscardUnmatchUnittest;
friend class LastMatchedLineNoDiscardUnmatchUnittest;
friend class LastMatchedLineMultilineUnittest;
friend class LogFileReaderCheckpointUnittest;

protected:
Expand Down
Loading
Loading