Search in sources :

Example 31 with IndicesService

use of org.elasticsearch.indices.IndicesService in project crate by crate.

the class RetentionLeaseSyncActionTests method testRetentionLeaseSyncActionOnReplica.

@Test
public void testRetentionLeaseSyncActionOnReplica() throws WriteStateException {
    final IndicesService indicesService = mock(IndicesService.class);
    final Index index = new Index("index", "uuid");
    final IndexService indexService = mock(IndexService.class);
    when(indicesService.indexServiceSafe(index)).thenReturn(indexService);
    final int id = randomIntBetween(0, 4);
    final IndexShard indexShard = mock(IndexShard.class);
    when(indexService.getShard(id)).thenReturn(indexShard);
    final ShardId shardId = new ShardId(index, id);
    when(indexShard.shardId()).thenReturn(shardId);
    final RetentionLeaseSyncAction action = new RetentionLeaseSyncAction(Settings.EMPTY, transportService, clusterService, indicesService, threadPool, shardStateAction);
    final RetentionLeases retentionLeases = mock(RetentionLeases.class);
    final RetentionLeaseSyncAction.Request request = new RetentionLeaseSyncAction.Request(indexShard.shardId(), retentionLeases);
    final TransportWriteAction.WriteReplicaResult<RetentionLeaseSyncAction.Request> result = action.shardOperationOnReplica(request, indexShard);
    // the retention leases on the shard should be updated
    verify(indexShard).updateRetentionLeasesOnReplica(retentionLeases);
    // the retention leases on the shard should be persisted
    verify(indexShard).persistRetentionLeases();
    // the result should indicate success
    final AtomicBoolean success = new AtomicBoolean();
    result.runPostReplicaActions(ActionListener.wrap(r -> success.set(true), e -> fail(e.toString())));
    assertTrue(success.get());
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterServiceUtils.createClusterService(org.elasticsearch.test.ClusterServiceUtils.createClusterService) ClusterService(org.elasticsearch.cluster.service.ClusterService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Index(org.elasticsearch.index.Index) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) Settings(org.elasticsearch.common.settings.Settings) WriteStateException(org.elasticsearch.gateway.WriteStateException) ThreadPool(org.elasticsearch.threadpool.ThreadPool) IndicesService(org.elasticsearch.indices.IndicesService) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) IOUtils(io.crate.common.io.IOUtils) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) ActionTestUtils(org.elasticsearch.action.support.ActionTestUtils) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) ActionListener(org.elasticsearch.action.ActionListener) TransportWriteAction(org.elasticsearch.action.support.replication.TransportWriteAction) Mockito.mock(org.mockito.Mockito.mock) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) Index(org.elasticsearch.index.Index) ShardId(org.elasticsearch.index.shard.ShardId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransportWriteAction(org.elasticsearch.action.support.replication.TransportWriteAction) Test(org.junit.Test)

Example 32 with IndicesService

use of org.elasticsearch.indices.IndicesService in project elasticsearch by elastic.

the class IndicesShardStoreRequestIT method testCorruptedShards.

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/12416")
public void testCorruptedShards() throws Exception {
    String index = "test";
    internalCluster().ensureAtLeastNumDataNodes(2);
    assertAcked(prepareCreate(index).setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, "5").put(MockFSIndexStore.INDEX_CHECK_INDEX_ON_CLOSE_SETTING.getKey(), false)));
    indexRandomData(index);
    ensureGreen(index);
    logger.info("--> disable allocation");
    disableAllocation(index);
    logger.info("--> corrupt random shard copies");
    Map<Integer, Set<String>> corruptedShardIDMap = new HashMap<>();
    Index idx = resolveIndex(index);
    for (String node : internalCluster().nodesInclude(index)) {
        IndicesService indexServices = internalCluster().getInstance(IndicesService.class, node);
        IndexService indexShards = indexServices.indexServiceSafe(idx);
        for (Integer shardId : indexShards.shardIds()) {
            IndexShard shard = indexShards.getShard(shardId);
            if (randomBoolean()) {
                shard.failShard("test", new CorruptIndexException("test corrupted", ""));
                Set<String> nodes = corruptedShardIDMap.get(shardId);
                if (nodes == null) {
                    nodes = new HashSet<>();
                }
                nodes.add(node);
                corruptedShardIDMap.put(shardId, nodes);
            }
        }
    }
    IndicesShardStoresResponse rsp = client().admin().indices().prepareShardStores(index).setShardStatuses("all").get();
    ImmutableOpenIntMap<List<IndicesShardStoresResponse.StoreStatus>> shardStatuses = rsp.getStoreStatuses().get(index);
    assertNotNull(shardStatuses);
    assertThat(shardStatuses.size(), greaterThan(0));
    for (IntObjectCursor<List<IndicesShardStoresResponse.StoreStatus>> shardStatus : shardStatuses) {
        for (IndicesShardStoresResponse.StoreStatus status : shardStatus.value) {
            if (corruptedShardIDMap.containsKey(shardStatus.key) && corruptedShardIDMap.get(shardStatus.key).contains(status.getNode().getName())) {
                assertThat(status.getStoreException(), notNullValue());
            } else {
                assertNull(status.getStoreException());
            }
        }
    }
    logger.info("--> enable allocation");
    enableAllocation(index);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) Index(org.elasticsearch.index.Index) List(java.util.List)

Example 33 with IndicesService

use of org.elasticsearch.indices.IndicesService in project elasticsearch by elastic.

the class MetaDataIndexTemplateServiceTests method putTemplateDetail.

private List<Throwable> putTemplateDetail(PutRequest request) throws Exception {
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    ClusterService clusterService = getInstanceFromNode(ClusterService.class);
    MetaDataCreateIndexService createIndexService = new MetaDataCreateIndexService(Settings.EMPTY, clusterService, indicesService, null, null, null, null, null, xContentRegistry());
    MetaDataIndexTemplateService service = new MetaDataIndexTemplateService(Settings.EMPTY, clusterService, createIndexService, new AliasValidator(Settings.EMPTY), indicesService, new IndexScopedSettings(Settings.EMPTY, IndexScopedSettings.BUILT_IN_INDEX_SETTINGS), xContentRegistry());
    final List<Throwable> throwables = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(1);
    service.putTemplate(request, new MetaDataIndexTemplateService.PutListener() {

        @Override
        public void onResponse(MetaDataIndexTemplateService.PutResponse response) {
            latch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            throwables.add(e);
            latch.countDown();
        }
    });
    latch.await();
    return throwables;
}
Also used : IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) AliasValidator(org.elasticsearch.cluster.metadata.AliasValidator) ArrayList(java.util.ArrayList) IndicesService(org.elasticsearch.indices.IndicesService) MetaDataCreateIndexService(org.elasticsearch.cluster.metadata.MetaDataCreateIndexService) CountDownLatch(java.util.concurrent.CountDownLatch) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException) ClusterService(org.elasticsearch.cluster.service.ClusterService) MetaDataIndexTemplateService(org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService)

Example 34 with IndicesService

use of org.elasticsearch.indices.IndicesService in project elasticsearch by elastic.

the class IndexWithShadowReplicasIT method testShadowReplicaNaturalRelocation.

/**
     * Tests that shadow replicas can be "naturally" rebalanced and relocated
     * around the cluster. By "naturally" I mean without using the reroute API
     */
// This test failed on CI when trying to assert that all the shard data has been deleted
// from the index path. It has not been reproduced locally. Despite the IndicesService
// deleting the index and hence, deleting all the shard data for the index, the test
// failure still showed some Lucene files in the data directory for that index. Not sure
// why that is, so turning on more logging here.
@TestLogging("org.elasticsearch.indices:TRACE,org.elasticsearch.env:TRACE,_root:DEBUG")
public void testShadowReplicaNaturalRelocation() throws Exception {
    Path dataPath = createTempDir();
    Settings nodeSettings = nodeSettings(dataPath);
    final List<String> nodes = internalCluster().startNodes(2, nodeSettings);
    String IDX = "test";
    Settings idxSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 5).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).put(IndexMetaData.SETTING_DATA_PATH, dataPath.toAbsolutePath().toString()).put(IndexMetaData.SETTING_SHADOW_REPLICAS, true).put(IndexMetaData.SETTING_SHARED_FILESYSTEM, true).build();
    prepareCreate(IDX).setSettings(idxSettings).addMapping("doc", "foo", "type=text").get();
    ensureGreen(IDX);
    int docCount = randomIntBetween(10, 100);
    List<IndexRequestBuilder> builders = new ArrayList<>();
    for (int i = 0; i < docCount; i++) {
        builders.add(client().prepareIndex(IDX, "doc", i + "").setSource("foo", "bar"));
    }
    indexRandom(true, true, true, builders);
    flushAndRefresh(IDX);
    // start a third node, with 5 shards each on the other nodes, they
    // should relocate some to the third node
    final String node3 = internalCluster().startNode(nodeSettings);
    nodes.add(node3);
    assertBusy(new Runnable() {

        @Override
        public void run() {
            client().admin().cluster().prepareHealth().setWaitForNodes("3").get();
            ClusterStateResponse resp = client().admin().cluster().prepareState().get();
            RoutingNodes nodes = resp.getState().getRoutingNodes();
            for (RoutingNode node : nodes) {
                logger.info("--> node has {} shards (needs at least 2)", node.numberOfOwningShards());
                assertThat("at least 2 shards on node", node.numberOfOwningShards(), greaterThanOrEqualTo(2));
            }
        }
    });
    ensureYellow(IDX);
    logger.info("--> performing query");
    SearchResponse resp = client().prepareSearch(IDX).setQuery(matchAllQuery()).get();
    assertHitCount(resp, docCount);
    assertAcked(client().admin().indices().prepareDelete(IDX));
    assertAllIndicesRemovedAndDeletionCompleted(internalCluster().getInstances(IndicesService.class));
    assertPathHasBeenCleared(dataPath);
//TODO: uncomment the test below when https://github.com/elastic/elasticsearch/issues/17695 is resolved.
//assertIndicesDirsDeleted(nodes);
}
Also used : Path(java.nio.file.Path) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) IndicesService(org.elasticsearch.indices.IndicesService) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) Settings(org.elasticsearch.common.settings.Settings) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging)

Example 35 with IndicesService

use of org.elasticsearch.indices.IndicesService in project elasticsearch by elastic.

the class IndexShardIT method testDurableFlagHasEffect.

public void testDurableFlagHasEffect() {
    createIndex("test");
    ensureGreen();
    client().prepareIndex("test", "bar", "1").setSource("{}", XContentType.JSON).get();
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    IndexService test = indicesService.indexService(resolveIndex("test"));
    IndexShard shard = test.getShardOrNull(0);
    setDurability(shard, Translog.Durability.REQUEST);
    assertFalse(ShardUtilsTests.getShardEngine(shard).getTranslog().syncNeeded());
    setDurability(shard, Translog.Durability.ASYNC);
    client().prepareIndex("test", "bar", "2").setSource("{}", XContentType.JSON).get();
    assertTrue(ShardUtilsTests.getShardEngine(shard).getTranslog().syncNeeded());
    setDurability(shard, Translog.Durability.REQUEST);
    client().prepareDelete("test", "bar", "1").get();
    assertFalse(ShardUtilsTests.getShardEngine(shard).getTranslog().syncNeeded());
    setDurability(shard, Translog.Durability.ASYNC);
    client().prepareDelete("test", "bar", "2").get();
    assertTrue(ShardUtilsTests.getShardEngine(shard).getTranslog().syncNeeded());
    setDurability(shard, Translog.Durability.REQUEST);
    assertNoFailures(client().prepareBulk().add(client().prepareIndex("test", "bar", "3").setSource("{}", XContentType.JSON)).add(client().prepareDelete("test", "bar", "1")).get());
    assertFalse(ShardUtilsTests.getShardEngine(shard).getTranslog().syncNeeded());
    setDurability(shard, Translog.Durability.ASYNC);
    assertNoFailures(client().prepareBulk().add(client().prepareIndex("test", "bar", "4").setSource("{}", XContentType.JSON)).add(client().prepareDelete("test", "bar", "3")).get());
    setDurability(shard, Translog.Durability.REQUEST);
    assertTrue(ShardUtilsTests.getShardEngine(shard).getTranslog().syncNeeded());
}
Also used : IndexService(org.elasticsearch.index.IndexService) IndicesService(org.elasticsearch.indices.IndicesService)

Aggregations

IndicesService (org.elasticsearch.indices.IndicesService)57 IndexService (org.elasticsearch.index.IndexService)41 IndexShard (org.elasticsearch.index.shard.IndexShard)29 Index (org.elasticsearch.index.Index)21 ClusterState (org.elasticsearch.cluster.ClusterState)12 Settings (org.elasticsearch.common.settings.Settings)12 ClusterService (org.elasticsearch.cluster.service.ClusterService)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)9 ShardId (org.elasticsearch.index.shard.ShardId)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)7 ByteSizeValue (org.elasticsearch.common.unit.ByteSizeValue)7 Test (org.junit.Test)7 List (java.util.List)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)6 HashMap (java.util.HashMap)5 Set (java.util.Set)5 ActionListener (org.elasticsearch.action.ActionListener)5