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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.server.search;
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsNodes;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.core.cluster.IndexAction;
import org.sonar.core.cluster.IndexAction.Method;
import org.sonar.core.cluster.WorkQueue;
import org.sonar.core.db.Dao;
import org.sonar.core.profiling.Profiling;
import org.sonar.core.profiling.Profiling.Level;
import org.sonar.core.profiling.StopWatch;
import java.io.Serializable;
public abstract class BaseIndex<K extends Serializable> implements Index<K> {
private static final String PROFILE_DOMAIN = "es";
private static final Logger LOG = LoggerFactory.getLogger(BaseIndex.class);
public static final String ES_CLUSTER_NAME = "sonarqube";
private static final String LOCAL_ES_NODE_HOST = "localhost";
private static final int LOCAL_ES_NODE_PORT = 9300;
private final Profiling profiling;
private Client client;
private WorkQueue workQueue;
private IndexSynchronizer<K> synchronizer;
protected Dao<?, K> dao;
public BaseIndex(WorkQueue workQueue, Dao<?, K> dao, Profiling profiling) {
this.profiling = profiling;
this.workQueue = workQueue;
this.synchronizer = new IndexSynchronizer<K>(this, dao, this.workQueue);
this.dao = dao;
}
protected Dao<?, K> getDao() {
return this.dao;
}
protected Client getClient() {
return this.client;
}
/* Component Methods */
@Override
public void start() {
/* Connect to the local ES Cluster */
this.connect();
/* Setup the index if necessary */
this.intializeIndex();
/* Launch synchronization */
synchronizer.start();
}
@Override
public void stop() {
if (client != null) {
client.close();
}
}
private StopWatch createWatch() {
return profiling.start(PROFILE_DOMAIN, Level.FULL);
}
public void connect() {
/* Settings to access our local ES node */
Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.sniff", true)
.put("cluster.name", ES_CLUSTER_NAME)
.put("node.name", "localclient_")
.build();
this.client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(LOCAL_ES_NODE_HOST, LOCAL_ES_NODE_PORT));
/*
* Cannot do that yet, need version >= 1.0
* ImmutableList<DiscoveryNode> nodes = client.connectedNodes();
* if (nodes.isEmpty()) {
* throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!");
* } else {
* log.info("connected to nodes: " + nodes.toString());
* }
*/
}
/* Cluster And ES Stats/Client methods */
private void intializeIndex() {
String index = this.getIndexName();
IndicesExistsResponse indexExistsResponse = client.admin().indices()
.prepareExists(index).execute().actionGet();
if (!indexExistsResponse.isExists()) {
client.admin().indices().prepareCreate(index)
.setSettings(getIndexSettings())
.addMapping(getType(), getMapping())
.execute().actionGet();
}
}
public ClusterStatsNodes getNodesStats() {
StopWatch watch = createWatch();
try {
return client.admin().cluster().prepareClusterStats().get().getNodesStats();
} finally {
watch.stop("ping from transport client");
}
}
/* Index Action Methods */
@Override
public void executeAction(IndexAction<K> action) {
if (action.getMethod().equals(Method.DELETE)) {
this.delete(action.getKey());
} else if (action.getMethod().equals(Method.INSERT)) {
this.insert(action.getKey());
} else if (action.getMethod().equals(Method.UPDATE)) {
this.update(action.getKey());
}
}
/* Index management methods */
protected abstract Settings getIndexSettings();
protected abstract String getType();
protected abstract XContentBuilder getMapping();
/* Base CRUD methods */
protected abstract QueryBuilder getKeyQuery(K key);
@Override
public Hit getByKey(K key) {
getClient().prepareSearch(this.getIndexName())
.setQuery(getKeyQuery(key))
.get();
return null;
}
@Override
public void insert(K key) {
this.update(key);
}
@Override
public void update(K key) {
IndexResponse result = getClient().index(new IndexRequest()
.type(this.getType())
.index(this.getIndexName())
.source(this.normalize(key))).actionGet();
}
@Override
public void delete(K key) {
DeleteByQueryResponse result = getClient().prepareDeleteByQuery(this.getIndexName())
.setQuery(getKeyQuery(key)).get();
}
/* Synchronization methods */
Long lastSynch = 0l;
long cooldown = 30000;
@Override
public void setLastSynchronization(Long time) {
if (time > (getLastSynchronization() + cooldown)) {
LOG.trace("Updating synchTime updating");
lastSynch = time;
} else {
LOG.trace("Not updating synchTime, still cooling down");
}
}
@Override
public Long getLastSynchronization() {
// need to read that in the admin index;
return 0l;
}
}
|