FrankYang0529 commented on code in PR #19568: URL: https://github.com/apache/kafka/pull/19568#discussion_r2078951004
########## transaction-coordinator/src/main/java/org/apache/kafka/coordinator/transaction/TransactionState.java: ########## @@ -0,0 +1,164 @@ +/* + * 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 java.util.Arrays; +import java.util.EnumSet; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * Represents the states of a transaction in the transaction coordinator. + * This enum corresponds to the Scala sealed trait TransactionState in kafka.coordinator.transaction. + */ +public enum TransactionState { + /** + * Transaction has not existed yet + * <p> + * transition: received AddPartitionsToTxnRequest => Ongoing + * received AddOffsetsToTxnRequest => Ongoing + * received EndTxnRequest with abort and TransactionV2 enabled => PrepareAbort + */ + EMPTY((byte) 0, org.apache.kafka.clients.admin.TransactionState.EMPTY.toString(), true), + /** + * Transaction has started and ongoing + * <p> + * transition: received EndTxnRequest with commit => PrepareCommit + * received EndTxnRequest with abort => PrepareAbort + * received AddPartitionsToTxnRequest => Ongoing + * received AddOffsetsToTxnRequest => Ongoing + */ + ONGOING((byte) 1, org.apache.kafka.clients.admin.TransactionState.ONGOING.toString(), false), + /** + * Group is preparing to commit + * transition: received acks from all partitions => CompleteCommit + */ + PREPARE_COMMIT((byte) 2, org.apache.kafka.clients.admin.TransactionState.PREPARE_COMMIT.toString(), false), + /** + * Group is preparing to abort + * <p> + * transition: received acks from all partitions => CompleteAbort + * <p> + * Note, In transaction v2, we allow Empty, CompleteCommit, CompleteAbort to transition to PrepareAbort. because the + * client may not know the txn state on the server side, it needs to send endTxn request when uncertain. + */ + PREPARE_ABORT((byte) 3, org.apache.kafka.clients.admin.TransactionState.PREPARE_ABORT.toString(), false), + /** + * Group has completed commit + * <p> + * Will soon be removed from the ongoing transaction cache + */ + COMPLETE_COMMIT((byte) 4, org.apache.kafka.clients.admin.TransactionState.COMPLETE_COMMIT.toString(), true), + /** + * Group has completed abort + * <p> + * Will soon be removed from the ongoing transaction cache + */ + COMPLETE_ABORT((byte) 5, org.apache.kafka.clients.admin.TransactionState.COMPLETE_ABORT.toString(), true), + /** + * TransactionalId has expired and is about to be removed from the transaction cache + */ + DEAD((byte) 6, "Dead", false), + /** + * We are in the middle of bumping the epoch and fencing out older producers. + */ + PREPARE_EPOCH_FENCE((byte) 7, org.apache.kafka.clients.admin.TransactionState.PREPARE_EPOCH_FENCE.toString(), false); + + private static final Map<String, TransactionState> NAME_TO_ENUM = Arrays.stream(values()) + .collect(Collectors.toUnmodifiableMap(TransactionState::stateName, Function.identity())); + + private static final Map<Byte, TransactionState> ID_TO_ENUM = Arrays.stream(values()) + .collect(Collectors.toUnmodifiableMap(TransactionState::id, Function.identity())); + + public static final Set<TransactionState> ALL_STATES = Set.copyOf(EnumSet.allOf(TransactionState.class)); + + private final byte id; + private final String stateName; + public static final Map<TransactionState, Set<TransactionState>> VALID_PREVIOUS_STATES = Map.of( + EMPTY, Set.copyOf(EnumSet.of(EMPTY, COMPLETE_COMMIT, COMPLETE_ABORT)), Review Comment: Update it. Thanks. -- 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