anjy7 commented on code in PR #22669: URL: https://github.com/apache/kafka/pull/22669#discussion_r3572559242
########## raft/src/testFixtures/java/org/apache/kafka/raft/RaftClientBenchmarkContext.java: ########## @@ -0,0 +1,277 @@ +/* + * 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.Uuid; +import org.apache.kafka.common.protocol.ApiKeys; +import org.apache.kafka.raft.RaftClientTestContext.RaftProtocol; +import org.apache.kafka.server.common.KRaftVersion; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public final class RaftClientBenchmarkContext { + // Standardized JMH iteration counts, shared by all raft benchmarks so every benchmark of a given + // mode is configured identically. + + // SingleShotTime measures a single operation per iteration, so it needs many iterations to build + // a stable distribution. + public static final int SINGLE_SHOT_WARMUP_ITERATIONS = 50; + public static final int SINGLE_SHOT_MEASUREMENT_ITERATIONS = 30; + public static final int SINGLE_SHOT_FORKS = 5; + + // AverageTime averages many operations within each timed iteration, so it needs fewer. + public static final int AVERAGE_TIME_WARMUP_ITERATIONS = 5; + public static final int AVERAGE_TIME_MEASUREMENT_ITERATIONS = 10; + public static final int AVERAGE_TIME_FORKS = 3; + + // Default to the newest version of each (the highest-ordinal enum constant). Enum natural order + // is ordinal order, so this picks the last-declared constant; relies on the constants being + // declared oldest-to-newest, which avoids updating these when a new version is added. + public static final KRaftVersion DEFAULT_KRAFT_VERSION = + Arrays.stream(KRaftVersion.values()).max(Comparator.naturalOrder()).orElseThrow(); + public static final RaftProtocol DEFAULT_RAFT_PROTOCOL = + Arrays.stream(RaftProtocol.values()).max(Comparator.naturalOrder()).orElseThrow(); + + private final RaftClientTestContext context; + private final MockLog log; + private final MockNetworkChannel channel; + + private final ReplicaKey localKey; + private final List<ReplicaKey> startingVoters; + private final List<ReplicaKey> startingObservers; + + // Each tracks one cumulative mock counter as a drainable delta against a baseline. The baseline is + // snapshotted at construction and re-baselined by zeroCountersOnSetup() at the end of benchmark setup. + private final DrainableCounter logFlushes; + private final DrainableCounter logReads; + private final DrainableCounter logTruncations; + private final DrainableCounter rpcRequestsSent; + private final DrainableCounter quorumStateWrites; + private final DrainableCounter quorumStateReads; + + private RaftClientBenchmarkContext( + RaftClientTestContext context, + ReplicaKey localKey, + List<ReplicaKey> startingVoters, + List<ReplicaKey> startingObservers + ) { + this.context = context; + this.log = context.log; + this.channel = context.channel; + this.localKey = localKey; + this.startingVoters = List.copyOf(startingVoters); + this.startingObservers = List.copyOf(startingObservers); + this.logFlushes = new DrainableCounter(log::flushCount); + this.logReads = new DrainableCounter(log::readCount); + this.logTruncations = new DrainableCounter(log::truncationCount); + this.rpcRequestsSent = new DrainableCounter(channel::requestsSent); + this.quorumStateWrites = new DrainableCounter(context::quorumStateWriteCount); + this.quorumStateReads = new DrainableCounter(context::quorumStateReadCount); + } + + /** + * Builds the local node as a voter in the Unattached state (a voter with no leader yet) in a + * {@code voterCount}-node cluster. The local node is a voter because only a voter can drive the + * measured operation {@link RaftClientTestContext#unattachedToLeader()} (on {@link #testContext()}) + * — an Unattached → Leader election, a path observers cannot take. A single-voter cluster is + * rejected because such a node elects itself at initialization, before any measured poll. + */ + public static RaftClientBenchmarkContext unattachedVoter(int voterCount) throws Exception { + return unattachedVoter(voterCount, DEFAULT_KRAFT_VERSION, DEFAULT_RAFT_PROTOCOL); + } + + public static RaftClientBenchmarkContext unattachedVoter( + int voterCount, + KRaftVersion kraftVersion, + RaftProtocol raftProtocol + ) throws Exception { + if (voterCount < 2) { + throw new IllegalArgumentException("voterCount must be at least 2; a single voter self-elects at init"); + } + List<ReplicaKey> voterKeys = replicaKeys(randomReplicaId(), voterCount); + ReplicaKey local = voterKeys.get(0); + return new RaftClientBenchmarkContext( + buildContext(local, voterKeys, kraftVersion, raftProtocol), local, voterKeys, List.of()); + } + + public static RaftClientBenchmarkContext leader(int voterCount) throws Exception { + return leader(voterCount, 0, DEFAULT_KRAFT_VERSION, DEFAULT_RAFT_PROTOCOL); + } + + public static RaftClientBenchmarkContext leader(int voterCount, int observerCount) throws Exception { + return leader(voterCount, observerCount, DEFAULT_KRAFT_VERSION, DEFAULT_RAFT_PROTOCOL); + } + + /** + * Builds a leader in a cluster of {@code voterCount} voters and {@code observerCount} observers. + */ + public static RaftClientBenchmarkContext leader( + int voterCount, + int observerCount, + KRaftVersion kraftVersion, + RaftProtocol raftProtocol + ) throws Exception { Review Comment: I dont think that would be right here because `context.unattachedToLeader()` throws `Exception` -- 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]
