Search in sources :

Example 6 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class UpdateIT method testConcurrentUpdateWithRetryOnConflict.

public void testConcurrentUpdateWithRetryOnConflict() throws Exception {
    final boolean useBulkApi = randomBoolean();
    createTestIndex();
    ensureGreen();
    int numberOfThreads = scaledRandomIntBetween(2, 5);
    final CountDownLatch latch = new CountDownLatch(numberOfThreads);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final int numberOfUpdatesPerThread = scaledRandomIntBetween(100, 500);
    final List<Exception> failures = new CopyOnWriteArrayList<>();
    for (int i = 0; i < numberOfThreads; i++) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    startLatch.await();
                    for (int i = 0; i < numberOfUpdatesPerThread; i++) {
                        if (i % 100 == 0) {
                            logger.debug("Client [{}] issued [{}] of [{}] requests", Thread.currentThread().getName(), i, numberOfUpdatesPerThread);
                        }
                        if (useBulkApi) {
                            UpdateRequestBuilder updateRequestBuilder = client().prepareUpdate(indexOrAlias(), "type1", Integer.toString(i)).setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).setRetryOnConflict(Integer.MAX_VALUE).setUpsert(jsonBuilder().startObject().field("field", 1).endObject());
                            client().prepareBulk().add(updateRequestBuilder).execute().actionGet();
                        } else {
                            client().prepareUpdate(indexOrAlias(), "type1", Integer.toString(i)).setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).setRetryOnConflict(Integer.MAX_VALUE).setUpsert(jsonBuilder().startObject().field("field", 1).endObject()).execute().actionGet();
                        }
                    }
                    logger.info("Client [{}] issued all [{}] requests.", Thread.currentThread().getName(), numberOfUpdatesPerThread);
                } catch (InterruptedException e) {
                    // test infrastructure kills long-running tests by interrupting them, thus we handle this case separately
                    logger.warn("Test was forcefully stopped. Client [{}] may still have outstanding requests.", Thread.currentThread().getName());
                    failures.add(e);
                    Thread.currentThread().interrupt();
                } catch (Exception e) {
                    failures.add(e);
                } finally {
                    latch.countDown();
                }
            }
        };
        Thread updater = new Thread(r);
        updater.setName("UpdateIT-Client-" + i);
        updater.start();
    }
    startLatch.countDown();
    latch.await();
    for (Throwable throwable : failures) {
        logger.info("Captured failure on concurrent update:", throwable);
    }
    assertThat(failures.size(), equalTo(0));
    for (int i = 0; i < numberOfUpdatesPerThread; i++) {
        GetResponse response = client().prepareGet("test", "type1", Integer.toString(i)).execute().actionGet();
        assertThat(response.getId(), equalTo(Integer.toString(i)));
        assertThat(response.isExists(), equalTo(true));
        assertThat(response.getVersion(), equalTo((long) numberOfThreads));
        assertThat((Integer) response.getSource().get("field"), equalTo(numberOfThreads));
    }
}
Also used : SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) GetResponse(org.elasticsearch.action.get.GetResponse) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 7 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class PercolateQueryBuilder method doRewrite.

@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws IOException {
    if (document != null) {
        return this;
    }
    GetRequest getRequest = new GetRequest(indexedDocumentIndex, indexedDocumentType, indexedDocumentId);
    getRequest.preference("_local");
    getRequest.routing(indexedDocumentRouting);
    getRequest.preference(indexedDocumentPreference);
    if (indexedDocumentVersion != null) {
        getRequest.version(indexedDocumentVersion);
    }
    GetResponse getResponse = queryShardContext.getClient().get(getRequest).actionGet();
    if (getResponse.isExists() == false) {
        throw new ResourceNotFoundException("indexed document [{}/{}/{}] couldn't be found", indexedDocumentIndex, indexedDocumentType, indexedDocumentId);
    }
    if (getResponse.isSourceEmpty()) {
        throw new IllegalArgumentException("indexed document [" + indexedDocumentIndex + "/" + indexedDocumentType + "/" + indexedDocumentId + "] source disabled");
    }
    final BytesReference source = getResponse.getSourceAsBytesRef();
    return new PercolateQueryBuilder(field, documentType, source, XContentFactory.xContentType(source));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) GetRequest(org.elasticsearch.action.get.GetRequest) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 8 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class SizeMappingIT method testBasic.

public void testBasic() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type", "_size", "enabled=true"));
    final String source = "{\"f\":10}";
    indexRandom(true, client().prepareIndex("test", "type", "1").setSource(source, XContentType.JSON));
    GetResponse getResponse = client().prepareGet("test", "type", "1").setStoredFields("_size").get();
    assertNotNull(getResponse.getField("_size"));
    assertEquals(source.length(), getResponse.getField("_size").getValue());
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse)

Example 9 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class DiscoveryWithServiceDisruptionsIT method testRejoinDocumentExistsInAllShardCopies.

/**
     * Test that a document which is indexed on the majority side of a partition, is available from the minority side,
     * once the partition is healed
     */
public void testRejoinDocumentExistsInAllShardCopies() throws Exception {
    List<String> nodes = startCluster(3);
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 2)).get());
    ensureGreen("test");
    nodes = new ArrayList<>(nodes);
    Collections.shuffle(nodes, random());
    String isolatedNode = nodes.get(0);
    String notIsolatedNode = nodes.get(1);
    TwoPartitions partitions = isolateNode(isolatedNode);
    NetworkDisruption scheme = addRandomDisruptionType(partitions);
    scheme.startDisrupting();
    ensureStableCluster(2, notIsolatedNode);
    assertFalse(client(notIsolatedNode).admin().cluster().prepareHealth("test").setWaitForYellowStatus().get().isTimedOut());
    IndexResponse indexResponse = internalCluster().client(notIsolatedNode).prepareIndex("test", "type").setSource("field", "value").get();
    assertThat(indexResponse.getVersion(), equalTo(1L));
    logger.info("Verifying if document exists via node[{}]", notIsolatedNode);
    GetResponse getResponse = internalCluster().client(notIsolatedNode).prepareGet("test", "type", indexResponse.getId()).setPreference("_local").get();
    assertThat(getResponse.isExists(), is(true));
    assertThat(getResponse.getVersion(), equalTo(1L));
    assertThat(getResponse.getId(), equalTo(indexResponse.getId()));
    scheme.stopDisrupting();
    ensureStableCluster(3);
    ensureGreen("test");
    for (String node : nodes) {
        logger.info("Verifying if document exists after isolating node[{}] via node[{}]", isolatedNode, node);
        getResponse = internalCluster().client(node).prepareGet("test", "type", indexResponse.getId()).setPreference("_local").get();
        assertThat(getResponse.isExists(), is(true));
        assertThat(getResponse.getVersion(), equalTo(1L));
        assertThat(getResponse.getId(), equalTo(indexResponse.getId()));
    }
}
Also used : TwoPartitions(org.elasticsearch.test.disruption.NetworkDisruption.TwoPartitions) IndexResponse(org.elasticsearch.action.index.IndexResponse) NetworkDisruption(org.elasticsearch.test.disruption.NetworkDisruption) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 10 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class DocumentActionsIT method testIndexActions.

public void testIndexActions() throws Exception {
    createIndex();
    NumShards numShards = getNumShards(getConcreteIndexName());
    logger.info("Running Cluster Health");
    ensureGreen();
    logger.info("Indexing [type1/1]");
    IndexResponse indexResponse = client().prepareIndex().setIndex("test").setType("type1").setId("1").setSource(source("1", "test")).setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    assertThat(indexResponse.getIndex(), equalTo(getConcreteIndexName()));
    assertThat(indexResponse.getId(), equalTo("1"));
    assertThat(indexResponse.getType(), equalTo("type1"));
    logger.info("Refreshing");
    RefreshResponse refreshResponse = refresh();
    assertThat(refreshResponse.getSuccessfulShards(), equalTo(numShards.totalNumShards));
    logger.info("--> index exists?");
    assertThat(indexExists(getConcreteIndexName()), equalTo(true));
    logger.info("--> index exists?, fake index");
    assertThat(indexExists("test1234565"), equalTo(false));
    logger.info("Clearing cache");
    ClearIndicesCacheResponse clearIndicesCacheResponse = client().admin().indices().clearCache(clearIndicesCacheRequest("test").recycler(true).fieldDataCache(true).queryCache(true)).actionGet();
    assertNoFailures(clearIndicesCacheResponse);
    assertThat(clearIndicesCacheResponse.getSuccessfulShards(), equalTo(numShards.totalNumShards));
    logger.info("Force Merging");
    waitForRelocation(ClusterHealthStatus.GREEN);
    ForceMergeResponse mergeResponse = forceMerge();
    assertThat(mergeResponse.getSuccessfulShards(), equalTo(numShards.totalNumShards));
    GetResponse getResult;
    logger.info("Get [type1/1]");
    for (int i = 0; i < 5; i++) {
        getResult = client().prepareGet("test", "type1", "1").setOperationThreaded(false).execute().actionGet();
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
        assertThat("cycle #" + i, getResult.getSourceAsString(), equalTo(source("1", "test").string()));
        assertThat("cycle(map) #" + i, (String) getResult.getSourceAsMap().get("name"), equalTo("test"));
        getResult = client().get(getRequest("test").type("type1").id("1").operationThreaded(true)).actionGet();
        assertThat("cycle #" + i, getResult.getSourceAsString(), equalTo(source("1", "test").string()));
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
    }
    logger.info("Get [type1/1] with script");
    for (int i = 0; i < 5; i++) {
        getResult = client().prepareGet("test", "type1", "1").setStoredFields("name").execute().actionGet();
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
        assertThat(getResult.isExists(), equalTo(true));
        assertThat(getResult.getSourceAsBytes(), nullValue());
        assertThat(getResult.getField("name").getValues().get(0).toString(), equalTo("test"));
    }
    logger.info("Get [type1/2] (should be empty)");
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("2")).actionGet();
        assertThat(getResult.isExists(), equalTo(false));
    }
    logger.info("Delete [type1/1]");
    DeleteResponse deleteResponse = client().prepareDelete("test", "type1", "1").execute().actionGet();
    assertThat(deleteResponse.getIndex(), equalTo(getConcreteIndexName()));
    assertThat(deleteResponse.getId(), equalTo("1"));
    assertThat(deleteResponse.getType(), equalTo("type1"));
    logger.info("Refreshing");
    client().admin().indices().refresh(refreshRequest("test")).actionGet();
    logger.info("Get [type1/1] (should be empty)");
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.isExists(), equalTo(false));
    }
    logger.info("Index [type1/1]");
    client().index(indexRequest("test").type("type1").id("1").source(source("1", "test"))).actionGet();
    logger.info("Index [type1/2]");
    client().index(indexRequest("test").type("type1").id("2").source(source("2", "test2"))).actionGet();
    logger.info("Flushing");
    FlushResponse flushResult = client().admin().indices().prepareFlush("test").execute().actionGet();
    assertThat(flushResult.getSuccessfulShards(), equalTo(numShards.totalNumShards));
    assertThat(flushResult.getFailedShards(), equalTo(0));
    logger.info("Refreshing");
    client().admin().indices().refresh(refreshRequest("test")).actionGet();
    logger.info("Get [type1/1] and [type1/2]");
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
        assertThat("cycle #" + i, getResult.getSourceAsString(), equalTo(source("1", "test").string()));
        getResult = client().get(getRequest("test").type("type1").id("2")).actionGet();
        String ste1 = getResult.getSourceAsString();
        String ste2 = source("2", "test2").string();
        assertThat("cycle #" + i, ste1, equalTo(ste2));
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
    }
    logger.info("Count");
    // check count
    for (int i = 0; i < 5; i++) {
        // test successful
        SearchResponse countResponse = client().prepareSearch("test").setSize(0).setQuery(termQuery("_type", "type1")).execute().actionGet();
        assertNoFailures(countResponse);
        assertThat(countResponse.getHits().getTotalHits(), equalTo(2L));
        assertThat(countResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
        assertThat(countResponse.getFailedShards(), equalTo(0));
        // count with no query is a match all one
        countResponse = client().prepareSearch("test").setSize(0).execute().actionGet();
        assertThat("Failures " + countResponse.getShardFailures(), countResponse.getShardFailures() == null ? 0 : countResponse.getShardFailures().length, equalTo(0));
        assertThat(countResponse.getHits().getTotalHits(), equalTo(2L));
        assertThat(countResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
        assertThat(countResponse.getFailedShards(), equalTo(0));
    }
}
Also used : ClearIndicesCacheResponse(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse) ForceMergeResponse(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse) FlushResponse(org.elasticsearch.action.admin.indices.flush.FlushResponse) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) GetResponse(org.elasticsearch.action.get.GetResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

GetResponse (org.elasticsearch.action.get.GetResponse)149 Test (org.junit.Test)41 Map (java.util.Map)27 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)25 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)18 ESSyncConfig (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig)17 Dml (com.alibaba.otter.canal.client.adapter.support.Dml)17 Date (java.util.Date)17 LinkedHashMap (java.util.LinkedHashMap)17 DataSource (javax.sql.DataSource)14 GetRequestBuilder (org.elasticsearch.action.get.GetRequestBuilder)14 SearchResponse (org.elasticsearch.action.search.SearchResponse)13 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)12 IOException (java.io.IOException)11 GetRequest (org.elasticsearch.action.get.GetRequest)11 ElasticsearchException (org.elasticsearch.ElasticsearchException)9 Settings (org.elasticsearch.common.settings.Settings)9 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)7