jsancio commented on a change in pull request #9476: URL: https://github.com/apache/kafka/pull/9476#discussion_r510448694
########## File path: raft/src/test/java/org/apache/kafka/raft/RaftClientTestContext.java ########## @@ -0,0 +1,648 @@ +/* + * 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; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.OptionalInt; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.stream.Collectors; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.message.BeginQuorumEpochRequestData; +import org.apache.kafka.common.message.BeginQuorumEpochResponseData; +import org.apache.kafka.common.message.DescribeQuorumResponseData.ReplicaState; +import org.apache.kafka.common.message.DescribeQuorumResponseData; +import org.apache.kafka.common.message.EndQuorumEpochRequestData; +import org.apache.kafka.common.message.EndQuorumEpochResponseData; +import org.apache.kafka.common.message.FetchRequestData; +import org.apache.kafka.common.message.FetchResponseData; +import org.apache.kafka.common.message.LeaderChangeMessage.Voter; +import org.apache.kafka.common.message.LeaderChangeMessage; +import org.apache.kafka.common.message.VoteRequestData; +import org.apache.kafka.common.message.VoteResponseData; +import org.apache.kafka.common.metrics.Metrics; +import org.apache.kafka.common.protocol.ApiKeys; +import org.apache.kafka.common.protocol.ApiMessage; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.record.CompressionType; +import org.apache.kafka.common.record.ControlRecordType; +import org.apache.kafka.common.record.ControlRecordUtils; +import org.apache.kafka.common.record.MemoryRecords; +import org.apache.kafka.common.record.Record; +import org.apache.kafka.common.record.Records; +import org.apache.kafka.common.record.SimpleRecord; +import org.apache.kafka.common.requests.BeginQuorumEpochResponse; +import org.apache.kafka.common.requests.DescribeQuorumResponse; +import org.apache.kafka.common.requests.VoteResponse; +import org.apache.kafka.common.utils.LogContext; +import org.apache.kafka.common.utils.MockTime; +import org.apache.kafka.common.utils.Utils; +import org.apache.kafka.test.TestUtils; +import org.mockito.Mockito; +import static org.apache.kafka.raft.RaftUtil.hasValidTopicPartition; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +final class RaftClientTestContext { + private static final int FETCH_MAX_WAIT_MS = 0; + + static final TopicPartition METADATA_PARTITION = new TopicPartition("metadata", 0); + static final int LOCAL_ID = 0; + + static final int ELECTION_BACKOFF_MAX_MS = 100; + static final int ELECTION_TIMEOUT_MS = 10000; + // fetch timeout is usually larger than election timeout + static final int FETCH_TIMEOUT_MS = 50000; + static final int REQUEST_TIMEOUT_MS = 5000; + static final int RETRY_BACKOFF_MS = 50; + + private final QuorumStateStore quorumStateStore; + private final Random random; + + final KafkaRaftClient client; + final Metrics metrics; + final MockLog log; + final MockNetworkChannel channel; + final MockTime time; + final Set<Integer> voters; + + public static final class Builder { + private final QuorumStateStore quorumStateStore = new MockQuorumStateStore(); + private final Random random = Mockito.spy(new Random(1)); + private final MockLog log = new MockLog(METADATA_PARTITION); + private final Set<Integer> voters; + + Builder(Set<Integer> voters) { + this.voters = voters; + } + + Builder withElectedLeader(int epoch, int leaderId) throws IOException { + quorumStateStore.writeElectionState(ElectionState.withElectedLeader(epoch, leaderId, voters)); + return this; + } + + Builder withUnknownLeader(int epoch) throws IOException { + quorumStateStore.writeElectionState(ElectionState.withUnknownLeader(epoch, voters)); + return this; + } + + Builder withVotedCandidate(int epoch, int votedId) throws IOException { + quorumStateStore.writeElectionState(ElectionState.withVotedCandidate(epoch, votedId, voters)); + return this; + } + + Builder updateRandom(Consumer<Random> consumer) { + consumer.accept(random); + return this; + } + + Builder updateLog(Consumer<MockLog> consumer) { + consumer.accept(log); + return this; + } + + RaftClientTestContext build() throws IOException { + MockTime time = new MockTime(); + Metrics metrics = new Metrics(time); + MockNetworkChannel channel = new MockNetworkChannel(); + LogContext logContext = new LogContext(); + QuorumState quorum = new QuorumState(LOCAL_ID, voters, ELECTION_TIMEOUT_MS, FETCH_TIMEOUT_MS, + quorumStateStore, time, logContext, random); + + Map<Integer, InetSocketAddress> voterAddresses = voters.stream().collect(Collectors.toMap( + Function.identity(), + RaftClientTestContext::mockAddress + )); Review comment: Done. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org