arugal commented on a change in pull request #4220: sniffer processing profile 
task and report status and snapshot
URL: https://github.com/apache/skywalking/pull/4220#discussion_r368208724
 
 

 ##########
 File path: 
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ProfileTaskChannelService.java
 ##########
 @@ -0,0 +1,243 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.apm.agent.core.profile;
+
+import io.grpc.Channel;
+import io.grpc.Status;
+import io.grpc.StatusRuntimeException;
+import io.grpc.stub.StreamObserver;
+import org.apache.skywalking.apm.agent.core.boot.BootService;
+import org.apache.skywalking.apm.agent.core.boot.DefaultImplementor;
+import org.apache.skywalking.apm.agent.core.boot.DefaultNamedThreadFactory;
+import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
+import org.apache.skywalking.apm.agent.core.commands.CommandService;
+import org.apache.skywalking.apm.agent.core.conf.Config;
+import org.apache.skywalking.apm.agent.core.conf.RemoteDownstreamConfig;
+import org.apache.skywalking.apm.agent.core.dictionary.DictionaryUtil;
+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.remote.*;
+import org.apache.skywalking.apm.network.common.Commands;
+import 
org.apache.skywalking.apm.network.language.profile.ProfileTaskCommandQuery;
+import 
org.apache.skywalking.apm.network.language.profile.ProfileTaskFinishReport;
+import org.apache.skywalking.apm.network.language.profile.ProfileTaskGrpc;
+import org.apache.skywalking.apm.network.language.profile.ThreadSnapshot;
+import org.apache.skywalking.apm.util.RunnableWithExceptionProtection;
+
+import java.util.ArrayList;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
+import static 
org.apache.skywalking.apm.agent.core.conf.Config.Collector.GRPC_UPSTREAM_TIMEOUT;
+
+/**
+ * Sniffer and backend, about the communication service of profile task 
protocol.
+ * 1. Sniffer will check has new profile task list every {@link 
Config.Collector#GET_PROFILE_TASK_INTERVAL} second.
+ * 2. When there is a new profile task snapshot, the data is transferred to 
the back end. use {@link LinkedBlockingQueue}
+ * 3. When profiling task finish, it will send task finish status to backend
+ *
+ * @author MrPro
+ */
+@DefaultImplementor
+public class ProfileTaskChannelService implements BootService, Runnable, 
GRPCChannelListener {
+    private static final ILog logger = 
LogManager.getLogger(ProfileTaskChannelService.class);
+
+    // channel status
+    private volatile GRPCChannelStatus status = GRPCChannelStatus.DISCONNECT;
+
+    // gRPC stub
+    private volatile ProfileTaskGrpc.ProfileTaskBlockingStub 
profileTaskBlockingStub;
+    private volatile ProfileTaskGrpc.ProfileTaskStub profileTaskStub;
+
+    // segment snapshot sender
+    private final LinkedBlockingQueue<TracingThreadSnapshot> snapshotQueue = 
new LinkedBlockingQueue<>(Config.Profile.SNAPSHOT_TRANSPORT_BUFFER_SIZE);
+    private volatile ScheduledFuture<?> sendSnapshotFuture;
+
+    // query task list schedule
+    private volatile ScheduledFuture<?> getTaskListFuture;
+
+    @Override
+    public void run() {
+        if (RemoteDownstreamConfig.Agent.SERVICE_ID != 
DictionaryUtil.nullValue()
+                && RemoteDownstreamConfig.Agent.SERVICE_INSTANCE_ID != 
DictionaryUtil.nullValue()
+        ) {
+            if (status == GRPCChannelStatus.CONNECTED) {
+                try {
+                    ProfileTaskCommandQuery.Builder builder = 
ProfileTaskCommandQuery.newBuilder();
+
+                    // sniffer info
+                    
builder.setServiceId(RemoteDownstreamConfig.Agent.SERVICE_ID).setInstanceId(RemoteDownstreamConfig.Agent.SERVICE_INSTANCE_ID);
+
+                    // last command create time
+                    
builder.setLastCommandTime(ServiceManager.INSTANCE.findService(ProfileTaskExecutionService.class).getLastCommandCreateTime());
+
+                    Commands commands = 
profileTaskBlockingStub.withDeadlineAfter(GRPC_UPSTREAM_TIMEOUT, 
TimeUnit.SECONDS).getProfileTaskCommands(builder.build());
+                    
ServiceManager.INSTANCE.findService(CommandService.class).receiveCommand(commands);
+                } catch (Throwable t) {
+                    if (!(t instanceof StatusRuntimeException)) {
+                        logger.error(t, "Query profile task from backend 
fail.");
+                        return;
+                    }
+                    final StatusRuntimeException statusRuntimeException = 
(StatusRuntimeException) t;
+                    if (statusRuntimeException.getStatus().getCode() == 
Status.Code.UNIMPLEMENTED) {
+                        logger.warn("Backend doesn't support profiling, 
profiling will be disabled");
+                        if (getTaskListFuture != null) {
+                            getTaskListFuture.cancel(true);
+                        }
+
+                        // stop snapshot sender
+                        if (sendSnapshotFuture != null) {
+                            sendSnapshotFuture.cancel(true);
+                        }
+                    }
+                }
+            }
+        }
+
+    }
+
+    @Override
+    public void prepare() throws Throwable {
+        
ServiceManager.INSTANCE.findService(GRPCChannelManager.class).addChannelListener(this);
+    }
+
+    @Override
+    public void boot() throws Throwable {
+        if (Config.Profile.ACTIVE) {
+            // query task list
+            getTaskListFuture = Executors.newSingleThreadScheduledExecutor(new 
DefaultNamedThreadFactory("ProfileGetTaskService"))
+                    .scheduleWithFixedDelay(new 
RunnableWithExceptionProtection(this, new 
RunnableWithExceptionProtection.CallbackWhenException() {
+                        @Override
+                        public void handle(Throwable t) {
+                            logger.error("Query profile task list failure.", 
t);
+                        }
+                    }), 0, Config.Collector.GET_PROFILE_TASK_INTERVAL, 
TimeUnit.SECONDS);
+
+            sendSnapshotFuture = 
Executors.newSingleThreadScheduledExecutor(new 
DefaultNamedThreadFactory("ProfileSendSnapshotService"))
+                    .scheduleWithFixedDelay(new 
RunnableWithExceptionProtection(new SnapshotSender(), new 
RunnableWithExceptionProtection.CallbackWhenException() {
+                        @Override public void handle(Throwable t) {
+                            logger.error("Profile segment snapshot upload 
failure.", t);
+                        }
+                    }), 0, 500, TimeUnit.MILLISECONDS);
+        }
+    }
+
+    @Override
+    public void onComplete() throws Throwable {
+    }
+
+    @Override
+    public void shutdown() throws Throwable {
+        if (getTaskListFuture != null) {
+            getTaskListFuture.cancel(true);
+        }
+
+        if (sendSnapshotFuture != null) {
+            sendSnapshotFuture.cancel(true);
+        }
+    }
+
+    @Override
+    public void statusChanged(GRPCChannelStatus status) {
+        if (GRPCChannelStatus.CONNECTED.equals(status)) {
+            Channel channel = 
ServiceManager.INSTANCE.findService(GRPCChannelManager.class).getChannel();
+            profileTaskBlockingStub = ProfileTaskGrpc.newBlockingStub(channel);
+            profileTaskStub = ProfileTaskGrpc.newStub(channel);
+        } else {
+            profileTaskBlockingStub = null;
+            profileTaskStub = null;
+        }
+        this.status = status;
+    }
+
+    /**
+     * add a new profiling snapshot, send to {@link #snapshotQueue}
+     */
+    public void addProfilingSnapshot(TracingThreadSnapshot snapshot) {
+        snapshotQueue.add(snapshot);
 
 Review comment:
   sorry, I was wrong.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to