/
NodeBuilder.java
154 lines (139 loc) · 4.89 KB
/
NodeBuilder.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.node;
import org.elasticsearch.common.settings.Settings;
/**
* A node builder is used to construct a {@link Node} instance.
* <p/>
* <p>Settings will be loaded relative to the ES home (with or without <tt>config/</tt> prefix) and if not found,
* within the classpath (with or without <tt>config/<tt> prefix). The settings file loaded can either be named
* <tt>elasticsearch.yml</tt> or <tt>elasticsearch.json</tt>).
* <p/>
* <p>Explicit settings can be passed by using the {@link #settings(org.elasticsearch.common.settings.Settings)} method.
* <p/>
* <p>In any case, settings will be resolved from system properties as well that are either prefixed with <tt>es.</tt>
* or <tt>elasticsearch.</tt>.
* <p/>
* <p>An example for creating a simple node with optional settings loaded from the classpath:
* <p/>
* <pre>
* Node node = NodeBuilder.nodeBuilder().node();
* </pre>
* <p/>
* <p>An example for creating a node with explicit settings (in this case, a node in the cluster that does not hold
* data):
* <p/>
* <pre>
* Node node = NodeBuilder.nodeBuilder()
* .settings(Settings.settingsBuilder().put("node.data", false)
* .node();
* </pre>
* <p/>
* <p>When done with the node, make sure you call {@link Node#close()} on it.
*
*
*/
public class NodeBuilder {
private final Settings.Builder settings = Settings.settingsBuilder();
/**
* A convenient factory method to create a {@link NodeBuilder}.
*/
public static NodeBuilder nodeBuilder() {
return new NodeBuilder();
}
/**
* Set addition settings simply by working directly against the settings builder.
*/
public Settings.Builder settings() {
return settings;
}
/**
* Set addition settings simply by working directly against the settings builder.
*/
public Settings.Builder getSettings() {
return settings;
}
/**
* Explicit node settings to set.
*/
public NodeBuilder settings(Settings.Builder settings) {
return settings(settings.build());
}
/**
* Explicit node settings to set.
*/
public NodeBuilder settings(Settings settings) {
this.settings.put(settings);
return this;
}
/**
* Is the node going to be a client node which means it will hold no data (<tt>node.data</tt> is
* set to <tt>false</tt>) and other optimizations by different modules.
*
* @param client Should the node be just a client node or not.
*/
public NodeBuilder client(boolean client) {
settings.put("node.client", client);
return this;
}
/**
* Is the node going to be allowed to allocate data (shards) to it or not. This setting map to
* the <tt>node.data</tt> setting. Note, when setting {@link #client(boolean)}, the node will
* not hold any data by default.
*
* @param data Should the node be allocated data to or not.
*/
public NodeBuilder data(boolean data) {
settings.put("node.data", data);
return this;
}
/**
* Is the node a local node. A local node is a node that uses a local (JVM level) discovery and
* transport. Other (local) nodes started within the same JVM (actually, class-loader) will be
* discovered and communicated with. Nodes outside of the JVM will not be discovered.
*
* @param local Should the node be local or not
*/
public NodeBuilder local(boolean local) {
settings.put("node.local", local);
return this;
}
/**
* The cluster name this node is part of (maps to the <tt>cluster.name</tt> setting). Defaults
* to <tt>elasticsearch</tt>.
*
* @param clusterName The cluster name this node is part of.
*/
public NodeBuilder clusterName(String clusterName) {
settings.put("cluster.name", clusterName);
return this;
}
/**
* Builds the node without starting it.
*/
public Node build() {
return new Node(settings.build());
}
/**
* {@link #build()}s and starts the node.
*/
public Node node() {
return build().start();
}
}