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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.search;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
public class EsSettings implements EsSettingsMBean {
private static final Logger LOGGER = LoggerFactory.getLogger(EsSettings.class);
public static final String PROP_MARVEL_HOSTS = "sonar.search.marvelHosts";
public static final String CLUSTER_SEARCH_NODE_NAME = "sonar.cluster.search.nodeName";
public static final String STANDALONE_NODE_NAME = "sonarqube";
private final Props props;
private final boolean clusterEnabled;
private final String clusterName;
private final String nodeName;
EsSettings(Props props) {
this.props = props;
this.clusterName = props.nonNullValue(ProcessProperties.CLUSTER_NAME);
this.clusterEnabled = props.valueAsBoolean(ProcessProperties.CLUSTER_ENABLED);
if (this.clusterEnabled) {
this.nodeName = props.value(CLUSTER_SEARCH_NODE_NAME, "sonarqube-" + UUID.randomUUID().toString());
} else {
this.nodeName = STANDALONE_NODE_NAME;
}
}
@Override
public int getHttpPort() {
return props.valueAsInt(ProcessProperties.SEARCH_HTTP_PORT, -1);
}
@Override
public String getClusterName() {
return clusterName;
}
@Override
public String getNodeName() {
return nodeName;
}
Map<String, String> build() {
Map<String, String> builder = new HashMap<>();
configureFileSystem(builder);
configureNetwork(builder);
configureCluster(builder);
configureMarvel(builder);
return builder;
}
private void configureFileSystem(Map<String, String> builder) {
File homeDir = props.nonNullValueAsFile(ProcessProperties.PATH_HOME);
File dataDir;
File logDir;
// data dir
String dataPath = props.value(ProcessProperties.PATH_DATA);
if (StringUtils.isNotEmpty(dataPath)) {
dataDir = new File(dataPath, "es");
} else {
dataDir = new File(homeDir, "data/es");
}
builder.put("path.data", dataDir.getAbsolutePath());
String tempPath = props.value(ProcessProperties.PATH_TEMP);
builder.put("path.home", new File(tempPath, "es").getAbsolutePath());
// log dir
String logPath = props.value(ProcessProperties.PATH_LOGS);
if (StringUtils.isNotEmpty(logPath)) {
logDir = new File(logPath);
} else {
logDir = new File(homeDir, "log");
}
builder.put("path.logs", logDir.getAbsolutePath());
}
private void configureNetwork(Map<String, String> builder) {
InetAddress host = readHost();
int port = Integer.parseInt(props.nonNullValue(ProcessProperties.SEARCH_PORT));
LOGGER.info("Elasticsearch listening on {}:{}", host, port);
builder.put("transport.tcp.port", String.valueOf(port));
builder.put("transport.host", String.valueOf(host.getHostAddress()));
builder.put("network.host", String.valueOf(host.getHostAddress()));
// Elasticsearch sets the default value of TCP reuse address to true only on non-MSWindows machines, but why ?
builder.put("network.tcp.reuse_address", String.valueOf(true));
int httpPort = getHttpPort();
if (httpPort < 0) {
// standard configuration
builder.put("http.enabled", String.valueOf(false));
} else {
LOGGER.warn("Elasticsearch HTTP connector is enabled on port {}. MUST NOT BE USED FOR PRODUCTION", httpPort);
// see https://github.com/lmenezes/elasticsearch-kopf/issues/195
builder.put("http.cors.enabled", String.valueOf(true));
builder.put("http.cors.allow-origin", "*");
builder.put("http.enabled", String.valueOf(true));
builder.put("http.host", host.getHostAddress());
builder.put("http.port", String.valueOf(httpPort));
}
}
private InetAddress readHost() {
String hostProperty = props.nonNullValue(ProcessProperties.SEARCH_HOST);
try {
return InetAddress.getByName(hostProperty);
} catch (UnknownHostException e) {
throw new IllegalStateException("Can not resolve host [" + hostProperty + "]. Please check network settings and property " + ProcessProperties.SEARCH_HOST, e);
}
}
private void configureCluster(Map<String, String> builder) {
// Default value in a standalone mode, not overridable
int minimumMasterNodes = 1;
String initialStateTimeOut = "30s";
if (clusterEnabled) {
minimumMasterNodes = props.valueAsInt(ProcessProperties.SEARCH_MINIMUM_MASTER_NODES, 2);
initialStateTimeOut = props.value(ProcessProperties.SEARCH_INITIAL_STATE_TIMEOUT, "120s");
String hosts = props.value(ProcessProperties.CLUSTER_SEARCH_HOSTS, "");
LOGGER.info("Elasticsearch cluster enabled. Connect to hosts [{}]", hosts);
builder.put("discovery.zen.ping.unicast.hosts", hosts);
}
builder.put("discovery.zen.minimum_master_nodes", String.valueOf(minimumMasterNodes));
builder.put("discovery.initial_state_timeout", initialStateTimeOut);
builder.put("cluster.name", getClusterName());
builder.put("cluster.routing.allocation.awareness.attributes", "rack_id");
builder.put("node.attr.rack_id", nodeName);
builder.put("node.name", nodeName);
builder.put("node.data", String.valueOf(true));
builder.put("node.master", String.valueOf(true));
}
private void configureMarvel(Map<String, String> builder) {
Set<String> marvels = new TreeSet<>();
marvels.addAll(Arrays.asList(StringUtils.split(props.value(PROP_MARVEL_HOSTS, ""), ",")));
// If we're collecting indexing data send them to the Marvel host(s)
if (!marvels.isEmpty()) {
String hosts = StringUtils.join(marvels, ",");
LOGGER.info("Elasticsearch Marvel is enabled for %s", hosts);
builder.put("marvel.agent.exporter.es.hosts", hosts);
}
}
}
|