|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.apm.agent.core.asyncprofiler; |
| 20 | + |
| 21 | +import com.google.protobuf.ByteString; |
| 22 | +import io.grpc.Channel; |
| 23 | +import io.grpc.stub.ClientCallStreamObserver; |
| 24 | +import io.grpc.stub.ClientResponseObserver; |
| 25 | +import io.grpc.stub.StreamObserver; |
| 26 | +import org.apache.skywalking.apm.agent.core.boot.BootService; |
| 27 | +import org.apache.skywalking.apm.agent.core.boot.DefaultImplementor; |
| 28 | +import org.apache.skywalking.apm.agent.core.boot.ServiceManager; |
| 29 | +import org.apache.skywalking.apm.agent.core.conf.Config; |
| 30 | +import org.apache.skywalking.apm.agent.core.logging.api.ILog; |
| 31 | +import org.apache.skywalking.apm.agent.core.logging.api.LogManager; |
| 32 | +import org.apache.skywalking.apm.agent.core.remote.GRPCChannelListener; |
| 33 | +import org.apache.skywalking.apm.agent.core.remote.GRPCChannelManager; |
| 34 | +import org.apache.skywalking.apm.agent.core.remote.GRPCChannelStatus; |
| 35 | +import org.apache.skywalking.apm.agent.core.remote.GRPCStreamServiceStatus; |
| 36 | +import org.apache.skywalking.apm.network.language.asyncprofiler.v10.AsyncProfilerCollectionResponse; |
| 37 | +import org.apache.skywalking.apm.network.language.asyncprofiler.v10.AsyncProfilerData; |
| 38 | +import org.apache.skywalking.apm.network.language.asyncprofiler.v10.AsyncProfilerMetaData; |
| 39 | +import org.apache.skywalking.apm.network.language.asyncprofiler.v10.AsyncProfilerTaskGrpc; |
| 40 | +import org.apache.skywalking.apm.network.language.asyncprofiler.v10.AsyncProfilingStatus; |
| 41 | + |
| 42 | +import java.io.File; |
| 43 | +import java.io.FileInputStream; |
| 44 | +import java.io.IOException; |
| 45 | +import java.nio.file.Files; |
| 46 | +import java.util.concurrent.TimeUnit; |
| 47 | + |
| 48 | +import static org.apache.skywalking.apm.agent.core.conf.Config.AsyncProfiler.DATA_CHUNK_SIZE; |
| 49 | +import static org.apache.skywalking.apm.agent.core.conf.Config.Collector.GRPC_UPSTREAM_TIMEOUT; |
| 50 | + |
| 51 | +@DefaultImplementor |
| 52 | +public class AsyncProfilerDataSender implements BootService, GRPCChannelListener { |
| 53 | + private static final ILog LOGGER = LogManager.getLogger(AsyncProfilerDataSender.class); |
| 54 | + |
| 55 | + private volatile GRPCChannelStatus status = GRPCChannelStatus.DISCONNECT; |
| 56 | + |
| 57 | + private volatile AsyncProfilerTaskGrpc.AsyncProfilerTaskStub asyncProfilerTaskStub; |
| 58 | + |
| 59 | + @Override |
| 60 | + public void prepare() throws Throwable { |
| 61 | + ServiceManager.INSTANCE.findService(GRPCChannelManager.class).addChannelListener(this); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void boot() throws Throwable { |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onComplete() throws Throwable { |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void shutdown() throws Throwable { |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void statusChanged(GRPCChannelStatus status) { |
| 81 | + if (GRPCChannelStatus.CONNECTED.equals(status)) { |
| 82 | + Channel channel = ServiceManager.INSTANCE.findService(GRPCChannelManager.class).getChannel(); |
| 83 | + asyncProfilerTaskStub = AsyncProfilerTaskGrpc.newStub(channel); |
| 84 | + } else { |
| 85 | + asyncProfilerTaskStub = null; |
| 86 | + } |
| 87 | + this.status = status; |
| 88 | + } |
| 89 | + |
| 90 | + public void sendData(AsyncProfilerTask task, File dumpFile) throws IOException, InterruptedException { |
| 91 | + if (status != GRPCChannelStatus.CONNECTED) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + try (FileInputStream fileInputStream = new FileInputStream(dumpFile)) { |
| 96 | + long fileSize = Files.size(dumpFile.toPath()); |
| 97 | + int size = Math.toIntExact(fileSize); |
| 98 | + final GRPCStreamServiceStatus status = new GRPCStreamServiceStatus(false); |
| 99 | + StreamObserver<AsyncProfilerData> dataStreamObserver = asyncProfilerTaskStub.withDeadlineAfter( |
| 100 | + GRPC_UPSTREAM_TIMEOUT, TimeUnit.SECONDS |
| 101 | + ).collect(new ClientResponseObserver<AsyncProfilerData, AsyncProfilerCollectionResponse>() { |
| 102 | + ClientCallStreamObserver<AsyncProfilerData> requestStream; |
| 103 | + |
| 104 | + @Override |
| 105 | + public void beforeStart(ClientCallStreamObserver<AsyncProfilerData> requestStream) { |
| 106 | + this.requestStream = requestStream; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void onNext(AsyncProfilerCollectionResponse value) { |
| 111 | + if (AsyncProfilingStatus.TERMINATED_BY_OVERSIZE.equals(value.getType())) { |
| 112 | + LOGGER.warn("JFR is too large to be received by the oap server"); |
| 113 | + } else { |
| 114 | + byte[] buf = new byte[DATA_CHUNK_SIZE]; |
| 115 | + try { |
| 116 | + int bytesRead; |
| 117 | + while ((bytesRead = fileInputStream.read(buf)) != -1) { |
| 118 | + AsyncProfilerData asyncProfilerData = AsyncProfilerData.newBuilder() |
| 119 | + .setContent(ByteString.copyFrom(buf, 0, bytesRead)) |
| 120 | + .build(); |
| 121 | + requestStream.onNext(asyncProfilerData); |
| 122 | + } |
| 123 | + } catch (IOException e) { |
| 124 | + LOGGER.error("Failed to read JFR file and failed to upload to oap", e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + requestStream.onCompleted(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void onError(Throwable t) { |
| 133 | + status.finished(); |
| 134 | + LOGGER.error(t, "Send async profiler task data to collector fail with a grpc internal exception."); |
| 135 | + ServiceManager.INSTANCE.findService(GRPCChannelManager.class).reportError(t); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void onCompleted() { |
| 140 | + status.finished(); |
| 141 | + } |
| 142 | + }); |
| 143 | + AsyncProfilerMetaData metaData = AsyncProfilerMetaData.newBuilder() |
| 144 | + .setService(Config.Agent.SERVICE_NAME) |
| 145 | + .setServiceInstance(Config.Agent.INSTANCE_NAME) |
| 146 | + .setType(AsyncProfilingStatus.PROFILING_SUCCESS) |
| 147 | + .setContentSize(size) |
| 148 | + .setTaskId(task.getTaskId()) |
| 149 | + .build(); |
| 150 | + AsyncProfilerData asyncProfilerData = AsyncProfilerData.newBuilder().setMetaData(metaData).build(); |
| 151 | + dataStreamObserver.onNext(asyncProfilerData); |
| 152 | + |
| 153 | + status.wait4Finish(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public void sendError(AsyncProfilerTask task, String errorMessage) { |
| 158 | + if (status != GRPCChannelStatus.CONNECTED) { |
| 159 | + return; |
| 160 | + } |
| 161 | + final GRPCStreamServiceStatus status = new GRPCStreamServiceStatus(false); |
| 162 | + StreamObserver<AsyncProfilerData> dataStreamObserver = asyncProfilerTaskStub.withDeadlineAfter( |
| 163 | + GRPC_UPSTREAM_TIMEOUT, TimeUnit.SECONDS |
| 164 | + ).collect(new StreamObserver<AsyncProfilerCollectionResponse>() { |
| 165 | + @Override |
| 166 | + public void onNext(AsyncProfilerCollectionResponse value) { |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void onError(Throwable t) { |
| 171 | + status.finished(); |
| 172 | + LOGGER.error(t, "Send async profiler task execute error fail with a grpc internal exception."); |
| 173 | + ServiceManager.INSTANCE.findService(GRPCChannelManager.class).reportError(t); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public void onCompleted() { |
| 178 | + status.finished(); |
| 179 | + } |
| 180 | + }); |
| 181 | + AsyncProfilerMetaData metaData = AsyncProfilerMetaData.newBuilder() |
| 182 | + .setService(Config.Agent.SERVICE_NAME) |
| 183 | + .setServiceInstance(Config.Agent.INSTANCE_NAME) |
| 184 | + .setTaskId(task.getTaskId()) |
| 185 | + .setType(AsyncProfilingStatus.EXECUTION_TASK_ERROR) |
| 186 | + .setContentSize(-1) |
| 187 | + .build(); |
| 188 | + AsyncProfilerData asyncProfilerData = AsyncProfilerData.newBuilder() |
| 189 | + .setMetaData(metaData) |
| 190 | + .setErrorMessage(errorMessage) |
| 191 | + .build(); |
| 192 | + dataStreamObserver.onNext(asyncProfilerData); |
| 193 | + dataStreamObserver.onCompleted(); |
| 194 | + status.wait4Finish(); |
| 195 | + } |
| 196 | +} |
0 commit comments