Search in sources :

Example 21 with IndicesService

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

the class TransportShardDeleteActionTest method prepare.

@Before
public void prepare() throws Exception {
    indexUUID = UUIDs.randomBase64UUID();
    IndicesService indicesService = mock(IndicesService.class);
    IndexService indexService = mock(IndexService.class);
    when(indicesService.indexServiceSafe(new Index(TABLE_IDENT.indexNameOrAlias(), indexUUID))).thenReturn(indexService);
    indexShard = mock(IndexShard.class);
    when(indexService.getShard(0)).thenReturn(indexShard);
    transportShardDeleteAction = new TransportShardDeleteAction(MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, THREAD_POOL, clusterService.getClusterSettings()), mock(ClusterService.class), indicesService, mock(TasksService.class), mock(ThreadPool.class), mock(ShardStateAction.class), mock(SchemaUpdateClient.class));
}
Also used : IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) Index(org.elasticsearch.index.Index) Before(org.junit.Before)

Example 22 with IndicesService

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

the class ClusterDisruptionIT method testRestartNodeWhileIndexing.

@Test
public void testRestartNodeWhileIndexing() throws Exception {
    startCluster(3);
    String index = toIndexName(sqlExecutor.getCurrentSchema(), "t", null);
    int numberOfReplicas = between(1, 2);
    logger.info("creating table with {} shards and {} replicas", 1, numberOfReplicas);
    execute("create table t (id int primary key, x string) clustered into 1 shards with (number_of_replicas = 2, " + "\"write.wait_for_active_shards\" = 1)");
    AtomicBoolean stopped = new AtomicBoolean();
    Thread[] threads = new Thread[between(1, 4)];
    AtomicInteger docID = new AtomicInteger();
    Set<String> ackedDocs = ConcurrentCollections.newConcurrentSet();
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new Thread(() -> {
            while (stopped.get() == false && docID.get() < 5000) {
                String id = Integer.toString(docID.incrementAndGet());
                try {
                    execute("insert into t (id, x) values (?, ?)", new Object[] { id, randomInt(5000) });
                    logger.info("--> index id={}", id);
                    ackedDocs.add(id);
                } catch (Exception ignore) {
                    logger.info("--> fail to index id={}", id);
                }
            }
        });
        threads[i].start();
    }
    ensureGreen();
    assertBusy(() -> assertThat(docID.get(), greaterThanOrEqualTo(100)));
    internalCluster().restartRandomDataNode(new InternalTestCluster.RestartCallback());
    ensureGreen();
    assertBusy(() -> assertThat(docID.get(), greaterThanOrEqualTo(200)));
    stopped.set(true);
    for (Thread thread : threads) {
        thread.join();
    }
    ClusterState clusterState = internalCluster().clusterService().state();
    for (ShardRouting shardRouting : clusterState.routingTable().allShards(index)) {
        String nodeName = clusterState.nodes().get(shardRouting.currentNodeId()).getName();
        IndicesService indicesService = internalCluster().getInstance(IndicesService.class, nodeName);
        IndexShard shard = indicesService.getShardOrNull(shardRouting.shardId());
        Set<String> docs = IndexShardTestCase.getShardDocUIDs(shard);
        assertThat("shard [" + shard.routingEntry() + "] docIds [" + docs + "] vs " + " acked docIds [" + ackedDocs + "]", ackedDocs, everyItem(is(in(docs))));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) InternalTestCluster(org.elasticsearch.test.InternalTestCluster) ElasticsearchException(org.elasticsearch.ElasticsearchException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) DuplicateKeyException(io.crate.exceptions.DuplicateKeyException) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Test(org.junit.Test)

Example 23 with IndicesService

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

the class InternalTestCluster method routingKeyForShard.

synchronized String routingKeyForShard(Index index, int shard, Random random) {
    assertThat(shard, greaterThanOrEqualTo(0));
    assertThat(shard, greaterThanOrEqualTo(0));
    for (NodeAndClient n : nodes.values()) {
        Node node = n.node;
        IndicesService indicesService = getInstanceFromNode(IndicesService.class, node);
        ClusterService clusterService = getInstanceFromNode(ClusterService.class, node);
        IndexService indexService = indicesService.indexService(index);
        if (indexService != null) {
            assertThat(indexService.getIndexSettings().getSettings().getAsInt(IndexMetadata.SETTING_NUMBER_OF_SHARDS, -1), greaterThan(shard));
            OperationRouting operationRouting = clusterService.operationRouting();
            while (true) {
                String routing = RandomStrings.randomAsciiLettersOfLength(random, 10);
                final int targetShard = operationRouting.indexShards(clusterService.state(), index.getName(), null, routing).shardId().getId();
                if (shard == targetShard) {
                    return routing;
                }
            }
        }
    }
    fail("Could not find a node that holds " + index);
    return null;
}
Also used : ClusterService(org.elasticsearch.cluster.service.ClusterService) IndexService(org.elasticsearch.index.IndexService) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Node(org.elasticsearch.node.Node) MockNode(org.elasticsearch.node.MockNode) OperationRouting(org.elasticsearch.cluster.routing.OperationRouting) IndicesService(org.elasticsearch.indices.IndicesService)

Example 24 with IndicesService

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

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) {
                try {
                    CommitStats commitStats = indexShard.commitStats();
                    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);
                        }
                    }
                } catch (AlreadyClosedException e) {
                // the engine is closed or if the shard is recovering
                }
            }
        }
    }
}
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) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException)

Example 25 with IndicesService

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

the class GlobalCheckpointSyncIT method testPersistLocalCheckpoint.

public void testPersistLocalCheckpoint() {
    internalCluster().ensureAtLeastNumDataNodes(2);
    execute("create table test(id integer) clustered into 1 shards with" + "(\"global_checkpoint_sync.interval\" = ?, \"translog.durability\" = ?, number_of_replicas = ?)", new Object[] { "10ms", Translog.Durability.REQUEST.toString(), randomIntBetween(0, 1) });
    var indexName = getFqn("test");
    ensureGreen(indexName);
    int numDocs = randomIntBetween(1, 20);
    logger.info("numDocs {}", numDocs);
    long maxSeqNo = 0;
    for (int i = 0; i < numDocs; i++) {
        maxSeqNo = (long) execute("insert into test(id) values(?) returning _seq_no", new Object[] { i }).rows()[0][0];
        logger.info("got {}", maxSeqNo);
    }
    for (IndicesService indicesService : internalCluster().getDataNodeInstances(IndicesService.class)) {
        for (IndexService indexService : indicesService) {
            for (IndexShard shard : indexService) {
                final SeqNoStats seqNoStats = shard.seqNoStats();
                assertThat(maxSeqNo, equalTo(seqNoStats.getMaxSeqNo()));
                assertThat(seqNoStats.getLocalCheckpoint(), equalTo(seqNoStats.getMaxSeqNo()));
                ;
            }
        }
    }
}
Also used : IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) 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