use of org.opensearch.action.LatchedActionListener in project asynchronous-search by opensearch-project.
the class DeleteAsynchronousSearchSingleNodeIT method assertConcurrentDeletes.
private void assertConcurrentDeletes(String id, TriConsumer<AtomicInteger, AtomicInteger, AtomicInteger> assertionConsumer, int concurrentRuns) throws InterruptedException {
AtomicInteger numDeleteAcknowledged = new AtomicInteger();
AtomicInteger numDeleteUnAcknowledged = new AtomicInteger();
AtomicInteger numResourceNotFound = new AtomicInteger();
TestThreadPool testThreadPool = null;
try {
testThreadPool = new TestThreadPool(DeleteAsynchronousSearchSingleNodeIT.class.getName());
int numThreads = concurrentRuns;
List<Runnable> operationThreads = new ArrayList<>();
CountDownLatch countDownLatch = new CountDownLatch(numThreads);
for (int i = 0; i < numThreads; i++) {
Runnable thread = () -> {
logger.info("Triggering asynchronous search delete --->");
DeleteAsynchronousSearchRequest deleteAsynchronousSearchRequest = new DeleteAsynchronousSearchRequest(id);
executeDeleteAsynchronousSearch(client(), deleteAsynchronousSearchRequest, new LatchedActionListener<>(new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
if (acknowledgedResponse.isAcknowledged()) {
numDeleteAcknowledged.incrementAndGet();
} else {
numDeleteUnAcknowledged.incrementAndGet();
}
}
@Override
public void onFailure(Exception e) {
if (e instanceof ResourceNotFoundException) {
numResourceNotFound.incrementAndGet();
}
}
}, countDownLatch));
};
operationThreads.add(thread);
}
TestThreadPool finalTestThreadPool = testThreadPool;
operationThreads.forEach(runnable -> finalTestThreadPool.executor("generic").execute(runnable));
countDownLatch.await();
assertionConsumer.apply(numDeleteAcknowledged, numDeleteUnAcknowledged, numResourceNotFound);
} finally {
ThreadPool.terminate(testThreadPool, 500, TimeUnit.MILLISECONDS);
}
}
use of org.opensearch.action.LatchedActionListener in project asynchronous-search by opensearch-project.
the class AsynchronousSearchContextPermitsTests method testAsyncBlockOperationsRace.
public void testAsyncBlockOperationsRace() throws Exception {
// we racily submit operations and a delay, and then ensure that all operations were actually completed
final int operations = scaledRandomIntBetween(1, 64);
final CyclicBarrier barrier = new CyclicBarrier(1 + 1 + operations);
final CountDownLatch operationLatch = new CountDownLatch(1 + operations);
final Set<Integer> values = Collections.newSetFromMap(new ConcurrentHashMap<>());
final List<Thread> threads = new ArrayList<>();
for (int i = 0; i < operations; i++) {
final int value = i;
final Thread thread = new Thread(() -> {
try {
barrier.await();
} catch (final BrokenBarrierException | InterruptedException e) {
throw new RuntimeException(e);
}
ActionListener<Releasable> onAcquired = new ActionListener<Releasable>() {
@Override
public void onResponse(Releasable releasable) {
values.add(value);
releasable.close();
}
@Override
public void onFailure(Exception e) {
}
};
permits.asyncAcquirePermit(new LatchedActionListener<Releasable>(onAcquired, operationLatch), TimeValue.timeValueMinutes(1), "");
});
thread.start();
threads.add(thread);
}
final Thread blockingThread = new Thread(() -> {
try {
barrier.await();
} catch (final BrokenBarrierException | InterruptedException e) {
throw new RuntimeException(e);
}
ActionListener<Releasable> onAcquired = new ActionListener<Releasable>() {
@Override
public void onResponse(Releasable releasable) {
values.add(operations);
releasable.close();
}
@Override
public void onFailure(Exception e) {
}
};
permits.asyncAcquirePermit(new LatchedActionListener<Releasable>(onAcquired, operationLatch), TimeValue.timeValueMinutes(1), "");
});
blockingThread.start();
barrier.await();
operationLatch.await();
for (final Thread thread : threads) {
thread.join();
}
blockingThread.join();
// check that all operations completed
for (int i = 0; i < operations; i++) {
assertTrue(values.contains(i));
}
assertTrue(values.contains(operations));
/*
* The block operation is executed on another thread and the operations can have completed before this thread has returned all the
* permits to the semaphore. We wait here until all generic threads are idle as an indication that all permits have been returned to
* the semaphore.
*/
assertBusy(() -> {
for (final ThreadPoolStats.Stats stats : threadPool.stats()) {
if (ThreadPool.Names.GENERIC.equals(stats.getName())) {
assertThat("Expected no active threads in GENERIC pool", stats.getActive(), equalTo(0));
return;
}
}
fail("Failed to find stats for the GENERIC thread pool");
});
}
use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.
the class CRUDDocumentationIT method testGetSource.
public void testGetSource() throws Exception {
RestHighLevelClient client = highLevelClient();
{
Request createIndex = new Request("PUT", "/posts");
createIndex.setJsonEntity("{\n" + " \"mappings\" : {\n" + " \"properties\" : {\n" + " \"message\" : {\n" + " \"type\": \"text\",\n" + " \"store\": true\n" + " }\n" + " }\n" + " }\n" + "}");
Response response = client().performRequest(createIndex);
assertEquals(200, response.getStatusLine().getStatusCode());
IndexRequest indexRequest = new IndexRequest("posts").id("1").source("user", "foobar", "postDate", new Date(), "message", "trying out OpenSearch");
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
}
// tag::get-source-request
GetSourceRequest getSourceRequest = new GetSourceRequest(// <1>
"posts", // <2>
"1");
// end::get-source-request
// tag::get-source-request-optional
// <2>
String[] includes = Strings.EMPTY_ARRAY;
String[] excludes = new String[] { "postDate" };
getSourceRequest.fetchSourceContext(// <1>
new FetchSourceContext(true, includes, excludes));
// end::get-source-request-optional
// tag::get-source-request-routing
// <1>
getSourceRequest.routing("routing");
// end::get-source-request-routing
// tag::get-source-request-preference
// <1>
getSourceRequest.preference("preference");
// end::get-source-request-preference
// tag::get-source-request-realtime
// <1>
getSourceRequest.realtime(false);
// end::get-source-request-realtime
// tag::get-source-request-refresh
// <1>
getSourceRequest.refresh(true);
// end::get-source-request-refresh
{
// tag::get-source-execute
GetSourceResponse response = client.getSource(getSourceRequest, RequestOptions.DEFAULT);
// end::get-source-execute
// tag::get-source-response
Map<String, Object> source = response.getSource();
// end::get-source-response
Map<String, Object> expectSource = new HashMap<>();
expectSource.put("user", "foobar");
expectSource.put("message", "trying out OpenSearch");
assertEquals(expectSource, source);
}
{
GetSourceRequest request = new GetSourceRequest("posts", "1");
// tag::get-source-execute-listener
ActionListener<GetSourceResponse> listener = new ActionListener<GetSourceResponse>() {
@Override
public void onResponse(GetSourceResponse getResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-source-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::get-source-execute-async
// <1>
client.getSourceAsync(request, RequestOptions.DEFAULT, listener);
// end::get-source-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}
use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.
the class CRUDDocumentationIT method testMultiGet.
@SuppressWarnings("unused")
public void testMultiGet() throws Exception {
RestHighLevelClient client = highLevelClient();
{
Request createIndex = new Request("PUT", "/index");
createIndex.setJsonEntity("{\n" + " \"mappings\" : {\n" + " \"properties\" : {\n" + " \"foo\" : {\n" + " \"type\": \"text\",\n" + " \"store\": true\n" + " }\n" + " }\n" + " }\n" + "}");
Response response = client().performRequest(createIndex);
assertEquals(200, response.getStatusLine().getStatusCode());
}
Map<String, Object> source = new HashMap<>();
source.put("foo", "val1");
source.put("bar", "val2");
source.put("baz", "val3");
client.index(new IndexRequest("index").id("example_id").source(source).setRefreshPolicy(RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
{
// tag::multi-get-request
MultiGetRequest request = new MultiGetRequest();
request.add(new MultiGetRequest.Item(// <1>
"index", // <2>
"example_id"));
// <3>
request.add(new MultiGetRequest.Item("index", "another_id"));
// end::multi-get-request
// Add a missing index so we can test it.
request.add(new MultiGetRequest.Item("missing_index", "id"));
// tag::multi-get-request-item-extras
request.add(new MultiGetRequest.Item("index", "with_routing").routing(// <1>
"some_routing"));
request.add(new MultiGetRequest.Item("index", "with_version").versionType(// <2>
VersionType.EXTERNAL).version(// <3>
10123L));
// end::multi-get-request-item-extras
// tag::multi-get-request-top-level-extras
// <1>
request.preference("some_preference");
// <2>
request.realtime(false);
// <3>
request.refresh(true);
// end::multi-get-request-top-level-extras
// tag::multi-get-execute
MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
// end::multi-get-execute
// tag::multi-get-response
MultiGetItemResponse firstItem = response.getResponses()[0];
// <1>
assertNull(firstItem.getFailure());
// <2>
GetResponse firstGet = firstItem.getResponse();
String index = firstItem.getIndex();
String id = firstItem.getId();
if (firstGet.isExists()) {
long version = firstGet.getVersion();
// <3>
String sourceAsString = firstGet.getSourceAsString();
// <4>
Map<String, Object> sourceAsMap = firstGet.getSourceAsMap();
// <5>
byte[] sourceAsBytes = firstGet.getSourceAsBytes();
} else {
// <6>
}
// end::multi-get-response
assertTrue(firstGet.isExists());
assertEquals(source, firstGet.getSource());
MultiGetItemResponse missingIndexItem = response.getResponses()[2];
// tag::multi-get-indexnotfound
// <1>
assertNull(missingIndexItem.getResponse());
// <2>
Exception e = missingIndexItem.getFailure().getFailure();
// <3>
OpenSearchException ee = (OpenSearchException) e;
// TODO status is broken! fix in a followup
// assertEquals(RestStatus.NOT_FOUND, ee.status()); // <4>
assertThat(e.getMessage(), // <5>
containsString("reason=no such index [missing_index]"));
// end::multi-get-indexnotfound
ActionListener<MultiGetResponse> listener;
// tag::multi-get-execute-listener
listener = new ActionListener<MultiGetResponse>() {
@Override
public void onResponse(MultiGetResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::multi-get-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::multi-get-execute-async
// <1>
client.mgetAsync(request, RequestOptions.DEFAULT, listener);
// end::multi-get-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
{
MultiGetRequest request = new MultiGetRequest();
// tag::multi-get-request-no-source
request.add(new MultiGetRequest.Item("index", "example_id").fetchSourceContext(// <1>
FetchSourceContext.DO_NOT_FETCH_SOURCE));
// end::multi-get-request-no-source
MultiGetItemResponse item = unwrapAndAssertExample(client.mget(request, RequestOptions.DEFAULT));
assertNull(item.getResponse().getSource());
}
{
MultiGetRequest request = new MultiGetRequest();
// tag::multi-get-request-source-include
String[] includes = new String[] { "foo", "*r" };
String[] excludes = Strings.EMPTY_ARRAY;
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
request.add(new MultiGetRequest.Item("index", "example_id").fetchSourceContext(// <1>
fetchSourceContext));
// end::multi-get-request-source-include
MultiGetItemResponse item = unwrapAndAssertExample(client.mget(request, RequestOptions.DEFAULT));
assertThat(item.getResponse().getSource(), hasEntry("foo", "val1"));
assertThat(item.getResponse().getSource(), hasEntry("bar", "val2"));
assertThat(item.getResponse().getSource(), not(hasKey("baz")));
}
{
MultiGetRequest request = new MultiGetRequest();
// tag::multi-get-request-source-exclude
String[] includes = Strings.EMPTY_ARRAY;
String[] excludes = new String[] { "foo", "*r" };
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
request.add(new MultiGetRequest.Item("index", "example_id").fetchSourceContext(// <1>
fetchSourceContext));
// end::multi-get-request-source-exclude
MultiGetItemResponse item = unwrapAndAssertExample(client.mget(request, RequestOptions.DEFAULT));
assertThat(item.getResponse().getSource(), not(hasKey("foo")));
assertThat(item.getResponse().getSource(), not(hasKey("bar")));
assertThat(item.getResponse().getSource(), hasEntry("baz", "val3"));
}
{
MultiGetRequest request = new MultiGetRequest();
// tag::multi-get-request-stored
request.add(new MultiGetRequest.Item("index", "example_id").storedFields(// <1>
"foo"));
MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
MultiGetItemResponse item = response.getResponses()[0];
// <2>
String value = item.getResponse().getField("foo").getValue();
// end::multi-get-request-stored
assertNull(item.getResponse().getSource());
assertEquals("val1", value);
}
{
// tag::multi-get-conflict
MultiGetRequest request = new MultiGetRequest();
request.add(new MultiGetRequest.Item("index", "example_id").version(1000L));
MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
MultiGetItemResponse item = response.getResponses()[0];
// <1>
assertNull(item.getResponse());
// <2>
Exception e = item.getFailure().getFailure();
// <3>
OpenSearchException ee = (OpenSearchException) e;
// TODO status is broken! fix in a followup
// assertEquals(RestStatus.CONFLICT, ee.status()); // <4>
assertThat(e.getMessage(), containsString("version conflict, current version [1] is " + // <5>
"different than the one provided [1000]"));
// end::multi-get-conflict
}
}
use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.
the class CRUDDocumentationIT method testIndex.
@SuppressWarnings("unused")
public void testIndex() throws Exception {
RestHighLevelClient client = highLevelClient();
{
// tag::index-request-map
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "foobar");
jsonMap.put("postDate", new Date());
jsonMap.put("message", "trying out OpenSearch");
IndexRequest indexRequest = new IndexRequest("posts").id("1").source(// <1>
jsonMap);
// end::index-request-map
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
}
{
// tag::index-request-xcontent
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.field("user", "foobar");
builder.timeField("postDate", new Date());
builder.field("message", "trying out OpenSearch");
}
builder.endObject();
IndexRequest indexRequest = new IndexRequest("posts").id("1").source(// <1>
builder);
// end::index-request-xcontent
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
assertEquals(DocWriteResponse.Result.UPDATED, indexResponse.getResult());
}
{
// tag::index-request-shortcut
IndexRequest indexRequest = new IndexRequest("posts").id("1").source("user", "foobar", "postDate", new Date(), "message", // <1>
"trying out OpenSearch");
// end::index-request-shortcut
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
assertEquals(DocWriteResponse.Result.UPDATED, indexResponse.getResult());
}
{
// tag::index-request-string
// <1>
IndexRequest request = new IndexRequest("posts");
// <2>
request.id("1");
String jsonString = "{" + "\"user\":\"foobar\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"trying out OpenSearch\"" + "}";
// <3>
request.source(jsonString, XContentType.JSON);
// end::index-request-string
// tag::index-execute
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
// end::index-execute
assertEquals(DocWriteResponse.Result.UPDATED, indexResponse.getResult());
// tag::index-response
String index = indexResponse.getIndex();
String id = indexResponse.getId();
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
// <1>
} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
// <2>
}
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
// <3>
}
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
// <4>
String reason = failure.reason();
}
}
// end::index-response
}
{
IndexRequest request = new IndexRequest("posts").id("1");
// tag::index-request-routing
// <1>
request.routing("routing");
// end::index-request-routing
// tag::index-request-timeout
// <1>
request.timeout(TimeValue.timeValueSeconds(1));
// <2>
request.timeout("1s");
// end::index-request-timeout
// tag::index-request-refresh
// <1>
request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
// <2>
request.setRefreshPolicy("wait_for");
// end::index-request-refresh
// tag::index-request-version
// <1>
request.version(2);
// end::index-request-version
// tag::index-request-version-type
// <1>
request.versionType(VersionType.EXTERNAL);
// end::index-request-version-type
// tag::index-request-op-type
// <1>
request.opType(DocWriteRequest.OpType.CREATE);
// <2>
request.opType("create");
// end::index-request-op-type
// tag::index-request-pipeline
// <1>
request.setPipeline("pipeline");
// end::index-request-pipeline
}
{
// tag::index-conflict
IndexRequest request = new IndexRequest("posts").id("1").source("field", "value").setIfSeqNo(10L).setIfPrimaryTerm(20);
try {
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
} catch (OpenSearchException e) {
if (e.status() == RestStatus.CONFLICT) {
// <1>
}
}
// end::index-conflict
}
{
// tag::index-optype
IndexRequest request = new IndexRequest("posts").id("1").source("field", "value").opType(DocWriteRequest.OpType.CREATE);
try {
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
} catch (OpenSearchException e) {
if (e.status() == RestStatus.CONFLICT) {
// <1>
}
}
// end::index-optype
}
{
IndexRequest request = new IndexRequest("posts").id("async").source("field", "value");
ActionListener<IndexResponse> listener;
// tag::index-execute-listener
listener = new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::index-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::index-execute-async
// <1>
client.indexAsync(request, RequestOptions.DEFAULT, listener);
// end::index-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}
Aggregations