31
31
import org .elasticsearch .common .settings .Setting ;
32
32
import org .elasticsearch .common .settings .Setting .Property ;
33
33
import org .elasticsearch .common .settings .Settings ;
34
+ import org .elasticsearch .common .transport .TransportAddress ;
34
35
import org .elasticsearch .discovery .single .SingleNodeDiscovery ;
36
+ import org .elasticsearch .discovery .zen .SettingsBasedHostsProvider ;
35
37
import org .elasticsearch .discovery .zen .UnicastHostsProvider ;
36
38
import org .elasticsearch .discovery .zen .ZenDiscovery ;
37
39
import org .elasticsearch .plugins .DiscoveryPlugin ;
42
44
import java .util .Collection ;
43
45
import java .util .Collections ;
44
46
import java .util .HashMap ;
47
+ import java .util .HashSet ;
45
48
import java .util .List ;
46
49
import java .util .Map ;
47
50
import java .util .Objects ;
48
- import java .util .Optional ;
51
+ import java .util .Set ;
49
52
import java .util .function .BiConsumer ;
50
53
import java .util .function .Function ;
51
54
import java .util .function .Supplier ;
55
+ import java .util .stream .Collectors ;
52
56
53
57
/**
54
58
* A module for loading classes for node discovery.
@@ -57,18 +61,18 @@ public class DiscoveryModule {
57
61
58
62
public static final Setting <String > DISCOVERY_TYPE_SETTING =
59
63
new Setting <>("discovery.type" , "zen" , Function .identity (), Property .NodeScope );
60
- public static final Setting <Optional <String >> DISCOVERY_HOSTS_PROVIDER_SETTING =
61
- new Setting <> ("discovery.zen.hosts_provider" , ( String ) null , Optional :: ofNullable , Property .NodeScope );
64
+ public static final Setting <List <String >> DISCOVERY_HOSTS_PROVIDER_SETTING =
65
+ Setting . listSetting ("discovery.zen.hosts_provider" , Collections . emptyList (), Function . identity () , Property .NodeScope );
62
66
63
67
private final Discovery discovery ;
64
68
65
69
public DiscoveryModule (Settings settings , ThreadPool threadPool , TransportService transportService ,
66
70
NamedWriteableRegistry namedWriteableRegistry , NetworkService networkService , MasterService masterService ,
67
71
ClusterApplier clusterApplier , ClusterSettings clusterSettings , List <DiscoveryPlugin > plugins ,
68
72
AllocationService allocationService ) {
69
- final UnicastHostsProvider hostsProvider ;
70
73
final Collection <BiConsumer <DiscoveryNode ,ClusterState >> joinValidators = new ArrayList <>();
71
- Map <String , Supplier <UnicastHostsProvider >> hostProviders = new HashMap <>();
74
+ final Map <String , Supplier <UnicastHostsProvider >> hostProviders = new HashMap <>();
75
+ hostProviders .put ("settings" , () -> new SettingsBasedHostsProvider (settings , transportService ));
72
76
for (DiscoveryPlugin plugin : plugins ) {
73
77
plugin .getZenHostsProviders (transportService , networkService ).entrySet ().forEach (entry -> {
74
78
if (hostProviders .put (entry .getKey (), entry .getValue ()) != null ) {
@@ -80,17 +84,32 @@ public DiscoveryModule(Settings settings, ThreadPool threadPool, TransportServic
80
84
joinValidators .add (joinValidator );
81
85
}
82
86
}
83
- Optional <String > hostsProviderName = DISCOVERY_HOSTS_PROVIDER_SETTING .get (settings );
84
- if (hostsProviderName .isPresent ()) {
85
- Supplier <UnicastHostsProvider > hostsProviderSupplier = hostProviders .get (hostsProviderName .get ());
86
- if (hostsProviderSupplier == null ) {
87
- throw new IllegalArgumentException ("Unknown zen hosts provider [" + hostsProviderName .get () + "]" );
88
- }
89
- hostsProvider = Objects .requireNonNull (hostsProviderSupplier .get ());
90
- } else {
91
- hostsProvider = Collections ::emptyList ;
87
+ List <String > hostsProviderNames = DISCOVERY_HOSTS_PROVIDER_SETTING .get (settings );
88
+ // for bwc purposes, add settings provider even if not explicitly specified
89
+ if (hostsProviderNames .contains ("settings" ) == false ) {
90
+ List <String > extendedHostsProviderNames = new ArrayList <>();
91
+ extendedHostsProviderNames .add ("settings" );
92
+ extendedHostsProviderNames .addAll (hostsProviderNames );
93
+ hostsProviderNames = extendedHostsProviderNames ;
94
+ }
95
+
96
+ final Set <String > missingProviderNames = new HashSet <>(hostsProviderNames );
97
+ missingProviderNames .removeAll (hostProviders .keySet ());
98
+ if (missingProviderNames .isEmpty () == false ) {
99
+ throw new IllegalArgumentException ("Unknown zen hosts providers " + missingProviderNames );
92
100
}
93
101
102
+ List <UnicastHostsProvider > filteredHostsProviders = hostsProviderNames .stream ()
103
+ .map (hostProviders ::get ).map (Supplier ::get ).collect (Collectors .toList ());
104
+
105
+ final UnicastHostsProvider hostsProvider = hostsResolver -> {
106
+ final List <TransportAddress > addresses = new ArrayList <>();
107
+ for (UnicastHostsProvider provider : filteredHostsProviders ) {
108
+ addresses .addAll (provider .buildDynamicHosts (hostsResolver ));
109
+ }
110
+ return Collections .unmodifiableList (addresses );
111
+ };
112
+
94
113
Map <String , Supplier <Discovery >> discoveryTypes = new HashMap <>();
95
114
discoveryTypes .put ("zen" ,
96
115
() -> new ZenDiscovery (settings , threadPool , transportService , namedWriteableRegistry , masterService , clusterApplier ,
0 commit comments