Skip to content
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

Control max heap dumps via env var #529

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/main/kotlin/io/emeraldpay/dshackle/HeapDumpCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import java.util.concurrent.TimeUnit

class HeapDumpCreator {
companion object {
private const val MAX_DUMPS = 3

private val log = LoggerFactory.getLogger(HeapDumpCreator::class.java)
private val executorService = Executors.newSingleThreadScheduledExecutor()

fun init() {
val envVars = System.getenv()
val enableCreateDumps = envVars["HEAP_DUMP_ENABLE"]?.toBoolean() ?: true
val dumpsPath = envVars["HEAP_DUMP_PATH"] ?: "/etc/dshackle/dumps"
val maxDumps = envVars["MAX_HEAP_DUMPS"]?.toInt() ?: 3
val dumpsPathExists = heapDumpsPathExists(dumpsPath)
.also {
if (!it) {
Expand All @@ -33,7 +32,7 @@ class HeapDumpCreator {
}

if (enableCreateDumps && dumpsPathExists) {
executorService.scheduleAtFixedRate({ dumpCheckTask(dumpsPath) }, 0, 30, TimeUnit.MINUTES)
executorService.scheduleAtFixedRate({ dumpCheckTask(dumpsPath, maxDumps) }, 0, 30, TimeUnit.MINUTES)

val appName = envVars["DSHACKLE_APP_NAME"] ?: "dshackle"

Expand Down Expand Up @@ -64,15 +63,15 @@ class HeapDumpCreator {
}
}

private fun dumpCheckTask(dumpsPath: String) {
private fun dumpCheckTask(dumpsPath: String, maxDumps: Int) {
val files = File(dumpsPath).listFiles()
?.filter { it.name.endsWith("hprof") }
?: emptyList()

if (files.isNotEmpty()) {
log.warn("There are some heap dumps, please send them to dshackle developers")
}
if (files.size >= MAX_DUMPS) {
if (files.size >= maxDumps) {
files.minBy {
Files.readAttributes(Paths.get(it.absolutePath), BasicFileAttributes::class.java).creationTime()
}.run {
Expand Down
Loading