AHeise commented on code in PR #154: URL: https://github.com/apache/flink-connector-kafka/pull/154#discussion_r2014503601
########## flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/sink/internal/TransactionNamingStrategyImpl.java: ########## @@ -0,0 +1,77 @@ +/* + * 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.flink.connector.kafka.sink.internal; + +import org.apache.flink.connector.kafka.sink.KafkaSinkOptions.TransactionNamingStrategy; + +import static org.apache.flink.util.Preconditions.checkState; + +/** Implementation of {@link TransactionNamingStrategy}. */ +public enum TransactionNamingStrategyImpl { + INCREMENTING { + /** + * For each checkpoint we create new {@link FlinkKafkaInternalProducer} so that new + * transactions will not clash with transactions created during previous checkpoints ({@code + * producer.initTransactions()} assures that we obtain new producerId and epoch counters). + * + * <p>Ensures that all transaction ids in between lastCheckpointId and checkpointId are + * initialized. + */ + @Override + public FlinkKafkaInternalProducer<byte[], byte[]> getTransactionalProducer( + Context context) { + long expectedCheckpointId = context.getNextCheckpointId(); + long lastCheckpointId = context.getLastCheckpointId(); + checkState( + expectedCheckpointId > lastCheckpointId, + "Expected %s > %s", + expectedCheckpointId, + lastCheckpointId); + // in case checkpoints have been aborted, Flink would create non-consecutive transaction + // ids + // this loop ensures that all gaps are filled with initialized (empty) transactions + for (long checkpointId = lastCheckpointId + 1; + checkpointId < expectedCheckpointId; + checkpointId++) { + context.recycle(context.getProducer(context.buildTransactionalId(checkpointId))); + } Review Comment: This is the previous logic expressed through the context. ``` private FlinkKafkaInternalProducer<byte[], byte[]> getTransactionalProducer(long checkpointId) { checkState( checkpointId > lastCheckpointId, "Expected %s > %s", checkpointId, lastCheckpointId); FlinkKafkaInternalProducer<byte[], byte[]> producer = null; // in case checkpoints have been aborted, Flink would create non-consecutive transaction ids // this loop ensures that all gaps are filled with initialized (empty) transactions for (long id = lastCheckpointId + 1; id <= checkpointId; id++) { String transactionalId = TransactionalIdFactory.buildTransactionalId( transactionalIdPrefix, kafkaSinkContext.getParallelInstanceId(), id); producer = getOrCreateTransactionalProducer(transactionalId); } this.lastCheckpointId = checkpointId; assert producer != null; LOG.info("Created new transactional producer {}", producer.getTransactionalId()); return producer; } ``` Now that I'm looking at the old code, this probably leaked producers en mass... -- 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]
