Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

LoomDoctor Logo

LoomDoctor

Real-Time Virtual Thread Pinning Diagnostics, Carrier Starvation Detection, and Telemetry for Java 21+ & Spring Boot 3

Build Status Maven Central License Java 21+ Spring Boot 3


Overview

With Project Loom in Java 21, high-throughput concurrency is accessible via Virtual Threads (Thread.ofVirtual()). However, in production microservices, virtual thread migrations frequently suffer from two severe anti-patterns:

  1. Thread Pinning: Synchronized blocks and native JNI methods lock virtual threads to underlying operating system carrier threads (ForkJoinPool), starving the entire JVM of executor capacity.
  2. Carrier & Pool Starvation: When tens of thousands of virtual threads block simultaneously on undersized database connection pools (such as HikariCP), queues explode, leading to systemic latency spikes and connection deadlocks.

LoomDoctor is a lightweight runtime telemetry and diagnostic library for Java 21+ and Spring Boot 3 that identifies pinning events in real time, inspects connection pool health against virtual thread pressure, and exposes operational insights via Prometheus and a native Actuator endpoint (/actuator/loom-doctor).


Terminal Diagnostics Demo

1. Real-Time Carrier Pinning Detection

When a virtual thread executes blocking I/O inside a synchronized block or native frame, it pins the underlying OS carrier thread, starving the entire carrier pool. LoomDoctor intercepts and flags this immediately:

[LOOM-DOCTOR] 🚨 VIRTUAL THREAD PINNING DETECTED
┌─ Virtual Thread:   #4821 [http-nio-8080-exec-14]
├─ Carrier Thread:   ForkJoinPool-1-worker-3 (PINNED for 38.4 ms)
├─ Blocking Reason:  SYNCHRONIZED_MONITOR
├─ Monitored Object: com.example.inventory.WarehouseService@2b71e49
├─ Call Site:        at com.example.inventory.WarehouseService.checkStock(WarehouseService.java:64)
│                    at com.example.inventory.OrderController.submit(OrderController.java:29)
└─ Prescription:     Refactor 'synchronized' block holding socket read to java.util.concurrent.locks.ReentrantLock.

2. Live Actuator Telemetry (curl -s /actuator/loom-doctor | jq .)

{
  "status": "DEGRADED",
  "carrierPool": {
    "parallelism": 16,
    "activeCarriers": 16,
    "pinnedCarriers": 6,
    "utilization": "100.0%"
  },
  "diagnostics": {
    "totalPinningEvents": 28,
    "primaryBottleneck": "com.example.inventory.WarehouseService#checkStock",
    "hikariPoolStatus": "PRESSURE_WARNING (Active: 10/10, Pending Virtual Threads: 412)",
    "recommendation": "CRITICAL: Carrier pool saturation. 6 carriers pinned while 412 virtual threads wait for DB connections."
  }
}

Key Features

  • Real-Time Pinning Detection: Detects virtual threads pinned to carrier threads via synchronized monitors or native calls.
  • Connection Pool Starvation Alarms: Proactively flags connection exhaustion before database deadlocks cascade across your services.
  • Actionable Engineering Prescriptions: Generates precise diagnostic reports with stack trace evidence and remediation guidance (e.g., refactoring synchronized to ReentrantLock).
  • Production Actuator Endpoint: Live JSON telemetry accessible via /actuator/loom-doctor.

Installation

Core Engine

<dependency>
    <groupId>io.github.frodygr</groupId>
    <artifactId>loomdoctor-core</artifactId>
    <version>0.1.0</version>
</dependency>

Spring Boot 3 Starter

<dependency>
    <groupId>io.github.frodygr</groupId>
    <artifactId>loomdoctor-spring-boot-starter</artifactId>
    <version>0.1.0</version>
</dependency>

Quickstart

Native Java Usage

LoomDoctor doctor = new LoomDoctor();

// Subscribe to pinning alerts
doctor.getPinningDetector().addListener(event -> {
    System.err.printf("[PINNING ALERT] %s on carrier %s (%d ms)%n",
        event.threadName(), event.carrierThreadName(), event.durationNanos() / 1_000_000);
});

// Run diagnostics
LoomDiagnosticsReport report = doctor.diagnose();
System.out.println("Health: " + report.status()); // HEALTHY, DEGRADED, or CRITICAL
report.recommendations().forEach(System.out::println);

Spring Boot Actuator

Enable the endpoint in application.yml:

management:
  endpoints:
    web:
      exposure:
        include: "health,info,loom-doctor"

Access real-time diagnostics at:
GET http://localhost:8080/actuator/loom-doctor

{
  "status": "DEGRADED",
  "totalPinningEvents": 4,
  "recentPinningSamples": [
    {
      "threadName": "virtual-worker-12",
      "carrierThreadName": "ForkJoinPool-1-worker-3",
      "eventType": "SYNCHRONIZED_BLOCK",
      "durationNanos": 18240000
    }
  ],
  "recommendations": [
    "Pinning Alert: 4 pinning events recorded. Replace synchronized blocks holding I/O with ReentrantLock."
  ]
}

Documentation

Full architectural deep-dives, JFR integration guides, and API specifications are available in the Official Wiki.


License

Licensed under the Apache License, Version 2.0.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages