Search in sources :

Example 6 with IndexShard

use of org.elasticsearch.index.shard.IndexShard in project elasticsearch by elastic.

the class InternalTestCluster method assertSameSyncIdSameDocs.

private void assertSameSyncIdSameDocs() {
    Map<String, Long> docsOnShards = new HashMap<>();
    final Collection<NodeAndClient> nodesAndClients = nodes.values();
    for (NodeAndClient nodeAndClient : nodesAndClients) {
        IndicesService indexServices = getInstance(IndicesService.class, nodeAndClient.name);
        for (IndexService indexService : indexServices) {
            for (IndexShard indexShard : indexService) {
                CommitStats commitStats = indexShard.commitStats();
                if (commitStats != null) {
                    // null if the engine is closed or if the shard is recovering
                    String syncId = commitStats.getUserData().get(Engine.SYNC_COMMIT_ID);
                    if (syncId != null) {
                        long liveDocsOnShard = commitStats.getNumDocs();
                        if (docsOnShards.get(syncId) != null) {
                            assertThat("sync id is equal but number of docs does not match on node " + nodeAndClient.name + ". expected " + docsOnShards.get(syncId) + " but got " + liveDocsOnShard, docsOnShards.get(syncId), equalTo(liveDocsOnShard));
                        } else {
                            docsOnShards.put(syncId, liveDocsOnShard);
                        }
                    }
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) IndexService(org.elasticsearch.index.IndexService) CommitStats(org.elasticsearch.index.engine.CommitStats) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService)

Example 7 with IndexShard

use of org.elasticsearch.index.shard.IndexShard in project elasticsearch by elastic.

the class IndexServiceTests method testRescheduleAsyncFsync.

public void testRescheduleAsyncFsync() throws Exception {
    Settings settings = Settings.builder().put(IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING.getKey(), // very often :)
    "100ms").put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.REQUEST).build();
    IndexService indexService = createIndex("test", settings);
    ensureGreen("test");
    assertNull(indexService.getFsyncTask());
    IndexMetaData metaData = IndexMetaData.builder(indexService.getMetaData()).settings(Settings.builder().put(indexService.getMetaData().getSettings()).put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC)).build();
    indexService.updateMetaData(metaData);
    assertNotNull(indexService.getFsyncTask());
    assertTrue(indexService.getRefreshTask().mustReschedule());
    client().prepareIndex("test", "test", "1").setSource("{\"foo\": \"bar\"}", XContentType.JSON).get();
    IndexShard shard = indexService.getShard(0);
    assertBusy(() -> {
        assertFalse(shard.getTranslog().syncNeeded());
    });
    metaData = IndexMetaData.builder(indexService.getMetaData()).settings(Settings.builder().put(indexService.getMetaData().getSettings()).put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.REQUEST)).build();
    indexService.updateMetaData(metaData);
    assertNull(indexService.getFsyncTask());
    metaData = IndexMetaData.builder(indexService.getMetaData()).settings(Settings.builder().put(indexService.getMetaData().getSettings()).put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC)).build();
    indexService.updateMetaData(metaData);
    assertNotNull(indexService.getFsyncTask());
}
Also used : IndexShard(org.elasticsearch.index.shard.IndexShard) Settings(org.elasticsearch.common.settings.Settings) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 8 with IndexShard

use of org.elasticsearch.index.shard.IndexShard in project elasticsearch by elastic.

the class IndexServiceTests method testAsyncFsyncActuallyWorks.

public void testAsyncFsyncActuallyWorks() throws Exception {
    Settings settings = Settings.builder().put(IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING.getKey(), // very often :)
    "100ms").put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC).build();
    IndexService indexService = createIndex("test", settings);
    ensureGreen("test");
    assertTrue(indexService.getRefreshTask().mustReschedule());
    client().prepareIndex("test", "test", "1").setSource("{\"foo\": \"bar\"}", XContentType.JSON).get();
    IndexShard shard = indexService.getShard(0);
    assertBusy(() -> {
        assertFalse(shard.getTranslog().syncNeeded());
    });
}
Also used : IndexShard(org.elasticsearch.index.shard.IndexShard) Settings(org.elasticsearch.common.settings.Settings)

Example 9 with IndexShard

use of org.elasticsearch.index.shard.IndexShard in project elasticsearch by elastic.

the class IndexServiceTests method testRefreshActuallyWorks.

public void testRefreshActuallyWorks() throws Exception {
    IndexService indexService = createIndex("test", Settings.EMPTY);
    ensureGreen("test");
    IndexService.AsyncRefreshTask refreshTask = indexService.getRefreshTask();
    assertEquals(1000, refreshTask.getInterval().millis());
    assertTrue(indexService.getRefreshTask().mustReschedule());
    // now disable
    IndexMetaData metaData = IndexMetaData.builder(indexService.getMetaData()).settings(Settings.builder().put(indexService.getMetaData().getSettings()).put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), -1)).build();
    indexService.updateMetaData(metaData);
    client().prepareIndex("test", "test", "1").setSource("{\"foo\": \"bar\"}", XContentType.JSON).get();
    IndexShard shard = indexService.getShard(0);
    try (Engine.Searcher searcher = shard.acquireSearcher("test")) {
        TopDocs search = searcher.searcher().search(new MatchAllDocsQuery(), 10);
        assertEquals(0, search.totalHits);
    }
    // refresh every millisecond
    metaData = IndexMetaData.builder(indexService.getMetaData()).settings(Settings.builder().put(indexService.getMetaData().getSettings()).put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "1ms")).build();
    indexService.updateMetaData(metaData);
    assertBusy(() -> {
        try (Engine.Searcher searcher = shard.acquireSearcher("test")) {
            TopDocs search = searcher.searcher().search(new MatchAllDocsQuery(), 10);
            assertEquals(1, search.totalHits);
        } catch (IOException e) {
            fail(e.getMessage());
        }
    });
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) IndexShard(org.elasticsearch.index.shard.IndexShard) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Engine(org.elasticsearch.index.engine.Engine) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 10 with IndexShard

use of org.elasticsearch.index.shard.IndexShard in project elasticsearch by elastic.

the class IndexWithShadowReplicasIT method testRestoreToShadow.

/**
     * Tests the case where we create an index without shadow replicas, snapshot it and then restore into
     * an index with shadow replicas enabled.
     */
public void testRestoreToShadow() throws ExecutionException, InterruptedException {
    final Path dataPath = createTempDir();
    Settings nodeSettings = nodeSettings(dataPath);
    internalCluster().startNodes(3, nodeSettings);
    Settings idxSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).build();
    assertAcked(prepareCreate("foo").setSettings(idxSettings));
    ensureGreen();
    final int numDocs = randomIntBetween(10, 100);
    for (int i = 0; i < numDocs; i++) {
        client().prepareIndex("foo", "doc", "" + i).setSource("foo", "bar").get();
    }
    assertNoFailures(client().admin().indices().prepareFlush().setForce(true).execute().actionGet());
    assertAcked(client().admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath())));
    CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("foo").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    assertThat(client().admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    Settings shadowSettings = Settings.builder().put(IndexMetaData.SETTING_DATA_PATH, dataPath.toAbsolutePath().toString()).put(IndexMetaData.SETTING_SHADOW_REPLICAS, true).put(IndexMetaData.SETTING_SHARED_FILESYSTEM, true).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 2).build();
    logger.info("--> restore the index into shadow replica index");
    RestoreSnapshotResponse restoreSnapshotResponse = client().admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndexSettings(shadowSettings).setWaitForCompletion(true).setRenamePattern("(.+)").setRenameReplacement("$1-copy").execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    ensureGreen();
    refresh();
    Index index = resolveIndex("foo-copy");
    for (IndicesService service : internalCluster().getDataNodeInstances(IndicesService.class)) {
        if (service.hasIndex(index)) {
            IndexShard shard = service.indexServiceSafe(index).getShardOrNull(0);
            if (shard.routingEntry().primary()) {
                assertFalse(shard instanceof ShadowIndexShard);
            } else {
                assertTrue(shard instanceof ShadowIndexShard);
            }
        }
    }
    logger.info("--> performing query");
    SearchResponse resp = client().prepareSearch("foo-copy").setQuery(matchAllQuery()).get();
    assertHitCount(resp, numDocs);
}
Also used : Path(java.nio.file.Path) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) ShadowIndexShard(org.elasticsearch.index.shard.ShadowIndexShard) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) ShadowIndexShard(org.elasticsearch.index.shard.ShadowIndexShard) Settings(org.elasticsearch.common.settings.Settings) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

IndexShard (org.elasticsearch.index.shard.IndexShard)94 IndexService (org.elasticsearch.index.IndexService)51 IndicesService (org.elasticsearch.indices.IndicesService)20 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)19 ShardId (org.elasticsearch.index.shard.ShardId)19 IOException (java.io.IOException)16 Engine (org.elasticsearch.index.engine.Engine)16 ElasticsearchException (org.elasticsearch.ElasticsearchException)13 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)13 Settings (org.elasticsearch.common.settings.Settings)11 Translog (org.elasticsearch.index.translog.Translog)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 HashMap (java.util.HashMap)8 IndexRequest (org.elasticsearch.action.index.IndexRequest)8 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)8 ClusterState (org.elasticsearch.cluster.ClusterState)7 Releasable (org.elasticsearch.common.lease.Releasable)7 ClusterService (org.elasticsearch.cluster.service.ClusterService)6 Path (java.nio.file.Path)5 ArrayList (java.util.ArrayList)5