-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from mesos/ehancement/362-ReportIPAddress
Executor IPAddress discovery. Executor will now send IP address over …
- Loading branch information
Showing
9 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
commons/src/main/java/org/apache/mesos/elasticsearch/common/AdaptorIPAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.apache.mesos.elasticsearch.common; | ||
|
||
import java.net.InetAddress; | ||
import java.net.NetworkInterface; | ||
import java.net.SocketException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Obtains the first IP address from an adaptor | ||
*/ | ||
public class AdaptorIPAddress { | ||
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(AdaptorIPAddress.class); | ||
/** | ||
* @return an InetAddress for the adaptor eth0 or en0 (so unit tests work on mac) | ||
* @throws SocketException if adaptor doesn't exist or it doesn't have an IP Address | ||
*/ | ||
public static InetAddress eth0() throws SocketException { | ||
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); | ||
ArrayList<NetworkInterface> netList = Collections.list(nets); | ||
LOGGER.debug(netList.stream().map(NetworkInterface::getName).collect(Collectors.joining(", "))); | ||
NetworkInterface eth0 = netList.stream().filter(s -> s.getName().matches("eth0|en0")).findFirst().get(); | ||
ArrayList<InetAddress> inetAddresses = Collections.list(eth0.getInetAddresses()); | ||
Optional<InetAddress> address = inetAddresses.stream().filter(inetAddress -> inetAddress.getHostAddress().matches("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")).findFirst(); | ||
LOGGER.debug(eth0.getDisplayName() + ": " + address.get().getHostAddress()); | ||
return address.get(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
commons/src/main/java/org/apache/mesos/elasticsearch/common/SerializableIPAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.apache.mesos.elasticsearch.common; | ||
|
||
import org.apache.commons.lang.builder.HashCodeBuilder; | ||
|
||
import java.io.Serializable; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
/** | ||
* IP address to be sent over a mesos framework message | ||
*/ | ||
public class SerializableIPAddress implements Serializable { | ||
public InetAddress getAddress() { | ||
return address; | ||
} | ||
|
||
private final InetAddress address; | ||
|
||
public SerializableIPAddress(InetAddress address) { | ||
this.address = address; | ||
} | ||
|
||
public byte[] toBytes() { | ||
return address.getAddress(); | ||
} | ||
|
||
public static SerializableIPAddress fromBytes(byte[] data) throws UnknownHostException { | ||
return new SerializableIPAddress(InetAddress.getByAddress(data)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return address.getHostAddress(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return new HashCodeBuilder(17, 37). | ||
append(address). | ||
toHashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == this) { | ||
return true; | ||
} | ||
if (obj == null || obj.getClass() != this.getClass()) { | ||
return false; | ||
} | ||
|
||
SerializableIPAddress inetAddress = (SerializableIPAddress) obj; | ||
return this.getAddress().equals(inetAddress.getAddress()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> | ||
|
||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> | ||
<appender name="console" class="org.apache.log4j.ConsoleAppender"> | ||
<param name="Target" value="System.out"/> | ||
<layout class="org.apache.log4j.PatternLayout"> | ||
<param name="ConversionPattern" value="[%p] %d %c %M - %m%n"/> | ||
</layout> | ||
</appender> | ||
|
||
<logger name="org.apache.mesos.elasticsearch"> | ||
<level value="DEBUG"/> | ||
</logger> | ||
|
||
<root> | ||
<level value="WARN"/> | ||
<appender-ref ref="console" /> | ||
</root> | ||
|
||
</log4j:configuration> |
19 changes: 19 additions & 0 deletions
19
commons/src/test/java/org/apache/mesos/elasticsearch/common/AdaptorIPAddressTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.apache.mesos.elasticsearch.common; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* @author philwinder | ||
* @since 21/10/2015 | ||
*/ | ||
public class AdaptorIPAddressTest { | ||
|
||
@Test | ||
public void testEth0() throws Exception { | ||
assertNotNull(AdaptorIPAddress.eth0()); | ||
assertTrue(AdaptorIPAddress.eth0().getHostAddress().contains(".")); | ||
assertFalse(AdaptorIPAddress.eth0().getHostAddress().contains(":")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters