Skip to content

Commit bbad84b

Browse files
committed
fix: unreachable supernode ip in p2p-network
see issue: dragonflyoss#252 Signed-off-by: lowzj <[email protected]>
1 parent 01993c9 commit bbad84b

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/supernode/src/main/docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ ADD supernode.jar supernode.jar
2323

2424
EXPOSE 8001 8002
2525

26-
ENTRYPOINT ["/root/start.sh"]
26+
ENTRYPOINT /root/start.sh $0 $@
2727

src/supernode/src/main/docker/sources/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
nginx
44

5-
java -Djava.security.egd=file:/dev/./urandom -jar supernode.jar
5+
java -Djava.security.egd=file:/dev/./urandom "$@" -jar supernode.jar

src/supernode/src/main/java/com/dragonflyoss/dragonfly/supernode/config/SupernodeProperties.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@
1717
package com.dragonflyoss.dragonfly.supernode.config;
1818

1919
import javax.annotation.PostConstruct;
20+
import java.net.Inet4Address;
21+
import java.net.InetAddress;
22+
import java.net.NetworkInterface;
2023
import java.nio.file.Files;
2124
import java.nio.file.Paths;
2225
import java.util.ArrayList;
26+
import java.util.Enumeration;
2327
import java.util.List;
2428

25-
import com.dragonflyoss.dragonfly.supernode.common.Constants;
2629
import com.alibaba.fastjson.JSON;
2730

31+
import com.dragonflyoss.dragonfly.supernode.common.Constants;
2832
import lombok.Data;
2933
import lombok.extern.slf4j.Slf4j;
34+
import org.apache.commons.lang3.StringUtils;
3035
import org.springframework.boot.context.properties.ConfigurationProperties;
3136

3237
/**
@@ -68,6 +73,12 @@ public class SupernodeProperties {
6873
*/
6974
private String dfgetPath = Constants.DFGET_PATH;
7075

76+
/**
77+
* The advertise ip is used to set the ip that we advertise to other peer in the p2p-network.
78+
* By default, the first non-loop address is advertised.
79+
*/
80+
private String advertiseIp;
81+
7182
@PostConstruct
7283
public void init() {
7384
String cdnHome = baseHome + "/repo";
@@ -82,6 +93,48 @@ public void init() {
8293
log.error("create repo dir error", e);
8394
System.exit(1);
8495
}
96+
97+
setLocalIp();
98+
8599
log.info("cluster members: {}", JSON.toJSONString(cluster));
86100
}
101+
102+
private void setLocalIp() {
103+
if (StringUtils.isNotBlank(advertiseIp)) {
104+
Constants.localIp = advertiseIp;
105+
Constants.generateNodeCid();
106+
log.info("init local ip of supernode, use ip:{}", Constants.localIp);
107+
} else {
108+
List<String> ips = getAllIps();
109+
if (!ips.isEmpty()) {
110+
Constants.localIp = ips.get(0);
111+
Constants.generateNodeCid();
112+
}
113+
log.info("init local ip of supernode, ip list:{}, use ip:{}",
114+
JSON.toJSONString(ips), Constants.localIp);
115+
}
116+
}
117+
118+
private List<String> getAllIps() {
119+
List<String> ips = new ArrayList<String>();
120+
try {
121+
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface
122+
.getNetworkInterfaces();
123+
while (allNetInterfaces.hasMoreElements()) {
124+
NetworkInterface netInterface = allNetInterfaces.nextElement();
125+
Enumeration<InetAddress> addresses = netInterface
126+
.getInetAddresses();
127+
while (addresses.hasMoreElements()) {
128+
InetAddress ip = addresses.nextElement();
129+
if (ip instanceof Inet4Address && !ip.isAnyLocalAddress()
130+
&& !ip.isLoopbackAddress()) {
131+
ips.add(ip.getHostAddress());
132+
}
133+
}
134+
}
135+
} catch (Exception e) {
136+
log.error("getAllIps error:{}", e.getMessage(), e);
137+
}
138+
return ips;
139+
}
87140
}

0 commit comments

Comments
 (0)