Search in sources :

Example 1 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.

the class RareClusterStateIT method testDelayedMappingPropagationOnPrimary.

public void testDelayedMappingPropagationOnPrimary() throws Exception {
    // Here we want to test that things go well if there is a first request
    // that adds mappings but before mappings are propagated to all nodes
    // another index request introduces the same mapping. The master node
    // will reply immediately since it did not change the cluster state
    // but the change might not be on the node that performed the indexing
    // operation yet
    Settings settings = Settings.builder().put(DiscoverySettings.COMMIT_TIMEOUT_SETTING.getKey(), // explicitly set so it won't default to publish timeout
    "30s").put(DiscoverySettings.PUBLISH_TIMEOUT_SETTING.getKey(), // don't wait post commit as we are blocking things by design
    "0s").build();
    final List<String> nodeNames = internalCluster().startNodes(2, settings);
    assertFalse(client().admin().cluster().prepareHealth().setWaitForNodes("2").get().isTimedOut());
    final String master = internalCluster().getMasterName();
    assertThat(nodeNames, hasItem(master));
    String otherNode = null;
    for (String node : nodeNames) {
        if (node.equals(master) == false) {
            otherNode = node;
            break;
        }
    }
    assertNotNull(otherNode);
    // Don't allocate the shard on the master node
    assertAcked(prepareCreate("index").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put("index.routing.allocation.exclude._name", master)).get());
    ensureGreen();
    // Check routing tables
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    assertEquals(master, state.nodes().getMasterNode().getName());
    List<ShardRouting> shards = state.routingTable().allShards("index");
    assertThat(shards, hasSize(1));
    for (ShardRouting shard : shards) {
        if (shard.primary()) {
            // primary must not be on the master node
            assertFalse(state.nodes().getMasterNodeId().equals(shard.currentNodeId()));
        } else {
            // only primaries
            fail();
        }
    }
    // Block cluster state processing where our shard is
    BlockClusterStateProcessing disruption = new BlockClusterStateProcessing(otherNode, random());
    internalCluster().setDisruptionScheme(disruption);
    disruption.startDisrupting();
    // Add a new mapping...
    final AtomicReference<Object> putMappingResponse = new AtomicReference<>();
    client().admin().indices().preparePutMapping("index").setType("type").setSource("field", "type=long").execute(new ActionListener<PutMappingResponse>() {

        @Override
        public void onResponse(PutMappingResponse response) {
            putMappingResponse.set(response);
        }

        @Override
        public void onFailure(Exception e) {
            putMappingResponse.set(e);
        }
    });
    // ...and wait for mappings to be available on master
    assertBusy(new Runnable() {

        @Override
        public void run() {
            ImmutableOpenMap<String, MappingMetaData> indexMappings = client().admin().indices().prepareGetMappings("index").get().getMappings().get("index");
            assertNotNull(indexMappings);
            MappingMetaData typeMappings = indexMappings.get("type");
            assertNotNull(typeMappings);
            Object properties;
            try {
                properties = typeMappings.getSourceAsMap().get("properties");
            } catch (IOException e) {
                throw new AssertionError(e);
            }
            assertNotNull(properties);
            Object fieldMapping = ((Map<String, Object>) properties).get("field");
            assertNotNull(fieldMapping);
        }
    });
    final AtomicReference<Object> docIndexResponse = new AtomicReference<>();
    client().prepareIndex("index", "type", "1").setSource("field", 42).execute(new ActionListener<IndexResponse>() {

        @Override
        public void onResponse(IndexResponse response) {
            docIndexResponse.set(response);
        }

        @Override
        public void onFailure(Exception e) {
            docIndexResponse.set(e);
        }
    });
    // Wait a bit to make sure that the reason why we did not get a response
    // is that cluster state processing is blocked and not just that it takes
    // time to process the indexing request
    Thread.sleep(100);
    assertThat(putMappingResponse.get(), equalTo(null));
    assertThat(docIndexResponse.get(), equalTo(null));
    // Now make sure the indexing request finishes successfully
    disruption.stopDisrupting();
    assertBusy(new Runnable() {

        @Override
        public void run() {
            assertThat(putMappingResponse.get(), instanceOf(PutMappingResponse.class));
            PutMappingResponse resp = (PutMappingResponse) putMappingResponse.get();
            assertTrue(resp.isAcknowledged());
            assertThat(docIndexResponse.get(), instanceOf(IndexResponse.class));
            IndexResponse docResp = (IndexResponse) docIndexResponse.get();
            assertEquals(Arrays.toString(docResp.getShardInfo().getFailures()), 1, docResp.getShardInfo().getTotal());
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) BlockClusterStateProcessing(org.elasticsearch.test.disruption.BlockClusterStateProcessing) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) IOException(java.io.IOException) IndexResponse(org.elasticsearch.action.index.IndexResponse) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Settings(org.elasticsearch.common.settings.Settings) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings)

Example 2 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.

the class SimpleDataNodesIT method testDataNodes.

public void testDataNodes() throws Exception {
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).build());
    client().admin().indices().create(createIndexRequest("test").waitForActiveShards(ActiveShardCount.NONE)).actionGet();
    try {
        client().index(Requests.indexRequest("test").type("type1").id("1").source(source("1", "test"), XContentType.JSON).timeout(timeValueSeconds(1))).actionGet();
        fail("no allocation should happen");
    } catch (UnavailableShardsException e) {
    // all is well
    }
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).build());
    assertThat(client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes("2").setLocal(true).execute().actionGet().isTimedOut(), equalTo(false));
    // still no shard should be allocated
    try {
        client().index(Requests.indexRequest("test").type("type1").id("1").source(source("1", "test"), XContentType.JSON).timeout(timeValueSeconds(1))).actionGet();
        fail("no allocation should happen");
    } catch (UnavailableShardsException e) {
    // all is well
    }
    // now, start a node data, and see that it gets with shards
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), true).build());
    assertThat(client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes("3").setLocal(true).execute().actionGet().isTimedOut(), equalTo(false));
    IndexResponse indexResponse = client().index(Requests.indexRequest("test").type("type1").id("1").source(source("1", "test"), XContentType.JSON)).actionGet();
    assertThat(indexResponse.getId(), equalTo("1"));
    assertThat(indexResponse.getType(), equalTo("type1"));
}
Also used : UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 3 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.

the class TransportShardBulkActionTests method testShouldExecuteReplicaItem.

public void testShouldExecuteReplicaItem() throws Exception {
    // Successful index request should be replicated
    DocWriteRequest writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "foo", "bar");
    DocWriteResponse response = new IndexResponse(shardId, "type", "id", 1, 1, randomBoolean());
    BulkItemRequest request = new BulkItemRequest(0, writeRequest);
    request.setPrimaryResponse(new BulkItemResponse(0, DocWriteRequest.OpType.INDEX, response));
    assertTrue(TransportShardBulkAction.shouldExecuteReplicaItem(request, 0));
    // Failed index requests should not be replicated (for now!)
    writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "foo", "bar");
    response = new IndexResponse(shardId, "type", "id", 1, 1, randomBoolean());
    request = new BulkItemRequest(0, writeRequest);
    request.setPrimaryResponse(new BulkItemResponse(0, DocWriteRequest.OpType.INDEX, new BulkItemResponse.Failure("test", "type", "id", new IllegalArgumentException("i died"))));
    assertFalse(TransportShardBulkAction.shouldExecuteReplicaItem(request, 0));
    // NOOP requests should not be replicated
    writeRequest = new UpdateRequest("index", "type", "id");
    response = new UpdateResponse(shardId, "type", "id", 1, DocWriteResponse.Result.NOOP);
    request = new BulkItemRequest(0, writeRequest);
    request.setPrimaryResponse(new BulkItemResponse(0, DocWriteRequest.OpType.UPDATE, response));
    assertFalse(TransportShardBulkAction.shouldExecuteReplicaItem(request, 0));
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 4 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.

the class BulkRequestModifierTests method testPipelineFailures.

public void testPipelineFailures() {
    BulkRequest originalBulkRequest = new BulkRequest();
    for (int i = 0; i < 32; i++) {
        originalBulkRequest.add(new IndexRequest("index", "type", String.valueOf(i)));
    }
    TransportBulkAction.BulkRequestModifier modifier = new TransportBulkAction.BulkRequestModifier(originalBulkRequest);
    for (int i = 0; modifier.hasNext(); i++) {
        modifier.next();
        if (i % 2 == 0) {
            modifier.markCurrentItemAsFailed(new RuntimeException());
        }
    }
    // So half of the requests have "failed", so only the successful requests are left:
    BulkRequest bulkRequest = modifier.getBulkRequest();
    assertThat(bulkRequest.requests().size(), Matchers.equalTo(16));
    List<BulkItemResponse> responses = new ArrayList<>();
    ActionListener<BulkResponse> bulkResponseListener = modifier.wrapActionListenerIfNeeded(1L, new ActionListener<BulkResponse>() {

        @Override
        public void onResponse(BulkResponse bulkItemResponses) {
            responses.addAll(Arrays.asList(bulkItemResponses.getItems()));
        }

        @Override
        public void onFailure(Exception e) {
        }
    });
    List<BulkItemResponse> originalResponses = new ArrayList<>();
    for (DocWriteRequest actionRequest : bulkRequest.requests()) {
        IndexRequest indexRequest = (IndexRequest) actionRequest;
        IndexResponse indexResponse = new IndexResponse(new ShardId("index", "_na_", 0), indexRequest.type(), indexRequest.id(), 1, 1, true);
        originalResponses.add(new BulkItemResponse(Integer.parseInt(indexRequest.id()), indexRequest.opType(), indexResponse));
    }
    bulkResponseListener.onResponse(new BulkResponse(originalResponses.toArray(new BulkItemResponse[originalResponses.size()]), 0));
    assertThat(responses.size(), Matchers.equalTo(32));
    for (int i = 0; i < 32; i++) {
        assertThat(responses.get(i).getId(), Matchers.equalTo(String.valueOf(i)));
    }
}
Also used : ArrayList(java.util.ArrayList) IndexRequest(org.elasticsearch.action.index.IndexRequest) ShardId(org.elasticsearch.index.shard.ShardId) IndexResponse(org.elasticsearch.action.index.IndexResponse) DocWriteRequest(org.elasticsearch.action.DocWriteRequest)

Example 5 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.

the class AsyncBulkByScrollActionTests method testBulkResponseSetsLotsOfStatus.

public void testBulkResponseSetsLotsOfStatus() {
    testRequest.setAbortOnVersionConflict(false);
    int maxBatches = randomIntBetween(0, 100);
    long versionConflicts = 0;
    long created = 0;
    long updated = 0;
    long deleted = 0;
    for (int batches = 0; batches < maxBatches; batches++) {
        BulkItemResponse[] responses = new BulkItemResponse[randomIntBetween(0, 100)];
        for (int i = 0; i < responses.length; i++) {
            ShardId shardId = new ShardId(new Index("name", "uid"), 0);
            if (rarely()) {
                versionConflicts++;
                responses[i] = new BulkItemResponse(i, randomFrom(DocWriteRequest.OpType.values()), new Failure(shardId.getIndexName(), "type", "id" + i, new VersionConflictEngineException(shardId, "type", "id", "test")));
                continue;
            }
            boolean createdResponse;
            DocWriteRequest.OpType opType;
            switch(randomIntBetween(0, 2)) {
                case 0:
                    createdResponse = true;
                    opType = DocWriteRequest.OpType.CREATE;
                    created++;
                    break;
                case 1:
                    createdResponse = false;
                    opType = randomFrom(DocWriteRequest.OpType.INDEX, DocWriteRequest.OpType.UPDATE);
                    updated++;
                    break;
                case 2:
                    createdResponse = false;
                    opType = DocWriteRequest.OpType.DELETE;
                    deleted++;
                    break;
                default:
                    throw new RuntimeException("Bad scenario");
            }
            responses[i] = new BulkItemResponse(i, opType, new IndexResponse(shardId, "type", "id" + i, randomInt(20), randomInt(), createdResponse));
        }
        new DummyAsyncBulkByScrollAction().onBulkResponse(timeValueNanos(System.nanoTime()), new BulkResponse(responses, 0));
        assertEquals(versionConflicts, testTask.getStatus().getVersionConflicts());
        assertEquals(updated, testTask.getStatus().getUpdated());
        assertEquals(created, testTask.getStatus().getCreated());
        assertEquals(deleted, testTask.getStatus().getDeleted());
        assertEquals(versionConflicts, testTask.getStatus().getVersionConflicts());
    }
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) Index(org.elasticsearch.index.Index) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ShardId(org.elasticsearch.index.shard.ShardId) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) IndexResponse(org.elasticsearch.action.index.IndexResponse) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Failure(org.elasticsearch.action.bulk.BulkItemResponse.Failure) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) SearchFailure(org.elasticsearch.action.bulk.byscroll.ScrollableHitSource.SearchFailure)

Aggregations

IndexResponse (org.elasticsearch.action.index.IndexResponse)103 Test (org.junit.Test)26 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 IOException (java.io.IOException)15 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)15 HashMap (java.util.HashMap)13 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)13 IndexRequest (org.elasticsearch.action.index.IndexRequest)13 ElasticsearchException (org.elasticsearch.ElasticsearchException)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)8 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)7 Settings (org.elasticsearch.common.settings.Settings)7 ArrayList (java.util.ArrayList)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)6 ExecutionException (java.util.concurrent.ExecutionException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)5