Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/497#discussion_r94429839
--- Diff:
locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsUtil.java
---
@@ -354,30 +355,60 @@ public static String
getFirstReachableAddress(ComputeServiceContext context, Nod
return getFirstReachableAddress(node, Duration.FIVE_MINUTES);
}
+ /**
+ * Uses {@link Networking#isReachablePredicate()} to determine
reachability.
+ * @see #getReachableAddresses(NodeMetadata, Duration, Predicate)
+ */
public static String getFirstReachableAddress(NodeMetadata node,
Duration timeout) {
- return getFirstReachableAddress(node, timeout, new
Networking.IsReachablePredicate());
+ return getFirstReachableAddress(node, timeout,
Networking.isReachablePredicate());
}
+ /** @see #getReachableAddresses(NodeMetadata, Duration, Predicate) */
public static String getFirstReachableAddress(NodeMetadata node,
Duration timeout, Predicate<? super HostAndPort> socketTester) {
+ Iterable<HostAndPort> addresses = getReachableAddresses(node,
timeout, socketTester);
+ HostAndPort address = Iterables.getFirst(addresses, null);
+ if (address != null) {
+ return address.getHostText();
+ } else {
+ throw new IllegalStateException("No reachable IPs for " + node
+ "; check whether the node is " +
+ "reachable and whether it meets the requirements of
the HostAndPort tester: " + socketTester);
+ }
+ }
+
+ /**
+ * @return The public and private addresses of node that are reachable.
+ * @see #getReachableAddresses(Iterable, Duration, Predicate)
+ */
+ public static Iterable<HostAndPort> getReachableAddresses(NodeMetadata
node, Duration timeout, Predicate<? super HostAndPort> socketTester) {
final int port = node.getLoginPort();
- List<HostAndPort> sockets = FluentIterable
- .from(Iterables.concat(node.getPublicAddresses(),
node.getPrivateAddresses()))
+ return
getReachableAddresses(Iterables.concat(node.getPublicAddresses(),
node.getPrivateAddresses()), port, timeout, socketTester);
+ }
+
+ /** @see #getReachableAddresses(Iterable, Duration, Predicate) */
+ public static Iterable<HostAndPort>
getReachableAddresses(Iterable<String> hosts, final int port, Duration timeout,
Predicate<? super HostAndPort> socketTester) {
+ FluentIterable<HostAndPort> sockets = FluentIterable
+ .from(hosts)
.transform(new Function<String, HostAndPort>() {
@Override public HostAndPort apply(String input) {
return HostAndPort.fromParts(input, port);
- }})
- .toList();
-
+ }});
+ return getReachableAddresses(sockets, timeout, socketTester);
+ }
+
+ /**
+ * Uses {@link ReachableSocketFinder} to determine which sockets are
reachable. Iterators
+ * are unmodifiable and are lazily evaluated.
+ * @param sockets The host-and-ports to test
+ * @param timeout Max time to try to connect to the ip:port
+ * @param socketTester A predicate determining reachability.
+ */
+ public static Iterable<HostAndPort>
getReachableAddresses(Iterable<HostAndPort> sockets, Duration timeout,
Predicate<? super HostAndPort> socketTester) {
ListeningExecutorService executor =
MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
try {
ReachableSocketFinder finder = new
ReachableSocketFinder(socketTester, executor);
- HostAndPort result = finder.findOpenSocketOnNode(sockets,
timeout);
- return result.getHostText();
- } catch (Exception e) {
--- End diff --
The `catch` here is useful for adding context to the exception - the node.
Could be useful when troubleshooting and cross-referncing logs.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---