fix: jdk8 send jfr data error (#725)

This commit is contained in:
zhengziyi0117 2024-10-31 17:31:33 +08:00 committed by GitHub
parent 54a3372a8f
commit 26e59485cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 65 additions and 70 deletions

View File

@ -39,10 +39,10 @@ import org.apache.skywalking.apm.network.language.asyncprofiler.v10.AsyncProfile
import org.apache.skywalking.apm.network.language.asyncprofiler.v10.AsyncProfilerTaskGrpc; import org.apache.skywalking.apm.network.language.asyncprofiler.v10.AsyncProfilerTaskGrpc;
import org.apache.skywalking.apm.network.language.asyncprofiler.v10.AsyncProfilingStatus; import org.apache.skywalking.apm.network.language.asyncprofiler.v10.AsyncProfilingStatus;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.file.Files;
import java.nio.channels.FileChannel;
import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.apache.skywalking.apm.agent.core.conf.Config.AsyncProfiler.DATA_CHUNK_SIZE; import static org.apache.skywalking.apm.agent.core.conf.Config.AsyncProfiler.DATA_CHUNK_SIZE;
@ -87,12 +87,14 @@ public class AsyncProfilerDataSender implements BootService, GRPCChannelListener
this.status = status; this.status = status;
} }
public void sendData(AsyncProfilerTask task, FileChannel channel) throws IOException, InterruptedException { public void sendData(AsyncProfilerTask task, File dumpFile) throws IOException, InterruptedException {
if (status != GRPCChannelStatus.CONNECTED || Objects.isNull(channel) || !channel.isOpen()) { if (status != GRPCChannelStatus.CONNECTED) {
return; return;
} }
int size = Math.toIntExact(channel.size()); try (FileInputStream fileInputStream = new FileInputStream(dumpFile)) {
long fileSize = Files.size(dumpFile.toPath());
int size = Math.toIntExact(fileSize);
final GRPCStreamServiceStatus status = new GRPCStreamServiceStatus(false); final GRPCStreamServiceStatus status = new GRPCStreamServiceStatus(false);
StreamObserver<AsyncProfilerData> dataStreamObserver = asyncProfilerTaskStub.withDeadlineAfter( StreamObserver<AsyncProfilerData> dataStreamObserver = asyncProfilerTaskStub.withDeadlineAfter(
GRPC_UPSTREAM_TIMEOUT, TimeUnit.SECONDS GRPC_UPSTREAM_TIMEOUT, TimeUnit.SECONDS
@ -109,15 +111,14 @@ public class AsyncProfilerDataSender implements BootService, GRPCChannelListener
if (AsyncProfilingStatus.TERMINATED_BY_OVERSIZE.equals(value.getType())) { if (AsyncProfilingStatus.TERMINATED_BY_OVERSIZE.equals(value.getType())) {
LOGGER.warn("JFR is too large to be received by the oap server"); LOGGER.warn("JFR is too large to be received by the oap server");
} else { } else {
ByteBuffer buf = ByteBuffer.allocateDirect(DATA_CHUNK_SIZE); byte[] buf = new byte[DATA_CHUNK_SIZE];
try { try {
while (channel.read(buf) > 0) { int bytesRead;
buf.flip(); while ((bytesRead = fileInputStream.read(buf)) != -1) {
AsyncProfilerData asyncProfilerData = AsyncProfilerData.newBuilder() AsyncProfilerData asyncProfilerData = AsyncProfilerData.newBuilder()
.setContent(ByteString.copyFrom(buf)) .setContent(ByteString.copyFrom(buf, 0, bytesRead))
.build(); .build();
requestStream.onNext(asyncProfilerData); requestStream.onNext(asyncProfilerData);
buf.clear();
} }
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("Failed to read JFR file and failed to upload to oap", e); LOGGER.error("Failed to read JFR file and failed to upload to oap", e);
@ -151,6 +152,7 @@ public class AsyncProfilerDataSender implements BootService, GRPCChannelListener
status.wait4Finish(); status.wait4Finish();
} }
}
public void sendError(AsyncProfilerTask task, String errorMessage) { public void sendError(AsyncProfilerTask task, String errorMessage) {
if (status != GRPCChannelStatus.CONNECTED) { if (status != GRPCChannelStatus.CONNECTED) {

View File

@ -27,9 +27,7 @@ import org.apache.skywalking.apm.agent.core.logging.api.ILog;
import org.apache.skywalking.apm.agent.core.logging.api.LogManager; import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@ -90,21 +88,16 @@ public class AsyncProfilerTaskExecutionService implements BootService {
} }
private void stopWhenSuccess(AsyncProfilerTask task) { private void stopWhenSuccess(AsyncProfilerTask task) {
// stop task and send data
try { try {
File dumpFile = task.stop(getAsyncProfiler()); File dumpFile = task.stop(getAsyncProfiler());
// stop task if (dumpFile != null && dumpFile.exists()) {
try (FileInputStream fileInputStream = new FileInputStream(dumpFile)) {
// upload file
FileChannel channel = fileInputStream.getChannel();
AsyncProfilerDataSender dataSender = ServiceManager.INSTANCE.findService(AsyncProfilerDataSender.class); AsyncProfilerDataSender dataSender = ServiceManager.INSTANCE.findService(AsyncProfilerDataSender.class);
dataSender.sendData(task, channel); dataSender.sendData(task, dumpFile);
}
if (!dumpFile.delete()) { if (!dumpFile.delete()) {
LOGGER.warn("Fail to delete the dump file of async profiler."); LOGGER.warn("Fail to delete the dump file of async profiler.");
} }
}
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("stop async profiler task error", e); LOGGER.error("stop async profiler task error", e);
return; return;