Skip to content

Commit

Permalink
[Java]: Prevent creating both unresolved InetSocketAddress AND except…
Browse files Browse the repository at this point in the history
…ion on re-resolution failures while also preserving RuntimeException with UnknownHostException for initial resolution failures. Make sure that name resolution transport updates time before re-resolution. Revert beahvior if checking for unresolved re-resolutions and logging error. Changing exception caught in re-resolution to handle any Throwable to account for format and name lookup/translation format errors.
  • Loading branch information
tmontgomery committed May 18, 2021
1 parent 5b16757 commit 62362a0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
20 changes: 16 additions & 4 deletions aeron-driver/src/main/java/io/aeron/driver/DriverConductor.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,18 @@ void onReResolveEndpoint(
final InetSocketAddress newAddress = UdpChannel.resolve(
endpoint, CommonContext.ENDPOINT_PARAM_NAME, true, nameResolver);

if (!address.equals(newAddress))
if (newAddress.isUnresolved())
{
ctx.errorHandler().onError(new UnknownHostException(
"endpoint could not be re-resolved: endpoint=" + endpoint));
errorCounter.increment();
}
else if (!address.equals(newAddress))
{
senderProxy.onResolutionChange(channelEndpoint, endpoint, newAddress);
}
}
catch (final UnknownHostException ex)
catch (final Throwable ex)
{
ctx.errorHandler().onError(ex);
errorCounter.increment();
Expand All @@ -362,12 +368,18 @@ void onReResolveControl(
final InetSocketAddress newAddress = UdpChannel.resolve(
control, CommonContext.MDC_CONTROL_PARAM_NAME, true, nameResolver);

if (!address.equals(newAddress))
if (newAddress.isUnresolved())
{
ctx.errorHandler().onError(new UnknownHostException(
"control could not be re-resolved: control=" + control));
errorCounter.increment();
}
else if (!address.equals(newAddress))
{
receiverProxy.onResolutionChange(channelEndpoint, udpChannel, newAddress);
}
}
catch (final UnknownHostException ex)
catch (final Throwable ex)
{
ctx.errorHandler().onError(ex);
errorCounter.increment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ private void sendSelfResolutions(final long nowMs)
{
if (nowMs > bootstrapNeighborResolveDeadlineMs)
{
bootstrapNeighborAddress = UdpNameResolutionTransport.getInetSocketAddress(bootstrapNeighbor);
bootstrapNeighborResolveDeadlineMs = nowMs + TIMEOUT_MS;
bootstrapNeighborAddress = UdpNameResolutionTransport.getInetSocketAddress(bootstrapNeighbor);
}

sendResolutionFrameTo(byteBuffer, bootstrapNeighborAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

class SocketAddressParser
{
Expand All @@ -45,11 +44,9 @@ enum IpV6State
* @param isReResolution for the parse.
* @param nameResolver to be used for resolving hostnames.
* @return An {@link InetSocketAddress} for the parsed input.
* @throws UnknownHostException if address cannot be resolved.
*/
static InetSocketAddress parse(
final String value, final String uriParamName, final boolean isReResolution, final NameResolver nameResolver)
throws UnknownHostException
{
if (Strings.isEmpty(value))
{
Expand All @@ -69,13 +66,6 @@ static InetSocketAddress parse(
throw new IllegalArgumentException("invalid format: " + value);
}

if (address.isUnresolved())
{
throw new UnknownHostException(
"unresolved - " + uriParamName + "=" + value + ", name-and-port=" + nameAndPort +
", name-resolver=" + nameResolver.getClass().getName());
}

return address;
}

Expand Down
16 changes: 14 additions & 2 deletions aeron-driver/src/main/java/io/aeron/driver/media/UdpChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,9 @@ public static InetSocketAddress destinationAddress(final ChannelUri uri, final N
* @param isReResolution for the resolution
* @param nameResolver to be used for hostname.
* @return address for endpoint
* @throws UnknownHostException if the endpoint can not be resolved.
*/
public static InetSocketAddress resolve(
final String endpoint, final String uriParamName, final boolean isReResolution, final NameResolver nameResolver)
throws UnknownHostException
{
return SocketAddressParser.parse(endpoint, uriParamName, isReResolution, nameResolver);
}
Expand Down Expand Up @@ -770,6 +768,13 @@ private static InetSocketAddress getEndpointAddress(final ChannelUri uri, final
{
address = SocketAddressParser.parse(
endpointValue, CommonContext.ENDPOINT_PARAM_NAME, false, nameResolver);

if (address.isUnresolved())
{
throw new UnknownHostException(
"unresolved - " + CommonContext.ENDPOINT_PARAM_NAME + "=" + endpointValue +
", name-resolver=" + nameResolver.getClass().getName());
}
}
catch (final UnknownHostException ex)
{
Expand All @@ -790,6 +795,13 @@ private static InetSocketAddress getExplicitControlAddress(final ChannelUri uri,
{
address = SocketAddressParser.parse(
controlValue, CommonContext.MDC_CONTROL_PARAM_NAME, false, nameResolver);

if (address.isUnresolved())
{
throw new UnknownHostException(
"unresolved - " + CommonContext.MDC_CONTROL_PARAM_NAME + "=" + controlValue +
", name-resolver=" + nameResolver.getClass().getName());
}
}
catch (final UnknownHostException ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,25 @@ public static InetSocketAddress getInterfaceAddress(final String addressAndPort)
*/
public static InetSocketAddress getInetSocketAddress(final String hostAndPort)
{
InetSocketAddress address = null;

try
{
return SocketAddressParser.parse(
address = SocketAddressParser.parse(
hostAndPort, CommonContext.ENDPOINT_PARAM_NAME, false, DefaultNameResolver.INSTANCE);

if (address.isUnresolved())
{
throw new UnknownHostException(
"unresolved - " + CommonContext.ENDPOINT_PARAM_NAME + "=" + hostAndPort +
", name-resolver=" + DefaultNameResolver.INSTANCE.getClass().getName());
}
}
catch (final UnknownHostException ex)
{
LangUtil.rethrowUnchecked(ex);
return null;
}

return address;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import io.aeron.logbuffer.FragmentHandler;
import io.aeron.logbuffer.Header;
import io.aeron.logbuffer.LogBufferDescriptor;
import io.aeron.test.*;
import io.aeron.test.SlowTest;
import io.aeron.test.Tests;
import io.aeron.test.driver.DistinctErrorLogTestWatcher;
import io.aeron.test.driver.MediaDriverTestWatcher;
import io.aeron.test.driver.TestMediaDriver;
Expand All @@ -43,7 +44,6 @@
import static io.aeron.CommonContext.ENDPOINT_PARAM_NAME;
import static io.aeron.CommonContext.MDC_CONTROL_PARAM_NAME;
import static io.aeron.driver.status.SystemCounterDescriptor.RESOLUTION_CHANGES;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -395,9 +395,8 @@ public void shouldReportErrorOnReResolveFailure() throws IOException
Tests.awaitCounterDelta(
client.countersReader(), SystemCounterDescriptor.ERRORS.id(), initialErrorCount, 1);

final Matcher<String> exceptionMessageMatcher = allOf(
containsString("endpoint=" + ENDPOINT_WITH_ERROR_NAME),
containsString("name-and-port=" + BAD_ADDRESS));
final Matcher<String> exceptionMessageMatcher =
containsString("endpoint=" + ENDPOINT_WITH_ERROR_NAME);

SystemTests.waitForErrorToOccur(
client.context().aeronDirectoryName(),
Expand Down

0 comments on commit 62362a0

Please sign in to comment.