wu-sheng commented on a change in pull request #4172: Provide profile task downstream to sniffer URL: https://github.com/apache/skywalking/pull/4172#discussion_r364097508
########## File path: apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ProfileTaskExecutionService.java ########## @@ -0,0 +1,207 @@ +/* + * 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 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.logging.api.ILog; +import org.apache.skywalking.apm.agent.core.logging.api.LogManager; +import org.apache.skywalking.apm.network.constants.ProfileConstants; +import org.apache.skywalking.apm.util.StringUtil; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicReference; + +/** + * Profile task executor, use {@link #addProfileTask(ProfileTask)} to add a new profile task. + * + * @author MrPro + */ +@DefaultImplementor +public class ProfileTaskExecutionService implements BootService { + + private static final ILog logger = LogManager.getLogger(ProfileTaskExecutionService.class); + + // add a schedule while waiting for the task to start or finish + private final static ScheduledExecutorService PROFILE_TASK_SCHEDULE = Executors.newSingleThreadScheduledExecutor(new DefaultNamedThreadFactory("PROFILE-TASK-SCHEDULE")); + + // last command create time, use to next query task list + private volatile long lastCommandCreateTime = -1; + + // current processing profile task context + private final AtomicReference<ProfileTaskExecutionContext> taskExecutionContext = new AtomicReference<>(); + + // profile task list, include running and waiting running tasks + private final List<ProfileTask> profileTaskList = Collections.synchronizedList(new LinkedList<>()); + + /** + * get profile task from OAP + * @param task + */ + public void addProfileTask(ProfileTask task) { + // update last command create time + if (task.getCreateTime() > lastCommandCreateTime) { + lastCommandCreateTime = task.getCreateTime(); + } + + // check profile task limit + final String dataError = checkProfileTaskSuccess(task); + if (dataError != null) { + logger.warn("check command error, cannot process this profile task. reason: {}", dataError); + return; + } + + // add task to list + profileTaskList.add(task); + + // check task start now or make a schedule + long timeFromStartMills = task.getStartTime() - System.currentTimeMillis(); + if (timeFromStartMills < 0) { + // task already can start + processProfileTask(task); + } else { + // need to be a schedule to start task + PROFILE_TASK_SCHEDULE.schedule(new Runnable() { + @Override + public void run() { + processProfileTask(task); + } + }, timeFromStartMills, TimeUnit.MILLISECONDS); + } + } + + /** + * real process a new profile task Review comment: This comment is not right. Should re-word to > Active the selected profile task to execution task, and start a removal task for it. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
