-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support async-profiler feature
- Loading branch information
zhengziyi
committed
Sep 30, 2024
1 parent
e78dd7c
commit 2283de3
Showing
79 changed files
with
4,873 additions
and
42 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
...pache/skywalking/oap/server/network/trace/component/command/AsyncProfilerTaskCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.apache.skywalking.oap.server.network.trace.component.command; | ||
|
||
import org.apache.skywalking.apm.network.common.v3.Command; | ||
import org.apache.skywalking.apm.network.common.v3.KeyStringValuePair; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class AsyncProfilerTaskCommand extends BaseCommand implements Serializable, Deserializable<AsyncProfilerTaskCommand> { | ||
public static final Deserializable<AsyncProfilerTaskCommand> DESERIALIZER = new AsyncProfilerTaskCommand("", "", 0, null, "", 0); | ||
public static final String NAME = "AsyncProfileTaskQuery"; | ||
|
||
private final String taskId; | ||
private final int duration; | ||
private final String execArgs; | ||
private final long createTime; | ||
|
||
public AsyncProfilerTaskCommand(String serialNumber, String taskId, int duration, | ||
List<String> events, String execArgs, long createTime) { | ||
super(NAME, serialNumber); | ||
this.taskId = taskId; | ||
this.duration = duration; | ||
this.createTime = createTime; | ||
String comma = ","; | ||
StringBuilder sb = new StringBuilder(); | ||
if (Objects.nonNull(events) && !events.isEmpty()) { | ||
sb.append("event=") | ||
.append(String.join(comma, events)) | ||
.append(comma); | ||
} | ||
if (execArgs != null && !execArgs.isEmpty()) { | ||
sb.append(execArgs); | ||
} | ||
this.execArgs = sb.toString(); | ||
} | ||
|
||
public AsyncProfilerTaskCommand(String serialNumber, String taskId, int duration, | ||
String execArgs, long createTime) { | ||
super(NAME, serialNumber); | ||
this.taskId = taskId; | ||
this.duration = duration; | ||
this.execArgs = execArgs; | ||
this.createTime = createTime; | ||
} | ||
|
||
@Override | ||
public AsyncProfilerTaskCommand deserialize(Command command) { | ||
final List<KeyStringValuePair> argsList = command.getArgsList(); | ||
String taskId = null; | ||
int duration = 0; | ||
String execArgs = null; | ||
long createTime = 0; | ||
String serialNumber = null; | ||
for (final KeyStringValuePair pair : argsList) { | ||
if ("SerialNumber".equals(pair.getKey())) { | ||
serialNumber = pair.getValue(); | ||
} else if ("TaskId".equals(pair.getKey())) { | ||
taskId = pair.getValue(); | ||
} else if ("Duration".equals(pair.getKey())) { | ||
duration = Integer.parseInt(pair.getValue()); | ||
} else if ("ExecArgs".equals(pair.getKey())) { | ||
execArgs = pair.getValue(); | ||
} else if ("CreateTime".equals(pair.getKey())) { | ||
createTime = Long.parseLong(pair.getValue()); | ||
} | ||
} | ||
return new AsyncProfilerTaskCommand(serialNumber, taskId, duration, execArgs, createTime); | ||
} | ||
|
||
@Override | ||
public Command.Builder serialize() { | ||
final Command.Builder builder = commandBuilder(); | ||
builder.addArgs(KeyStringValuePair.newBuilder().setKey("TaskId").setValue(taskId)) | ||
.addArgs(KeyStringValuePair.newBuilder().setKey("Duration").setValue(String.valueOf(duration))) | ||
.addArgs(KeyStringValuePair.newBuilder().setKey("ExecArgs").setValue(execArgs)) | ||
.addArgs(KeyStringValuePair.newBuilder().setKey("CreateTime").setValue(String.valueOf(createTime))); | ||
return builder; | ||
} | ||
|
||
public String getTaskId() { | ||
return taskId; | ||
} | ||
|
||
public int getDuration() { | ||
return duration; | ||
} | ||
|
||
public String getExecArgs() { | ||
return execArgs; | ||
} | ||
|
||
public long getCreateTime() { | ||
return createTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...ore/src/main/java/org/apache/skywalking/oap/server/core/cache/AsyncProfilerTaskCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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.oap.server.core.cache; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import org.apache.skywalking.oap.server.core.CoreModuleConfig; | ||
import org.apache.skywalking.oap.server.core.analysis.TimeBucket; | ||
import org.apache.skywalking.oap.server.core.query.type.AsyncProfilerTask; | ||
import org.apache.skywalking.oap.server.core.storage.StorageModule; | ||
import org.apache.skywalking.oap.server.core.storage.profiling.asyncprofiler.IAsyncProfilerTaskQueryDAO; | ||
import org.apache.skywalking.oap.server.library.module.ModuleManager; | ||
import org.apache.skywalking.oap.server.library.module.Service; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.time.Duration; | ||
import java.util.Objects; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class AsyncProfilerTaskCache implements Service { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(AsyncProfilerTaskCache.class); | ||
|
||
private final Cache<String, AsyncProfilerTask> serviceId2taskCache; | ||
|
||
private final ModuleManager moduleManager; | ||
|
||
private IAsyncProfilerTaskQueryDAO taskQueryDAO; | ||
|
||
public AsyncProfilerTaskCache(ModuleManager moduleManager, CoreModuleConfig moduleConfig) { | ||
this.moduleManager = moduleManager; | ||
long initialSize = moduleConfig.getMaxSizeOfProfileTask() / 10L; | ||
int initialCapacitySize = (int) (initialSize > Integer.MAX_VALUE ? Integer.MAX_VALUE : initialSize); | ||
|
||
serviceId2taskCache = CacheBuilder.newBuilder() | ||
.initialCapacity(initialCapacitySize) | ||
.maximumSize(moduleConfig.getMaxSizeOfProfileTask()) | ||
// remove old profile task data | ||
.expireAfterWrite(Duration.ofMinutes(1)) | ||
.build(); | ||
} | ||
|
||
private IAsyncProfilerTaskQueryDAO getTaskQueryDAO() { | ||
if (Objects.isNull(taskQueryDAO)) { | ||
taskQueryDAO = moduleManager.find(StorageModule.NAME) | ||
.provider() | ||
.getService(IAsyncProfilerTaskQueryDAO.class); | ||
} | ||
return taskQueryDAO; | ||
} | ||
|
||
public AsyncProfilerTask getAsyncProfilerTask(String serviceId) { | ||
return serviceId2taskCache.getIfPresent(serviceId); | ||
} | ||
|
||
public void saveTask(String serviceId, AsyncProfilerTask task) { | ||
if (task == null) { | ||
return ; | ||
} | ||
|
||
serviceId2taskCache.put(serviceId, task); | ||
} | ||
|
||
/** | ||
* use for every db query, -5 start time | ||
*/ | ||
public long getCacheStartTimeBucket() { | ||
return TimeBucket.getMinuteTimeBucket(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5)); | ||
} | ||
|
||
/** | ||
* use for every db query, +5 end time(because use task start time to search) | ||
*/ | ||
public long getCacheEndTimeBucket() { | ||
return TimeBucket.getMinuteTimeBucket(System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.