use of java.net.InetAddress in project flink by apache.
the class InstanceManagerTest method testReportHeartbeat.
@Test
public void testReportHeartbeat() {
try {
InstanceManager cm = new InstanceManager();
final int dataPort = 20000;
ResourceID resID1 = ResourceID.generate();
ResourceID resID2 = ResourceID.generate();
ResourceID resID3 = ResourceID.generate();
HardwareDescription hardwareDescription = HardwareDescription.extractFromSystem(4096);
InetAddress address = InetAddress.getByName("127.0.0.1");
// register three instances
TaskManagerLocation ici1 = new TaskManagerLocation(resID1, address, dataPort);
TaskManagerLocation ici2 = new TaskManagerLocation(resID2, address, dataPort + 1);
TaskManagerLocation ici3 = new TaskManagerLocation(resID3, address, dataPort + 2);
JavaTestKit probe1 = new JavaTestKit(system);
JavaTestKit probe2 = new JavaTestKit(system);
JavaTestKit probe3 = new JavaTestKit(system);
InstanceID instanceID1 = cm.registerTaskManager(new ActorTaskManagerGateway(new AkkaActorGateway(probe1.getRef(), leaderSessionID)), ici1, hardwareDescription, 1);
InstanceID instanceID2 = cm.registerTaskManager(new ActorTaskManagerGateway(new AkkaActorGateway(probe2.getRef(), leaderSessionID)), ici2, hardwareDescription, 1);
InstanceID instanceID3 = cm.registerTaskManager(new ActorTaskManagerGateway(new AkkaActorGateway(probe3.getRef(), leaderSessionID)), ici3, hardwareDescription, 1);
// report some immediate heart beats
assertTrue(cm.reportHeartBeat(instanceID1));
assertTrue(cm.reportHeartBeat(instanceID2));
assertTrue(cm.reportHeartBeat(instanceID3));
// report heart beat for non-existing instance
assertFalse(cm.reportHeartBeat(new InstanceID()));
final long WAIT = 200;
CommonTestUtils.sleepUninterruptibly(WAIT);
Iterator<Instance> it = cm.getAllRegisteredInstances().iterator();
Instance instance1 = it.next();
long h1 = instance1.getLastHeartBeat();
long h2 = it.next().getLastHeartBeat();
long h3 = it.next().getLastHeartBeat();
// send one heart beat again and verify that the
assertTrue(cm.reportHeartBeat(instance1.getId()));
long newH1 = instance1.getLastHeartBeat();
long now = System.currentTimeMillis();
assertTrue(now - h1 >= WAIT);
assertTrue(now - h2 >= WAIT);
assertTrue(now - h3 >= WAIT);
assertTrue(now - newH1 <= WAIT);
cm.shutdown();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
Assert.fail("Test erroneous: " + e.getMessage());
}
}
use of java.net.InetAddress in project flink by apache.
the class InstanceTest method testAllocatingAndCancellingSlots.
@Test
public void testAllocatingAndCancellingSlots() {
try {
ResourceID resourceID = ResourceID.generate();
HardwareDescription hardwareDescription = new HardwareDescription(4, 2L * 1024 * 1024 * 1024, 1024 * 1024 * 1024, 512 * 1024 * 1024);
InetAddress address = InetAddress.getByName("127.0.0.1");
TaskManagerLocation connection = new TaskManagerLocation(resourceID, address, 10001);
Instance instance = new Instance(new ActorTaskManagerGateway(DummyActorGateway.INSTANCE), connection, new InstanceID(), hardwareDescription, 4);
assertEquals(4, instance.getTotalNumberOfSlots());
assertEquals(4, instance.getNumberOfAvailableSlots());
assertEquals(0, instance.getNumberOfAllocatedSlots());
SimpleSlot slot1 = instance.allocateSimpleSlot(new JobID());
SimpleSlot slot2 = instance.allocateSimpleSlot(new JobID());
SimpleSlot slot3 = instance.allocateSimpleSlot(new JobID());
SimpleSlot slot4 = instance.allocateSimpleSlot(new JobID());
assertNotNull(slot1);
assertNotNull(slot2);
assertNotNull(slot3);
assertNotNull(slot4);
assertEquals(0, instance.getNumberOfAvailableSlots());
assertEquals(4, instance.getNumberOfAllocatedSlots());
assertEquals(6, slot1.getSlotNumber() + slot2.getSlotNumber() + slot3.getSlotNumber() + slot4.getSlotNumber());
// no more slots
assertNull(instance.allocateSimpleSlot(new JobID()));
try {
instance.returnAllocatedSlot(slot2);
fail("instance accepted a non-cancelled slot.");
} catch (IllegalArgumentException e) {
// good
}
// release the slots. this returns them to the instance
slot1.releaseSlot();
slot2.releaseSlot();
slot3.releaseSlot();
slot4.releaseSlot();
assertEquals(4, instance.getNumberOfAvailableSlots());
assertEquals(0, instance.getNumberOfAllocatedSlots());
assertFalse(instance.returnAllocatedSlot(slot1));
assertFalse(instance.returnAllocatedSlot(slot2));
assertFalse(instance.returnAllocatedSlot(slot3));
assertFalse(instance.returnAllocatedSlot(slot4));
assertEquals(4, instance.getNumberOfAvailableSlots());
assertEquals(0, instance.getNumberOfAllocatedSlots());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of java.net.InetAddress in project flink by apache.
the class InstanceTest method testInstanceDies.
@Test
public void testInstanceDies() {
try {
ResourceID resourceID = ResourceID.generate();
HardwareDescription hardwareDescription = new HardwareDescription(4, 2L * 1024 * 1024 * 1024, 1024 * 1024 * 1024, 512 * 1024 * 1024);
InetAddress address = InetAddress.getByName("127.0.0.1");
TaskManagerLocation connection = new TaskManagerLocation(resourceID, address, 10001);
Instance instance = new Instance(new ActorTaskManagerGateway(DummyActorGateway.INSTANCE), connection, new InstanceID(), hardwareDescription, 3);
assertEquals(3, instance.getNumberOfAvailableSlots());
SimpleSlot slot1 = instance.allocateSimpleSlot(new JobID());
SimpleSlot slot2 = instance.allocateSimpleSlot(new JobID());
SimpleSlot slot3 = instance.allocateSimpleSlot(new JobID());
instance.markDead();
assertEquals(0, instance.getNumberOfAllocatedSlots());
assertEquals(0, instance.getNumberOfAvailableSlots());
assertTrue(slot1.isCanceled());
assertTrue(slot2.isCanceled());
assertTrue(slot3.isCanceled());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of java.net.InetAddress in project hadoop by apache.
the class DNS method getIPs.
/**
* Returns all the IPs associated with the provided interface, if any, in
* textual form.
*
* @param strInterface
* The name of the network interface or sub-interface to query
* (eg eth0 or eth0:0) or the string "default"
* @param returnSubinterfaces
* Whether to return IPs associated with subinterfaces of
* the given interface
* @return A string vector of all the IPs associated with the provided
* interface. The local host IP is returned if the interface
* name "default" is specified or there is an I/O error looking
* for the given interface.
* @throws UnknownHostException
* If the given interface is invalid
*
*/
public static String[] getIPs(String strInterface, boolean returnSubinterfaces) throws UnknownHostException {
if ("default".equals(strInterface)) {
return new String[] { cachedHostAddress };
}
NetworkInterface netIf;
try {
netIf = NetworkInterface.getByName(strInterface);
if (netIf == null) {
netIf = getSubinterface(strInterface);
}
} catch (SocketException e) {
LOG.warn("I/O error finding interface " + strInterface + ": " + e.getMessage());
return new String[] { cachedHostAddress };
}
if (netIf == null) {
throw new UnknownHostException("No such interface " + strInterface);
}
// NB: Using a LinkedHashSet to preserve the order for callers
// that depend on a particular element being 1st in the array.
// For example, getDefaultIP always returns the first element.
LinkedHashSet<InetAddress> allAddrs = new LinkedHashSet<InetAddress>();
allAddrs.addAll(Collections.list(netIf.getInetAddresses()));
if (!returnSubinterfaces) {
allAddrs.removeAll(getSubinterfaceInetAddrs(netIf));
}
String[] ips = new String[allAddrs.size()];
int i = 0;
for (InetAddress addr : allAddrs) {
ips[i++] = addr.getHostAddress();
}
return ips;
}
use of java.net.InetAddress in project hadoop by apache.
the class DNS method getHosts.
/**
* Returns all the host names associated by the provided nameserver with the
* address bound to the specified network interface
*
* @param strInterface
* The name of the network interface or subinterface to query
* (e.g. eth0 or eth0:0)
* @param nameserver
* The DNS host name
* @param tryfallbackResolution
* if true and if reverse DNS resolution fails then attempt to
* resolve the hostname with
* {@link InetAddress#getCanonicalHostName()} which includes
* hosts file resolution.
* @return A string vector of all host names associated with the IPs tied to
* the specified interface
* @throws UnknownHostException if the given interface is invalid
*/
public static String[] getHosts(String strInterface, @Nullable String nameserver, boolean tryfallbackResolution) throws UnknownHostException {
final List<String> hosts = new Vector<String>();
final List<InetAddress> addresses = getIPsAsInetAddressList(strInterface, true);
for (InetAddress address : addresses) {
try {
hosts.add(reverseDns(address, nameserver));
} catch (NamingException ignored) {
}
}
if (hosts.isEmpty() && tryfallbackResolution) {
for (InetAddress address : addresses) {
final String canonicalHostName = address.getCanonicalHostName();
// Don't use the result if it looks like an IP address.
if (!InetAddresses.isInetAddress(canonicalHostName)) {
hosts.add(canonicalHostName);
}
}
}
if (hosts.isEmpty()) {
LOG.warn("Unable to determine hostname for interface " + strInterface);
hosts.add(cachedHostname);
}
return hosts.toArray(new String[hosts.size()]);
}
Aggregations