You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/05/29 09:51:23 UTC

[GitHub] [flink] zentol commented on a change in pull request #11586: [FLINK-5552][runtime] make JMXServer static per JVM

zentol commented on a change in pull request #11586:
URL: https://github.com/apache/flink/pull/11586#discussion_r432371112



##########
File path: flink-core/src/main/java/org/apache/flink/configuration/JMXServerOptions.java
##########
@@ -0,0 +1,46 @@
+/*
+ * 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.flink.configuration;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.annotation.docs.Documentation;
+
+import static org.apache.flink.configuration.ConfigOptions.key;
+
+/**
+ * The set of configuration options relating to heartbeat manager settings.
+ */
+@PublicEvolving
+public class JMXServerOptions {
+
+	/** Port configured to enable JMX server for metrics and debugging. */
+	@Documentation.Section(Documentation.Sections.EXPERT_DEBUGGING_AND_TUNING)
+	public static final ConfigOption<String>JMX_SERVER_PORT =
+		key("jmx.server.port")
+			.defaultValue("-1")

Review comment:
       having no default value at all would make this easier to understand imo

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/management/JMXServer.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.flink.runtime.management;
+
+import org.apache.flink.configuration.JMXServerOptions;
+import org.apache.flink.util.NetUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXServiceURL;
+import javax.management.remote.rmi.RMIConnectorServer;
+import javax.management.remote.rmi.RMIJRMPServerImpl;
+
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.net.MalformedURLException;
+import java.rmi.NoSuchObjectException;
+import java.rmi.NotBoundException;
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+import java.rmi.registry.Registry;
+import java.rmi.server.UnicastRemoteObject;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicReference;
+
+
+/**
+ * JMX Server implementation that JMX clients can connect to.
+ *
+ * <p>Heavily based on j256 simplejmx project
+ *
+ * <p>https://github.com/j256/simplejmx/blob/master/src/main/java/com/j256/simplejmx/server/JmxServer.java
+ */
+public class JMXServer {
+	private static final Logger LOG = LoggerFactory.getLogger(JMXServer.class);
+
+	private static JMXServer instance = null;
+
+	private final AtomicReference<Remote> rmiServerReference = new AtomicReference<>();
+
+	private Registry rmiRegistry;
+	private JMXConnectorServer connector;
+	private int port;
+
+	/**
+	 * Construct a new JMV-wide JMX server or acquire existing JMX server.
+	 *
+	 * <p>If JMXServer static instance is already constructed, it will not be
+	 * reconstruct again. Instead a warning sign will be posted if the desired
+	 * port configuration doesn't match the existing JMXServer static instance.
+	 *
+	 * @param portsConfig port configuration of the JMX server.
+	 * @return JMXServer static instance.
+	 */
+	public static JMXServer startInstance(String portsConfig) {
+		if (instance == null) {
+			if (!portsConfig.equals(JMXServerOptions.JMX_SERVER_PORT.defaultValue())) {
+				instance = startJMXServerWithPortRanges(portsConfig);
+			} else {
+				LOG.warn("JMX Server start failed. No explicit JMX port is configured.");
+				instance = null;
+			}
+		} else {
+			LOG.warn("JVM-wide JMXServer already started at port: " + instance.port);
+		}
+		return instance;
+	}
+
+	/**
+	 * Acquire existing JMX server. or null if not started.
+	 *
+	 * @return the JMXServer static instance.
+	 */
+	public static JMXServer getInstance() {
+		return instance;
+	}
+
+	/**
+	 * Stop the JMX server.
+	 */
+	public static void stopInstance() throws IOException {
+		if (instance != null) {
+			instance.stop();
+			instance = null;
+		}
+	}
+
+	public static int getPort() {

Review comment:
       return an Optional<Integer> instead, then you can probably also get rid of `JMXServer#getInstance`

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/management/JMXServer.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.flink.runtime.management;
+
+import org.apache.flink.configuration.JMXServerOptions;
+import org.apache.flink.util.NetUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXServiceURL;
+import javax.management.remote.rmi.RMIConnectorServer;
+import javax.management.remote.rmi.RMIJRMPServerImpl;
+
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.net.MalformedURLException;
+import java.rmi.NoSuchObjectException;
+import java.rmi.NotBoundException;
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+import java.rmi.registry.Registry;
+import java.rmi.server.UnicastRemoteObject;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicReference;
+
+
+/**
+ * JMX Server implementation that JMX clients can connect to.
+ *
+ * <p>Heavily based on j256 simplejmx project
+ *
+ * <p>https://github.com/j256/simplejmx/blob/master/src/main/java/com/j256/simplejmx/server/JmxServer.java
+ */
+public class JMXServer {
+	private static final Logger LOG = LoggerFactory.getLogger(JMXServer.class);
+
+	private static JMXServer instance = null;
+
+	private final AtomicReference<Remote> rmiServerReference = new AtomicReference<>();
+
+	private Registry rmiRegistry;
+	private JMXConnectorServer connector;
+	private int port;
+
+	/**
+	 * Construct a new JMV-wide JMX server or acquire existing JMX server.
+	 *
+	 * <p>If JMXServer static instance is already constructed, it will not be
+	 * reconstruct again. Instead a warning sign will be posted if the desired

Review comment:
       ```suggestion
   	 * reconstructed again. Instead a warning will be logged if the given
   ```

##########
File path: flink-annotations/src/main/java/org/apache/flink/annotation/docs/Documentation.java
##########
@@ -82,6 +82,7 @@
 		public static final String STATE_BACKEND_ROCKSDB = "state_backend_rocksdb";
 
 		public static final String EXPERT_CLASS_LOADING = "expert_class_loading";
+		public static final String EXPERT_DEBUGGING_AND_TUNING = "expert_debugging_and_tuning";

Review comment:
       the new section must be integrated into the documentation, by embedding the resulting .html file.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/management/JMXServer.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.flink.runtime.management;
+
+import org.apache.flink.configuration.JMXServerOptions;
+import org.apache.flink.util.NetUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXServiceURL;
+import javax.management.remote.rmi.RMIConnectorServer;
+import javax.management.remote.rmi.RMIJRMPServerImpl;
+
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.net.MalformedURLException;
+import java.rmi.NoSuchObjectException;
+import java.rmi.NotBoundException;
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+import java.rmi.registry.Registry;
+import java.rmi.server.UnicastRemoteObject;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicReference;
+
+
+/**
+ * JMX Server implementation that JMX clients can connect to.
+ *
+ * <p>Heavily based on j256 simplejmx project
+ *
+ * <p>https://github.com/j256/simplejmx/blob/master/src/main/java/com/j256/simplejmx/server/JmxServer.java
+ */
+public class JMXServer {
+	private static final Logger LOG = LoggerFactory.getLogger(JMXServer.class);
+
+	private static JMXServer instance = null;
+
+	private final AtomicReference<Remote> rmiServerReference = new AtomicReference<>();
+
+	private Registry rmiRegistry;
+	private JMXConnectorServer connector;
+	private int port;
+
+	/**
+	 * Construct a new JMV-wide JMX server or acquire existing JMX server.
+	 *
+	 * <p>If JMXServer static instance is already constructed, it will not be
+	 * reconstruct again. Instead a warning sign will be posted if the desired
+	 * port configuration doesn't match the existing JMXServer static instance.
+	 *
+	 * @param portsConfig port configuration of the JMX server.
+	 * @return JMXServer static instance.
+	 */
+	public static JMXServer startInstance(String portsConfig) {
+		if (instance == null) {
+			if (!portsConfig.equals(JMXServerOptions.JMX_SERVER_PORT.defaultValue())) {
+				instance = startJMXServerWithPortRanges(portsConfig);
+			} else {
+				LOG.warn("JMX Server start failed. No explicit JMX port is configured.");
+				instance = null;
+			}
+		} else {
+			LOG.warn("JVM-wide JMXServer already started at port: " + instance.port);
+		}
+		return instance;
+	}
+
+	/**
+	 * Acquire existing JMX server. or null if not started.
+	 *
+	 * @return the JMXServer static instance.
+	 */
+	public static JMXServer getInstance() {
+		return instance;
+	}
+
+	/**
+	 * Stop the JMX server.
+	 */
+	public static void stopInstance() throws IOException {
+		if (instance != null) {
+			instance.stop();
+			instance = null;
+		}
+	}
+
+	public static int getPort() {
+		if (instance != null) {
+			return instance.port;
+		} else {
+			return -1;
+		}
+	}
+
+	private static JMXServer startJMXServerWithPortRanges(String portsConfig) {
+		Iterator<Integer> ports = NetUtils.getPortRangeFromString(portsConfig);

Review comment:
       I'd move this into `#startInstance`; if portsConfig is an empty string then the iterator is empty, allowing you to easily check for the port to not be configured.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/management/JMXServer.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.flink.runtime.management;
+
+import org.apache.flink.configuration.JMXServerOptions;
+import org.apache.flink.util.NetUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXServiceURL;
+import javax.management.remote.rmi.RMIConnectorServer;
+import javax.management.remote.rmi.RMIJRMPServerImpl;
+
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.net.MalformedURLException;
+import java.rmi.NoSuchObjectException;
+import java.rmi.NotBoundException;
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+import java.rmi.registry.Registry;
+import java.rmi.server.UnicastRemoteObject;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicReference;
+
+
+/**
+ * JMX Server implementation that JMX clients can connect to.
+ *
+ * <p>Heavily based on j256 simplejmx project
+ *
+ * <p>https://github.com/j256/simplejmx/blob/master/src/main/java/com/j256/simplejmx/server/JmxServer.java
+ */
+public class JMXServer {
+	private static final Logger LOG = LoggerFactory.getLogger(JMXServer.class);
+
+	private static JMXServer instance = null;
+
+	private final AtomicReference<Remote> rmiServerReference = new AtomicReference<>();
+
+	private Registry rmiRegistry;
+	private JMXConnectorServer connector;
+	private int port;
+
+	/**
+	 * Construct a new JMV-wide JMX server or acquire existing JMX server.
+	 *
+	 * <p>If JMXServer static instance is already constructed, it will not be
+	 * reconstruct again. Instead a warning sign will be posted if the desired
+	 * port configuration doesn't match the existing JMXServer static instance.
+	 *
+	 * @param portsConfig port configuration of the JMX server.
+	 * @return JMXServer static instance.
+	 */
+	public static JMXServer startInstance(String portsConfig) {
+		if (instance == null) {
+			if (!portsConfig.equals(JMXServerOptions.JMX_SERVER_PORT.defaultValue())) {
+				instance = startJMXServerWithPortRanges(portsConfig);
+			} else {
+				LOG.warn("JMX Server start failed. No explicit JMX port is configured.");
+				instance = null;
+			}
+		} else {
+			LOG.warn("JVM-wide JMXServer already started at port: " + instance.port);
+		}
+		return instance;
+	}
+
+	/**
+	 * Acquire existing JMX server. or null if not started.
+	 *
+	 * @return the JMXServer static instance.
+	 */
+	public static JMXServer getInstance() {

Review comment:
       return an Optional<JMXServer> instead

##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/management/JMXServerTest.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.flink.runtime.management;
+
+import org.junit.Test;
+
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanServer;
+import javax.management.MBeanServerConnection;
+import javax.management.ObjectName;
+import javax.management.remote.JMXConnector;
+import javax.management.remote.JMXConnectorFactory;
+import javax.management.remote.JMXServiceURL;
+
+import java.lang.management.ManagementFactory;
+import java.net.ServerSocket;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for the JMXServer.
+ */
+public class JMXServerTest {
+
+	/**
+	 * Verifies initialize with port range.
+	 */
+	@Test
+	public void testJMXServerInit() throws Exception {
+		try {
+			JMXServer.startInstance("23456-23466");
+			assertNotNull(JMXServer.getInstance());
+		} finally {
+			JMXServer.stopInstance();
+		}
+	}
+
+	/**
+	 * Verifies initialize failure with occupied port.
+	 */
+	@Test
+	public void testJMXServerInitWithInvalidPorts() throws Exception {
+		try {
+			ServerSocket socket = new ServerSocket(23456);
+			assertEquals(23456, socket.getLocalPort());
+			JMXServer.startInstance("23456");
+			assertNull(JMXServer.getInstance());

Review comment:
       this line should never be reached

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/management/JMXServer.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.flink.runtime.management;
+
+import org.apache.flink.configuration.JMXServerOptions;
+import org.apache.flink.util.NetUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXServiceURL;
+import javax.management.remote.rmi.RMIConnectorServer;
+import javax.management.remote.rmi.RMIJRMPServerImpl;
+
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.net.MalformedURLException;
+import java.rmi.NoSuchObjectException;
+import java.rmi.NotBoundException;
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+import java.rmi.registry.Registry;
+import java.rmi.server.UnicastRemoteObject;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicReference;
+
+
+/**
+ * JMX Server implementation that JMX clients can connect to.
+ *
+ * <p>Heavily based on j256 simplejmx project
+ *
+ * <p>https://github.com/j256/simplejmx/blob/master/src/main/java/com/j256/simplejmx/server/JmxServer.java
+ */
+public class JMXServer {

Review comment:
       I'd say that the JMXServer itself should not have the singleton logic, and instead have something like a SingletonHolder object for that stuff.
   There is for example no reason why the tests should work with a singleton; this usually just leads to issues.

##########
File path: flink-core/src/main/java/org/apache/flink/configuration/JMXServerOptions.java
##########
@@ -0,0 +1,46 @@
+/*
+ * 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.flink.configuration;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.annotation.docs.Documentation;
+
+import static org.apache.flink.configuration.ConfigOptions.key;
+
+/**
+ * The set of configuration options relating to heartbeat manager settings.

Review comment:
       needs an update

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/management/JMXServer.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.flink.runtime.management;
+
+import org.apache.flink.configuration.JMXServerOptions;
+import org.apache.flink.util.NetUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXServiceURL;
+import javax.management.remote.rmi.RMIConnectorServer;
+import javax.management.remote.rmi.RMIJRMPServerImpl;
+
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.net.MalformedURLException;
+import java.rmi.NoSuchObjectException;
+import java.rmi.NotBoundException;
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+import java.rmi.registry.Registry;
+import java.rmi.server.UnicastRemoteObject;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicReference;
+
+
+/**
+ * JMX Server implementation that JMX clients can connect to.
+ *
+ * <p>Heavily based on j256 simplejmx project
+ *
+ * <p>https://github.com/j256/simplejmx/blob/master/src/main/java/com/j256/simplejmx/server/JmxServer.java
+ */
+public class JMXServer {
+	private static final Logger LOG = LoggerFactory.getLogger(JMXServer.class);
+
+	private static JMXServer instance = null;
+
+	private final AtomicReference<Remote> rmiServerReference = new AtomicReference<>();
+
+	private Registry rmiRegistry;
+	private JMXConnectorServer connector;
+	private int port;
+
+	/**
+	 * Construct a new JMV-wide JMX server or acquire existing JMX server.
+	 *
+	 * <p>If JMXServer static instance is already constructed, it will not be
+	 * reconstruct again. Instead a warning sign will be posted if the desired
+	 * port configuration doesn't match the existing JMXServer static instance.
+	 *
+	 * @param portsConfig port configuration of the JMX server.
+	 * @return JMXServer static instance.
+	 */
+	public static JMXServer startInstance(String portsConfig) {

Review comment:
       these methods should be made thread-safe, otherwise you can end up starting 2 servers and never shutting one of them down.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/management/JMXServer.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.flink.runtime.management;
+
+import org.apache.flink.configuration.JMXServerOptions;
+import org.apache.flink.util.NetUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXServiceURL;
+import javax.management.remote.rmi.RMIConnectorServer;
+import javax.management.remote.rmi.RMIJRMPServerImpl;
+
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.net.MalformedURLException;
+import java.rmi.NoSuchObjectException;
+import java.rmi.NotBoundException;
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+import java.rmi.registry.Registry;
+import java.rmi.server.UnicastRemoteObject;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicReference;
+
+
+/**
+ * JMX Server implementation that JMX clients can connect to.
+ *
+ * <p>Heavily based on j256 simplejmx project
+ *
+ * <p>https://github.com/j256/simplejmx/blob/master/src/main/java/com/j256/simplejmx/server/JmxServer.java
+ */
+public class JMXServer {

Review comment:
       This should have a private constructor if all instantiations are supposed to go through `startInstance`. If not, then an explicit constructor might clarify things a bit.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org