提交 bbe825f5 编写于 作者: F fuyou001 提交者: vongosling

ROCKETMQ-277 Fix UnknownHostException when nothing configuration in etc file...

ROCKETMQ-277 Fix UnknownHostException when nothing configuration in etc file closes apache/incubator-rocketmq#145
上级 4c5e58b4
...@@ -16,37 +16,21 @@ ...@@ -16,37 +16,21 @@
*/ */
package org.apache.rocketmq.common; package org.apache.rocketmq.common;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.rocketmq.common.annotation.ImportantField; import org.apache.rocketmq.common.annotation.ImportantField;
import org.apache.rocketmq.common.constant.LoggerName; import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.common.help.FAQUrl; import org.apache.rocketmq.common.help.FAQUrl;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
public class MixAll { public class MixAll {
private static final Logger log = LoggerFactory.getLogger(LoggerName.COMMON_LOGGER_NAME); private static final Logger log = LoggerFactory.getLogger(LoggerName.COMMON_LOGGER_NAME);
...@@ -141,16 +125,6 @@ public class MixAll { ...@@ -141,16 +125,6 @@ public class MixAll {
return 0; return 0;
} }
public static long createBrokerId(final String ip, final int port) {
InetSocketAddress isa = new InetSocketAddress(ip, port);
byte[] ipArray = isa.getAddress().getAddress();
ByteBuffer bb = ByteBuffer.allocate(8);
bb.put(ipArray);
bb.putInt(port);
long value = bb.getLong(0);
return Math.abs(value);
}
public static void string2File(final String str, final String fileName) throws IOException { public static void string2File(final String str, final String fileName) throws IOException {
String tmpFile = fileName + ".tmp"; String tmpFile = fileName + ".tmp";
...@@ -240,11 +214,6 @@ public class MixAll { ...@@ -240,11 +214,6 @@ public class MixAll {
return null; return null;
} }
public static String findClassPath(Class<?> c) {
URL url = c.getProtectionDomain().getCodeSource().getLocation();
return url.getPath();
}
public static void printObjectProperties(final Logger logger, final Object object) { public static void printObjectProperties(final Logger logger, final Object object) {
printObjectProperties(logger, object, false); printObjectProperties(logger, object, false);
} }
...@@ -394,23 +363,52 @@ public class MixAll { ...@@ -394,23 +363,52 @@ public class MixAll {
return inetAddressList; return inetAddressList;
} }
public static boolean isLocalAddr(String address) {
for (String addr : LOCAL_INET_ADDRESS) {
if (address.contains(addr))
return true;
}
return false;
}
private static String localhost() { private static String localhost() {
try { try {
InetAddress addr = InetAddress.getLocalHost(); return InetAddress.getLocalHost().getHostAddress();
return addr.getHostAddress();
} catch (Throwable e) { } catch (Throwable e) {
throw new RuntimeException("InetAddress java.net.InetAddress.getLocalHost() throws UnknownHostException" try {
+ FAQUrl.suggestTodo(FAQUrl.UNKNOWN_HOST_EXCEPTION), String candidatesHost = getLocalhostByNetworkInterface();
e); if (candidatesHost != null)
return candidatesHost;
} catch (Exception ignored) {
}
throw new RuntimeException("InetAddress java.net.InetAddress.getLocalHost() throws UnknownHostException" + FAQUrl.suggestTodo(FAQUrl.UNKNOWN_HOST_EXCEPTION), e);
}
}
//FIXME Reverse logic comparing to RemotingUtil method, consider refactor in RocketMQ 5.0
public static String getLocalhostByNetworkInterface() throws SocketException {
List<String> candidatesHost = new ArrayList<String>();
Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
while (enumeration.hasMoreElements()) {
NetworkInterface networkInterface = enumeration.nextElement();
// Workaround for docker0 bridge
if ("docker0".equals(networkInterface.getName()) || !networkInterface.isUp()) {
continue;
}
Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
while (addrs.hasMoreElements()) {
InetAddress address = addrs.nextElement();
if (address.isLoopbackAddress()) {
continue;
}
//ip4 highter priority
if (address instanceof Inet6Address) {
candidatesHost.add(address.getHostAddress());
continue;
}
return address.getHostAddress();
}
}
if (!candidatesHost.isEmpty()) {
return candidatesHost.get(0);
} }
return null;
} }
public static boolean compareAndIncreaseOnly(final AtomicLong target, final long value) { public static boolean compareAndIncreaseOnly(final AtomicLong target, final long value) {
...@@ -426,16 +424,6 @@ public class MixAll { ...@@ -426,16 +424,6 @@ public class MixAll {
return false; return false;
} }
public static String localhostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (Throwable e) {
throw new RuntimeException("InetAddress java.net.InetAddress.getLocalHost() throws UnknownHostException"
+ FAQUrl.suggestTodo(FAQUrl.UNKNOWN_HOST_EXCEPTION),
e);
}
}
public static String humanReadableByteCount(long bytes, boolean si) { public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024; int unit = si ? 1000 : 1024;
if (bytes < unit) if (bytes < unit)
...@@ -445,19 +433,4 @@ public class MixAll { ...@@ -445,19 +433,4 @@ public class MixAll {
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
} }
public Set<String> list2Set(List<String> values) {
Set<String> result = new HashSet<String>();
for (String v : values) {
result.add(v);
}
return result;
}
public List<String> set2List(Set<String> values) {
List<String> result = new ArrayList<String>();
for (String v : values) {
result.add(v);
}
return result;
}
} }
...@@ -17,17 +17,14 @@ ...@@ -17,17 +17,14 @@
package org.apache.rocketmq.common; package org.apache.rocketmq.common;
import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.InetAddress; import java.net.InetAddress;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -93,4 +90,10 @@ public class MixAllTest { ...@@ -93,4 +90,10 @@ public class MixAllTest {
MixAll.string2File("MixAll_testString2File", fileName); MixAll.string2File("MixAll_testString2File", fileName);
assertThat(MixAll.file2String(fileName)).isEqualTo("MixAll_testString2File"); assertThat(MixAll.file2String(fileName)).isEqualTo("MixAll_testString2File");
} }
@Test
public void testGetLocalhostByNetworkInterface() throws Exception {
assertThat(MixAll.LOCALHOST).isNotNull();
assertThat(MixAll.getLocalhostByNetworkInterface()).isNotNull();
}
} }
...@@ -17,11 +17,6 @@ ...@@ -17,11 +17,6 @@
package org.apache.rocketmq.remoting.common; package org.apache.rocketmq.remoting.common;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import org.apache.rocketmq.remoting.exception.RemotingConnectException; import org.apache.rocketmq.remoting.exception.RemotingConnectException;
import org.apache.rocketmq.remoting.exception.RemotingSendRequestException; import org.apache.rocketmq.remoting.exception.RemotingSendRequestException;
import org.apache.rocketmq.remoting.exception.RemotingTimeoutException; import org.apache.rocketmq.remoting.exception.RemotingTimeoutException;
...@@ -29,6 +24,12 @@ import org.apache.rocketmq.remoting.protocol.RemotingCommand; ...@@ -29,6 +24,12 @@ import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class RemotingHelper { public class RemotingHelper {
public static final String ROCKETMQ_REMOTING = "RocketmqRemoting"; public static final String ROCKETMQ_REMOTING = "RocketmqRemoting";
public static final String DEFAULT_CHARSET = "UTF-8"; public static final String DEFAULT_CHARSET = "UTF-8";
...@@ -168,17 +169,6 @@ public class RemotingHelper { ...@@ -168,17 +169,6 @@ public class RemotingHelper {
return ""; return "";
} }
public static String parseChannelRemoteName(final Channel channel) {
if (null == channel) {
return "";
}
final InetSocketAddress remote = (InetSocketAddress) channel.remoteAddress();
if (remote != null) {
return remote.getAddress().getHostName();
}
return "";
}
public static String parseSocketAddressAddr(SocketAddress socketAddress) { public static String parseSocketAddressAddr(SocketAddress socketAddress) {
if (socketAddress != null) { if (socketAddress != null) {
final String addr = socketAddress.toString(); final String addr = socketAddress.toString();
...@@ -190,13 +180,4 @@ public class RemotingHelper { ...@@ -190,13 +180,4 @@ public class RemotingHelper {
return ""; return "";
} }
public static String parseSocketAddressName(SocketAddress socketAddress) {
final InetSocketAddress addrs = (InetSocketAddress) socketAddress;
if (addrs != null) {
return addrs.getAddress().getHostName();
}
return "";
}
} }
...@@ -19,20 +19,17 @@ package org.apache.rocketmq.remoting.common; ...@@ -19,20 +19,17 @@ package org.apache.rocketmq.remoting.common;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.Inet6Address; import java.net.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.nio.channels.Selector; import java.nio.channels.Selector;
import java.nio.channels.SocketChannel; import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider; import java.nio.channels.spi.SelectorProvider;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RemotingUtil { public class RemotingUtil {
public static final String OS_NAME = System.getProperty("os.name"); public static final String OS_NAME = System.getProperty("os.name");
......
...@@ -73,61 +73,6 @@ public abstract class ServiceThread implements Runnable { ...@@ -73,61 +73,6 @@ public abstract class ServiceThread implements Runnable {
return JOIN_TIME; return JOIN_TIME;
} }
public void stop() {
this.stop(false);
}
public void stop(final boolean interrupt) {
this.stopped = true;
log.info("stop thread " + this.getServiceName() + " interrupt " + interrupt);
synchronized (this) {
if (!this.hasNotified) {
this.hasNotified = true;
this.notify();
}
}
if (interrupt) {
this.thread.interrupt();
}
}
public void makeStop() {
this.stopped = true;
log.info("makestop thread " + this.getServiceName());
}
public void wakeup() {
synchronized (this) {
if (!this.hasNotified) {
this.hasNotified = true;
this.notify();
}
}
}
protected void waitForRunning(long interval) {
synchronized (this) {
if (this.hasNotified) {
this.hasNotified = false;
this.onWaitEnd();
return;
}
try {
this.wait(interval);
} catch (InterruptedException e) {
log.error("Interrupted", e);
} finally {
this.hasNotified = false;
this.onWaitEnd();
}
}
}
protected void onWaitEnd() {
}
public boolean isStopped() { public boolean isStopped() {
return stopped; return stopped;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册