Skip to content

Commit

Permalink
Merge branch 'refs/heads/feature'
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyuanlu committed Aug 6, 2024
2 parents f92c6e5 + 0b23cf7 commit fb44278
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ public static boolean checkAndMkdirs(@NonNull File file) {
return true;
}

public static void delete(File file) throws IOException {
java.nio.file.Files.delete(Paths.get(file.getAbsolutePath()));
}

public static void deleteIfExists(File file) throws IOException {
java.nio.file.Files.deleteIfExists(Paths.get(file.getAbsolutePath()));
}
Expand Down
14 changes: 7 additions & 7 deletions eden-components/eden-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@
<cglib.version>3.3.0</cglib.version>
<javassist.version>3.26.0-GA</javassist.version>
<commons-lang3.version>3.9</commons-lang3.version>
<commons-collections4.version>4.4</commons-collections4.version>
<commons-text.version>1.10.0</commons-text.version>
<commons-collections4.version>4.5.0-M2</commons-collections4.version>
<commons-text.version>1.12.0</commons-text.version>
<commons-beanutils.version>1.9.4</commons-beanutils.version>
<commons-codec.version>1.10</commons-codec.version>
<commons-codec.version>1.17.0</commons-codec.version>
<commons-collections.version>3.2.2</commons-collections.version>
<commons-fileupload.version>1.5</commons-fileupload.version>
<commons-io.version>2.7</commons-io.version>
<commons-logging.version>1.2</commons-logging.version>
<commons-io.version>2.16.1</commons-io.version>
<commons-logging.version>1.3.3</commons-logging.version>
<commons-math3.version>3.6.1</commons-math3.version>
<commons-net.version>3.9.0</commons-net.version>
<commons-cli.version>1.4</commons-cli.version>
<commons-net.version>3.11.1</commons-net.version>
<commons-cli.version>1.8.0</commons-cli.version>
<jackson-bom.version>2.14.1</jackson-bom.version>
<fastjson.version>2.0.22</fastjson.version>
<fastjson2.version>2.0.22</fastjson2.version>
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.info("Update cache from db");
log.debug("Update cache from db");
try {
List<String> dbTags = leafAllocDAO.getAllTags();
if (dbTags == null || dbTags.isEmpty()) {
Expand All @@ -155,7 +155,7 @@ private void updateCacheFromDb() {
segment.setMax(0);
segment.setStep(0);
cache.put(tag, buffer);
log.debug("Add tag {} from db to IdCache, SegmentBuffer {}", tag, buffer);
log.debug("Add tag {} from db to IdCache", tag);
}
for (String tag : dbTags) {
removeTagsSet.remove(tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import com.alibaba.arthas.spring.ArthasProperties;
import com.alibaba.arthas.spring.StringUtils;
import com.taobao.arthas.agent.attach.ArthasAgent;
import com.taobao.arthas.agent.attach.AttachArthasClassloader;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.agent.ByteBuddyAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition;
Expand All @@ -37,8 +39,16 @@
import org.ylzl.eden.arthas.spring.boot.env.SpringArthasProperties;
import org.ylzl.eden.spring.framework.bootstrap.constant.SpringProperties;

import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.lang.instrument.Instrumentation;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Arthas 自动装配
Expand Down Expand Up @@ -66,6 +76,20 @@ public class ArthasAutoConfiguration {

private static final String ARTHAS_AGENT_START_SUCCESS = "Arthas agent start success";

private static final int TEMP_DIR_ATTEMPTS = 10000;

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 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 DESTROY = "destroy";

private final ApplicationContext applicationContext;

private final Environment environment;
Expand All @@ -83,10 +107,10 @@ public ArthasEnvironmentChangeListener arthasEnvironmentChangeListener(
@Bean
public ArthasAgent arthasAgent(@Autowired @Qualifier(ARTHAS_CONFIG_MAP) Map<String, String> arthasConfigMap) {
arthasConfigMap = StringUtils.removeDashKey(arthasConfigMap);
return initArthasAgent(arthasConfigMap, environment, arthasProperties);
return init(arthasConfigMap, environment, arthasProperties);
}

public static ArthasAgent initArthasAgent(Map<String, String> arthasConfigMap, Environment environment,
public static ArthasAgent init(Map<String, String> arthasConfigMap, Environment environment,
ArthasProperties arthasProperties) {
ArthasProperties.updateArthasConfigMapDefaultValue(arthasConfigMap);

Expand All @@ -107,4 +131,80 @@ public static ArthasAgent initArthasAgent(Map<String, String> arthasConfigMap, E
log.info(ARTHAS_AGENT_START_SUCCESS);
return arthasAgent;
}

public static void destroy(Map<String, String> arthasConfigMap,
ArthasProperties arthasProperties) {
String arthasHome = arthasProperties.getHome();
try {
Instrumentation instrumentation = ByteBuddyAgent.install();

if (arthasHome == null || arthasHome.trim().isEmpty()) {
ClassLoader classLoader = ArthasAgent.class.getClassLoader();
URL coreJarUrl = classLoader.getResource(ARTHAS_BIN_ZIP);
if (coreJarUrl != null) {
File tempArthasDir = findTempDir();
arthasHome = tempArthasDir.getAbsolutePath();
} else {
throw new IllegalArgumentException("can not getResources arthas-bin.zip from classloader: "
+ classLoader);
}
}
File arthasCoreJarFile = new File(arthasHome, ARTHAS_CORE_JAR);
if (!arthasCoreJarFile.exists()) {
throw new IllegalArgumentException("can not find arthas-core.jar under arthasHome: {}" + arthasHome);
}
AttachArthasClassloader arthasClassLoader = new AttachArthasClassloader(
new URL[] { arthasCoreJarFile.toURI().toURL() });
Class<?> bootstrapClass = arthasClassLoader.loadClass(ARTHAS_BOOTSTRAP);

Object bootstrap = bootstrapClass.getMethod(GET_INSTANCE, Instrumentation.class, Map.class)
.invoke(null, instrumentation, arthasConfigMap);
bootstrapClass.getMethod(DESTROY).invoke(bootstrap);
} catch (Throwable e) {
log.error(e.getMessage(), e);
}
}

private static List<File> listArthasDirs(File baseDir) {
return Stream.of(baseDir.listFiles((dir, name) -> ARTHAS_DIR_PATTERN.matcher(name).matches()))
.collect(Collectors.toList());
}

private static Date extractTimestamp(File dir) {
Matcher matcher = ARTHAS_DIR_PATTERN.matcher(dir.getName());
if (matcher.matches()) {
String timestampStr = matcher.group(1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
try {
return sdf.parse(timestampStr);
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid timestamp format in directory name: " + dir.getName(), e);
}
}
return null;
}

private static File findLatestArthasDir(File baseDir) {
List<File> dirs = listArthasDirs(baseDir);
if (!dirs.isEmpty()) {
return dirs.stream()
.max(Comparator.comparing(ArthasAutoConfiguration::extractTimestamp))
.orElse(null);
}
return null;
}

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

// 尝试查找时间戳最大的以 "arthas-" 开头的目录
File latestDir = findLatestArthasDir(baseDir);
if (latestDir != null) {
return latestDir;
}

throw new IllegalStateException("Failed to find directory within " + TEMP_DIR_ATTEMPTS
+ " attempts (tried " + baseName + "0 to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + ')');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public class ArthasEnvironmentChangeListener implements ApplicationListener<Envi

public static final String ARTHAS_AGENT = "arthasAgent";

public static final String REGISTER_ARTHAS = "Register arthas";
public static final String REGISTER_ARTHAS = "Register arthas to {}";

public static final String ARTHAS_ALREADY_REGISTERED = "Arthas already registered";

public static final String DESTROY_ARTHAS = "Destroy arthas";
public static final String DESTROY_ARTHAS = "Destroy arthas from {}";

public static final String ARTHAS_ALREADY_DESTROY = "Arthas already destroy";

Expand Down Expand Up @@ -82,13 +82,13 @@ private void registerBean() {
log.info(ARTHAS_ALREADY_REGISTERED);
return;
}
log.info(REGISTER_ARTHAS);
log.info(REGISTER_ARTHAS, arthasProperties.getTunnelServer());
DefaultListableBeanFactory defaultListableBeanFactory = getDefaultListableBeanFactory();
if (defaultListableBeanFactory.containsBean(ARTHAS_AGENT)) {
((ArthasAgent) defaultListableBeanFactory.getBean(ARTHAS_AGENT)).init();
} else {
defaultListableBeanFactory.registerSingleton(ARTHAS_AGENT,
ArthasAutoConfiguration.initArthasAgent(arthasConfigMap, environment, arthasProperties));
ArthasAutoConfiguration.init(arthasConfigMap, environment, arthasProperties));
}
}

Expand All @@ -97,11 +97,12 @@ private void destroyBean() {
log.info(ARTHAS_ALREADY_DESTROY);
return;
}
log.info(DESTROY_ARTHAS);
log.info(DESTROY_ARTHAS, arthasProperties.getTunnelServer());
DefaultListableBeanFactory defaultListableBeanFactory = getDefaultListableBeanFactory();
if (defaultListableBeanFactory.containsBean(ARTHAS_AGENT)) {
defaultListableBeanFactory.destroySingleton(ARTHAS_AGENT);
}
ArthasAutoConfiguration.destroy(arthasConfigMap, arthasProperties);
}

@NotNull
Expand Down

0 comments on commit fb44278

Please sign in to comment.