chia7712 commented on code in PR #19699:
URL: https://github.com/apache/kafka/pull/19699#discussion_r2279665390


##########
transaction-coordinator/src/main/java/org/apache/kafka/coordinator/transaction/TxnTransitMetadata.java:
##########
@@ -19,7 +19,7 @@
 import org.apache.kafka.common.TopicPartition;
 import org.apache.kafka.server.common.TransactionVersion;
 
-import java.util.Set;
+import java.util.HashSet;
 
 /**
  * Immutable object representing the target transition of the transaction 
metadata

Review Comment:
   `Immutable` is no longer correct. Could you please update it in the 
follow-up?



##########
transaction-coordinator/src/main/java/org/apache/kafka/coordinator/transaction/TransactionMetadata.java:
##########
@@ -0,0 +1,662 @@
+/*
+ * 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.transaction;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.LogLevelConfig;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.record.RecordBatch;
+import org.apache.kafka.server.common.TransactionVersion;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.MarkerFactory;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.Supplier;
+
+public class TransactionMetadata {
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(TransactionMetadata.class);
+    private final String transactionalId;
+    private long producerId;
+    private long prevProducerId;
+    private long nextProducerId;
+    private short producerEpoch;
+    private short lastProducerEpoch;
+    private int txnTimeoutMs;
+    private TransactionState state;
+    // The topicPartitions is mutable, so using HashSet, instead of Set.
+    private HashSet<TopicPartition> topicPartitions;
+    private volatile long txnStartTimestamp;
+    private volatile long txnLastUpdateTimestamp;
+    private TransactionVersion clientTransactionVersion;
+
+    // pending state is used to indicate the state that this transaction is 
going to
+    // transit to, and for blocking future attempts to transit it again if it 
is not legal;
+    // initialized as the same as the current state
+    private Optional<TransactionState> pendingState;
+
+    // Indicates that during a previous attempt to fence a producer, the 
bumped epoch may not have been
+    // successfully written to the log. If this is true, we will not bump the 
epoch again when fencing
+    private boolean hasFailedEpochFence;
+
+    private final ReentrantLock lock;
+
+    public static boolean isEpochExhausted(short producerEpoch) {
+        return producerEpoch >= Short.MAX_VALUE - 1;
+    }
+
+    /**
+     * @param transactionalId          transactional id
+     * @param producerId               producer id
+     * @param prevProducerId           producer id for the last committed 
transaction with this transactional ID
+     * @param nextProducerId           Latest producer ID sent to the producer 
for the given transactional ID
+     * @param producerEpoch            current epoch of the producer
+     * @param lastProducerEpoch        last epoch of the producer
+     * @param txnTimeoutMs             timeout to be used to abort long 
running transactions
+     * @param state                    current state of the transaction
+     * @param topicPartitions          current set of partitions that are part 
of this transaction
+     * @param txnStartTimestamp        time the transaction was started, i.e., 
when first partition is added
+     * @param txnLastUpdateTimestamp   updated when any operation updates the 
TransactionMetadata. To be used for expiration
+     * @param clientTransactionVersion TransactionVersion used by the client 
when the state was transitioned
+     */
+    public TransactionMetadata(String transactionalId,
+                               long producerId,
+                               long prevProducerId,
+                               long nextProducerId,
+                               short producerEpoch,
+                               short lastProducerEpoch,
+                               int txnTimeoutMs,
+                               TransactionState state,
+                               Set<TopicPartition> topicPartitions,
+                               long txnStartTimestamp,
+                               long txnLastUpdateTimestamp,
+                               TransactionVersion clientTransactionVersion) {
+        this.transactionalId = transactionalId;
+        this.producerId = producerId;
+        this.prevProducerId = prevProducerId;
+        this.nextProducerId = nextProducerId;
+        this.producerEpoch = producerEpoch;
+        this.lastProducerEpoch = lastProducerEpoch;
+        this.txnTimeoutMs = txnTimeoutMs;
+        this.state = state;
+        this.topicPartitions = new HashSet<>(topicPartitions);

Review Comment:
   Since `topicPartitions` is always empty in production when creating 
`TransactionMetadata`, we could remove it from the constructor to simplify the 
code



-- 
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

Reply via email to