|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.common.lock; |
| 19 | + |
| 20 | +import org.apache.doris.common.Config; |
| 21 | + |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +/** |
| 26 | + * Abstract base class for a monitored lock that tracks lock acquisition, |
| 27 | + * release, and attempt times. It provides mechanisms for monitoring the |
| 28 | + * duration for which a lock is held and logging any instances where locks |
| 29 | + * are held longer than a specified timeout or fail to be acquired within |
| 30 | + * a specified timeout. |
| 31 | + */ |
| 32 | +public abstract class AbstractMonitoredLock { |
| 33 | + |
| 34 | + private static final Logger LOG = LoggerFactory.getLogger(AbstractMonitoredLock.class); |
| 35 | + |
| 36 | + // Thread-local variable to store the lock start time |
| 37 | + private final ThreadLocal<Long> lockStartTime = new ThreadLocal<>(); |
| 38 | + |
| 39 | + |
| 40 | + /** |
| 41 | + * Method to be called after successfully acquiring the lock. |
| 42 | + * Sets the start time for the lock. |
| 43 | + */ |
| 44 | + protected void afterLock() { |
| 45 | + lockStartTime.set(System.nanoTime()); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Method to be called after releasing the lock. |
| 50 | + * Calculates the lock hold time and logs a warning if it exceeds the hold timeout. |
| 51 | + */ |
| 52 | + protected void afterUnlock() { |
| 53 | + Long startTime = lockStartTime.get(); |
| 54 | + if (startTime != null) { |
| 55 | + long lockHoldTimeNanos = System.nanoTime() - startTime; |
| 56 | + long lockHoldTimeMs = lockHoldTimeNanos >> 20; |
| 57 | + if (lockHoldTimeMs > Config.max_lock_hold_threshold_seconds * 1000) { |
| 58 | + Thread currentThread = Thread.currentThread(); |
| 59 | + String stackTrace = getThreadStackTrace(currentThread.getStackTrace()); |
| 60 | + LOG.warn("Thread ID: {}, Thread Name: {} - Lock held for {} ms, exceeding hold timeout of {} ms " |
| 61 | + + "Thread stack trace:{}", |
| 62 | + currentThread.getId(), currentThread.getName(), lockHoldTimeMs, lockHoldTimeMs, stackTrace); |
| 63 | + } |
| 64 | + lockStartTime.remove(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Method to be called after attempting to acquire the lock using tryLock. |
| 70 | + * Logs a warning if the lock was not acquired within a reasonable time. |
| 71 | + * |
| 72 | + * @param acquired Whether the lock was successfully acquired |
| 73 | + * @param startTime The start time of the lock attempt |
| 74 | + */ |
| 75 | + protected void afterTryLock(boolean acquired, long startTime) { |
| 76 | + if (acquired) { |
| 77 | + afterLock(); |
| 78 | + return; |
| 79 | + } |
| 80 | + if (LOG.isDebugEnabled()) { |
| 81 | + long elapsedTime = (System.nanoTime() - startTime) >> 20; |
| 82 | + Thread currentThread = Thread.currentThread(); |
| 83 | + String stackTrace = getThreadStackTrace(currentThread.getStackTrace()); |
| 84 | + LOG.debug("Thread ID: {}, Thread Name: {} - Failed to acquire the lock within {} ms" |
| 85 | + + "\nThread blocking info:\n{}", |
| 86 | + currentThread.getId(), currentThread.getName(), elapsedTime, stackTrace); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Utility method to format the stack trace of a thread. |
| 92 | + * |
| 93 | + * @param stackTrace The stack trace elements of the thread |
| 94 | + * @return A formatted string of the stack trace |
| 95 | + */ |
| 96 | + private String getThreadStackTrace(StackTraceElement[] stackTrace) { |
| 97 | + StringBuilder sb = new StringBuilder(); |
| 98 | + for (StackTraceElement element : stackTrace) { |
| 99 | + sb.append("\tat ").append(element).append("\n"); |
| 100 | + } |
| 101 | + return sb.toString().replace("\n", "\\n"); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | + |
0 commit comments