josefk31 commented on code in PR #22505: URL: https://github.com/apache/kafka/pull/22505#discussion_r3422539077
########## raft/src/main/java/org/apache/kafka/raft/internals/ChangeVoterHandlerState.java: ########## @@ -0,0 +1,208 @@ +/* + * 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.raft.internals; + +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.raft.RaftUtil; + +import java.util.Optional; + +/** + * Manages the state of add and remove voter operations. + * <p> + * This class maintains at most one pending voter change operation at a time. Add voter and + * remove voter operations are mutually exclusive - only one type can be in progress at any + * given time. When an operation is reset or expires, its associated future is completed with + * an appropriate error response. + * <p> + * The class also updates the uncommitted voter change metric to reflect whether a voter + * change operation is currently pending. + */ +public final class ChangeVoterHandlerState { + private Optional<AddVoterHandlerState> addVoterHandlerState = Optional.empty(); + private Optional<RemoveVoterHandlerState> removeVoterHandlerState = Optional.empty(); + private final KafkaRaftMetrics kafkaRaftMetrics; + + /** + * Constructs a new ChangeVoterHandlerState. + * + * @param kafkaRaftMetrics the metrics instance to update when voter change state changes + */ + public ChangeVoterHandlerState(KafkaRaftMetrics kafkaRaftMetrics) { + this.kafkaRaftMetrics = kafkaRaftMetrics; + } + + /** + * Returns the current add voter handler state, if one exists. + * + * @return an Optional containing the add voter handler state, or empty if no add voter + * operation is pending + */ + public Optional<AddVoterHandlerState> addVoterHandlerState() { + return addVoterHandlerState; + } + + /** + * Resets the add voter handler state to the specified state. + * <p> + * If an add voter handler state already exists, its future will be completed with the + * provided error and message before being replaced. If the new state is non-empty and a + * remove voter handler state is currently present, this method throws an IllegalStateException + * to enforce mutual exclusivity. + * + * @param error the error to complete any existing add voter operation with + * @param message the error message to include in the response, or null for no message + * @param state the new add voter handler state, or empty to clear the state + * @throws IllegalStateException if attempting to set a non-empty add voter state while a + * remove voter state is already present + */ + public void resetAddVoterHandlerState( Review Comment: Nit: `resetXXXX` implies that something was previously "set" or "altered" and so we are moving to a "default" state. EG "factory reset" implies a return to defaults. In this case, the default state is that there is no pending add or remove operations. A better name might be "setAddVoterHandlerState". This also applies to `removeVoterHandlerState` ########## raft/src/main/java/org/apache/kafka/raft/internals/ChangeVoterHandlerState.java: ########## @@ -0,0 +1,208 @@ +/* + * 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.raft.internals; + +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.raft.RaftUtil; + +import java.util.Optional; + +/** + * Manages the state of add and remove voter operations. + * <p> + * This class maintains at most one pending voter change operation at a time. Add voter and + * remove voter operations are mutually exclusive - only one type can be in progress at any + * given time. When an operation is reset or expires, its associated future is completed with + * an appropriate error response. + * <p> + * The class also updates the uncommitted voter change metric to reflect whether a voter Review Comment: Nit: can we add a comment about the thread-safety of this class? It should only be called from the `RaftClient` thread IIUC. If multiple threads try to change this then it can have invalid results. -- 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]
