-
Notifications
You must be signed in to change notification settings - Fork 112
/
ElasticSearchCAPIBehavior.java
398 lines (340 loc) · 16.4 KB
/
ElasticSearchCAPIBehavior.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* Copyright (c) 2012 Couchbase, Inc. All rights reserved.
*
* Licensed 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.transport.couchbase.capi;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.codehaus.jackson.map.ObjectMapper;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Base64;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.metrics.CounterMetric;
import org.elasticsearch.common.metrics.MeanMetric;
import com.couchbase.capi.CAPIBehavior;
public class ElasticSearchCAPIBehavior implements CAPIBehavior {
protected ObjectMapper mapper = new ObjectMapper();
protected Client client;
protected ESLogger logger;
protected String defaultDocumentType;
protected String checkpointDocumentType;
protected String dynamicTypePath;
protected boolean resolveConflicts;
protected CounterMetric activeRevsDiffRequests;
protected MeanMetric meanRevsDiffRequests;
protected CounterMetric activeBulkDocsRequests;
protected MeanMetric meanBulkDocsRequests;
public ElasticSearchCAPIBehavior(Client client, ESLogger logger, String defaultDocumentType, String checkpointDocumentType, String dynamicTypePath, boolean resolveConflicts) {
this.client = client;
this.logger = logger;
this.defaultDocumentType = defaultDocumentType;
this.checkpointDocumentType = checkpointDocumentType;
this.dynamicTypePath = dynamicTypePath;
this.resolveConflicts = resolveConflicts;
this.activeRevsDiffRequests = new CounterMetric();
this.meanRevsDiffRequests = new MeanMetric();
this.activeBulkDocsRequests = new CounterMetric();
this.meanBulkDocsRequests = new MeanMetric();
}
@Override
public boolean databaseExists(String database) {
String index = getElasticSearchIndexNameFromDatabase(database);
IndicesExistsRequestBuilder existsBuilder = client.admin().indices().prepareExists(index);
IndicesExistsResponse response = existsBuilder.execute().actionGet();
if(response.exists()) {
return true;
}
return false;
}
@Override
public Map<String, Object> getDatabaseDetails(String database) {
if(databaseExists(database)) {
Map<String, Object> responseMap = new HashMap<String, Object>();
responseMap.put("db_name", getDatabaseNameWithoutUUID(database));
return responseMap;
}
return null;
}
@Override
public boolean createDatabase(String database) {
throw new UnsupportedOperationException("Creating indexes is not supported");
}
@Override
public boolean deleteDatabase(String database) {
throw new UnsupportedOperationException("Deleting indexes is not supported");
}
@Override
public boolean ensureFullCommit(String database) {
return true;
}
@Override
public Map<String, Object> revsDiff(String database,
Map<String, Object> revsMap) {
long start = System.currentTimeMillis();
activeRevsDiffRequests.inc();
logger.trace("_revs_diff request for {} : {}", database, revsMap);
// start with all entries in the response map
Map<String, Object> responseMap = new HashMap<String, Object>();
for (Entry<String, Object> entry : revsMap.entrySet()) {
String id = entry.getKey();
String revs = (String)entry.getValue();
Map<String, String> rev = new HashMap<String, String>();
rev.put("missing", revs);
responseMap.put(id, rev);
}
logger.trace("_revs_diff response for {} is: {}", database, responseMap);
// if resolve conflicts mode is enabled
// perform a multi-get query to find information
// about revisions we already have
if (resolveConflicts) {
String index = getElasticSearchIndexNameFromDatabase(database);
MultiGetResponse response = client.prepareMultiGet().add(index, defaultDocumentType, responseMap.keySet()).execute().actionGet();
if(response != null) {
Iterator<MultiGetItemResponse> iterator = response.iterator();
while(iterator.hasNext()) {
MultiGetItemResponse item = iterator.next();
if(item.response().exists()) {
String itemId = item.id();
Map<String, Object> source = item.response().sourceAsMap();
if(source != null) {
Map<String, Object> meta = (Map<String, Object>)source.get("meta");
if(meta != null) {
String rev = (String)meta.get("rev");
//retrieve the revision passed in from Couchbase
Map<String, String> sourceRevMap = (Map<String, String>)responseMap.get(itemId);
String sourceRev = sourceRevMap.get("missing");
if(rev.equals(sourceRev)) {
// if our revision is the same as the source rev
// remove it from the response map
responseMap.remove(itemId);
logger.trace("_revs_diff already have id: {} rev: {}", itemId, rev);
}
}
}
}
}
}
logger.trace("_revs_diff response AFTER conflict resolution {}", responseMap);
}
long end = System.currentTimeMillis();
meanRevsDiffRequests.inc(end - start);
activeRevsDiffRequests.dec();
return responseMap;
}
@Override
public List<Object> bulkDocs(String database, List<Map<String, Object>> docs) {
long start = System.currentTimeMillis();
activeBulkDocsRequests.inc();
String index = getElasticSearchIndexNameFromDatabase(database);
BulkRequestBuilder bulkBuilder = client.prepareBulk();
// keep a map of the id - rev for building the response
Map<String,String> revisions = new HashMap<String, String>();
for (Map<String, Object> doc : docs) {
logger.debug("Bulk doc entry is {}", docs);
// these are the top-level elements that could be in the document sent by Couchbase
Map<String, Object> meta = (Map<String, Object>)doc.get("meta");
Map<String, Object> json = (Map<String, Object>)doc.get("json");
String base64 = (String)doc.get("base64");
if(meta == null) {
// if there is no meta-data section, there is nothing we can do
logger.warn("Document without meta in bulk_docs, ignoring....");
continue;
} else if("non-JSON mode".equals(meta.get("att_reason"))) {
// optimization, this tells us the body isn't json
json = new HashMap<String, Object>();
} else if(json == null && base64 != null) {
// no plain json, let's try parsing the base64 data
try {
byte[] decodedData = Base64.decode(base64);
try {
// now try to parse the decoded data as json
json = (Map<String, Object>) mapper.readValue(decodedData, Map.class);
}
catch(IOException e) {
logger.error("Unable to parse decoded base64 data as JSON, indexing stub for id: {}", meta.get("id"));
logger.error("Body was: {} Parse error was: {}", new String(decodedData), e);
json = new HashMap<String, Object>();
}
} catch (IOException e) {
logger.error("Unable to decoded base64, indexing stub for id: {}", meta.get("id"));
logger.error("Base64 was was: {} Parse error was: {}", base64, e);
json = new HashMap<String, Object>();
}
}
// at this point we know we have the document meta-data
// and the document contents to be indexed are in json
Map<String, Object> toBeIndexed = new HashMap<String, Object>();
toBeIndexed.put("meta", meta);
toBeIndexed.put("doc", json);
String id = (String)meta.get("id");
String rev = (String)meta.get("rev");
revisions.put(id, rev);
long ttl = 0;
Integer expiration = (Integer)meta.get("expiration");
if(expiration != null) {
ttl = (expiration.longValue() * 1000) - System.currentTimeMillis();
}
String type = defaultDocumentType;
if(id.startsWith("_local/")) {
type = checkpointDocumentType;
}
boolean deleted = meta.containsKey("deleted") ? (Boolean)meta.get("deleted") : false;
if(deleted) {
DeleteRequest deleteRequest = client.prepareDelete(index, type, id).request();
bulkBuilder.add(deleteRequest);
} else {
IndexRequestBuilder indexBuilder = client.prepareIndex(index, type, id);
indexBuilder.setSource(toBeIndexed);
if(ttl > 0) {
indexBuilder.setTTL(ttl);
}
IndexRequest indexRequest = indexBuilder.request();
bulkBuilder.add(indexRequest);
}
}
List<Object> result = new ArrayList<Object>();
BulkResponse response = bulkBuilder.execute().actionGet();
if(response != null) {
for (BulkItemResponse bulkItemResponse : response.items()) {
Map<String, Object> itemResponse = new HashMap<String, Object>();
String itemId = bulkItemResponse.getId();
itemResponse.put("id", itemId);
if(bulkItemResponse.failed()) {
itemResponse.put("error", "failed");
itemResponse.put("reason", bulkItemResponse.failureMessage());
} else {
itemResponse.put("rev", revisions.get(itemId));
}
result.add(itemResponse);
}
}
long end = System.currentTimeMillis();
meanBulkDocsRequests.inc(end - start);
activeBulkDocsRequests.dec();
return result;
}
@Override
public Map<String, Object> getDocument(String database, String docId) {
return getDocumentElasticSearch(getElasticSearchIndexNameFromDatabase(database), docId, defaultDocumentType);
}
@Override
public Map<String, Object> getLocalDocument(String database, String docId) {
return getDocumentElasticSearch(getElasticSearchIndexNameFromDatabase(database), docId, checkpointDocumentType);
}
protected Map<String, Object> getDocumentElasticSearch(String index, String docId, String docType) {
GetResponse response = client.prepareGet(index, docType, docId).execute().actionGet();
if(response != null && response.exists()) {
Map<String,Object> esDocument = response.sourceAsMap();
return (Map<String, Object>)esDocument.get("doc");
}
return null;
}
@Override
public String storeDocument(String database, String docId, Map<String, Object> document) {
return storeDocumentElasticSearch(getElasticSearchIndexNameFromDatabase(database), docId, document, defaultDocumentType);
}
@Override
public String storeLocalDocument(String database, String docId,
Map<String, Object> document) {
return storeDocumentElasticSearch(getElasticSearchIndexNameFromDatabase(database), docId, document, checkpointDocumentType);
}
protected String storeDocumentElasticSearch(String index, String docId, Map<String, Object> document, String docType) {
// normally we just use the revision number present in the document
String documentRevision = (String)document.get("_rev");
if(documentRevision == null) {
// if there isn't one we need to generate a revision number
documentRevision = generateRevisionNumber();
document.put("_rev", documentRevision);
}
IndexRequestBuilder indexBuilder = client.prepareIndex(index, docType, docId);
indexBuilder.setSource(document);
IndexResponse response = indexBuilder.execute().actionGet();
if(response != null) {
return documentRevision;
}
return null;
}
protected String generateRevisionNumber() {
String documentRevision = "1-" + UUID.randomUUID().toString();
return documentRevision;
}
@Override
public InputStream getAttachment(String database, String docId,
String attachmentName) {
throw new UnsupportedOperationException("Attachments are not supported");
}
@Override
public String storeAttachment(String database, String docId,
String attachmentName, String contentType, InputStream input) {
throw new UnsupportedOperationException("Attachments are not supported");
}
@Override
public InputStream getLocalAttachment(String databsae, String docId,
String attachmentName) {
throw new UnsupportedOperationException("Attachments are not supported");
}
@Override
public String storeLocalAttachment(String database, String docId,
String attachmentName, String contentType, InputStream input) {
throw new UnsupportedOperationException("Attachments are not supported");
}
protected String getElasticSearchIndexNameFromDatabase(String database) {
String[] pieces = database.split("/", 2);
if(pieces.length < 2) {
return database;
} else {
return pieces[0];
}
}
protected String getDatabaseNameWithoutUUID(String database) {
int semicolonIndex = database.indexOf(';');
if(semicolonIndex >= 0) {
return database.substring(0, semicolonIndex);
}
return database;
}
public Map<String, Object> getStats() {
Map<String, Object> stats = new HashMap<String, Object>();
Map<String, Object> bulkDocsStats = new HashMap<String, Object>();
bulkDocsStats.put("activeCount", activeBulkDocsRequests.count());
bulkDocsStats.put("totalCount", meanBulkDocsRequests.count());
bulkDocsStats.put("totalTime", meanBulkDocsRequests.sum());
bulkDocsStats.put("avgTime", meanBulkDocsRequests.mean());
Map<String, Object> revsDiffStats = new HashMap<String, Object>();
revsDiffStats.put("activeCount", activeRevsDiffRequests.count());
revsDiffStats.put("totalCount", meanRevsDiffRequests.count());
revsDiffStats.put("totalTime", meanRevsDiffRequests.sum());
revsDiffStats.put("avgTime", meanRevsDiffRequests.mean());
stats.put("_bulk_docs", bulkDocsStats);
stats.put("_revs_diff", revsDiffStats);
return stats;
}
}