Alibaba Cloud ARMS application monitoring supports Java 21

Author: Mu Si & Shan Xie

Preface

On September 19 this year, Java 21, the latest LTS (Long Term Support) Java version, was officially released, bringing many heavyweight updates. For details, please refer to The Arrival of Java 21 [ 1 ] . Although Java 11 and Java 17 have not yet been widely popularized in China, and Java 8 still occupies a mainstream position, timely updating of the JDK version can bring many important values ​​​​to developers, including improvements in application performance and stability. and new features that can help boost productivity. As the most influential observable and APM service provider in Asia, the Alibaba Cloud ARMS team responded immediately to the GA release of Java 21 and took the lead in adapting Java 21 to help users better observe Java 21 applications!

Java 21 major new features

Java 21 brings 15 new features, including virtual threads, generational ZGC and other major features, as well as other optimizations. Let us take a sneak peek and experience these new features:

1. Virtual thread

Virtual Threads are definitely the most important new feature in Java 21. In previous Java versions, each java.lang.Thread object only corresponded to a thread in the operating system kernel, and threads in the operating system were A relatively expensive system resource: Thread creation, switching, destruction and other operations require entering the kernel state. In a high-concurrency scenario, if a large number of threads are created to process requests, multiple threads will be frequently suspended and switched, which consumes system resources.

A virtual thread is a lightweight user-mode thread. Unlike traditional threads, which are scheduled and run by the OS, virtual threads are scheduled and run by the underlying JDK, and their creation, scheduling, destruction and other operations are all completed by user-space library functions. , that is to say, the corresponding relationship between virtual threads and threads in the kernel is M:N, as shown in Figure 1:

Figure 1: Relationship diagram between threads and virtual threads

Therefore, in high-concurrency scenarios, the cost of creating a large number of virtual threads to process requests will be much lower than creating a large number of threads. In Java programs, the comparison between threads and virtual threads is shown in Table 1:

Table 1: Comparison table of threads and virtual threads

In Java 21, there are several ways to create and run virtual threads:

// 方式一:
Thread vt = Thread.startVirtualThread(() -> {});
// 方式二:
Thread.ofVirtual().unstarted(() -> {});
vt.start();
// 方式三:
ThreadFactory tf = Thread.ofVirtual().factory();
Thread vt = tf.newThread(() -> {});
vt.start();
// 方式四:
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
ThreadFactory tf = Thread.ofVirtual().factory();
Thread vt = tf.newThread(() -> {});
executor.submit(vt);

2. Generational ZGC

In Java 21, generational support for ZGC (Generational ZGC) is added to improve the performance of garbage collection. Before this, ZGC did not have the concept of generation. It would collect all objects every time it was run, without considering the age of the objects. However, according to the generational collection theory, most objects are ephemeral, and objects that have survived multiple garbage collection processes will be more difficult to die. Cleaning up young generation objects requires fewer resources and can clear up more memory; conversely, cleaning up old generation objects requires more resources and can clear out less memory. This means that ZGC can further improve garbage collection efficiency based on the generational collection theory. So in Java 21, the generational collection mechanism was introduced to collect young generation objects more frequently, which is very helpful for improving the performance of Java applications using ZGC.

To use ZGC on Java 21, you first need to add the -XX:+UseZGC option to the Java startup command to enable ZGC. By default, non-generational ZGC is enabled after adding this option. If you need to use generational ZGC, you need to add an additional -XX:+ZGenerational option.

# 使用分代式ZGC
$ java -XX:+UseZGC -XX:+ZGenerational ...

3. Other features

In addition to virtual threads and generational ZGC, Java 21 also introduces other interesting features, such as:

  • It is allowed to use the _ character to declare unnamed variables, similar to _ in the Go language.
  • The main method of anonymous classes and anonymous instances is allowed. The main function can be executed as shown below.
void main() {
    System.out.println("Hello, World!");
}

For specific other features, please refer to: openjdk.org/projects/jdk/21/ .

Monitor Java 21 applications using ARMS

In the latest 3.1.0 version of ARMS probe, we have supported Java 21. Developers can refer to this article to start the observable journey of Java 21 applications.

Writing Java 21 applications

First, you need to download and install JDK 21. You can download and install it from the official websites of manufacturers such as Oracle, or through third-party tools such as sdkman.

After installing JDK 21, you can refer to the following code to write a simple SpringBoot 3.x application, which uses the Record Patterns feature of Java 21 to help users deconstruct record objects in Java with concise syntax:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.6</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <groupId>xxx</groupId>
  <artifactId>xxx</artifactId>
  <version>xxx</version>
  <name>xxx</name>

  <properties>
    <java.version>21</java.version>
  </properties>


  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    record Point(int x, int y) {

    }

    @RestController
    static class DemoController {
        @RequestMapping("/demo")
        String demo(@RequestParam String type) {
            Object object = null;
            if ("record".equals(type)) {
                object = new Point(1, 2);
            } else {
                object = "object";
            }
            return solve(object);
        }

        private String solve(Object object) {
            if (object instanceof Point(int x, int y)) {
                return "Point: " + x + ":" + y;
            }
            return "Invalid Point";
        }
    }
}

Access ARMS

For Java applications, ARMS provides a variety of convenient access methods. You can refer to Application Monitoring Access Overview [ 2] for access. For applications running in Alibaba Cloud Container Service ACK [ 3] , you can have the simplest access method: implement automatic injection and configuration of probes based on Pilot mode, without modifying the image, and only need to add 2 Pods to the application Yaml Label can be connected to ARMS.

First install the ack-onepilot component for the ACK cluster, as shown in Figure 2:

Figure 2: Installing ack-onepilot

After the installation is complete, create a Java application in the ACK cluster and add the two Pod labels in Figure 3 to the spec.template.metadata field of the pod. Among them, armsPilotCreateAppName represents the application name connected to ARMS, which can be consistent with the Deployment name. ArmsPilotAutoEnable can be set to on. You can also directly edit the Deployment's Yaml file to add Pod labels.

Figure 3: Add tags

After the application is successfully deployed, the ARMS console link will be displayed on the ACK console, as shown in Figure 4. Click to jump to the ARMS console.

Figure 4: ARMS console operation options

After Java starts the application, the application will also print the log shown in Figure 5, indicating that the application has mounted the ARMS probe.

Figure 5: JDK21 application startup log

Note: ARMS' support for Java 21 relies on the 3.1.0 version probe. As of the date of publication of this article, the 3.1.0 version probe has not been officially released. You can scan the QR code on DingTalk to join the ARMS support Java 21 experience group to obtain 3.1 .0 version probe. After the 3.1.0 version is officially released, you can directly download the latest version of the probe from the ARMS official website, or automatically obtain the 3.1.0 version probe through the Pilot mode.

  • ARMS supports Java 21. Experience DingTalk group number: 54565000535*

View monitoring data

After the application starts successfully, request the /demo interface and you will get the response shown in Figure 7:

Figure 7: /demo interface response

After requesting the /demo interface, you can go to the application list page of the ARMS console and click to enter the application monitoring page, as shown in Figure 8, or directly jump to the ARMS console through the transfer on the ACK console.

Figure 8: ARMS console application list

It can be seen that ARMS has successfully identified Java 21 applications and collected relevant observable data, as shown in Figure 9-11. These observation data can help users quickly gain insight into system operating conditions, accelerate online problem troubleshooting efficiency, and improve business operations. stability. For more important functions of ARMS application monitoring, such as intelligent insights, call chain analysis, and CPU & memory diagnosis, please refer to the ARMS application monitoring help document [ 4] .

Figure 9: ARMS application meta information

Figure 10: ARMS application interface call information

Figure 11: ARMS should overview information

List of new features of ARMS 3.X version probes

  • It greatly improves the coverage of mainstream open source frameworks, supports complete time-consuming statistics of asynchronous frameworks such as Reactor Netty and Vert. Provides more accurate and richer indicators and span data. For details, please refer to Java components and frameworks supported by ARMS [ 5] .
  • Performance optimization makes application access more lightweight and less intrusive; in the 4C8G container scenario, the additional CPU overhead caused by mounting probes is reduced by 50% compared to previous versions, and the CPU overhead optimization range for scenarios using the asynchronous framework reaches 65% %, the performance is better; the startup time is greatly optimized, the probe mounting startup time is reduced to within 5 seconds, and the init-container startup time is reduced to 6 seconds through container access, and the overall probe startup time is reduced Reduced by 10s+;
  • Support code hotspot function. Since the general Tracing system can only bury core methods in mainstream open source software frameworks, when the time-consuming position appears in the user business logic that is missing from the Tracing burying point, a long period of time will appear in the final call chain. It cannot correspond to the specific code execution method, which leads to the problem of being unable to accurately judge the time consumption of business logic.

Figure 12: Tracing monitoring blind zone example diagram

Based on the industry's well-known open source continuous profiling tool Async Profiler [ 6] , the ARMS code hotspot function provides On & Off-CPU flame graphs at the call chain level by correlating the TraceId & SpanId information in the call chain, which can effectively monitor Tracing. Details of blind spots are restored to help users diagnose various common slow call chain problems.

Figure 13: ARMS support code hotspot function renderings

For more function introduction and usage details, please refer to Slow Call Chain Diagnosis Tool-ARMS Code Hotspot .

ARMS code hotspot experience exchange DingTalk group number: 22560019672

For more detailed introduction to the ARMS product family, please refer to the ARMS official help document [ 7] .

References:

[1] https://openjdk.org/projects/jdk/21/

[2] https://openjdk.org/jeps/439

[3] https://openjdk.org/jeps/440

[4] https://blogs.oracle.com/java/post/the-arrival-of-java-21

Related Links:

[1] The Arrival of Java 21

https://blogs.oracle.com/java/post/the-arrival-of-java-21

[2] Overview of application monitoring access

https://help.aliyun.com/zh/arms/application-monitoring/getting-started/overview

[3] Container service ACK

https://www.aliyun.com/product/kubernetes**

[4] ARMS application monitoring help document

https://help.aliyun.com/zh/arms/application-monitoring/product-overview/functional-characteristics

[5] Java components and frameworks supported by ARMS

https://help.aliyun.com/zh/arms/application-monitoring/developer-reference/java-components-and-frameworks-supported-by-arms

[6] Async Profiler

https://github.com/async-profiler/async-profiler

[7] ARMS official help document

https://help.aliyun.com/zh/arms/

The author of a well-known open source project lost his job due to mania - "Seeking money online" No Star, No Fix 2023 The world's top ten engineering achievements are released: ChatGPT, Hongmeng Operating System, China Space Station and other selected ByteDance were "banned" by OpenAI. Google announces the most popular Chrome extension in 2023 Academician Ni Guangnan: I hope domestic SSD will replace imported HDD to unlock Xiaomi mobile phone BL? First, do a Java programmer interview question. Arm laid off more than 70 Chinese engineers and planned to reorganize its Chinese software business. OpenKylin 2.0 reveals | UKUI 4.10 double diamond design, beautiful and high-quality! Manjaro 23.1 released, codenamed “Vulcan”
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/3874284/blog/10346411