Skip to content

ARTEMIS-6042 Improve ArtemisMBeanServerGuard.canInvoke implementation#6403

Draft
GChuf wants to merge 12 commits into
apache:mainfrom
GChuf:ARTEMIS-6042
Draft

ARTEMIS-6042 Improve ArtemisMBeanServerGuard.canInvoke implementation#6403
GChuf wants to merge 12 commits into
apache:mainfrom
GChuf:ARTEMIS-6042

Conversation

@GChuf

@GChuf GChuf commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@GChuf GChuf marked this pull request as draft April 30, 2026 10:21
@jbertram

jbertram commented May 4, 2026

Copy link
Copy Markdown
Contributor

I had Claude create a JMH test for this and the results look promising, especially when operationName is null.

/*
 * 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.activemq.artemis.tests.performance.jmh;

import org.apache.activemq.artemis.core.server.management.ArtemisMBeanServerGuard;
import org.apache.activemq.artemis.core.server.management.JMXAccessControlList;
import org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
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.Warmup;

import javax.security.auth.Subject;
import java.security.PrivilegedAction;
import java.util.concurrent.ThreadLocalRandom;

@State(Scope.Benchmark)
@Fork(2)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 8, time = 1)
@BenchmarkMode(Mode.Throughput)
public class MBeanServerGuardCanInvokePerfTest {

   private ArtemisMBeanServerGuard guard;
   private String[] objectNames;
   private String[] operationNames;
   private String[] operationNamesWithParams;
   private String allowListedObjectName;
   private Subject subjectWithViewRole;
   private Subject subjectWithAmqRole;
   private Subject subjectWithNoRole;

   @Param({"10", "50", "100"})
   int objectNameCount;

   @Setup
   public void init() {
      guard = new ArtemisMBeanServerGuard();
      JMXAccessControlList acl = JMXAccessControlList.createDefaultList();
      guard.setJMXAccessControlList(acl);

      // Create test object names from different domains
      objectNames = new String[objectNameCount];
      for (int i = 0; i < objectNameCount; i++) {
         if (i % 3 == 0) {
            objectNames[i] = "org.apache.activemq.artemis:broker=\"0.0.0.0\",component=addresses,address=\"address." + i + "\"";
         } else if (i % 3 == 1) {
            objectNames[i] = "org.apache.activemq.artemis:broker=\"0.0.0.0\",component=addresses,subcomponent=queues,routing-type=ANYCAST,name=\"queue-" + i + "\"";
         } else {
            objectNames[i] = "java.lang:type=Memory,name=HeapMemoryUsage-" + i;
         }
      }

      // Allowlisted object name (hawtio domain is in the default allowlist)
      allowListedObjectName = "hawtio:type=security,name=RBACRegistry";

      // Common operation names
      operationNames = new String[]{
         "getMessageCount",
         "listMessages",
         "sendMessage",
         "removeMessage",
         "getConsumerCount",
         "listConsumers",
         "getAttribute",
         "setAttribute",
         "invoke"
      };

      // Operation names with parameter lists
      operationNamesWithParams = new String[]{
         "sendMessage(java.lang.String)",
         "sendMessage(java.lang.String,java.lang.String)",
         "removeMessage(long)",
         "listMessages(java.lang.String)",
         "setAttribute(java.lang.String,java.lang.Object)"
      };

      // Set up test subjects with different roles
      subjectWithViewRole = new Subject();
      subjectWithViewRole.getPrincipals().add(new RolePrincipal("view"));

      subjectWithAmqRole = new Subject();
      subjectWithAmqRole.getPrincipals().add(new RolePrincipal("amq"));

      subjectWithNoRole = new Subject();
   }

   @Benchmark
   public boolean testCanInvokeAllowListed() {
      return guard.canInvoke(allowListedObjectName, "anyOperation");
   }

   @Benchmark
   public boolean testCanInvokeWithViewRole() {
      return Subject.doAs(subjectWithViewRole, (PrivilegedAction<Boolean>) () -> {
         int idx = ThreadLocalRandom.current().nextInt(objectNames.length);
         int opIdx = ThreadLocalRandom.current().nextInt(operationNames.length);
         return guard.canInvoke(objectNames[idx], operationNames[opIdx]);
      });
   }

   @Benchmark
   public boolean testCanInvokeWithAmqRole() {
      return Subject.doAs(subjectWithAmqRole, (PrivilegedAction<Boolean>) () -> {
         int idx = ThreadLocalRandom.current().nextInt(objectNames.length);
         int opIdx = ThreadLocalRandom.current().nextInt(operationNames.length);
         return guard.canInvoke(objectNames[idx], operationNames[opIdx]);
      });
   }

   @Benchmark
   public boolean testCanInvokeWithNoRole() {
      return Subject.doAs(subjectWithNoRole, (PrivilegedAction<Boolean>) () -> {
         int idx = ThreadLocalRandom.current().nextInt(objectNames.length);
         int opIdx = ThreadLocalRandom.current().nextInt(operationNames.length);
         return guard.canInvoke(objectNames[idx], operationNames[opIdx]);
      });
   }

   @Benchmark
   public boolean testCanInvokeWithParameterStripping() {
      return Subject.doAs(subjectWithViewRole, (PrivilegedAction<Boolean>) () -> {
         int idx = ThreadLocalRandom.current().nextInt(objectNames.length);
         int opIdx = ThreadLocalRandom.current().nextInt(operationNamesWithParams.length);
         return guard.canInvoke(objectNames[idx], operationNamesWithParams[opIdx]);
      });
   }

   @Benchmark
   public boolean testCanInvokeNullOperation() {
      return guard.canInvoke(objectNames[0], null);
   }

   @Benchmark
   public boolean testCanInvokeInvalidObjectName() {
      return guard.canInvoke("invalid:object:name:with:too:many:colons", "operation");
   }

   @Benchmark
   public boolean testCanInvokeMixedScenarios() {
      return Subject.doAs(subjectWithViewRole, (PrivilegedAction<Boolean>) () -> {
         int scenario = ThreadLocalRandom.current().nextInt(4);
         switch (scenario) {
            case 0:
               return guard.canInvoke(allowListedObjectName, "operation");
            case 1:
               int idx = ThreadLocalRandom.current().nextInt(objectNames.length);
               int opIdx = ThreadLocalRandom.current().nextInt(operationNames.length);
               return guard.canInvoke(objectNames[idx], operationNames[opIdx]);
            case 2:
               idx = ThreadLocalRandom.current().nextInt(objectNames.length);
               opIdx = ThreadLocalRandom.current().nextInt(operationNamesWithParams.length);
               return guard.canInvoke(objectNames[idx], operationNamesWithParams[opIdx]);
            default:
               return guard.canInvoke(objectNames[0], null);
         }
      });
   }
}

@GChuf

GChuf commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

@jbertram thanks for the test you provided - I finally ran it yesterday (i didn't realise more than a month has passed) and the results seemed a bit strange to me - I expected to see bigger improvements. So I explained a couple of things to claude and after a couple of tries I got another test out of it, which should be closer to the "console scenario". In my case, though, some improvements can be seen in the tests you provided as well.

Results:

JBertram's tests

main
Benchmark                                                         (objectNameCount)   Mode  Cnt        Score       Error  Units
MBeanServerGuardCanInvokePerfTest.testCanInvokeAllowListed                       10  thrpt   16  1452471.937 ± 22805.579  ops/s
MBeanServerGuardCanInvokePerfTest.testCanInvokeAllowListed                       50  thrpt   16  1472493.395 ± 28510.019  ops/s
MBeanServerGuardCanInvokePerfTest.testCanInvokeAllowListed                      100  thrpt   16  1447981.249 ± 34360.371  ops/s
MBeanServerGuardCanInvokePerfTest.testCanInvokeInvalidObjectName                 10  thrpt   16   604590.702 ± 16191.796  ops/s
MBeanServerGuardCanInvokePerfTest.testCanInvokeInvalidObjectName                 50  thrpt   16   610694.673 ±  7113.871  ops/s
MBeanServerGuardCanInvokePerfTest.testCanInvokeInvalidObjectName                100  thrpt   16   628876.398 ±  8770.463  ops/s
MBeanServerGuardCanInvokePerfTest.testCanInvokeNullOperation                     10  thrpt   16  1914158.654 ± 15324.318  ops/s
MBeanServerGuardCanInvokePerfTest.testCanInvokeNullOperation                     50  thrpt   16  1900909.821 ± 26807.880  ops/s
MBeanServerGuardCanInvokePerfTest.testCanInvokeNullOperation                    100  thrpt   16  1902533.223 ± 48632.228  ops/s

ARTEMIS-6042 (except the last commit)
MBeanServerGuardCanInvokePerfTest.testCanInvokeAllowListed                       10  thrpt   16    2750674.221 ±    37961.436  ops/s (+89%)
MBeanServerGuardCanInvokePerfTest.testCanInvokeAllowListed                       50  thrpt   16    2734767.447 ±    26081.250  ops/s (+85%)
MBeanServerGuardCanInvokePerfTest.testCanInvokeAllowListed                      100  thrpt   16    2690331.313 ±   169788.706  ops/s (+85%)
MBeanServerGuardCanInvokePerfTest.testCanInvokeInvalidObjectName                 10  thrpt   16     711172.067 ±    12913.097  ops/s (+17%)
MBeanServerGuardCanInvokePerfTest.testCanInvokeInvalidObjectName                 50  thrpt   16     733427.656 ±    14333.428  ops/s (+20%)
MBeanServerGuardCanInvokePerfTest.testCanInvokeInvalidObjectName                100  thrpt   16     738054.220 ±     7062.869  ops/s (+17%)
MBeanServerGuardCanInvokePerfTest.testCanInvokeNullOperation                     10  thrpt   16  889325992.447 ± 13148015.740  ops/s (+46360%)
MBeanServerGuardCanInvokePerfTest.testCanInvokeNullOperation                     50  thrpt   16  895038928.613 ±  9677822.348  ops/s (+47084%)
MBeanServerGuardCanInvokePerfTest.testCanInvokeNullOperation                    100  thrpt   16  891405790.721 ± 30036267.452  ops/s (+46853%)

"Console scenario" tests:

main:
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadAmqRole   thrpt   16  40.597 ± 0.610  ops/s
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadNoRole    thrpt   16   0.094 ± 0.001  ops/s
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadViewRole  thrpt   16   0.097 ± 0.002  ops/s


ARTEMIS-6042
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadAmqRole   thrpt   16  50.465 ± 0.695  ops/s (+24%)
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadNoRole    thrpt   16  14.338 ± 0.309  ops/s (+15253%)
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadViewRole  thrpt   16  13.834 ± 0.212  ops/s (+14261%)

I also tested gc with java -jar target/benchmark.jar MBeanServerGuardCanInvokePerfTest -prof gc, since claude thought it can decrease memory pressure (take a look at the last commit)

Putting just the gc.alloc.rate here:

main:
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadAmqRole:gc.alloc.rate        thrpt   16         979.006 ±  10.923  MB/sec
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadNoRole:gc.alloc.rate         thrpt   16         204.844 ±   2.719  MB/sec
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadViewRole:gc.alloc.rate       thrpt   16         211.390 ±   1.135  MB/sec

ARTEMIS-6042 (except the last commit)
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadAmqRole:gc.alloc.rate        thrpt   16       678.382 ±     13.282  MB/sec
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadNoRole:gc.alloc.rate         thrpt   16       183.129 ±      4.022  MB/sec
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadViewRole:gc.alloc.rate       thrpt   16       176.503 ±     12.003  MB/sec

ARTEMIS-6042
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadAmqRole:gc.alloc.rate        thrpt   16       591.859 ± 25.651  MB/sec
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadNoRole:gc.alloc.rate         thrpt   16       183.471 ±  2.184  MB/sec
MBeanServerGuardCanInvokePerfTest.testConsolePageLoadViewRole:gc.alloc.rate       thrpt   16       185.519 ±  2.447  MB/sec

@GChuf

GChuf commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

This is still a WIP, but I've put all tests into 1 file and added cache for object names in f0afa2c, and then changed from synchronized map to concurrent hash map in 3ceb8d1.
All of the tests/checks are green.

@jbertram can you guys check and confirm if this is something you'd be willing to review and merge eventually?


Results on my machine running java 21.0.2 (for objectNameCount 1000 where applicable)

Throughput

Benchmark main f0afa2c 3ceb8d1
testCanInvokeAllowListed 1,761,465 550,356 44,208,762
testCanInvokeInvalidObjectName 544,846 493,172 498,332
testCanInvokeMixedScenarios 1,404 132,199 329,699
testCanInvokeNullOperation 1,237,538 1,045,438,183 1,048,246,229
testCanInvokeWithAmqRole 205,195 235,535 234,300
testCanInvokeWithNoRole 696 95,535 238,326
testCanInvokeWithParameterStripping 667 88,486 240,743
testCanInvokeWithViewRole 706 84,518 238,184
testConsolePageLoadAmqRole 39 71 64
testConsolePageLoadNoRole 0.098 14 63
testConsolePageLoadViewRole 0.090 14 64

GC allocation per op

Benchmark main f0afa2c 3ceb8d1
testCanInvokeAllowListed 920 80 16
testCanInvokeInvalidObjectName 1,200 1,200 1,232
testCanInvokeMixedScenarios 155,908 1,452 1,111
testCanInvokeNullOperation 1,184 ≈0 ≈0
testCanInvokeWithAmqRole 3,953 1,797 1,800
testCanInvokeWithNoRole 313,304 1,797 1,800
testCanInvokeWithParameterStripping 312,481 1,865 1,871
testCanInvokeWithViewRole 312,268 1,797 1,800
testConsolePageLoadAmqRole 25,599,870 4,961,027 6,720,688
testConsolePageLoadNoRole 2,343,208,040 5,761,230 6,640,705
testConsolePageLoadViewRole 2,343,208,001 5,761,243 6,640,704

@jbertram

Copy link
Copy Markdown
Contributor

@GChuf I definitely think this is something we'd merge assuming all the commits were referencing a valid Jira.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants