-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
[feat](lock)add deadlock detection tool and monitored lock implementations #39015
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33d0b41
[feat](lock)add deadlock detection tool and monitored lock implementa…
CalvinKirs 4efb2a2
[feat](lock)add deadlock detection tool and monitored lock implementa…
CalvinKirs f02ef96
[feat](lock)add deadlock detection tool and monitored lock implementa…
CalvinKirs e5db911
[feat](lock)add deadlock detection tool and monitored lock implementa…
CalvinKirs 9a7d28c
fix checkstyle
CalvinKirs b11ac6a
fix checkstyle
CalvinKirs 6e905dd
fix checkstyle
CalvinKirs 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 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 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 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 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 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 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
106 changes: 106 additions & 0 deletions
106
fe/fe-core/src/main/java/org/apache/doris/common/lock/AbstractMonitoredLock.java
This file contains 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,106 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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 org.apache.doris.common.lock; | ||
|
||
import org.apache.doris.common.Config; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Abstract base class for a monitored lock that tracks lock acquisition, | ||
* release, and attempt times. It provides mechanisms for monitoring the | ||
* duration for which a lock is held and logging any instances where locks | ||
* are held longer than a specified timeout or fail to be acquired within | ||
* a specified timeout. | ||
*/ | ||
public abstract class AbstractMonitoredLock { | ||
// Lock hold timeout in milliseconds | ||
protected static final long HOLD_TIMEOUT = Config.max_lock_hold_threshold_seconds * 1; | ||
private static final Logger LOG = LoggerFactory.getLogger(AbstractMonitoredLock.class); | ||
|
||
// Thread-local variable to store the lock start time | ||
private final ThreadLocal<Long> lockStartTime = new ThreadLocal<>(); | ||
|
||
|
||
/** | ||
* Method to be called after successfully acquiring the lock. | ||
* Sets the start time for the lock. | ||
*/ | ||
protected void afterLock() { | ||
lockStartTime.set(System.nanoTime()); | ||
} | ||
|
||
/** | ||
* Method to be called after releasing the lock. | ||
* Calculates the lock hold time and logs a warning if it exceeds the hold timeout. | ||
*/ | ||
protected void afterUnlock() { | ||
Long startTime = lockStartTime.get(); | ||
if (startTime != null) { | ||
long lockHoldTimeNanos = System.nanoTime() - startTime; | ||
long lockHoldTimeMs = lockHoldTimeNanos >> 20; | ||
if (lockHoldTimeMs > HOLD_TIMEOUT) { | ||
Thread currentThread = Thread.currentThread(); | ||
String stackTrace = getThreadStackTrace(currentThread.getStackTrace()); | ||
LOG.warn("Thread ID: {}, Thread Name: {} - Lock held for {} ms, exceeding hold timeout of {} ms " | ||
+ "Thread stack trace:{}", | ||
currentThread.getId(), currentThread.getName(), lockHoldTimeMs, HOLD_TIMEOUT, stackTrace); | ||
} | ||
lockStartTime.remove(); | ||
} | ||
} | ||
|
||
/** | ||
* Method to be called after attempting to acquire the lock using tryLock. | ||
* Logs a warning if the lock was not acquired within a reasonable time. | ||
* | ||
* @param acquired Whether the lock was successfully acquired | ||
* @param startTime The start time of the lock attempt | ||
*/ | ||
protected void afterTryLock(boolean acquired, long startTime) { | ||
if (acquired) { | ||
afterLock(); | ||
return; | ||
} | ||
if (LOG.isDebugEnabled()) { | ||
long elapsedTime = (System.nanoTime() - startTime) >> 20; | ||
Thread currentThread = Thread.currentThread(); | ||
String stackTrace = getThreadStackTrace(currentThread.getStackTrace()); | ||
LOG.debug("Thread ID: {}, Thread Name: {} - Failed to acquire the lock within {} ms" | ||
+ "\nThread blocking info:\n{}", | ||
currentThread.getId(), currentThread.getName(), elapsedTime, stackTrace); | ||
} | ||
} | ||
|
||
/** | ||
* Utility method to format the stack trace of a thread. | ||
* | ||
* @param stackTrace The stack trace elements of the thread | ||
* @return A formatted string of the stack trace | ||
*/ | ||
private String getThreadStackTrace(StackTraceElement[] stackTrace) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (StackTraceElement element : stackTrace) { | ||
sb.append("\tat ").append(element).append("\n"); | ||
} | ||
return sb.toString().replace("\n", "\\n"); | ||
} | ||
} | ||
|
||
|
81 changes: 81 additions & 0 deletions
81
fe/fe-core/src/main/java/org/apache/doris/common/lock/DeadlockMonitor.java
This file contains 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,81 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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 org.apache.doris.common.lock; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.ThreadInfo; | ||
import java.lang.management.ThreadMXBean; | ||
import java.util.Arrays; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* A utility class for monitoring and reporting deadlocks in a Java application. | ||
* <p> | ||
* This class uses the Java Management API to periodically check for deadlocked threads | ||
* and logs detailed information about any detected deadlocks. It can be configured to | ||
* run at a fixed interval. | ||
* </p> | ||
*/ | ||
public class DeadlockMonitor { | ||
private static final Logger LOG = LoggerFactory.getLogger(DeadlockMonitor.class); | ||
private final ThreadMXBean threadMXBean; | ||
private final ScheduledExecutorService scheduler; | ||
|
||
public DeadlockMonitor() { | ||
this.threadMXBean = ManagementFactory.getThreadMXBean(); | ||
this.scheduler = Executors.newScheduledThreadPool(1); | ||
} | ||
|
||
/** | ||
* Starts monitoring for deadlocks at a fixed rate. | ||
* | ||
* @param period the period between successive executions | ||
* @param unit the time unit of the period parameter | ||
*/ | ||
public void startMonitoring(long period, TimeUnit unit) { | ||
scheduler.scheduleAtFixedRate(this::detectAndReportDeadlocks, 5, period, unit); | ||
} | ||
|
||
/** | ||
* Detects and reports deadlocks if any are found. | ||
*/ | ||
public void detectAndReportDeadlocks() { | ||
// Get IDs of threads that are deadlocked | ||
long[] deadlockedThreadIds = threadMXBean.findDeadlockedThreads(); | ||
|
||
// Check if there are no deadlocked threads | ||
if (deadlockedThreadIds == null || deadlockedThreadIds.length == 0) { | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("No deadlocks detected."); | ||
} | ||
return; | ||
} | ||
|
||
// Get information about deadlocked threads | ||
ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(deadlockedThreadIds, true, true); | ||
String deadlockReportString = Arrays.toString(threadInfos).replace("\n", "\\n"); | ||
// Log the deadlock report | ||
LOG.warn("Deadlocks detected {}", deadlockReportString); | ||
} | ||
|
||
} |
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.
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.
And make it mutable