mumrah commented on code in PR #15622: URL: https://github.com/apache/kafka/pull/15622#discussion_r1883117273
########## metadata/src/main/java/org/apache/kafka/controller/SlowEventsLogger.java: ########## @@ -0,0 +1,87 @@ +/* + * 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.kafka.controller; + +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.util.function.Supplier; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; + +/** + * Track the p99 for controller event queue processing time. If we encounter an event that takes longer + * than this cached p99 time, we will log it at INFO level on the controller logger. + */ +public class SlowEventsLogger { + /** + * Don't report any p99 events below this threshold. This prevents the controller from reporting p99 event + * times in the idle case where p99 event times are essentially the average as well. + */ + private final long minSlowEventTimeNs; + + /** + * Function that returns the current p99 time in millis. This call can be expensive, and since the histogram is + * biased towards the last 5 minutes of data, we only need to update this p99 every so often. + */ + private final Supplier<Double> thresholdMsSupplier; + + private final Logger log; + + /** + * The current p99 threshold in nanos. + */ + private long thresholdNs; + + public SlowEventsLogger( + int minSlowEventTimeMs, + Supplier<Double> thresholdMsSupplier, + LogContext logContext + ) { + this.minSlowEventTimeNs = MILLISECONDS.toNanos(minSlowEventTimeMs); + this.thresholdMsSupplier = thresholdMsSupplier; + this.thresholdNs = minSlowEventTimeMs; + this.log = logContext.logger(SlowEventsLogger.class); + } + + /** + * Produce an INFO log if the given event ran for longer than the current p99 event processing time. + * + * @return true if a slow event was logged, false otherwise. + */ + public boolean maybeLogEvent(String name, long durationNs) { + if (durationNs >= minSlowEventTimeNs && durationNs >= thresholdNs) { + log.info("Slow controller event {} processed in {} us which is greater than p99 of {} us", + name, + NANOSECONDS.toMicros(durationNs), + NANOSECONDS.toMicros(thresholdNs) + ); + return true; + } else if (durationNs >= thresholdNs) { + return false; + } + return false; Review Comment: Ah yea, that was left over from some debugging. -- 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]
