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



##########
File path: docs/_includes/generated/stop_configuration.html
##########
@@ -0,0 +1,12 @@
+<table class="table table-bordered">

Review comment:
       where do these come from? O.o

##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/management/JMXService.java
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.util.NetUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Optional;
+
+/**
+ * Provide a JVM-wide singleton JMX Service.
+ */
+public class JMXService {
+       private static final Logger LOG = 
LoggerFactory.getLogger(JMXService.class);
+       private static JMXServer jmxServer = null;
+
+       /**
+        * Acquire the global singleton JMXServer instance.
+        *
+        * @return JMXServer static instance.
+        */
+       public static JMXServer getInstance() {
+               return jmxServer;
+       }
+
+       /**
+        * Start the JMV-wide singleton JMX server.
+        *
+        * <p>If JMXServer static instance is already started, it will not be
+        * started again. Instead a warning will be logged indicating which port
+        * the existing JMXServer static instance is exposing.
+        *
+        * @param portsConfig port configuration of the JMX server.
+        */
+       public static synchronized void startInstance(String portsConfig) {
+               if (jmxServer == null) {
+                       if (portsConfig != null) {
+                               Iterator<Integer> ports = 
NetUtils.getPortRangeFromString(portsConfig);
+                               if (ports.hasNext()) {
+                                       jmxServer = 
startJMXServerWithPortRanges(ports);
+                               }
+                               if (jmxServer == null) {
+                                       LOG.error("Could not start JMx server 
on any configured port(s) in: " + portsConfig);
+                               }
+                       }
+               } else {
+                       LOG.warn("JVM-wide JMXServer already started at port: " 
+ jmxServer.getPort());
+               }
+       }
+
+       /**
+        * Stop the JMX server.
+        */
+       public static synchronized void stopInstance() throws IOException {
+               if (jmxServer != null) {
+                       jmxServer.stop();
+                       jmxServer = null;
+               }
+       }
+
+       public static Optional<Integer> getPort() {
+               if (jmxServer == null) {

Review comment:
       this _could_ be shortened to 
`Optional.ofNullable(jmxServer).map(JMXServer::getPort);`

##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/management/JMXService.java
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.util.NetUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Optional;
+
+/**
+ * Provide a JVM-wide singleton JMX Service.
+ */
+public class JMXService {
+       private static final Logger LOG = 
LoggerFactory.getLogger(JMXService.class);
+       private static JMXServer jmxServer = null;
+
+       /**
+        * Acquire the global singleton JMXServer instance.
+        *
+        * @return JMXServer static instance.
+        */
+       public static JMXServer getInstance() {

Review comment:
       Return an Optional instead.

##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/management/JMXService.java
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.util.NetUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Optional;
+
+/**
+ * Provide a JVM-wide singleton JMX Service.
+ */
+public class JMXService {
+       private static final Logger LOG = 
LoggerFactory.getLogger(JMXService.class);
+       private static JMXServer jmxServer = null;
+
+       /**
+        * Acquire the global singleton JMXServer instance.
+        *
+        * @return JMXServer static instance.
+        */
+       public static JMXServer getInstance() {
+               return jmxServer;
+       }
+
+       /**
+        * Start the JMV-wide singleton JMX server.
+        *
+        * <p>If JMXServer static instance is already started, it will not be
+        * started again. Instead a warning will be logged indicating which port
+        * the existing JMXServer static instance is exposing.
+        *
+        * @param portsConfig port configuration of the JMX server.
+        */
+       public static synchronized void startInstance(String portsConfig) {
+               if (jmxServer == null) {
+                       if (portsConfig != null) {
+                               Iterator<Integer> ports = 
NetUtils.getPortRangeFromString(portsConfig);
+                               if (ports.hasNext()) {
+                                       jmxServer = 
startJMXServerWithPortRanges(ports);
+                               }
+                               if (jmxServer == null) {
+                                       LOG.error("Could not start JMx server 
on any configured port(s) in: " + portsConfig);

Review comment:
       ```suggestion
                                        LOG.error("Could not start JMX server 
on any configured port(s) in: " + portsConfig);
   ```

##########
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 JMX server.
+ */
+@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")
+                       .noDefaultValue()
+                       .withDescription("The port range for the JMX server to 
start the registry. The " +
+                               "port config can be a single port: \"9123\", a 
range of ports: \"50100-50200\", " +
+                               "or a list of ranges and ports: 
\"50100-50200,50300-50400,51234\". \n" +
+                               "this option overrides metrics.reporter.*.port 
configurations.");

Review comment:
       ```suggestion
                                "This option overrides metrics.reporter.*.port 
option.");
   ```

##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/management/JMXServerTest.java
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.After;
+import org.junit.Before;
+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 static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test for {@link JMXServer} functionality.
+ */
+public class JMXServerTest {
+
+       @Before
+       public void setUp() throws Exception {
+               JMXService.startInstance("23456-23466");
+       }
+
+       @After
+       public void tierDown() throws Exception {
+               JMXService.stopInstance();
+       }
+
+       /**
+        * Verifies initialize, registered mBean and retrieval via attribute.
+        */
+       @Test
+       public void testJMXServiceRegisterMBean() throws Exception {
+               TestObject testObject = new TestObject();
+               ObjectName testObjectName = new 
ObjectName("org.apache.flink.management", "key", "value");
+               MBeanServer mBeanServer = 
ManagementFactory.getPlatformMBeanServer();
+
+               try {
+                       JMXServer server = JMXService.getInstance();

Review comment:
       unused?

##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/management/JMXServiceTest.java
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 java.net.ServerSocket;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for the singleton usage via {@link JMXService}.
+ */
+public class JMXServiceTest {
+
+       /**
+        * Verifies initialize with port range.
+        */
+       @Test
+       public void testJMXServiceInit() throws Exception {
+               try {
+                       JMXService.startInstance("23456-23466");
+                       assertTrue(JMXService.getPort().isPresent());
+               } finally {
+                       JMXService.stopInstance();
+               }
+       }
+
+       /**
+        * Verifies initialize failure with occupied port.
+        */
+       @Test
+       public void testJMXServiceInitWithInvalidPorts() throws Exception {
+               ServerSocket socket = null;
+               try {
+                       socket = new ServerSocket(23456);
+                       assertEquals(23456, socket.getLocalPort());
+                       JMXService.startInstance("23456");
+                       assertNull(JMXService.getInstance());
+               } finally {
+                       if (socket != null) {
+                               socket.close();
+                       }
+               }

Review comment:
       ```suggestion
                try (ServerSocket socket = new ServerSocket(23456)) {
                        assertEquals(23456, socket.getLocalPort());
                        JMXService.startInstance("23456");
                        assertNull(JMXService.getInstance());
                }
   ```

##########
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 JMX server.
+ */
+@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")
+                       .noDefaultValue()
+                       .withDescription("The port range for the JMX server to 
start the registry. The " +

Review comment:
       ```suggestion
                        .withDescription(Description.builder()
                                .text(...port config...)
                                .lineBreak()
                                .text(...overrides...)
                                .build());
   ```




----------------------------------------------------------------
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:
[email protected]


Reply via email to