Skip to content

Commit

Permalink
fix: unreachable supernode ip in p2p-network
Browse files Browse the repository at this point in the history
see issue: dragonflyoss#252

Signed-off-by: lowzj <[email protected]>
  • Loading branch information
lowzj committed Mar 1, 2019
1 parent dbce79b commit 5b3f004
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/supernode/src/main/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ ADD supernode.jar supernode.jar

EXPOSE 8001 8002

ENTRYPOINT ["/root/start.sh"]
CMD ["-Dsupernode.advertiseIp="]
ENTRYPOINT /root/start.sh $0 $@

2 changes: 1 addition & 1 deletion src/supernode/src/main/docker/sources/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

nginx

java -Djava.security.egd=file:/dev/./urandom -jar supernode.jar
java -Djava.security.egd=file:/dev/./urandom "$@" -jar supernode.jar
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@
package com.dragonflyoss.dragonfly.supernode.config;

import javax.annotation.PostConstruct;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import com.dragonflyoss.dragonfly.supernode.common.Constants;
import com.alibaba.fastjson.JSON;

import com.dragonflyoss.dragonfly.supernode.common.Constants;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
Expand Down Expand Up @@ -68,6 +73,12 @@ public class SupernodeProperties {
*/
private String dfgetPath = Constants.DFGET_PATH;

/**
* The advertise ip is used to set the ip that we advertise to other peer in the p2p-network.
* By default, the first non-loop address is advertised.
*/
private String advertiseIp;

@PostConstruct
public void init() {
String cdnHome = baseHome + "/repo";
Expand All @@ -82,6 +93,48 @@ public void init() {
log.error("create repo dir error", e);
System.exit(1);
}

setLocalIp();

log.info("cluster members: {}", JSON.toJSONString(cluster));
}

private void setLocalIp() {
if (StringUtils.isNotBlank(advertiseIp)) {
Constants.localIp = advertiseIp;
Constants.generateNodeCid();
log.info("init local ip of supernode, use ip:{}", Constants.localIp);
} else {
List<String> ips = getAllIps();
if (!ips.isEmpty()) {
Constants.localIp = ips.get(0);
Constants.generateNodeCid();
}
log.info("init local ip of supernode, ip list:{}, use ip:{}",
JSON.toJSONString(ips), Constants.localIp);
}
}

private List<String> getAllIps() {
List<String> ips = new ArrayList<String>();
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface
.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = allNetInterfaces.nextElement();
Enumeration<InetAddress> addresses = netInterface
.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress ip = addresses.nextElement();
if (ip instanceof Inet4Address && !ip.isAnyLocalAddress()
&& !ip.isLoopbackAddress()) {
ips.add(ip.getHostAddress());
}
}
}
} catch (Exception e) {
log.error("getAllIps error:{}", e.getMessage(), e);
}
return ips;
}
}

0 comments on commit 5b3f004

Please sign in to comment.