-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-14735: Improve KRaft metadata image change performance at high … #13280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
7afe1e3
KAFKA-14735: Improve KRaft metadata image change performance at high …
rondagostino f195c20
Use vavr instead of Paguro
rondagostino d08b81b
Use PCollections instead of vavr
rondagostino 6ef59b3
Add wrapper classes
rondagostino d6c76c6
Merge remote-tracking branch 'apache/trunk' into KAFKA-14735
rondagostino a6887e2
cleanups
rondagostino 634a9e7
more cleanups
rondagostino 9140e66
Fix license check failure
rondagostino 009dd63
Merge remote-tracking branch 'apache/trunk' into KAFKA-14735
rondagostino 25947d8
Test PCollectionsHashSetWrapper.hashCode(), equals(), and toString()
rondagostino 849fd66
Test PCollectionsHashSetWrapper.forEach()
rondagostino baa60d6
Better clarity on testing
rondagostino a61408e
cleanup
rondagostino 7839943
Wrapper classes implement standard java interfaces
rondagostino c4667a6
Rename package pcoll to server.immutable
rondagostino 19ad442
More fine-grained import control
rondagostino c2e1467
Rename classes
rondagostino b711e2a
Rename methods
rondagostino 8b2a177
Merge remote-tracking branch 'apache/trunk' into KAFKA-14735
rondagostino 6b24920
Make underlying() non-public
rondagostino 79078d7
Eliminate factory in favor of static methods on interfaces
rondagostino 323e771
Merge remote-tracking branch 'apache/trunk' into KAFKA-14735
rondagostino 2515cab
Eliminate direct field access
rondagostino 3d4f03e
Merge remote-tracking branch 'apache/trunk' into KAFKA-14735
rondagostino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
235 changes: 235 additions & 0 deletions
235
...benchmarks/src/main/java/org/apache/kafka/jmh/metadata/KRaftMetadataRequestBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| /* | ||
| * 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.jmh.metadata; | ||
|
|
||
| import kafka.coordinator.transaction.TransactionCoordinator; | ||
| import kafka.network.RequestChannel; | ||
| import kafka.network.RequestConvertToJson; | ||
| import kafka.server.AutoTopicCreationManager; | ||
| import kafka.server.BrokerTopicStats; | ||
| import kafka.server.ClientQuotaManager; | ||
| import kafka.server.ClientRequestQuotaManager; | ||
| import kafka.server.ControllerMutationQuotaManager; | ||
| import kafka.server.FetchManager; | ||
| import kafka.server.ForwardingManager; | ||
| import kafka.server.KafkaApis; | ||
| import kafka.server.KafkaConfig; | ||
| import kafka.server.KafkaConfig$; | ||
| import kafka.server.MetadataCache; | ||
| import kafka.server.QuotaFactory; | ||
| import kafka.server.RaftSupport; | ||
| import kafka.server.ReplicaManager; | ||
| import kafka.server.ReplicationQuotaManager; | ||
| import kafka.server.SimpleApiVersionManager; | ||
| import kafka.server.builders.KafkaApisBuilder; | ||
| import kafka.server.metadata.KRaftMetadataCache; | ||
| import kafka.server.metadata.MockConfigRepository; | ||
| import org.apache.kafka.common.Uuid; | ||
| import org.apache.kafka.common.memory.MemoryPool; | ||
| import org.apache.kafka.common.message.ApiMessageType; | ||
| import org.apache.kafka.common.message.UpdateMetadataRequestData.UpdateMetadataEndpoint; | ||
| import org.apache.kafka.common.metadata.PartitionRecord; | ||
| import org.apache.kafka.common.metadata.RegisterBrokerRecord; | ||
| import org.apache.kafka.common.metadata.TopicRecord; | ||
| import org.apache.kafka.common.metrics.Metrics; | ||
| import org.apache.kafka.common.network.ClientInformation; | ||
| import org.apache.kafka.common.network.ListenerName; | ||
| import org.apache.kafka.common.requests.MetadataRequest; | ||
| import org.apache.kafka.common.requests.RequestContext; | ||
| import org.apache.kafka.common.requests.RequestHeader; | ||
| import org.apache.kafka.common.security.auth.KafkaPrincipal; | ||
| import org.apache.kafka.common.security.auth.SecurityProtocol; | ||
| import org.apache.kafka.common.utils.Time; | ||
| import org.apache.kafka.coordinator.group.GroupCoordinator; | ||
| import org.apache.kafka.image.MetadataDelta; | ||
| import org.apache.kafka.image.MetadataImage; | ||
| import org.apache.kafka.image.MetadataProvenance; | ||
| import org.mockito.Mockito; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.TearDown; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import scala.Option; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Properties; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| @State(Scope.Benchmark) | ||
| @Fork(value = 1) | ||
| @Warmup(iterations = 5) | ||
| @Measurement(iterations = 15) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
|
|
||
| public class KRaftMetadataRequestBenchmark { | ||
| @Param({"500", "1000", "5000"}) | ||
| private int topicCount; | ||
| @Param({"10", "20", "50"}) | ||
| private int partitionCount; | ||
|
|
||
| private RequestChannel requestChannel = Mockito.mock(RequestChannel.class, Mockito.withSettings().stubOnly()); | ||
| private RequestChannel.Metrics requestChannelMetrics = Mockito.mock(RequestChannel.Metrics.class); | ||
| private ReplicaManager replicaManager = Mockito.mock(ReplicaManager.class); | ||
| private GroupCoordinator groupCoordinator = Mockito.mock(GroupCoordinator.class); | ||
| private TransactionCoordinator transactionCoordinator = Mockito.mock(TransactionCoordinator.class); | ||
| private AutoTopicCreationManager autoTopicCreationManager = Mockito.mock(AutoTopicCreationManager.class); | ||
| private Metrics metrics = new Metrics(); | ||
| private int brokerId = 1; | ||
| private ForwardingManager forwardingManager = Mockito.mock(ForwardingManager.class); | ||
| private KRaftMetadataCache metadataCache = MetadataCache.kRaftMetadataCache(brokerId); | ||
| private ClientQuotaManager clientQuotaManager = Mockito.mock(ClientQuotaManager.class); | ||
| private ClientRequestQuotaManager clientRequestQuotaManager = Mockito.mock(ClientRequestQuotaManager.class); | ||
| private ControllerMutationQuotaManager controllerMutationQuotaManager = Mockito.mock(ControllerMutationQuotaManager.class); | ||
| private ReplicationQuotaManager replicaQuotaManager = Mockito.mock(ReplicationQuotaManager.class); | ||
| private QuotaFactory.QuotaManagers quotaManagers = new QuotaFactory.QuotaManagers(clientQuotaManager, | ||
| clientQuotaManager, clientRequestQuotaManager, controllerMutationQuotaManager, replicaQuotaManager, | ||
| replicaQuotaManager, replicaQuotaManager, Option.empty()); | ||
| private FetchManager fetchManager = Mockito.mock(FetchManager.class); | ||
| private BrokerTopicStats brokerTopicStats = new BrokerTopicStats(); | ||
| private KafkaPrincipal principal = new KafkaPrincipal(KafkaPrincipal.USER_TYPE, "test-user"); | ||
| private KafkaApis kafkaApis; | ||
| private RequestChannel.Request allTopicMetadataRequest; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() { | ||
| initializeMetadataCache(); | ||
| kafkaApis = createKafkaApis(); | ||
| allTopicMetadataRequest = buildAllTopicMetadataRequest(); | ||
| } | ||
|
|
||
| private void initializeMetadataCache() { | ||
| MetadataDelta buildupMetadataDelta = new MetadataDelta(MetadataImage.EMPTY); | ||
| IntStream.range(0, 5).forEach(brokerId -> { | ||
| RegisterBrokerRecord.BrokerEndpointCollection endpoints = new RegisterBrokerRecord.BrokerEndpointCollection(); | ||
| endpoints(brokerId).forEach(endpoint -> | ||
| endpoints.add(new RegisterBrokerRecord.BrokerEndpoint(). | ||
| setHost(endpoint.host()). | ||
| setPort(endpoint.port()). | ||
| setName(endpoint.listener()). | ||
| setSecurityProtocol(endpoint.securityProtocol()))); | ||
| buildupMetadataDelta.replay(new RegisterBrokerRecord(). | ||
| setBrokerId(brokerId). | ||
| setBrokerEpoch(100L). | ||
| setFenced(false). | ||
| setRack(null). | ||
| setEndPoints(endpoints). | ||
| setIncarnationId(Uuid.fromString(Uuid.randomUuid().toString()))); | ||
| }); | ||
| IntStream.range(0, topicCount).forEach(topicNum -> { | ||
| Uuid topicId = Uuid.randomUuid(); | ||
| buildupMetadataDelta.replay(new TopicRecord().setName("topic-" + topicNum).setTopicId(topicId)); | ||
| IntStream.range(0, partitionCount).forEach(partitionId -> | ||
| buildupMetadataDelta.replay(new PartitionRecord(). | ||
| setPartitionId(partitionId). | ||
| setTopicId(topicId). | ||
| setReplicas(Arrays.asList(0, 1, 3)). | ||
| setIsr(Arrays.asList(0, 1, 3)). | ||
| setRemovingReplicas(Collections.emptyList()). | ||
| setAddingReplicas(Collections.emptyList()). | ||
| setLeader(partitionCount % 5). | ||
| setLeaderEpoch(0))); | ||
| }); | ||
| metadataCache.setImage(buildupMetadataDelta.apply(MetadataProvenance.EMPTY)); | ||
| } | ||
|
|
||
| private List<UpdateMetadataEndpoint> endpoints(final int brokerId) { | ||
| return Collections.singletonList( | ||
| new UpdateMetadataEndpoint() | ||
| .setHost("host_" + brokerId) | ||
| .setPort(9092) | ||
| .setSecurityProtocol(SecurityProtocol.PLAINTEXT.id) | ||
| .setListener(ListenerName.forSecurityProtocol(SecurityProtocol.PLAINTEXT).value())); | ||
| } | ||
|
|
||
| private KafkaApis createKafkaApis() { | ||
| Properties kafkaProps = new Properties(); | ||
| kafkaProps.put(KafkaConfig$.MODULE$.NodeIdProp(), brokerId + ""); | ||
| kafkaProps.put(KafkaConfig$.MODULE$.ProcessRolesProp(), "broker"); | ||
| kafkaProps.put(KafkaConfig$.MODULE$.QuorumVotersProp(), "9000@foo:8092"); | ||
| kafkaProps.put(KafkaConfig$.MODULE$.ControllerListenerNamesProp(), "CONTROLLER"); | ||
| KafkaConfig config = new KafkaConfig(kafkaProps); | ||
| return new KafkaApisBuilder(). | ||
| setRequestChannel(requestChannel). | ||
| setMetadataSupport(new RaftSupport(forwardingManager, metadataCache)). | ||
| setReplicaManager(replicaManager). | ||
| setGroupCoordinator(groupCoordinator). | ||
| setTxnCoordinator(transactionCoordinator). | ||
| setAutoTopicCreationManager(autoTopicCreationManager). | ||
| setBrokerId(brokerId). | ||
| setConfig(config). | ||
| setConfigRepository(new MockConfigRepository()). | ||
| setMetadataCache(metadataCache). | ||
| setMetrics(metrics). | ||
| setAuthorizer(Optional.empty()). | ||
| setQuotas(quotaManagers). | ||
| setFetchManager(fetchManager). | ||
| setBrokerTopicStats(brokerTopicStats). | ||
| setClusterId("clusterId"). | ||
| setTime(Time.SYSTEM). | ||
| setTokenManager(null). | ||
| setApiVersionManager(new SimpleApiVersionManager(ApiMessageType.ListenerType.BROKER, false)). | ||
| build(); | ||
| } | ||
|
|
||
| @TearDown(Level.Trial) | ||
| public void tearDown() { | ||
| kafkaApis.close(); | ||
| metrics.close(); | ||
| } | ||
|
|
||
| private RequestChannel.Request buildAllTopicMetadataRequest() { | ||
| MetadataRequest metadataRequest = MetadataRequest.Builder.allTopics().build(); | ||
| RequestHeader header = new RequestHeader(metadataRequest.apiKey(), metadataRequest.version(), "", 0); | ||
| ByteBuffer bodyBuffer = metadataRequest.serialize(); | ||
|
|
||
| RequestContext context = new RequestContext(header, "1", null, principal, | ||
| ListenerName.forSecurityProtocol(SecurityProtocol.PLAINTEXT), | ||
| SecurityProtocol.PLAINTEXT, ClientInformation.EMPTY, false); | ||
| return new RequestChannel.Request(1, context, 0, MemoryPool.NONE, bodyBuffer, requestChannelMetrics, Option.empty()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void testMetadataRequestForAllTopics() { | ||
| kafkaApis.handleTopicMetadataRequest(allTopicMetadataRequest); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public String testRequestToJson() { | ||
| return RequestConvertToJson.requestDesc(allTopicMetadataRequest.header(), allTopicMetadataRequest.requestLog(), allTopicMetadataRequest.isForwarded()).toString(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void testTopicIdInfo() { | ||
| metadataCache.topicIdInfo(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, "anyone" on the server side :) I don't think we want the kafka client to take this dependency. At least not yet, until we have a very clear use-case in mind.
So with that in mind, sadly it might be better to add this "allow" to server-common, metadata, and core individually, rather than up here. (although that's slightly more work I realize)