Skip to content

Commit

Permalink
Merge pull request #205 from shiyindaxiaojie/feature
Browse files Browse the repository at this point in the history
Feature
  • Loading branch information
shiyindaxiaojie authored Aug 8, 2024
2 parents de6f50d + ae0676a commit ef58c03
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 138 deletions.
16 changes: 13 additions & 3 deletions eden-components/eden-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@
<mysql-connector-j.version>8.0.32</mysql-connector-j.version>
<mariadb-java-client.version>3.1.1</mariadb-java-client.version>
<sqlite.version>3.42.0.0</sqlite.version>
<mybatis.version>3.5.6</mybatis.version>
<mybatis-spring.version>2.0.3</mybatis-spring.version>
<mybatis-spring-boot.version>2.1.1</mybatis-spring-boot.version>
<mybatis.version>3.5.16</mybatis.version>
<mybatis-spring.version>2.1.2</mybatis-spring.version>
<mybatis-spring-boot.version>2.1.4</mybatis-spring-boot.version>
<mybatis-plus.version>3.5.7</mybatis-plus.version>
<pagehelper.version>5.3.1</pagehelper.version>
<tk-mybatis.version>1.2.4</tk-mybatis.version>
Expand Down Expand Up @@ -888,6 +888,16 @@
<artifactId>arthas-spring-boot-starter</artifactId>
<version>${arthas.version}</version>
</dependency>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-spy</artifactId>
<version>${arthas.version}</version>
</dependency>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-agent-attach</artifactId>
<version>${arthas.version}</version>
</dependency>

<!-- Web -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private SpringLiquibase buildLiquibase(DataSource dataSource) {
}

private void updateCacheFromDb() {
log.debug("Update cache from db");
log.debug("Leaf update cache from db");
try {
List<String> dbTags = leafAllocDAO.getAllTags();
if (dbTags == null || dbTags.isEmpty()) {
Expand All @@ -155,24 +155,24 @@ private void updateCacheFromDb() {
segment.setMax(0);
segment.setStep(0);
cache.put(tag, buffer);
log.debug("Add tag {} from db to IdCache", tag);
log.debug("Leaf add tag {} from db to IdCache", tag);
}
for (String tag : dbTags) {
removeTagsSet.remove(tag);
}
for (String tag : removeTagsSet) {
cache.remove(tag);
log.debug("Remove tag {} from IdCache", tag);
log.debug("Leaf remove tag {} from IdCache", tag);
}
} catch (Exception e) {
log.warn("Update cache from db exception", e);
log.warn("Leaf update cache from db exception", e);
}
}

private void updateCacheFromDbAtEveryMinute() {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = new Thread(r);
t.setName("check-idCache-thread");
t.setName("leaf-check-id-cache-thread");
t.setDaemon(true);
return t;
});
Expand Down Expand Up @@ -229,9 +229,9 @@ private long getIdFromSegmentBuffer(final SegmentBuffer buffer) {
try {
updateSegmentFromDb(buffer.getKey(), next);
updateOk = true;
log.info("Update segment {} from db {}", buffer.getKey(), next);
log.info("Leaf update segment {} from db {}", buffer.getKey(), next);
} catch (Exception e) {
log.error("Update segment {} from db {} error", buffer.getKey(), e);
log.error("Leaf update segment {} from db {} error", buffer.getKey(), e);
} finally {
if (updateOk) {
buffer.wLock().lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,13 @@
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-spy</artifactId>
</dependency>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-agent-attach</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package org.ylzl.eden.arthas.spring.boot.attach;

import com.taobao.arthas.agent.attach.ArthasAgent;
import com.taobao.arthas.agent.attach.AttachArthasClassloader;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.agent.ByteBuddyAgent;
import org.zeroturnaround.zip.ZipUtil;

import java.arthas.SpyAPI;
import java.io.File;
import java.lang.instrument.Instrumentation;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
* 自定义 ArthasAgent
*
* @author <a href="mailto:[email protected]">gyl</a>
* @since 2.4.13
*/
@Slf4j
public class CustomArthasAgent extends ArthasAgent {

private static final int TEMP_DIR_ATTEMPTS = 10000;

private static final String ARTHAS_CORE_JAR = "arthas-core.jar";
private static final String ARTHAS_BOOTSTRAP = "com.taobao.arthas.core.server.ArthasBootstrap";
private static final String GET_INSTANCE = "getInstance";
private static final String IS_BIND = "isBind";

private static final Pattern ARTHAS_DIR_PATTERN = Pattern.compile("^arthas-(\\d{13})-.*$");
private static final String ARTHAS_BIN_ZIP = "arthas-bin.zip";
private static final String DESTROY = "destroy";

private String errorMessage;

private Map<String, String> configMap = new HashMap<String, String>();
private String arthasHome;
private boolean slientInit;
private Instrumentation instrumentation;

private Class<?> bootstrapClass;
private Object bootstrap;

public CustomArthasAgent() {
this(null, null, false, null);
}

public CustomArthasAgent(Map<String, String> configMap) {
this(configMap, null, false, null);
}

public CustomArthasAgent(String arthasHome) {
this(null, arthasHome, false, null);
}

public CustomArthasAgent(Map<String, String> configMap, String arthasHome, boolean slientInit,
Instrumentation instrumentation) {
if (configMap != null) {
this.configMap = configMap;
}

this.arthasHome = arthasHome;
this.slientInit = slientInit;
this.instrumentation = instrumentation;
}

public String getErrorMessage() {
return errorMessage;
}

@Override
public void init() throws IllegalStateException {
// 尝试判断arthas是否已在运行,如果是的话,直接就退出
try {
Class.forName("java.arthas.SpyAPI"); // 加载不到会抛异常
if (SpyAPI.isInited()) {
return;
}
} catch (Throwable e) {
// ignore
}

try {
if (instrumentation == null) {
instrumentation = ByteBuddyAgent.install();
}

// 检查 arthasHome
if (arthasHome == null || arthasHome.trim().isEmpty()) {
// 解压出 arthasHome
URL coreJarUrl = this.getClass().getClassLoader().getResource("arthas-bin.zip");
if (coreJarUrl != null) {
File tempArthasDir = createTempDir();
ZipUtil.unpack(coreJarUrl.openStream(), tempArthasDir);
arthasHome = tempArthasDir.getAbsolutePath();
} else {
throw new IllegalArgumentException("can not getResources arthas-bin.zip from classloader: "
+ this.getClass().getClassLoader());
}
}

// find arthas-core.jar
File arthasCoreJarFile = new File(arthasHome, ARTHAS_CORE_JAR);
if (!arthasCoreJarFile.exists()) {
throw new IllegalStateException("can not find arthas-core.jar under arthasHome: " + arthasHome);
}
AttachArthasClassloader arthasClassLoader = new AttachArthasClassloader(
new URL[] { arthasCoreJarFile.toURI().toURL() });

bootstrapClass = arthasClassLoader.loadClass(ARTHAS_BOOTSTRAP);
bootstrap = bootstrapClass.getMethod(GET_INSTANCE, Instrumentation.class, Map.class).invoke(null,
instrumentation, configMap);
boolean isBind = (Boolean) bootstrapClass.getMethod(IS_BIND).invoke(bootstrap);
if (!isBind) {
String errorMsg = "Arthas server port binding failed! Please check $HOME/logs/arthas/arthas.log for more details.";
throw new RuntimeException(errorMsg);
}
} catch (Throwable e) {
errorMessage = e.getMessage();
if (!slientInit) {
throw new IllegalStateException(e);
}
}
}

public void destroy() {
if (bootstrapClass == null) {
log.warn("Arthas is not already");
return;
}

try {
bootstrapClass.getMethod(DESTROY).invoke(bootstrap);
} catch (Throwable e) {
log.error(e.getMessage(), e);
}
}

private static File createTempDir() {
File baseDir = new File(System.getProperty("java.io.tmpdir"));
String baseName = "arthas-" + System.currentTimeMillis() + "-";

for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) {
File tempDir = new File(baseDir, baseName + counter);
if (tempDir.mkdir()) {
return tempDir;
}
}
throw new IllegalStateException("Failed to create directory within " + TEMP_DIR_ATTEMPTS + " attempts (tried "
+ baseName + "0 to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + ')');
}
}
Loading

0 comments on commit ef58c03

Please sign in to comment.