|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.astraea.common.metrics; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.lang.management.ManagementFactory; |
| 21 | +import java.net.MalformedURLException; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.function.Consumer; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import java.util.stream.Stream; |
| 29 | +import javax.management.AttributeNotFoundException; |
| 30 | +import javax.management.InstanceNotFoundException; |
| 31 | +import javax.management.MBeanException; |
| 32 | +import javax.management.MBeanFeatureInfo; |
| 33 | +import javax.management.MBeanServerConnection; |
| 34 | +import javax.management.ObjectInstance; |
| 35 | +import javax.management.ReflectionException; |
| 36 | +import javax.management.RuntimeMBeanException; |
| 37 | +import javax.management.remote.JMXConnectorFactory; |
| 38 | +import javax.management.remote.JMXServiceURL; |
| 39 | +import org.astraea.common.Utils; |
| 40 | + |
| 41 | +/** A MBeanClient used to retrieve mbean value from remote Jmx server. */ |
| 42 | +public interface JndiClient extends MBeanClient, AutoCloseable { |
| 43 | + |
| 44 | + /** |
| 45 | + * @param host the address of jmx server |
| 46 | + * @param port the port of jmx server |
| 47 | + * @return a mbean client using JNDI to lookup metrics. |
| 48 | + */ |
| 49 | + static JndiClient of(String host, int port) { |
| 50 | + try { |
| 51 | + return of( |
| 52 | + new JMXServiceURL( |
| 53 | + String.format( |
| 54 | + "service:jmx:rmi://%s:%s/jndi/rmi://%s:%s/jmxrmi", host, port, host, port))); |
| 55 | + } catch (MalformedURLException e) { |
| 56 | + throw new IllegalArgumentException(e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + static JndiClient of(JMXServiceURL jmxServiceURL) { |
| 61 | + return Utils.packException( |
| 62 | + () -> { |
| 63 | + var jmxConnector = JMXConnectorFactory.connect(jmxServiceURL); |
| 64 | + return new BasicMBeanClient( |
| 65 | + jmxConnector.getMBeanServerConnection(), |
| 66 | + jmxServiceURL.getHost(), |
| 67 | + jmxServiceURL.getPort()) { |
| 68 | + @Override |
| 69 | + public void close() { |
| 70 | + Utils.close(jmxConnector); |
| 71 | + } |
| 72 | + }; |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + static JndiClient local() { |
| 77 | + return new BasicMBeanClient(ManagementFactory.getPlatformMBeanServer(), Utils.hostname(), -1); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + default void close() {} |
| 82 | + |
| 83 | + class BasicMBeanClient implements JndiClient { |
| 84 | + |
| 85 | + private final MBeanServerConnection connection; |
| 86 | + final String host; |
| 87 | + |
| 88 | + final int port; |
| 89 | + |
| 90 | + BasicMBeanClient(MBeanServerConnection connection, String host, int port) { |
| 91 | + this.connection = connection; |
| 92 | + this.host = host; |
| 93 | + this.port = port; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public BeanObject bean(BeanQuery beanQuery) { |
| 98 | + return Utils.packException( |
| 99 | + () -> { |
| 100 | + // ask for MBeanInfo |
| 101 | + var mBeanInfo = connection.getMBeanInfo(beanQuery.objectName()); |
| 102 | + |
| 103 | + // create a list builder all available attributes name |
| 104 | + var attributeName = |
| 105 | + Arrays.stream(mBeanInfo.getAttributes()) |
| 106 | + .map(MBeanFeatureInfo::getName) |
| 107 | + .collect(Collectors.toList()); |
| 108 | + |
| 109 | + // query the result |
| 110 | + return queryBean(beanQuery, attributeName); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + BeanObject queryBean(BeanQuery beanQuery, Collection<String> attributeNameCollection) |
| 115 | + throws ReflectionException, |
| 116 | + InstanceNotFoundException, |
| 117 | + IOException, |
| 118 | + AttributeNotFoundException, |
| 119 | + MBeanException { |
| 120 | + // fetch attribute value from mbean server |
| 121 | + var attributeNameArray = attributeNameCollection.toArray(new String[0]); |
| 122 | + var attributeList = |
| 123 | + connection.getAttributes(beanQuery.objectName(), attributeNameArray).asList(); |
| 124 | + |
| 125 | + // collect attribute name & value into a map |
| 126 | + var attributes = new HashMap<String, Object>(); |
| 127 | + attributeList.forEach(attribute -> attributes.put(attribute.getName(), attribute.getValue())); |
| 128 | + |
| 129 | + // according to the javadoc of MBeanServerConnection#getAttributes, the API will |
| 130 | + // ignore any error occurring during the fetch process (for example, attribute not |
| 131 | + // exists). Below code check for such condition and try to figure out what exactly |
| 132 | + // the error is. put it into attributes return result. |
| 133 | + for (var str : attributeNameArray) { |
| 134 | + if (attributes.containsKey(str)) continue; |
| 135 | + try { |
| 136 | + attributes.put(str, connection.getAttribute(beanQuery.objectName(), str)); |
| 137 | + } catch (RuntimeMBeanException e) { |
| 138 | + if (!(e.getCause() instanceof UnsupportedOperationException)) |
| 139 | + throw new IllegalStateException(e); |
| 140 | + // the UnsupportedOperationException is thrown when we query unacceptable |
| 141 | + // attribute. we just skip it as it is normal case to |
| 142 | + // return "acceptable" attribute only |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // collect result, and build a new BeanObject as return result |
| 147 | + return new BeanObject(beanQuery.domainName(), beanQuery.properties(), attributes); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public Collection<BeanObject> beans( |
| 152 | + BeanQuery beanQuery, Consumer<RuntimeException> errorHandle) { |
| 153 | + return Utils.packException( |
| 154 | + () -> |
| 155 | + connection.queryMBeans(beanQuery.objectName(), null).stream() |
| 156 | + // Parallelize the sampling of bean objects. The underlying RMI is thread-safe. |
| 157 | + // https://github.com/skiptests/astraea/issues/1553#issuecomment-1461143723 |
| 158 | + .parallel() |
| 159 | + .map(ObjectInstance::getObjectName) |
| 160 | + .map(BeanQuery::fromObjectName) |
| 161 | + .flatMap( |
| 162 | + query -> { |
| 163 | + try { |
| 164 | + return Stream.of(bean(query)); |
| 165 | + } catch (RuntimeException e) { |
| 166 | + errorHandle.accept(e); |
| 167 | + return Stream.empty(); |
| 168 | + } |
| 169 | + }) |
| 170 | + .collect(Collectors.toUnmodifiableList())); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Returns the list of domains in which any MBean is currently registered. |
| 175 | + * |
| 176 | + * <p>The order of strings within the returned array is not defined. |
| 177 | + * |
| 178 | + * @return a {@link List} of domain name {@link String} |
| 179 | + */ |
| 180 | + List<String> domains() { |
| 181 | + return Utils.packException(() -> Arrays.asList(connection.getDomains())); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments