-
Notifications
You must be signed in to change notification settings - Fork 15.3k
KAFKA-19782: Improve perfomance Authorizer.authorizeByResourceType by using Prefix Trie #20912
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
Open
ekuvardin
wants to merge
19
commits into
apache:trunk
Choose a base branch
from
ekuvardin:KAFKA-19782
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
58894b1
KAFKA-19782 Add Patricia Trie to Authorizer
ekuvardin 281156d
KAFKA-19782 Add Patricia Trie to Authorizer
ekuvardin 718170e
KAFKA-19782 Add Patricia Trie to Authorizer
ekuvardin d65416e
KAFKA-19782 Add Patricia Trie to Authorizer
ekuvardin fbaa130
Merge remote-tracking branch 'origin/KAFKA-19782' into KAFKA-19782
ekuvardin fa35e6c
KAFKA-19782 Add Patricia Trie to Authorizer
ekuvardin d22a32f
Fix errors with finding prefix
ekuvardin b5b4f52
Add tests for PatriciaTrie
ekuvardin 18a6667
Add tests for PatriciaTrie
ekuvardin 582d7aa
Merge remote-tracking branch 'origin/KAFKA-19782' into KAFKA-19782
ekuvardin 0aa38eb
Merge remote-tracking branch 'origin/trunk' into KAFKA-19782
ekuvardin 4365817
Merge branch 'apache:trunk' into KAFKA-19782
ekuvardin e56b477
Fix checkstyle errors
ekuvardin 88079c1
Merge branch 'apache:trunk' into KAFKA-19782
ekuvardin c4342d7
Merge branch 'apache:trunk' into KAFKA-19782
ekuvardin e7024a3
Merge remote-tracking branch 'origin/trunk' into KAFKA-19782
ekuvardin 66a63e7
Fix checkstyle errors
ekuvardin a246f93
Merge remote-tracking branch 'origin/trunk' into KAFKA-19782
ekuvardin 2109a6d
Move test
ekuvardin 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
Empty file.
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
210 changes: 210 additions & 0 deletions
210
jmh-benchmarks/src/main/java/org/apache/kafka/jmh/acl/AuthorizeByResourceTypeSearch.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,210 @@ | ||
| /* | ||
| * 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.acl; | ||
|
|
||
| import org.apache.kafka.common.Uuid; | ||
| import org.apache.kafka.common.acl.*; | ||
| import org.apache.kafka.common.metrics.Metrics; | ||
| import org.apache.kafka.common.metrics.internals.PluginMetricsImpl; | ||
| import org.apache.kafka.common.network.ClientInformation; | ||
| import org.apache.kafka.common.network.ListenerName; | ||
| import org.apache.kafka.common.protocol.ApiKeys; | ||
| import org.apache.kafka.common.requests.RequestContext; | ||
| import org.apache.kafka.common.requests.RequestHeader; | ||
| import org.apache.kafka.common.resource.PatternType; | ||
| import org.apache.kafka.common.resource.ResourcePattern; | ||
| import org.apache.kafka.common.resource.ResourceType; | ||
| import org.apache.kafka.common.security.auth.KafkaPrincipal; | ||
| import org.apache.kafka.common.security.auth.SecurityProtocol; | ||
| import org.apache.kafka.metadata.authorizer.StandardAcl; | ||
| import org.apache.kafka.metadata.authorizer.StandardAuthorizer; | ||
| import org.apache.kafka.server.authorizer.AuthorizationResult; | ||
|
|
||
| import org.openjdk.jmh.annotations.*; | ||
| import org.openjdk.jmh.runner.Runner; | ||
| import org.openjdk.jmh.runner.RunnerException; | ||
| import org.openjdk.jmh.runner.options.Options; | ||
| import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
| import org.openjdk.jmh.runner.options.TimeValue; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.InetAddress; | ||
| import java.util.*; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * Benchmark (resourceNamePrefix) Mode Cnt Score Error Units | ||
| * AuthorizeByResourceTypeSearch.testAuthorizeByResourceType AuthorizeByResourceTypeSearch- avgt 7 4.252 ± 0.024 us/op | ||
| * AuthorizeByResourceTypeSearch.testAuthorizeByResourceType Authorize...Check- avgt 7 4.301 ± 0.030 us/op | ||
| * AuthorizeByResourceTypeSearch.testAuthorizeByResourceType Authorize...Difference- avgt 7 4.592 ± 0.042 us/op | ||
| * <p> | ||
| * | ||
| */ | ||
| @State(Scope.Benchmark) | ||
| @Fork(value = 1) | ||
| @Warmup(iterations = 5) | ||
| @Measurement(iterations = 15) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
| public class AuthorizeByResourceTypeSearch { | ||
|
|
||
| @Param({ | ||
| "AuthorizeByResourceTypeSearch-", | ||
| "AuthorizeByResourceTypeSearchOneMoreWordLongForDenyPatternCheck-", | ||
| "AuthorizeByResourceTypeSearchOneMoreWordLongForDenyPatternCheckAndWeAddOneMoreWordJustInCaseToShowBenchmarkDifference-", | ||
| }) | ||
| String resourceNamePrefix; | ||
|
|
||
| @Param({"4","10"}) | ||
| int typeOfPrefixedAndLiteralPattern; | ||
|
|
||
| private KafkaPrincipal principal = new KafkaPrincipal(KafkaPrincipal.USER_TYPE, "test-user"); | ||
| private String authorizeByResourceTypeHostName = "127.0.0.2"; | ||
| private StandardAuthorizer authorizer; | ||
| private RequestContext authorizeByResourceTypeContext; | ||
| private AclBindingFilter filter; | ||
| private AclOperation op; | ||
| private ResourceType resourceType; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() throws Exception { | ||
| authorizer = new StandardAuthorizer(); | ||
| filter = AclBindingFilter.ANY; | ||
| op = AclOperation.READ; | ||
| resourceType = ResourceType.TOPIC; | ||
| prepareAclCache(); | ||
| authorizeByResourceTypeContext = new RequestContext(new RequestHeader(ApiKeys.PRODUCE, Integer.valueOf(1).shortValue(), | ||
| "someclient", 1), "1", InetAddress.getByName(authorizeByResourceTypeHostName), principal, | ||
| ListenerName.normalised("listener"), SecurityProtocol.PLAINTEXT, ClientInformation.EMPTY, false); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * What we do in this test: | ||
| * <p> | ||
| * For every Allow Literal Pattern -- iterate on count of typeOfPrefixedAndLiteralPattern | ||
| * check in denyPatternsLiteral map -- check on exists then no found | ||
| * check in denyPatternsPrefixed -- find in denyPatternsPrefixed prefix | ||
| * return if allow (in this test we don't find) | ||
| * <p> | ||
| * for every Allow Prefix pattern | ||
| * check in denyPatternsPrefixed -- find in denyPatternsPrefixed prefix | ||
| * return allow | ||
| * <p> | ||
| * return deny - never reach | ||
| * <p> | ||
| * We iterate | ||
| * typeOfPrefixedAndLiteralPattern + 1 counts. | ||
| * Make (typeOfPrefixedAndLiteralPattern + 1) * 2 search in PatriciaTrie | ||
| */ | ||
| private void prepareAclCache() { | ||
| Map<ResourcePattern, Set<AccessControlEntry>> aclEntries = new HashMap<>(); | ||
|
|
||
| String prefix = "a"; | ||
|
|
||
| List<String> patterns = new ArrayList<>(); | ||
| for(int i = 0; i< typeOfPrefixedAndLiteralPattern; i++) { | ||
| patterns.add(resourceNamePrefix + prefix.repeat(i+1)); | ||
| } | ||
|
|
||
| String allowPattern = resourceNamePrefix; | ||
|
|
||
| for(String pattern : patterns) { | ||
| // PREFIX DENY | ||
| makeDeny(pattern, aclEntries, PatternType.PREFIXED); | ||
| // ALLOW LITERAL | ||
| makeAllow(pattern, aclEntries, PatternType.LITERAL); | ||
| } | ||
|
|
||
| // Add one Allow | ||
| makeAllow(allowPattern, aclEntries, PatternType.PREFIXED); | ||
|
|
||
|
|
||
| // makeDeny(denyPattern1, aclEntries); | ||
| setupAcls(aclEntries); | ||
| } | ||
|
|
||
| private void makeDeny(String denyPattern, Map<ResourcePattern, Set<AccessControlEntry>> aclEntries, PatternType patternType) { | ||
| ResourcePattern resource = new ResourcePattern(ResourceType.TOPIC, denyPattern, | ||
| patternType); | ||
|
|
||
| Set<AccessControlEntry> entries = aclEntries.computeIfAbsent(resource, k -> new HashSet<>()); | ||
|
|
||
| AccessControlEntry denyAce = new AccessControlEntry(principal.toString(), authorizeByResourceTypeHostName, | ||
| AclOperation.READ, AclPermissionType.DENY); | ||
|
|
||
| entries.add(denyAce); | ||
| } | ||
|
|
||
| private void makeAllow(String denyPattern, Map<ResourcePattern, Set<AccessControlEntry>> aclEntries, PatternType patternType) { | ||
| ResourcePattern resourceAllow = new ResourcePattern(ResourceType.TOPIC, denyPattern, | ||
| patternType); | ||
|
|
||
| Set<AccessControlEntry> entriesAllow = aclEntries.computeIfAbsent(resourceAllow, k -> new HashSet<>()); | ||
|
|
||
| AccessControlEntry allowAce = new AccessControlEntry(principal.toString(), authorizeByResourceTypeHostName, | ||
| AclOperation.READ, AclPermissionType.ALLOW); | ||
|
|
||
| entriesAllow.add(allowAce); | ||
| } | ||
|
|
||
| private void setupAcls(Map<ResourcePattern, Set<AccessControlEntry>> aclEntries) { | ||
| for (Map.Entry<ResourcePattern, Set<AccessControlEntry>> entryMap : aclEntries.entrySet()) { | ||
| ResourcePattern resourcePattern = entryMap.getKey(); | ||
|
|
||
| for (AccessControlEntry accessControlEntry : entryMap.getValue()) { | ||
| StandardAcl standardAcl = StandardAcl.fromAclBinding(new AclBinding(resourcePattern, accessControlEntry)); | ||
| authorizer.addAcl(Uuid.randomUuid(), standardAcl); | ||
| } | ||
| authorizer.completeInitialLoad(); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| @Setup(Level.Iteration) | ||
| public void setupIteration() { | ||
| authorizer.withPluginMetrics(new PluginMetricsImpl(new Metrics(), new HashMap<>(1000000))); | ||
| } | ||
|
|
||
| @TearDown(Level.Trial) | ||
| public void tearDown() throws IOException { | ||
| authorizer.close(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public AuthorizationResult testAuthorizeByResourceType() { | ||
| return authorizer.authorizeByResourceType(authorizeByResourceTypeContext, op, resourceType); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| Options opt = new OptionsBuilder() | ||
| .include(AuthorizeByResourceTypeSearch.class.getSimpleName()) | ||
| .warmupIterations(7) | ||
| .warmupTime(TimeValue.seconds(1)) | ||
| .measurementIterations(7) | ||
| .measurementTime(TimeValue.seconds(1)) | ||
| .timeUnit(TimeUnit.MICROSECONDS) | ||
| .forks(1) | ||
| .build(); | ||
| try { | ||
| new Runner(opt).run(); | ||
| } catch (RunnerException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } |
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.
not 100% sure introducing a new client dependency is a good idea
Uh oh!
There was an error while loading. Please reload this page.
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.
I tried to find a PrefixTrie or PatriciaTrie in the libraries we already use, but without success.
I don’t consider writing our own implementation to be a good idea.
What makes the situation even worse is that we already have a version of commons-collections-3.2.2 used somewhere deep in the codebase, but it is not compatible with the newer commons-collections4-4.5.0 (they even split it into a separate “4” module specifically because of this).
If you have any other ideas, please drop me a message in my profile email. I will arrange meeting
Uh oh!
There was an error while loading. Please reload this page.
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.
@Nikita-Shupletsov Shall we discuss this dependency with a wider group?