use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method bulkDelete.
@Override
public void bulkDelete(BulkDeleteRequest request) {
if (request.ids == null || request.ids.isEmpty())
throw Exceptions.error("request.ids must not be empty");
StopWatch watch = new StopWatch();
String index = request.index == null ? this.index : request.index;
BulkRequestBuilder builder = client().prepareBulk();
for (String id : request.ids) {
builder.add(client().prepareDelete(index, type, id));
}
long esTookTime = 0;
try {
BulkResponse response = builder.get();
esTookTime = response.getTook().nanos();
if (response.hasFailures())
throw new SearchException(response.buildFailureMessage());
} catch (ElasticsearchException e) {
// due to elastic search uses async executor to run, we have to wrap the exception to retain the original place caused the exception
throw new SearchException(e);
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("elasticsearch", elapsedTime, 0, request.ids.size());
logger.debug("bulkDelete, index={}, type={}, size={}, esTookTime={}, elapsedTime={}", index, type, request.ids.size(), esTookTime, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method delete.
@Override
public boolean delete(DeleteRequest request) {
StopWatch watch = new StopWatch();
String index = request.index == null ? this.index : request.index;
boolean deleted = false;
try {
DeleteResponse response = client().prepareDelete(index, type, request.id).get();
deleted = response.getResult() == DocWriteResponse.Result.DELETED;
return deleted;
} catch (ElasticsearchException e) {
// due to elastic search uses async executor to run, we have to wrap the exception to retain the original place caused the exception
throw new SearchException(e);
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("elasticsearch", elapsedTime, 0, deleted ? 1 : 0);
logger.debug("delete, index={}, type={}, id={}, elapsedTime={}", index, type, request.id, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method analyze.
@Override
public List<String> analyze(AnalyzeRequest request) {
StopWatch watch = new StopWatch();
String index = request.index == null ? this.index : request.index;
try {
AnalyzeResponse response = client().admin().indices().prepareAnalyze(index, request.text).setAnalyzer(request.analyzer).get();
return response.getTokens().stream().map(AnalyzeResponse.AnalyzeToken::getTerm).collect(Collectors.toList());
} catch (ElasticsearchException e) {
// due to elastic search uses async executor to run, we have to wrap the exception to retain the original place caused the exception
throw new SearchException(e);
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("elasticsearch", elapsedTime);
logger.debug("analyze, index={}, analyzer={}, elapsedTime={}", index, request.analyzer, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method forEach.
@Override
public void forEach(ForEach<T> forEach) {
if (forEach.consumer == null)
throw new Error("forEach.consumer must not be null");
if (forEach.query == null)
throw new Error("forEach.query must not be null");
if (forEach.scrollTimeout == null)
throw new Error("forEach.scrollTimeout must not be null");
if (forEach.limit == null || forEach.limit <= 0)
throw new Error("forEach.limit must not be null and greater than 0");
StopWatch watch = new StopWatch();
TimeValue keepAlive = TimeValue.timeValueNanos(forEach.scrollTimeout.toNanos());
long esTookTime = 0;
String index = forEach.index == null ? this.index : forEach.index;
int totalHits = 0;
try {
SearchRequestBuilder builder = client().prepareSearch(index).setQuery(forEach.query).addSort(SortBuilders.fieldSort("_doc")).setScroll(keepAlive).setSize(forEach.limit);
logger.debug("foreach, index={}, type={}, request={}", index, type, builder);
org.elasticsearch.action.search.SearchResponse searchResponse = builder.get();
while (true) {
esTookTime += searchResponse.getTook().nanos();
if (searchResponse.getFailedShards() > 0)
logger.warn("some shard failed, response={}", searchResponse);
SearchHit[] hits = searchResponse.getHits().getHits();
if (hits.length == 0)
break;
totalHits += hits.length;
for (SearchHit hit : hits) {
forEach.consumer.accept(reader.fromJSON(BytesReference.toBytes(hit.getSourceRef())));
}
String scrollId = searchResponse.getScrollId();
searchResponse = client().prepareSearchScroll(scrollId).setScroll(keepAlive).get();
}
} catch (ElasticsearchException e) {
// due to elastic search uses async executor to run, we have to wrap the exception to retain the original place caused the exception
throw new SearchException(e);
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("elasticsearch", elapsedTime, totalHits, 0);
logger.debug("foreach, totalHits={}, esTookTime={}, elapsedTime={}", totalHits, esTookTime, elapsedTime);
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method bulkIndex.
@Override
public void bulkIndex(BulkIndexRequest<T> request) {
if (request.sources == null || request.sources.isEmpty())
throw Exceptions.error("request.sources must not be empty");
StopWatch watch = new StopWatch();
String index = request.index == null ? this.index : request.index;
BulkRequestBuilder builder = client().prepareBulk();
for (Map.Entry<String, T> entry : request.sources.entrySet()) {
String id = entry.getKey();
T source = entry.getValue();
validator.validate(source);
byte[] document = writer.toJSON(source);
builder.add(client().prepareIndex(index, type, id).setSource(document, XContentType.JSON));
}
long esTookTime = 0;
try {
BulkResponse response = builder.get();
esTookTime = response.getTook().nanos();
if (response.hasFailures())
throw new SearchException(response.buildFailureMessage());
} catch (ElasticsearchException e) {
// due to elastic search uses async executor to run, we have to wrap the exception to retain the original place caused the exception
throw new SearchException(e);
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("elasticsearch", elapsedTime, 0, request.sources.size());
logger.debug("bulkIndex, index={}, type={}, size={}, esTookTime={}, elapsedTime={}", index, type, request.sources.size(), esTookTime, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
Aggregations