From 35388ac062385815de57a900b2c9f0a3f1cb05ca Mon Sep 17 00:00:00 2001 From: xiaqi1210 Date: Fri, 16 Jun 2023 21:26:34 +0800 Subject: [PATCH] Fix the apm-jdk-threadpool-plugin does not work in certain scenarios (#556) --- .../agent/core/logging/core/FileWriter.java | 70 +++++++++++-------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core/FileWriter.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core/FileWriter.java index eed675055..4020b7a98 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core/FileWriter.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core/FileWriter.java @@ -18,6 +18,10 @@ package org.apache.skywalking.apm.agent.core.logging.core; +import org.apache.skywalking.apm.agent.core.conf.Config; +import org.apache.skywalking.apm.agent.core.conf.Constants; +import org.apache.skywalking.apm.util.RunnableWithExceptionProtection; + import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -30,13 +34,8 @@ import java.util.Comparator; import java.util.Date; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Callable; -import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; -import org.apache.skywalking.apm.agent.core.boot.DefaultNamedThreadFactory; -import org.apache.skywalking.apm.agent.core.conf.Config; -import org.apache.skywalking.apm.agent.core.conf.Constants; -import org.apache.skywalking.apm.util.RunnableWithExceptionProtection; /** * The FileWriter support async file output, by using a queue as buffer. @@ -63,31 +62,42 @@ public class FileWriter implements IWriter { private FileWriter() { logBuffer = new ArrayBlockingQueue(1024); final ArrayList outputLogs = new ArrayList(200); - Executors.newSingleThreadScheduledExecutor(new DefaultNamedThreadFactory("LogFileWriter")) - .scheduleAtFixedRate(new RunnableWithExceptionProtection(new Runnable() { - @Override - public void run() { - try { - logBuffer.drainTo(outputLogs); - for (String log : outputLogs) { - writeToFile(log + Constants.LINE_SEPARATOR); - } - try { - if (fileOutputStream != null) { - fileOutputStream.flush(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } finally { - outputLogs.clear(); - } - } - }, new RunnableWithExceptionProtection.CallbackWhenException() { - @Override - public void handle(Throwable t) { - } - }), 0, 1, TimeUnit.SECONDS); + Thread logFlusherThread = new Thread(new RunnableWithExceptionProtection(new Runnable() { + + @Override + public void run() { + while (true) { + // flush log to file + try { + logBuffer.drainTo(outputLogs); + for (String log : outputLogs) { + writeToFile(log + Constants.LINE_SEPARATOR); + } + try { + if (fileOutputStream != null) { + fileOutputStream.flush(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } finally { + outputLogs.clear(); + } + + // flush log once per second + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException e) { + } + } + + } + }, new RunnableWithExceptionProtection.CallbackWhenException() { + @Override + public void handle(Throwable t) { + } + }), "SkywalkingAgent-LogFileWriter"); + logFlusherThread.start(); } /**