orpiske commented on code in PR #14768: URL: https://github.com/apache/camel/pull/14768#discussion_r1671653582
########## components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaFetchRecords.java: ########## @@ -343,6 +371,30 @@ protected void startPolling() { final KafkaRecordProcessorFacade recordProcessorFacade = createRecordProcessor(); while (isKafkaConsumerRunnableAndNotStopped() && isConnected() && pollExceptionStrategy.canContinue()) { + + if (commitRecordsRequested.compareAndSet(true, false)) { + try { + // we want to get details about last committed offsets (which MUST be done by this consumer thread) + Map<TopicPartition, OffsetAndMetadata> commits = consumer.committed(consumer.assignment()); + commitRecords.clear(); + for (var e : commits.entrySet()) { + KafkaTopicPosition p + = new KafkaTopicPosition( + e.getKey().topic(), e.getKey().partition(), e.getValue().offset(), + e.getValue().leaderEpoch().orElse(0)); + commitRecords.add(p); + } + CountDownLatch count = latch.get(); + if (count != null) { + count.countDown(); + } + } catch (Exception e) { + // ignore cannot get last commit details + LOG.debug("Cannot get last offset committed from Kafka brokers due to: {}. This exception is ignored.", + e.getMessage(), e); + } + } + Review Comment: IMHO, it would be better to do this using a decorator. The biggest problem here is that we are attaching an edge case behavior right into the hot path of the component. ########## components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaDevConsole.java: ########## @@ -0,0 +1,181 @@ +/* + * 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.camel.component.kafka; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.Route; +import org.apache.camel.spi.annotations.DevConsole; +import org.apache.camel.support.console.AbstractDevConsole; +import org.apache.camel.util.StopWatch; +import org.apache.camel.util.TimeUtils; +import org.apache.camel.util.json.JsonArray; +import org.apache.camel.util.json.JsonObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@DevConsole(name = "kafka", displayName = "Kafka", description = "Apache Kafka") +public class KafkaDevConsole extends AbstractDevConsole { + + private static final Logger LOG = LoggerFactory.getLogger(KafkaDevConsole.class); + + private static final long COMMITTED_TIMEOUT = 10000; + + /** + * Whether to include committed offset (sync operation to Kafka broker) + */ + public static final String COMMITTED = "committed"; + + public KafkaDevConsole() { + super("camel", "kafka", "Kafka", "Apache Kafka"); + } + + @Override + protected String doCallText(Map<String, Object> options) { + final boolean committed = "true".equals(options.getOrDefault(COMMITTED, "false")); + + StringBuilder sb = new StringBuilder(); + for (Route route : getCamelContext().getRoutes()) { + if (route.getConsumer() instanceof KafkaConsumer kc) { + sb.append(String.format("\n Route Id: %s", route.getRouteId())); + sb.append(String.format("\n From: %s", route.getEndpoint().getEndpointUri())); + for (KafkaFetchRecords t : kc.tasks()) { + sb.append(String.format("\n Worked Thread: %s", t.getThreadId())); + sb.append(String.format("\n Worker State: %s", t.getState())); + TaskHealthState hs = t.healthState(); + if (!hs.isReady()) { + sb.append(String.format("\n Worker Last Error: %s", hs.buildStateMessage())); + } + KafkaFetchRecords.GroupMetadata meta = t.getGroupMetadata(); + if (meta != null) { + sb.append(String.format("\n Group Id: %s", meta.groupId())); + sb.append(String.format("\n Group Instance Id: %s", meta.groupInstanceId())); + sb.append(String.format("\n Member Id: %s", meta.memberId())); + sb.append(String.format("\n Generation Id: %d", meta.generationId())); + } + if (t.getLastRecord() != null) { + sb.append(String.format("\n Last Topic: %s", t.getLastRecord().topic())); + sb.append(String.format("\n Last Partition: %d", t.getLastRecord().partition())); + sb.append(String.format("\n Last Offset: %d", t.getLastRecord().offset())); + } + if (committed) { + List<KafkaFetchRecords.KafkaTopicPosition> l = fetchCommitOffsets(kc, t); + if (l != null) { + for (KafkaFetchRecords.KafkaTopicPosition r : l) { + sb.append(String.format("\n Commit Topic: %s", r.topic())); + sb.append(String.format("\n Commit Partition: %s", r.partition())); + sb.append(String.format("\n Commit Offset: %s", r.offset())); + if (r.epoch() > 0) { + long delta = System.currentTimeMillis() - r.epoch(); + sb.append(String.format("\n Commit Offset Since: %s", + TimeUtils.printDuration(delta, true))); + } + } + } + } Review Comment: IMHO, this would be much better done by using a script language such as Velocity. Formatting the strings builtin are terribly hard to maintain. -- 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: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org