-
Notifications
You must be signed in to change notification settings - Fork 1
Fix/sender stalls jvm shutdown #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RomanDavlyatshin
wants to merge
4
commits into
main
Choose a base branch
from
fix/sender-stalls-jvm-shutdown
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c05c3aa
chore: empty commit
RomanDavlyatshin 963ce61
feat: add shutdown coordinator & flush data on JVM shutdown
RomanDavlyatshin b44a935
fix: remove lib-jvm-shared from gitignore and add changes for previou…
RomanDavlyatshin 0a1f9ad
fix: remove drill daemon thread factory
RomanDavlyatshin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,4 +6,3 @@ build/ | |
| **/kotlin/kni/ | ||
| kni-meta-info | ||
| **/commonGenerated/ | ||
| /lib-jvm-shared/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
java-agent/src/commonMain/kotlin/com/epam/drill/agent/lifecycle/AgentShutdownCoordinator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * Copyright 2020 - 2022 EPAM Systems | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.epam.drill.agent.lifecycle | ||
|
|
||
| expect object AgentShutdownCoordinator { | ||
| fun install() | ||
| fun shutdown() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
java-agent/src/jvmMain/kotlin/com/epam/drill/agent/lifecycle/AgentShutdownCoordinator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /** | ||
| * Copyright 2020 - 2022 EPAM Systems | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.epam.drill.agent.lifecycle | ||
|
|
||
| import com.epam.drill.agent.common.lifecycle.AgentShutdownRegistry | ||
| import com.epam.drill.agent.configuration.Configuration | ||
| import com.epam.drill.agent.configuration.ParameterDefinitions | ||
| import com.epam.drill.agent.transport.DataIngestMessageSender | ||
| import mu.KotlinLogging | ||
| import java.util.concurrent.TimeUnit | ||
| import java.util.concurrent.atomic.AtomicBoolean | ||
|
|
||
| /** | ||
| * Coordinates JVM shutdown: stop producers, then flush the shared message sender. | ||
| */ | ||
| actual object AgentShutdownCoordinator { | ||
| private val logger = KotlinLogging.logger {} | ||
| private val hookInstalled = AtomicBoolean(false) | ||
| private val shutdownCompleted = AtomicBoolean(false) | ||
|
|
||
| actual fun install() { | ||
| if (!hookInstalled.compareAndSet(false, true)) return | ||
| Runtime.getRuntime().addShutdownHook( | ||
| Thread({ shutdown() }, "drill-shutdown-hook") | ||
| ) | ||
| logger.debug { "Agent shutdown hook installed." } | ||
| } | ||
|
|
||
| actual fun shutdown() { | ||
| if (!shutdownCompleted.compareAndSet(false, true)) return | ||
| runShutdown() | ||
| } | ||
|
|
||
| private fun runShutdown() { | ||
| val flushTimeoutMs = Configuration.parameters[ParameterDefinitions.SHUTDOWN_FLUSH_TIMEOUT_MS].toLong() | ||
| val deadlineNanos = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(flushTimeoutMs) | ||
| logger.debug { "Agent shutdown started, flush timeout is ${flushTimeoutMs}ms." } | ||
|
|
||
| for (task in AgentShutdownRegistry.tasks()) { | ||
| val remainingMs = remainingMs(deadlineNanos) | ||
| if (remainingMs <= 0) { | ||
| logger.warn { "Shutdown flush timeout reached before task '${task.name}'." } | ||
| break | ||
| } | ||
| runCatching { | ||
| logger.debug { "Running shutdown task '${task.name}' (${remainingMs}ms remaining)." } | ||
| task.action(remainingMs) | ||
| }.onFailure { | ||
| logger.error(it) { "Shutdown task '${task.name}' failed." } | ||
| } | ||
| } | ||
|
|
||
| val remainingMs = remainingMs(deadlineNanos) | ||
| if (remainingMs <= 0) { | ||
| logger.warn { "Shutdown flush timeout exhausted, attempting a best-effort message sender flush." } | ||
| } else { | ||
| logger.debug { "Flushing message sender, ${remainingMs}ms remaining." } | ||
| } | ||
| runCatching { | ||
| DataIngestMessageSender.shutdownWithTimeout(remainingMs) | ||
| }.onFailure { | ||
| logger.error(it) { "Message sender shutdown failed." } | ||
| } | ||
| logger.debug { "Agent shutdown completed." } | ||
| } | ||
|
|
||
| private fun remainingMs(deadlineNanos: Long): Long = | ||
| TimeUnit.NANOSECONDS.toMillis(deadlineNanos - System.nanoTime()).coerceAtLeast(0) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ import java.util.concurrent.TimeUnit | |
|
|
||
| interface TestInfoSender { | ||
| fun startSendingTests() | ||
| fun stopSendingTests() | ||
| fun stopSendingTests(remainingMs: Long) | ||
| } | ||
|
|
||
| class IntervalTestInfoSender( | ||
|
|
@@ -36,25 +36,33 @@ class IntervalTestInfoSender( | |
| private val collectTests: () -> List<TestLaunchPayload> = { emptyList() } | ||
| ) : TestInfoSender { | ||
| private val logger = KotlinLogging.logger {} | ||
| private val scheduledThreadPool = Executors.newSingleThreadScheduledExecutor() | ||
| private val scheduledThreadPool = Executors.newSingleThreadScheduledExecutor { r -> | ||
| Thread(r, "drill-test-info-sender").apply { | ||
| isDaemon = true | ||
| } | ||
| } | ||
|
|
||
| override fun startSendingTests() { | ||
| scheduledThreadPool.scheduleAtFixedRate( | ||
| { sendTests(collectTests()) }, | ||
| { | ||
| try { | ||
| sendTests(collectTests()) | ||
| } catch (t: Throwable) { | ||
| logger.error(t) { "Test sending job failed" } | ||
| } | ||
| }, | ||
| 0, | ||
| intervalMs, | ||
| TimeUnit.MILLISECONDS | ||
| ) | ||
| logger.debug { "Test sending job is started." } | ||
| } | ||
|
|
||
| override fun stopSendingTests() { | ||
| override fun stopSendingTests(remainingMs: Long) { | ||
| sendTests(collectTests()) | ||
| messageSender.shutdown() | ||
| scheduledThreadPool.shutdown() | ||
| if (!scheduledThreadPool.awaitTermination(1, TimeUnit.SECONDS)) { | ||
| logger.error("Failed to send some tests prior to shutdown") | ||
| scheduledThreadPool.shutdownNow(); | ||
| if (remainingMs > 0 && !scheduledThreadPool.awaitTermination(remainingMs, TimeUnit.MILLISECONDS)) { | ||
| logger.warn { "Test sending scheduler did not stop within ${remainingMs}ms; leaving it for JVM exit." } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is |
||
| } | ||
| logger.info { "Test sending job is stopped." } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
java-agent/src/nativeMain/kotlin/com/epam/drill/agent/lifecycle/AgentShutdownCoordinator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * Copyright 2020 - 2022 EPAM Systems | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.epam.drill.agent.lifecycle | ||
|
|
||
| import com.epam.drill.agent.jvmapi.callObjectVoidMethod | ||
|
|
||
| actual object AgentShutdownCoordinator { | ||
| actual fun install(): Unit = | ||
| callObjectVoidMethod(AgentShutdownCoordinator::class, AgentShutdownCoordinator::install) | ||
|
|
||
| actual fun shutdown(): Unit = | ||
| callObjectVoidMethod(AgentShutdownCoordinator::class, AgentShutdownCoordinator::shutdown) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we wait for the full
remainingMsinstead of a shorter timeout specific to this step?If we block here for the entire remaining shutdown window due to a stuck task, other cleanup steps may not run in time.