jeffkbkim commented on code in PR #17823: URL: https://github.com/apache/kafka/pull/17823#discussion_r1847114422
########## coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/CoordinatorExecutorImpl.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.coordinator.common.runtime; + +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.CoordinatorLoadInProgressException; +import org.apache.kafka.common.errors.NotCoordinatorException; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.time.Duration; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; + +public class CoordinatorExecutorImpl<S extends CoordinatorShard<U>, U> implements CoordinatorExecutor<U> { + private static class TaskResult<R> { + final R result; + final Throwable exception; + + TaskResult( + R result, + Throwable exception + ) { + this.result = result; + this.exception = exception; + } + } + + private final Logger log; + private final TopicPartition shard; + private final CoordinatorRuntime<S, U> runtime; + private final ExecutorService executor; + private final Map<String, TaskRunnable<?>> tasks = new ConcurrentHashMap<>(); + + public CoordinatorExecutorImpl( + LogContext logContext, + TopicPartition shard, + CoordinatorRuntime<S, U> runtime, + ExecutorService executor + ) { + this.log = logContext.logger(CoordinatorExecutorImpl.class); + this.shard = shard; + this.runtime = runtime; + this.executor = executor; + } + + private <R> TaskResult<R> executeTask(TaskRunnable<R> task) { + try { + return new TaskResult<>(task.run(), null); + } catch (Throwable ex) { + return new TaskResult<>(null, ex); + } + } + + @Override + public <R> boolean schedule( + String key, Review Comment: for the regex use case, would the key be the regular expression? ########## coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/CoordinatorExecutorImpl.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.coordinator.common.runtime; + +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.CoordinatorLoadInProgressException; +import org.apache.kafka.common.errors.NotCoordinatorException; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.time.Duration; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; + +public class CoordinatorExecutorImpl<S extends CoordinatorShard<U>, U> implements CoordinatorExecutor<U> { + private static class TaskResult<R> { + final R result; + final Throwable exception; + + TaskResult( + R result, + Throwable exception + ) { + this.result = result; + this.exception = exception; + } + } + + private final Logger log; + private final TopicPartition shard; + private final CoordinatorRuntime<S, U> runtime; + private final ExecutorService executor; + private final Map<String, TaskRunnable<?>> tasks = new ConcurrentHashMap<>(); + + public CoordinatorExecutorImpl( + LogContext logContext, + TopicPartition shard, + CoordinatorRuntime<S, U> runtime, + ExecutorService executor + ) { + this.log = logContext.logger(CoordinatorExecutorImpl.class); + this.shard = shard; + this.runtime = runtime; + this.executor = executor; + } + + private <R> TaskResult<R> executeTask(TaskRunnable<R> task) { + try { + return new TaskResult<>(task.run(), null); + } catch (Throwable ex) { + return new TaskResult<>(null, ex); + } + } + + @Override + public <R> boolean schedule( + String key, + TaskRunnable<R> task, + TaskOperation<U, R> operation + ) { + // Put the task if the key is free. Otherwise, reject it. + if (tasks.putIfAbsent(key, task) != null) return false; + + // Submit the task. + executor.submit(() -> { + // If the task associated with the task is not us, it means + // that the task was either replaced or cancelled. We stop. + if (tasks.get(key) != task) return; + + // Execute the task. + final TaskResult<R> result = executeTask(task); + + // Schedule the operation. + runtime.scheduleWriteOperation( + key, + shard, + Duration.ofMillis(Long.MAX_VALUE), + coordinator -> { + // If the task associated with the task is not us, it means Review Comment: nit: " associated with the key has changed," ########## coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/CoordinatorExecutorImpl.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.coordinator.common.runtime; + +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.CoordinatorLoadInProgressException; +import org.apache.kafka.common.errors.NotCoordinatorException; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.time.Duration; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; + +public class CoordinatorExecutorImpl<S extends CoordinatorShard<U>, U> implements CoordinatorExecutor<U> { + private static class TaskResult<R> { + final R result; + final Throwable exception; + + TaskResult( + R result, + Throwable exception + ) { + this.result = result; + this.exception = exception; + } + } + + private final Logger log; + private final TopicPartition shard; + private final CoordinatorRuntime<S, U> runtime; + private final ExecutorService executor; + private final Map<String, TaskRunnable<?>> tasks = new ConcurrentHashMap<>(); + + public CoordinatorExecutorImpl( + LogContext logContext, + TopicPartition shard, + CoordinatorRuntime<S, U> runtime, + ExecutorService executor + ) { + this.log = logContext.logger(CoordinatorExecutorImpl.class); + this.shard = shard; + this.runtime = runtime; + this.executor = executor; + } + + private <R> TaskResult<R> executeTask(TaskRunnable<R> task) { + try { + return new TaskResult<>(task.run(), null); + } catch (Throwable ex) { + return new TaskResult<>(null, ex); + } + } + + @Override + public <R> boolean schedule( + String key, + TaskRunnable<R> task, + TaskOperation<U, R> operation + ) { + // Put the task if the key is free. Otherwise, reject it. + if (tasks.putIfAbsent(key, task) != null) return false; Review Comment: to confirm, this is set since we only need to perform the resolution once with the latest state and this would apply to the async assignment task as well? ########## coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/CoordinatorExecutorImpl.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.coordinator.common.runtime; + +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.CoordinatorLoadInProgressException; +import org.apache.kafka.common.errors.NotCoordinatorException; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.time.Duration; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; + +public class CoordinatorExecutorImpl<S extends CoordinatorShard<U>, U> implements CoordinatorExecutor<U> { + private static class TaskResult<R> { + final R result; + final Throwable exception; + + TaskResult( + R result, + Throwable exception + ) { + this.result = result; + this.exception = exception; + } + } + + private final Logger log; + private final TopicPartition shard; + private final CoordinatorRuntime<S, U> runtime; + private final ExecutorService executor; + private final Map<String, TaskRunnable<?>> tasks = new ConcurrentHashMap<>(); + + public CoordinatorExecutorImpl( + LogContext logContext, + TopicPartition shard, + CoordinatorRuntime<S, U> runtime, + ExecutorService executor + ) { + this.log = logContext.logger(CoordinatorExecutorImpl.class); + this.shard = shard; + this.runtime = runtime; + this.executor = executor; + } + + private <R> TaskResult<R> executeTask(TaskRunnable<R> task) { + try { + return new TaskResult<>(task.run(), null); + } catch (Throwable ex) { + return new TaskResult<>(null, ex); + } + } + + @Override + public <R> boolean schedule( + String key, + TaskRunnable<R> task, + TaskOperation<U, R> operation + ) { + // Put the task if the key is free. Otherwise, reject it. + if (tasks.putIfAbsent(key, task) != null) return false; + + // Submit the task. + executor.submit(() -> { + // If the task associated with the task is not us, it means + // that the task was either replaced or cancelled. We stop. + if (tasks.get(key) != task) return; + + // Execute the task. + final TaskResult<R> result = executeTask(task); + + // Schedule the operation. + runtime.scheduleWriteOperation( + key, + shard, + Duration.ofMillis(Long.MAX_VALUE), + coordinator -> { + // If the task associated with the task is not us, it means + // that the task was either replaced or cancelled. We stop. + if (!tasks.remove(key, task)) { + return new CoordinatorResult<>(Collections.emptyList(), null); Review Comment: my understanding is that the runtime will try appending the empty records and the event can be added to the current batch's deferred events and completed once the batch is committed. it seems to me this condition is considered a no-op but the event is potentially referenced for a long time. would it be better doing a full stop? ########## coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/CoordinatorExecutorImpl.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.coordinator.common.runtime; + +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.CoordinatorLoadInProgressException; +import org.apache.kafka.common.errors.NotCoordinatorException; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.time.Duration; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; + +public class CoordinatorExecutorImpl<S extends CoordinatorShard<U>, U> implements CoordinatorExecutor<U> { + private static class TaskResult<R> { + final R result; + final Throwable exception; + + TaskResult( + R result, + Throwable exception + ) { + this.result = result; + this.exception = exception; + } + } + + private final Logger log; + private final TopicPartition shard; + private final CoordinatorRuntime<S, U> runtime; + private final ExecutorService executor; + private final Map<String, TaskRunnable<?>> tasks = new ConcurrentHashMap<>(); + + public CoordinatorExecutorImpl( + LogContext logContext, + TopicPartition shard, + CoordinatorRuntime<S, U> runtime, + ExecutorService executor + ) { + this.log = logContext.logger(CoordinatorExecutorImpl.class); + this.shard = shard; + this.runtime = runtime; + this.executor = executor; + } + + private <R> TaskResult<R> executeTask(TaskRunnable<R> task) { + try { + return new TaskResult<>(task.run(), null); + } catch (Throwable ex) { + return new TaskResult<>(null, ex); + } + } + + @Override + public <R> boolean schedule( + String key, + TaskRunnable<R> task, + TaskOperation<U, R> operation + ) { + // Put the task if the key is free. Otherwise, reject it. + if (tasks.putIfAbsent(key, task) != null) return false; + + // Submit the task. + executor.submit(() -> { + // If the task associated with the task is not us, it means Review Comment: nit: " associated with the key has changed," -- 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]
