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

[INLONG-9190][Agent] Fix log file source clear buffer queue does not take effect #9191

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public void destroy() {
}

private void clearQueue(BlockingQueue<SourceData> queue) {
if (queue != null) {
if (queue == null) {
return;
}
while (queue != null && !queue.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ public class TestLogFileSource {

private static final Logger LOGGER = LoggerFactory.getLogger(TestLogFileSource.class);
private static final ClassLoader LOADER = TestLogFileSource.class.getClassLoader();
private static LogFileSource source;
private static AgentBaseTestsHelper helper;
private static final Gson GSON = new Gson();
private static final String[] check = {"hello line-end-symbol aa", "world line-end-symbol",
"agent line-end-symbol"};
private static InstanceProfile instanceProfile;

@BeforeClass
public static void setup() {
Expand All @@ -57,37 +59,47 @@ public static void setup() {
helper = new AgentBaseTestsHelper(TestLogFileSource.class.getName()).setupAgentHome();
String pattern = helper.getTestRootDir() + "/YYYYMMDD.log_[0-9]+";
TaskProfile taskProfile = helper.getTaskProfile(1, pattern, false, 0L, 0L, TaskStateEnum.RUNNING);
InstanceProfile instanceProfile = taskProfile.createInstanceProfile("",
instanceProfile = taskProfile.createInstanceProfile("",
fileName, "20230928");

}

private LogFileSource getSource() {
try {
instanceProfile.set(TaskConstants.INODE_INFO, FileDataUtils.getInodeInfo(instanceProfile.getInstanceId()));
source = new LogFileSource();
LogFileSource source = new LogFileSource();
Whitebox.setInternalState(source, "BATCH_READ_LINE_COUNT", 1);
Whitebox.setInternalState(source, "BATCH_READ_LINE_TOTAL_LEN", 10);
Whitebox.setInternalState(source, "PRINT_INTERVAL_MS", 0);
Whitebox.setInternalState(source, "SIZE_OF_BUFFER_TO_READ_FILE", 2);
Whitebox.setInternalState(source, "FINISH_READ_MAX_COUNT", 1);
Whitebox.setInternalState(source, "READ_WAIT_TIMEOUT_MS", 10);
source.init(instanceProfile);
return source;
} catch (Exception e) {
LOGGER.error("source init error {}", e);
Assert.assertTrue("source init error", false);
}
return null;
}

@AfterClass
public static void teardown() throws Exception {
source.destroy();
helper.teardownAgentHome();
}

@Test
public void testTaskManager() {
String[] check = {"hello line-end-symbol aa", "world line-end-symbol", "agent line-end-symbol"};
public void testLogFileSource() {
testFullRead();
testCleanQueue();
}

private void testFullRead() {
int srcLen = 0;
for (int i = 0; i < check.length; i++) {
srcLen += check[i].getBytes(StandardCharsets.UTF_8).length;
}
LogFileSource source = getSource();
await().atMost(2, TimeUnit.SECONDS).until(() -> source.sourceFinish());
int cnt = 0;
int leftBeforeRead = MemoryManager.getInstance().getLeft(AGENT_GLOBAL_READER_QUEUE_PERMIT);
Expand All @@ -101,9 +113,21 @@ public void testTaskManager() {
msg = source.read();
cnt++;
}
source.destroy();
Assert.assertTrue(cnt == 3);
Assert.assertTrue(srcLen == readLen);
int leftAfterRead = MemoryManager.getInstance().getLeft(AGENT_GLOBAL_READER_QUEUE_PERMIT);
Assert.assertTrue(leftAfterRead == DEFAULT_AGENT_GLOBAL_READER_QUEUE_PERMIT);
}

private void testCleanQueue() {
LogFileSource source = getSource();
await().atMost(2, TimeUnit.SECONDS).until(() -> source.sourceFinish());
for (int i = 0; i < 2; i++) {
source.read();
}
source.destroy();
int leftAfterRead = MemoryManager.getInstance().getLeft(AGENT_GLOBAL_READER_QUEUE_PERMIT);
Assert.assertTrue(leftAfterRead == DEFAULT_AGENT_GLOBAL_READER_QUEUE_PERMIT);
}
}