kevin-wu24 commented on code in PR #23136:
URL: https://github.com/apache/kafka/pull/23136#discussion_r3763555180


##########
raft/src/testFixtures/java/org/apache/kafka/raft/RaftClientContextBuilder.java:
##########
@@ -0,0 +1,439 @@
+/*
+ * 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 org.apache.kafka.common.Node;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.config.AbstractConfig;
+import org.apache.kafka.common.memory.MemoryPool;
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.common.record.internal.MemoryRecords;
+import org.apache.kafka.common.utils.MockTime;
+import org.apache.kafka.common.utils.internals.LogContext;
+import org.apache.kafka.raft.internals.StringSerde;
+import org.apache.kafka.server.common.Feature;
+import org.apache.kafka.server.common.KRaftVersion;
+import org.apache.kafka.server.common.OffsetAndEpoch;
+import org.apache.kafka.server.common.serialization.RecordSerde;
+import org.apache.kafka.snapshot.RecordsSnapshotWriter;
+import org.apache.kafka.snapshot.Snapshots;
+
+import org.mockito.Mockito;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static 
org.apache.kafka.raft.SharedRaftClientContext.RaftProtocol.KIP_853_PROTOCOL;
+
+public final class RaftClientContextBuilder {
+    static final int DEFAULT_ELECTION_TIMEOUT_MS = 10000;
+
+    static final RecordSerde<String> SERDE = new StringSerde();
+    static final TopicPartition METADATA_PARTITION = new 
TopicPartition("metadata", 0);
+    static final int ELECTION_BACKOFF_MAX_MS = 100;
+    static final int FETCH_MAX_WAIT_MS = 0;
+    // fetch timeout is usually larger than election timeout
+    static final int FETCH_TIMEOUT_MS = 50000;
+    private static final int DEFAULT_REQUEST_TIMEOUT_MS = 5000;
+    static final int RETRY_BACKOFF_MS = 50;
+    private static final int DEFAULT_APPEND_LINGER_MS = 0;
+
+    private final MockMessageQueue messageQueue = new MockMessageQueue();
+    private final MockTime time = new MockTime();
+    private final MockQuorumStateStore quorumStateStore = new 
MockQuorumStateStore();
+    private final MockableRandom random = new MockableRandom(1L);
+    private final LogContext logContext = new LogContext();
+    private final MockLog log = new MockLog(METADATA_PARTITION, 
Uuid.METADATA_TOPIC_ID, logContext);
+    private final String clusterId = Uuid.randomUuid().toString();
+    private final OptionalInt localId;
+    private KRaftVersion kraftVersion = KRaftVersion.KRAFT_VERSION_0;
+    private final Uuid localDirectoryId;
+
+    private int requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT_MS;
+    private int electionTimeoutMs = DEFAULT_ELECTION_TIMEOUT_MS;
+    private int appendLingerMs = DEFAULT_APPEND_LINGER_MS;
+    private MemoryPool memoryPool = MemoryPool.NONE;
+    private Optional<List<InetSocketAddress>> bootstrapServers = 
Optional.empty();
+    private SharedRaftClientContext.RaftProtocol raftProtocol = 
SharedRaftClientContext.RaftProtocol.KIP_595_PROTOCOL;
+    private boolean canBecomeVoter = false;
+    private VoterSet startingVoters = VoterSet.empty();
+    private Endpoints localListeners = Endpoints.empty();
+    private boolean isStartingVotersStatic = false;
+    private boolean autoJoin = false;
+    private int fetchSnapshotMaxBytes = 
QuorumConfig.DEFAULT_QUORUM_FETCH_SNAPSHOT_MAX_BYTES;
+    private int fetchMaxBytes = QuorumConfig.DEFAULT_QUORUM_FETCH_MAX_BYTES;
+
+    public RaftClientContextBuilder(int localId, Set<Integer> staticVoters) {
+        this(OptionalInt.of(localId), staticVoters);
+    }
+
+    public RaftClientContextBuilder(OptionalInt localId, Set<Integer> 
staticVoters) {
+        this(localId, Uuid.randomUuid());
+
+        withStaticVoters(staticVoters);
+    }
+
+    public RaftClientContextBuilder(int localId, Uuid localDirectoryId) {
+        this(OptionalInt.of(localId), localDirectoryId);
+    }
+
+    public RaftClientContextBuilder(OptionalInt localId, Uuid 
localDirectoryId) {
+        this.localId = localId;
+        this.localDirectoryId = localDirectoryId;
+    }
+
+    RaftClientContextBuilder withElectedLeader(int epoch, int leaderId) {
+        quorumStateStore.writeElectionState(
+            ElectionState.withElectedLeader(epoch, leaderId, Optional.empty(), 
startingVoters.voterIds()),
+            kraftVersion
+        );
+        return this;
+    }
+
+    RaftClientContextBuilder withUnknownLeader(int epoch) {
+        quorumStateStore.writeElectionState(
+            ElectionState.withUnknownLeader(epoch, startingVoters.voterIds()),
+            kraftVersion
+        );
+        return this;
+    }
+
+    RaftClientContextBuilder withVotedCandidate(int epoch, ReplicaKey 
votedKey) {
+        quorumStateStore.writeElectionState(
+            ElectionState.withVotedCandidate(epoch, votedKey, 
startingVoters.voterIds()),
+            kraftVersion
+        );
+        return this;
+    }
+
+    RaftClientContextBuilder updateRandom(Consumer<MockableRandom> consumer) {
+        consumer.accept(random);
+        return this;
+    }
+
+    RaftClientContextBuilder withMemoryPool(MemoryPool pool) {
+        this.memoryPool = pool;
+        return this;
+    }
+
+    RaftClientContextBuilder withAppendLingerMs(int appendLingerMs) {
+        this.appendLingerMs = appendLingerMs;
+        return this;
+    }
+
+    public RaftClientContextBuilder appendToLog(int epoch, List<String> 
records) {
+        MemoryRecords batch = RaftClientTestContext.buildBatch(

Review Comment:
   A bit of a nit, but we should move `buildBatch` to the 
`SharedRaftClientContext`, since the benchmarks will eventually need this 
functionality too to append batches.



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

Reply via email to