jonmeredith commented on code in PR #37: URL: https://github.com/apache/cassandra-in-jvm-dtest-api/pull/37#discussion_r1286399736
########## src/main/java/org/apache/cassandra/distributed/shared/Uninterruptibles.java: ########## @@ -0,0 +1,48 @@ +/* + * 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.cassandra.distributed.shared; + +import java.util.concurrent.TimeUnit; + +public class Uninterruptibles +{ + public static void sleepUninterruptibly(long sleepFor, TimeUnit unit) { + // copied from guava since dtest can't depend on guava + boolean interrupted = false; + + try { + long remainingNanos = unit.toNanos(sleepFor); + long end = System.nanoTime() + remainingNanos; + + while(true) { + try { + TimeUnit.NANOSECONDS.sleep(remainingNanos); + return; + } catch (InterruptedException var12) { Review Comment: `var12` - catchy disassembly name ########## src/main/java/org/apache/cassandra/distributed/shared/JMXUtil.java: ########## @@ -31,23 +36,85 @@ private JMXUtil() { } + private final static Logger LOGGER = LoggerFactory.getLogger(JMXUtil.class); + public static final String JMX_SERVICE_URL_FMT = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi"; + /** + * Create an instance of a {@link JMXConnector} to an in-jvm instance based on the input configuration. + * This overload uses 5 as the default number of retries which has been shown to be adequate in testing, + * and passes a null environment map to the connect call. + * @param config The instance configuration to use to get the necessary paramters to connect Review Comment: typo `paramters` ########## src/main/java/org/apache/cassandra/distributed/shared/JMXUtil.java: ########## @@ -31,23 +36,85 @@ private JMXUtil() { } + private final static Logger LOGGER = LoggerFactory.getLogger(JMXUtil.class); + public static final String JMX_SERVICE_URL_FMT = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi"; + /** + * Create an instance of a {@link JMXConnector} to an in-jvm instance based on the input configuration. + * This overload uses 5 as the default number of retries which has been shown to be adequate in testing, + * and passes a null environment map to the connect call. + * @param config The instance configuration to use to get the necessary paramters to connect + * @return A JMXConnector instance which can communicate with the specified instance via JMX + */ public static JMXConnector getJmxConnector(IInstanceConfig config) { + return getJmxConnector(config, 5, null); + } + + /** + * Create an instance of a {@link JMXConnector} to an in-jvm instance based on the input configuration. + * This overload uses 5 as the default number of retries which has been shown to be adequate in testing. + * @param config The instance configuration to use to get the necessary paramters to connect + * @param jmxEnv an optional map which specifies the JMX environment to use. Can be null. + * @return A JMXConnector instance which can communicate with the specified instance via JMX + */ + public static JMXConnector getJmxConnector(IInstanceConfig config, Map<String, ?> jmxEnv) { + return getJmxConnector(config, 5, jmxEnv); + } + + + /** + * Create an instance of a {@link JMXConnector} to an in-jvm instance based on the input configuration + * This overload passes a null environment map to the connect call. + * @param config The instance configuration to use to get the necessary paramters to connect + * @param numAttempts the number of retries to attempt before failing to connect. + * @return A JMXConnector instance which can communicate with the specified instance via JMX + */ + public static JMXConnector getJmxConnector(IInstanceConfig config, int numAttempts) + { + return getJmxConnector(config, numAttempts, null); + } + + /** + * Create an instance of a {@link JMXConnector} to an in-jvm instance based on the input configuration + * @param config The instance configuration to use to get the necessary paramters to connect + * @param numAttempts the number of retries to attempt before failing to connect. + * @param jmxEnv an optional map which specifies the JMX environment to use. Can be null. + * @return A JMXConnector instance which can communicate with the specified instance via JMX + */ + public static JMXConnector getJmxConnector(IInstanceConfig config, int numAttempts, Map<String, ?> jmxEnv) { String jmxHost = getJmxHost(config); String url = String.format(JMX_SERVICE_URL_FMT, jmxHost, config.jmxPort()); - try - { - return JMXConnectorFactory.connect(new JMXServiceURL(url), null); - } - catch (IOException e) + int attempts = 0; + Throwable lastThrown = null; + while (attempts < numAttempts) { - throw new RuntimeException(e); + attempts++; + try + { + JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(url), jmxEnv); + + LOGGER.info("Connected to JMX server at {} after {} attempt(s)", + url, attempts); + return connector; + } + + catch(MalformedURLException e) + { + throw new RuntimeException(e); + } + + catch (Throwable thrown) + { + lastThrown = thrown; + } + LOGGER.info("Could not connect to JMX on {} after {} attempts. Will retry.", url, attempts); + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); } + throw new RuntimeException("Could not start JMX - unreachable after 20 attempts", lastThrown); Review Comment: Can you fix the hard-coded attempts count. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

