/
ProxyBulkRequestBuilder.java
166 lines (148 loc) · 5.31 KB
/
ProxyBulkRequestBuilder.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
155
156
157
158
159
160
161
162
163
164
165
166
/*
* 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.es.request;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset.Entry;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ListenableActionFuture;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.TimeValue;
import org.sonar.api.utils.log.Profiler;
import org.sonar.server.es.EsClient;
import java.util.Set;
public class ProxyBulkRequestBuilder extends BulkRequestBuilder {
public ProxyBulkRequestBuilder(Client client) {
super(client);
}
@Override
public BulkResponse get() {
Profiler profiler = Profiler.createIfTrace(EsClient.LOGGER).start();
try {
return super.execute().actionGet();
} catch (Exception e) {
throw new IllegalStateException(String.format("Fail to execute %s", toString()), e);
} finally {
if (profiler.isTraceEnabled()) {
profiler.stopTrace(toString());
}
}
}
@Override
public BulkResponse get(TimeValue timeout) {
throw unsupported();
}
@Override
public BulkResponse get(String timeout) {
// easy to implement if needed (copy get())
throw unsupported();
}
@Override
public ListenableActionFuture<BulkResponse> execute() {
throw new UnsupportedOperationException("execute() should not be called as it's used for asynchronous");
}
private UnsupportedOperationException unsupported() {
throw new UnsupportedOperationException("See " + ProxyBulkRequestBuilder.class.getName());
}
@Override
public String toString() {
StringBuilder message = new StringBuilder();
message.append("Bulk[");
HashMultiset<BulkRequestKey> groupedRequests = HashMultiset.create();
for (int i = 0; i < request.requests().size(); i++) {
ActionRequest item = request.requests().get(i);
String requestType, index, docType;
if (item instanceof IndexRequest) {
IndexRequest request = (IndexRequest) item;
requestType = "index";
index = request.index();
docType = request.type();
} else if (item instanceof UpdateRequest) {
UpdateRequest request = (UpdateRequest) item;
requestType = "update";
index = request.index();
docType = request.type();
} else if (item instanceof DeleteRequest) {
DeleteRequest request = (DeleteRequest) item;
requestType = "delete";
index = request.index();
docType = request.type();
} else {
// Cannot happen, not allowed by BulkRequest's contract
throw new IllegalStateException("Unsupported bulk request type: " + item.getClass());
}
groupedRequests.add(new BulkRequestKey(requestType, index, docType));
}
Set<Entry<BulkRequestKey>> entrySet = groupedRequests.entrySet();
int size = entrySet.size();
int current = 0;
for (Entry<BulkRequestKey> requestEntry : entrySet) {
message.append(requestEntry.getCount()).append(" ").append(requestEntry.getElement().toString());
current++;
if (current < size) {
message.append(", ");
}
}
message.append("]");
return message.toString();
}
private static class BulkRequestKey {
private String requestType;
private String index;
private String docType;
private BulkRequestKey(String requestType, String index, String docType) {
this.requestType = requestType;
this.index = index;
this.docType = docType;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BulkRequestKey that = (BulkRequestKey) o;
if (!docType.equals(that.docType)) {
return false;
}
if (!index.equals(that.index)) {
return false;
}
return requestType.equals(that.requestType);
}
@Override
public int hashCode() {
int result = requestType.hashCode();
result = 31 * result + index.hashCode();
result = 31 * result + docType.hashCode();
return result;
}
@Override
public String toString() {
return String.format("%s request(s) on index %s and type %s", requestType, index, docType);
}
}
}