chia7712 commented on code in PR #16935: URL: https://github.com/apache/kafka/pull/16935#discussion_r1724004600
########## core/src/test/scala/unit/kafka/server/ThrottledChannelExpirationTest.scala: ########## @@ -23,6 +23,7 @@ import java.util.concurrent.{DelayQueue, TimeUnit} import org.apache.kafka.common.metrics.MetricConfig import org.apache.kafka.common.utils.MockTime import org.apache.kafka.server.config.ClientQuotaManagerConfig +import org.apache.kafka.server.{ThrottledChannel, ThrottleCallback} import org.junit.jupiter.api.Assertions._ import org.junit.jupiter.api.{BeforeEach, Test} Review Comment: please remove duplicate test `testThrottledChannelDelay` ########## server/src/main/java/org/apache/kafka/server/ThrottledChannel.java: ########## @@ -0,0 +1,71 @@ +/* + * 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.server; + +import org.apache.kafka.common.utils.Time; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +public class ThrottledChannel implements Delayed { + + private final Logger log = LoggerFactory.getLogger(getClass().getName()); Review Comment: `private static final Logger LOGGER = LoggerFactory.getLogger(ThrottledChannel.class);` ########## server/src/main/java/org/apache/kafka/server/ThrottleCallback.java: ########## @@ -0,0 +1,22 @@ +/* + * 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.server; Review Comment: Could you please move it to `org.apache.kafka.server.quota`? There are other quota-related classes which will be moved to there ########## server/src/main/java/org/apache/kafka/server/ThrottledChannel.java: ########## @@ -0,0 +1,71 @@ +/* + * 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.server; + +import org.apache.kafka.common.utils.Time; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +public class ThrottledChannel implements Delayed { + + private final Logger log = LoggerFactory.getLogger(getClass().getName()); + private final Time time; + private final long throttleTimeMs; + private final ThrottleCallback callback; + private final long endTimeNanos; + + /** + * Represents a request whose response has been delayed. + * @param time Time instance to use + * @param throttleTimeMs Delay associated with this request + * @param callback Callback for channel throttling + */ + public ThrottledChannel(Time time, int throttleTimeMs, ThrottleCallback callback) { + this.time = time; + this.throttleTimeMs = throttleTimeMs; + this.callback = callback; + this.endTimeNanos = time.nanoseconds() + TimeUnit.MILLISECONDS.toNanos(throttleTimeMs); + + // Notify the socket server that throttling has started for this channel. + callback.startThrottling(); + } + + // Notify the socket server that throttling has been done for this channel. + public void notifyThrottlingDone() { + log.trace(String.format("Channel throttled for: %d ms", throttleTimeMs)); + callback.endThrottling(); + } + + @Override + public long getDelay(TimeUnit unit) { + return unit.convert(endTimeNanos - time.nanoseconds(), TimeUnit.NANOSECONDS); + } + + @Override + public int compareTo(Delayed other) { + ThrottledChannel otherChannel = (ThrottledChannel) other; + return Long.compare(this.endTimeNanos, otherChannel.endTimeNanos); + } + + public long throttleTimeMs() { Review Comment: `int` ########## server/src/main/java/org/apache/kafka/server/ThrottledChannel.java: ########## @@ -0,0 +1,71 @@ +/* + * 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.server; + +import org.apache.kafka.common.utils.Time; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +public class ThrottledChannel implements Delayed { + + private final Logger log = LoggerFactory.getLogger(getClass().getName()); + private final Time time; + private final long throttleTimeMs; Review Comment: `int` ########## server/src/main/java/org/apache/kafka/server/ThrottledChannel.java: ########## @@ -0,0 +1,71 @@ +/* + * 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.server; + +import org.apache.kafka.common.utils.Time; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +public class ThrottledChannel implements Delayed { + + private final Logger log = LoggerFactory.getLogger(getClass().getName()); + private final Time time; + private final long throttleTimeMs; + private final ThrottleCallback callback; + private final long endTimeNanos; + + /** + * Represents a request whose response has been delayed. + * @param time Time instance to use + * @param throttleTimeMs Delay associated with this request + * @param callback Callback for channel throttling + */ + public ThrottledChannel(Time time, int throttleTimeMs, ThrottleCallback callback) { + this.time = time; + this.throttleTimeMs = throttleTimeMs; + this.callback = callback; + this.endTimeNanos = time.nanoseconds() + TimeUnit.MILLISECONDS.toNanos(throttleTimeMs); + + // Notify the socket server that throttling has started for this channel. + callback.startThrottling(); + } + + // Notify the socket server that throttling has been done for this channel. + public void notifyThrottlingDone() { + log.trace(String.format("Channel throttled for: %d ms", throttleTimeMs)); Review Comment: `log.trace("Channel throttled for: {} ms", throttleTimeMs);` ########## server/src/main/java/org/apache/kafka/server/ThrottledChannel.java: ########## @@ -0,0 +1,71 @@ +/* + * 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.server; + +import org.apache.kafka.common.utils.Time; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +public class ThrottledChannel implements Delayed { + + private final Logger log = LoggerFactory.getLogger(getClass().getName()); + private final Time time; + private final long throttleTimeMs; + private final ThrottleCallback callback; + private final long endTimeNanos; + + /** + * Represents a request whose response has been delayed. + * @param time Time instance to use + * @param throttleTimeMs Delay associated with this request + * @param callback Callback for channel throttling + */ + public ThrottledChannel(Time time, int throttleTimeMs, ThrottleCallback callback) { + this.time = time; + this.throttleTimeMs = throttleTimeMs; + this.callback = callback; + this.endTimeNanos = time.nanoseconds() + TimeUnit.MILLISECONDS.toNanos(throttleTimeMs); + + // Notify the socket server that throttling has started for this channel. + callback.startThrottling(); + } + + // Notify the socket server that throttling has been done for this channel. Review Comment: ```java /** * Notify the socket server that throttling has been done for this channel. */ ``` -- 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: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org