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());
}
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;
}
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]));
}
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));
}
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))));
}
}
Aggregations