Search in sources :

Example 46 with IndexShard

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

the class SearchServiceTests method testCloseSearchContextOnRewriteException.

public void testCloseSearchContextOnRewriteException() {
    createIndex("index");
    client().prepareIndex("index", "type", "1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).get();
    SearchService service = getInstanceFromNode(SearchService.class);
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index"));
    IndexShard indexShard = indexService.getShard(0);
    final int activeContexts = service.getActiveContexts();
    final int activeRefs = indexShard.store().refCount();
    expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearch("index").setQuery(new FailOnRewriteQueryBuilder()).get());
    assertEquals(activeContexts, service.getActiveContexts());
    assertEquals(activeRefs, indexShard.store().refCount());
}
Also used : IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService)

Example 47 with IndexShard

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

the class InternalCountOperation method prepareGetCount.

public CompletableFuture<Supplier<Long>> prepareGetCount(TransactionContext txnCtx, Index index, int shardId, Symbol filter) {
    IndexService indexService;
    try {
        indexService = indicesService.indexServiceSafe(index);
    } catch (IndexNotFoundException e) {
        if (IndexParts.isPartitioned(index.getName())) {
            return CompletableFuture.completedFuture(() -> 0L);
        } else {
            return CompletableFuture.failedFuture(e);
        }
    }
    IndexShard indexShard = indexService.getShard(shardId);
    CompletableFuture<Supplier<Long>> futureCount = new CompletableFuture<>();
    indexShard.awaitShardSearchActive(b -> {
        try {
            futureCount.complete(() -> syncCount(indexService, indexShard, txnCtx, filter));
        } catch (Throwable t) {
            futureCount.completeExceptionally(t);
        }
    });
    return futureCount;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Supplier(java.util.function.Supplier)

Example 48 with IndexShard

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

the class PKLookupOperation method lookup.

public BatchIterator<Row> lookup(UUID jobId, TransactionContext txnCtx, Supplier<RamAccounting> ramAccountingSupplier, Supplier<MemoryManager> memoryManagerSupplier, boolean ignoreMissing, Map<ShardId, List<PKAndVersion>> idsByShard, Collection<? extends Projection> projections, boolean requiresScroll, Function<Doc, Row> resultToRow) {
    ArrayList<BatchIterator<Row>> iterators = new ArrayList<>(idsByShard.size());
    for (Map.Entry<ShardId, List<PKAndVersion>> idsByShardEntry : idsByShard.entrySet()) {
        ShardId shardId = idsByShardEntry.getKey();
        IndexService indexService = indicesService.indexService(shardId.getIndex());
        if (indexService == null) {
            if (ignoreMissing) {
                continue;
            }
            throw new IndexNotFoundException(shardId.getIndex());
        }
        IndexShard shard = indexService.getShardOrNull(shardId.id());
        if (shard == null) {
            if (ignoreMissing) {
                continue;
            }
            throw new ShardNotFoundException(shardId);
        }
        Stream<Row> rowStream = idsByShardEntry.getValue().stream().map(pkAndVersion -> lookupDoc(shard, pkAndVersion.id(), pkAndVersion.version(), VersionType.EXTERNAL, pkAndVersion.seqNo(), pkAndVersion.primaryTerm())).filter(Objects::nonNull).map(resultToRow);
        if (projections.isEmpty()) {
            final Iterable<Row> rowIterable = requiresScroll ? rowStream.map(row -> new RowN(row.materialize())).collect(Collectors.toList()) : rowStream::iterator;
            iterators.add(InMemoryBatchIterator.of(rowIterable, SentinelRow.SENTINEL, true));
        } else {
            ProjectorFactory projectorFactory;
            try {
                projectorFactory = shardCollectSource.getProjectorFactory(shardId);
            } catch (ShardNotFoundException e) {
                if (ignoreMissing) {
                    continue;
                }
                throw e;
            }
            Projectors projectors = new Projectors(projections, jobId, txnCtx, ramAccountingSupplier.get(), memoryManagerSupplier.get(), projectorFactory);
            final Iterable<Row> rowIterable = requiresScroll && !projectors.providesIndependentScroll() ? rowStream.map(row -> new RowN(row.materialize())).collect(Collectors.toList()) : rowStream::iterator;
            iterators.add(projectors.wrap(InMemoryBatchIterator.of(rowIterable, SentinelRow.SENTINEL, true)));
        }
    }
    // noinspection unchecked
    return CompositeBatchIterator.seqComposite(iterators.toArray(new BatchIterator[0]));
}
Also used : IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) ArrayList(java.util.ArrayList) BatchIterator(io.crate.data.BatchIterator) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) CompositeBatchIterator(io.crate.data.CompositeBatchIterator) ShardId(org.elasticsearch.index.shard.ShardId) Projectors(io.crate.execution.engine.pipeline.Projectors) RowN(io.crate.data.RowN) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) ProjectorFactory(io.crate.execution.engine.pipeline.ProjectorFactory) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ArrayList(java.util.ArrayList) List(java.util.List) Row(io.crate.data.Row) SentinelRow(io.crate.data.SentinelRow) Map(java.util.Map)

Example 49 with IndexShard

use of org.elasticsearch.index.shard.IndexShard 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 50 with IndexShard

use of org.elasticsearch.index.shard.IndexShard 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)

Aggregations

IndexShard (org.elasticsearch.index.shard.IndexShard)173 IndexService (org.elasticsearch.index.IndexService)74 ShardId (org.elasticsearch.index.shard.ShardId)49 IndicesService (org.elasticsearch.indices.IndicesService)47 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)36 Test (org.junit.Test)35 IOException (java.io.IOException)29 Engine (org.elasticsearch.index.engine.Engine)26 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)21 ElasticsearchException (org.elasticsearch.ElasticsearchException)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)18 Settings (org.elasticsearch.common.settings.Settings)18 ArrayList (java.util.ArrayList)16 Translog (org.elasticsearch.index.translog.Translog)16 HashMap (java.util.HashMap)15 Index (org.elasticsearch.index.Index)15 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)13 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)12 List (java.util.List)11