suraj-goel commented on code in PR #18525: URL: https://github.com/apache/druid/pull/18525#discussion_r2461035575
########## extensions-core/kafka-indexing-service/src/main/java/org/apache/druid/indexing/kafka/KafkaHeaderBasedFilterEvaluator.java: ########## @@ -0,0 +1,141 @@ +/* + * 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.druid.indexing.kafka; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import org.apache.druid.indexing.kafka.supervisor.KafkaHeaderBasedFilterConfig; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.query.filter.Filter; +import org.apache.druid.query.filter.InDimFilter; +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.apache.kafka.common.header.Header; +import org.apache.kafka.common.header.Headers; + +import javax.annotation.Nullable; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.util.HashSet; +import java.util.Set; + +/** + * Evaluates Kafka header filters for pre-ingestion filtering. + */ +public class KafkaHeaderBasedFilterEvaluator +{ + private static final Logger log = new Logger(KafkaHeaderBasedFilterEvaluator.class); + + private final Filter filter; + private final Charset encoding; + private final Cache<ByteBuffer, String> stringDecodingCache; + private final Set<String> filterValues; + + public KafkaHeaderBasedFilterEvaluator(KafkaHeaderBasedFilterConfig headerBasedFilterConfig) + { + this.encoding = Charset.forName(headerBasedFilterConfig.getEncoding()); + this.stringDecodingCache = Caffeine.newBuilder() + .maximumSize(headerBasedFilterConfig.getStringDecodingCacheSize()) + .build(); + + this.filter = headerBasedFilterConfig.getFilter().toFilter(); + if (!(filter instanceof InDimFilter)) { + // Only InDimFilter supported + throw new IllegalStateException("Unsupported filter type: " + filter.getClass().getSimpleName()); + } + + // Convert SortedSet to HashSet for O(1) lookups instead of O(log n) TreeSet lookups + InDimFilter inFilter = (InDimFilter) filter; + this.filterValues = new HashSet<>(inFilter.getValues()); + + log.info("Initialized Kafka header filter with encoding [%s] - direct evaluation for [%s] with Caffeine string cache (max %d entries) and HashSet lookup (%d filter values)", + headerBasedFilterConfig.getEncoding(), + this.filter.getClass().getSimpleName(), + headerBasedFilterConfig.getStringDecodingCacheSize(), + this.filterValues.size()); + } + + + /** + * Evaluates whether a Kafka record should be included based on its headers. + * + * @param record the Kafka consumer record + * @return true if the record should be included, false if it should be filtered out + */ + public boolean shouldIncludeRecord(ConsumerRecord<byte[], byte[]> record) + { + try { + return evaluateInclusion(record.headers()); + } + catch (Exception e) { + log.warn( + e, + "Error evaluating header filter for record at topic [%s] partition [%d] offset [%d], including record", + record.topic(), + record.partition(), + record.offset() + ); + return true; // Default to including record on error + } + } + + private boolean evaluateInclusion(Headers headers) + { + InDimFilter inFilter = (InDimFilter) filter; + + // Permissive behavior: missing headers result in inclusion + if (headers == null) { + return true; + } + + Header header = headers.lastHeader(inFilter.getDimension()); + // Permissive behavior: header is null or empty + if (header == null || header.value() == null) { + return true; + } + + String headerValue = getDecodedHeaderValue(header.value()); + // Permissive behavior: failed to decode header value + if (headerValue == null) { + return true; + } + + return filterValues.contains(headerValue); Review Comment: This is a good suggestion but I have added a similar comment here - https://github.com/apache/druid/pull/18525#discussion_r2461024627 -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
