wu-sheng opened a new pull request, #810:
URL: https://github.com/apache/skywalking-java/pull/810

   ### Fix agent lifecycle events: the Start event missed the service instance 
name, and the Shutdown event was never delivered
   
   - [x] Add a unit test to verify that the fix works.
   - [x] Explain briefly why the bug exists and how to fix it.
   
   #### Why the bug exists
   
   `BootService#priority()` is documented as *"BootServices with higher 
priorities will be started earlier, and shut down later"*, but `ServiceManager` 
sorted the phases the opposite way — `prepare()`/`startup()` ascending and 
`shutdown()` descending. So the `Integer.MAX_VALUE` services ran **last** on 
boot and **first** on shutdown, breaking two lifecycle events:
   
   - `ServiceInstanceGenerator` (MAX) prepared **last**, so 
`EventReportServiceClient` built the Start event before `INSTANCE_NAME` was 
generated → empty `serviceInstance`.
   - `GRPCChannelManager` (MAX) shut down **first**, closing the gRPC channel 
before `EventReportServiceClient.shutdown()` could send the Shutdown event. A 
second defect compounded it: the event stub's deadline was fixed once at 
connect time (`withDeadlineAfter`), so for any JVM alive longer than the 
upstream timeout the Shutdown RPC failed with `DEADLINE_EXCEEDED` even on a 
live channel.
   
   #### How it's fixed
   
   - **`ServiceManager`**: sort `prepare()`/`startup()` descending and 
`shutdown()` ascending, matching the documented contract (foundational services 
come up first and go down last).
   - **`EventReportServiceClient`**: align with the stable 
`TraceSegmentServiceClient` / `ServiceManagementClient` idiom — `volatile` 
status/stub, a plain stub built on `CONNECTED` before the status is published, 
and a fresh deadline applied per send. The Start-event gate now uses explicit 
readiness (`bootCompleted` + `CONNECTED`), with `reported` meaning 
sent/in-flight, so a failed send retries on the next connect instead of being 
dropped (this also fixes the previously inverted `compareAndSet` gate).
   - **`KafkaProducerManager`**: no longer derives its priority from 
`GRPCChannelManager` — that coupling flipped under the corrected order and 
would close the shared producer before the Kafka reporters stop. It now uses a 
constant priority higher than the default-priority Kafka reporters, so the 
producer is closed only after they stop sending.
   
   #### Verification
   
   Added `ServiceManagerOrderingTest` (pins boot descending / shutdown 
ascending) and a `KafkaProducerManager` priority test; `apm-agent-core` and 
`kafka-reporter-plugin` suites pass with 0 checkstyle violations. Also verified 
end-to-end against a local OAP + BanyanDB: a service instrumented with the 
built agent reports a Start event carrying the instance name and, on graceful 
shutdown (including JVMs running well past the upstream timeout), a Shutdown 
event.
   
   - [x] Update the [`CHANGES` 
log](https://github.com/apache/skywalking-java/blob/main/CHANGES.md).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to