-
Notifications
You must be signed in to change notification settings - Fork 214
/
DockerSwarmServiceDiscovery.java
93 lines (81 loc) · 3.72 KB
/
DockerSwarmServiceDiscovery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.base.service;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import org.apache.pekko.actor.ExtendedActorSystem;
import org.apache.pekko.discovery.Lookup;
import org.apache.pekko.discovery.ServiceDiscovery;
import org.apache.pekko.dispatch.Futures;
import org.apache.pekko.pattern.Patterns;
import scala.Option;
import scala.collection.JavaConverters;
import scala.concurrent.Future;
import scala.concurrent.duration.FiniteDuration;
/**
* ServiceDiscovery usable for a Docker swarm based cluster.
* <p>
* One speciality of a Docker swarm based cluster is that the Docker swarm DNS sets a TTL of DNS entries to 600 seconds
* (10 minutes) - so if a cluster forms and not all DNS entries are "there" from the beginning, it takes 10 minutes
* until DNS caches used by the default Akka {@link org.apache.pekko.discovery.dns.DnsServiceDiscovery DnsServiceDiscovery} are
* evicted and another DNS lookup is done.
* </p>
* This implementation does not cache DNS entries and can therefore be used for bootstrapping in Docker swarm.
*/
public final class DockerSwarmServiceDiscovery extends ServiceDiscovery {
private final ExtendedActorSystem system;
/**
* Constructs a new instance of DockerSwarmServiceDiscovery.
*
* @param system the ActorSystem.
*/
public DockerSwarmServiceDiscovery(final ExtendedActorSystem system) {
this.system = system;
}
@Override
public Future<Resolved> lookup(final Lookup lookup, final FiniteDuration resolveTimeout) {
return Futures.<Resolved>firstCompletedOf(Arrays.asList(
resolveService(lookup),
scheduleTimeout(lookup, resolveTimeout)
), system.dispatcher());
}
private Future<Resolved> resolveService(final Lookup lookup) {
final String serviceName = lookup.serviceName();
return Futures.<Resolved>future(() -> {
final InetAddress[] allResolvedHosts;
try {
allResolvedHosts = InetAddress.getAllByName(serviceName);
} catch (final UnknownHostException e) {
return new Resolved(serviceName,
JavaConverters.<ResolvedTarget>asScalaBuffer(Collections.emptyList()).toList());
}
final List<ResolvedTarget> resolvedTargets = Arrays.stream(allResolvedHosts)
.map(a -> new ResolvedTarget(a.getCanonicalHostName(), Option.empty(), Option.apply(a)))
.collect(Collectors.toList());
final Resolved resolved = new Resolved(serviceName, JavaConverters.asScalaBuffer(resolvedTargets).toList());
system.log().debug("[DockerSwarmServiceDiscovery] Resolved <{}> via InetAddress to: {}", lookup,
resolved);
return resolved;
}, system.dispatcher());
}
private Future<Resolved> scheduleTimeout(final Lookup lookup, final FiniteDuration resolveTimeout) {
return Patterns.<Resolved>after(resolveTimeout, system.scheduler(), system.dispatcher(),
Futures.<Resolved>failed(
new TimeoutException("Lookup <" + lookup + "> timed out after " + resolveTimeout)));
}
}