Search in sources :

Example 1 with ShardUpsertRequest

use of io.crate.executor.transport.ShardUpsertRequest in project crate by crate.

the class IndexWriterCountBatchIteratorTest method getBulkShardProcessor.

private BulkShardProcessor<ShardUpsertRequest> getBulkShardProcessor() {
    UUID jobId = UUID.randomUUID();
    ShardUpsertRequest.Builder builder = new ShardUpsertRequest.Builder(CrateSettings.BULK_REQUEST_TIMEOUT.extractTimeValue(Settings.EMPTY), false, true, null, new Reference[] { new Reference(new ReferenceIdent(bulkImportIdent, DocSysColumns.RAW), RowGranularity.DOC, DataTypes.STRING) }, jobId, false);
    IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(Settings.EMPTY);
    return new BulkShardProcessor<>(internalCluster().getInstance(ClusterService.class), internalCluster().getInstance(TransportBulkCreateIndicesAction.class), indexNameExpressionResolver, Settings.EMPTY, internalCluster().getInstance(BulkRetryCoordinatorPool.class), false, 2, builder, internalCluster().getInstance(TransportShardUpsertAction.class)::execute, jobId);
}
Also used : BulkRetryCoordinatorPool(org.elasticsearch.action.bulk.BulkRetryCoordinatorPool) ClusterService(org.elasticsearch.cluster.ClusterService) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) TransportBulkCreateIndicesAction(org.elasticsearch.action.admin.indices.create.TransportBulkCreateIndicesAction) UUID(java.util.UUID) BulkShardProcessor(org.elasticsearch.action.bulk.BulkShardProcessor)

Example 2 with ShardUpsertRequest

use of io.crate.executor.transport.ShardUpsertRequest in project crate by crate.

the class BulkShardProcessorTest method testNonEsRejectedExceptionDoesNotResultInRetryButAborts.

@Test
public void testNonEsRejectedExceptionDoesNotResultInRetryButAborts() throws Throwable {
    expectedException.expect(RuntimeException.class);
    expectedException.expectMessage("a random exception");
    final AtomicReference<ActionListener<ShardResponse>> ref = new AtomicReference<>();
    BulkRequestExecutor<ShardUpsertRequest> transportShardBulkAction = (request, listener) -> ref.set(listener);
    BulkRetryCoordinator bulkRetryCoordinator = new BulkRetryCoordinator(threadPool);
    BulkRetryCoordinatorPool coordinatorPool = mock(BulkRetryCoordinatorPool.class);
    when(coordinatorPool.coordinator(any(ShardId.class))).thenReturn(bulkRetryCoordinator);
    ShardUpsertRequest.Builder builder = new ShardUpsertRequest.Builder(TimeValue.timeValueMillis(10), false, false, null, new Reference[] { fooRef }, UUID.randomUUID());
    final BulkShardProcessor<ShardUpsertRequest> bulkShardProcessor = new BulkShardProcessor<>(clusterService, mock(TransportBulkCreateIndicesAction.class), new IndexNameExpressionResolver(Settings.EMPTY), Settings.EMPTY, coordinatorPool, false, 1, builder, transportShardBulkAction, UUID.randomUUID());
    bulkShardProcessor.add("foo", new ShardUpsertRequest.Item("1", null, new Object[] { "bar1" }, null), null);
    ActionListener<ShardResponse> listener = ref.get();
    listener.onFailure(new RuntimeException("a random exception"));
    assertFalse(bulkShardProcessor.add("foo", new ShardUpsertRequest.Item("2", null, new Object[] { "bar2" }, null), null));
    try {
        bulkShardProcessor.result().get();
    } catch (ExecutionException e) {
        throw e.getCause();
    } finally {
        bulkShardProcessor.close();
    }
}
Also used : TransportBulkCreateIndicesAction(org.elasticsearch.action.admin.indices.create.TransportBulkCreateIndicesAction) ShardId(org.elasticsearch.index.shard.ShardId) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Matchers.isA(org.hamcrest.Matchers.isA) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) Matchers(org.mockito.Matchers) Mock(org.mockito.Mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) ClusterService(org.elasticsearch.cluster.ClusterService) TimeValue(org.elasticsearch.common.unit.TimeValue) ShardResponse(io.crate.executor.transport.ShardResponse) ThreadPool(org.elasticsearch.threadpool.ThreadPool) OperationRouting(org.elasticsearch.cluster.routing.OperationRouting) Answers(org.mockito.Answers) java.util.concurrent(java.util.concurrent) Reference(io.crate.metadata.Reference) Test(org.junit.Test) TableIdent(io.crate.metadata.TableIdent) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) CrateUnitTest(io.crate.test.integration.CrateUnitTest) Matchers.any(org.mockito.Matchers.any) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) RowGranularity(io.crate.metadata.RowGranularity) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) DataTypes(io.crate.types.DataTypes) ReferenceIdent(io.crate.metadata.ReferenceIdent) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) ActionListener(org.elasticsearch.action.ActionListener) Mockito.mock(org.mockito.Mockito.mock) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) ShardId(org.elasticsearch.index.shard.ShardId) ShardResponse(io.crate.executor.transport.ShardResponse) ActionListener(org.elasticsearch.action.ActionListener) TransportBulkCreateIndicesAction(org.elasticsearch.action.admin.indices.create.TransportBulkCreateIndicesAction) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 3 with ShardUpsertRequest

use of io.crate.executor.transport.ShardUpsertRequest in project crate by crate.

the class BulkShardProcessorTest method testThatAddAfterFailureBlocksDueToRetry.

@Test
public void testThatAddAfterFailureBlocksDueToRetry() throws Exception {
    ClusterService clusterService = mock(ClusterService.class);
    OperationRouting operationRouting = mock(OperationRouting.class);
    mockShard(operationRouting, 1);
    mockShard(operationRouting, 2);
    mockShard(operationRouting, 3);
    when(clusterService.operationRouting()).thenReturn(operationRouting);
    // listener will be executed 2 times, once for the successfully added row and once for the failure
    final CountDownLatch listenerLatch = new CountDownLatch(2);
    final AtomicReference<ActionListener<ShardResponse>> ref = new AtomicReference<>();
    BulkRequestExecutor<ShardUpsertRequest> transportShardBulkAction = (request, listener) -> {
        ref.set(listener);
        listenerLatch.countDown();
    };
    BulkRetryCoordinator bulkRetryCoordinator = new BulkRetryCoordinator(threadPool);
    BulkRetryCoordinatorPool coordinatorPool = mock(BulkRetryCoordinatorPool.class);
    when(coordinatorPool.coordinator(any(ShardId.class))).thenReturn(bulkRetryCoordinator);
    ShardUpsertRequest.Builder builder = new ShardUpsertRequest.Builder(TimeValue.timeValueMillis(10), false, false, null, new Reference[] { fooRef }, UUID.randomUUID());
    final BulkShardProcessor<ShardUpsertRequest> bulkShardProcessor = new BulkShardProcessor<>(clusterService, mock(TransportBulkCreateIndicesAction.class), new IndexNameExpressionResolver(Settings.EMPTY), Settings.EMPTY, coordinatorPool, false, 1, builder, transportShardBulkAction, UUID.randomUUID());
    bulkShardProcessor.add("foo", new ShardUpsertRequest.Item("1", null, new Object[] { "bar1" }, null), null);
    final ActionListener<ShardResponse> listener = ref.get();
    listener.onFailure(new EsRejectedExecutionException());
    // wait, failure retry lock is done in decoupled thread
    listenerLatch.await(10, TimeUnit.SECONDS);
    final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
    try {
        final AtomicBoolean hadBlocked = new AtomicBoolean(false);
        final AtomicBoolean hasBlocked = new AtomicBoolean(true);
        final CountDownLatch latch = new CountDownLatch(1);
        scheduledExecutorService.execute(new Runnable() {

            @Override
            public void run() {
                scheduledExecutorService.schedule(new Runnable() {

                    @Override
                    public void run() {
                        hadBlocked.set(hasBlocked.get());
                        latch.countDown();
                    }
                }, 10, TimeUnit.MILLISECONDS);
                bulkShardProcessor.add("foo", new ShardUpsertRequest.Item("2", null, new Object[] { "bar2" }, null), null);
                hasBlocked.set(false);
            }
        });
        latch.await();
        assertTrue(hadBlocked.get());
    } finally {
        scheduledExecutorService.shutdownNow();
    }
}
Also used : TransportBulkCreateIndicesAction(org.elasticsearch.action.admin.indices.create.TransportBulkCreateIndicesAction) ShardId(org.elasticsearch.index.shard.ShardId) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Matchers.isA(org.hamcrest.Matchers.isA) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) Matchers(org.mockito.Matchers) Mock(org.mockito.Mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) ClusterService(org.elasticsearch.cluster.ClusterService) TimeValue(org.elasticsearch.common.unit.TimeValue) ShardResponse(io.crate.executor.transport.ShardResponse) ThreadPool(org.elasticsearch.threadpool.ThreadPool) OperationRouting(org.elasticsearch.cluster.routing.OperationRouting) Answers(org.mockito.Answers) java.util.concurrent(java.util.concurrent) Reference(io.crate.metadata.Reference) Test(org.junit.Test) TableIdent(io.crate.metadata.TableIdent) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) CrateUnitTest(io.crate.test.integration.CrateUnitTest) Matchers.any(org.mockito.Matchers.any) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) RowGranularity(io.crate.metadata.RowGranularity) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) DataTypes(io.crate.types.DataTypes) ReferenceIdent(io.crate.metadata.ReferenceIdent) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) ActionListener(org.elasticsearch.action.ActionListener) Mockito.mock(org.mockito.Mockito.mock) ShardId(org.elasticsearch.index.shard.ShardId) ShardResponse(io.crate.executor.transport.ShardResponse) OperationRouting(org.elasticsearch.cluster.routing.OperationRouting) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterService(org.elasticsearch.cluster.ClusterService) ActionListener(org.elasticsearch.action.ActionListener) TransportBulkCreateIndicesAction(org.elasticsearch.action.admin.indices.create.TransportBulkCreateIndicesAction) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 4 with ShardUpsertRequest

use of io.crate.executor.transport.ShardUpsertRequest in project crate by crate.

the class BulkShardProcessorTest method testKill.

@Test
public void testKill() throws Exception {
    ClusterService clusterService = mock(ClusterService.class);
    OperationRouting operationRouting = mock(OperationRouting.class);
    mockShard(operationRouting, 1);
    mockShard(operationRouting, 2);
    mockShard(operationRouting, 3);
    when(clusterService.operationRouting()).thenReturn(operationRouting);
    final AtomicReference<ActionListener<ShardResponse>> ref = new AtomicReference<>();
    BulkRequestExecutor<ShardUpsertRequest> transportShardBulkAction = (request, listener) -> ref.set(listener);
    BulkRetryCoordinator bulkRetryCoordinator = new BulkRetryCoordinator(threadPool);
    BulkRetryCoordinatorPool coordinatorPool = mock(BulkRetryCoordinatorPool.class);
    when(coordinatorPool.coordinator(any(ShardId.class))).thenReturn(bulkRetryCoordinator);
    ShardUpsertRequest.Builder builder = new ShardUpsertRequest.Builder(TimeValue.timeValueMillis(10), false, false, null, new Reference[] { fooRef }, UUID.randomUUID());
    final BulkShardProcessor<ShardUpsertRequest> bulkShardProcessor = new BulkShardProcessor<>(clusterService, mock(TransportBulkCreateIndicesAction.class), new IndexNameExpressionResolver(Settings.EMPTY), Settings.EMPTY, coordinatorPool, false, 1, builder, transportShardBulkAction, UUID.randomUUID());
    assertThat(bulkShardProcessor.add("foo", new ShardUpsertRequest.Item("1", null, new Object[] { "bar1" }, null), null), is(true));
    bulkShardProcessor.kill(new InterruptedException());
    // A InterruptedException is thrown
    expectedException.expect(ExecutionException.class);
    expectedException.expectCause(isA(InterruptedException.class));
    bulkShardProcessor.result().get();
    // it's not possible to add more
    assertThat(bulkShardProcessor.add("foo", new ShardUpsertRequest.Item("1", null, new Object[] { "bar1" }, null), null), is(false));
}
Also used : TransportBulkCreateIndicesAction(org.elasticsearch.action.admin.indices.create.TransportBulkCreateIndicesAction) ShardId(org.elasticsearch.index.shard.ShardId) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Matchers.isA(org.hamcrest.Matchers.isA) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) Matchers(org.mockito.Matchers) Mock(org.mockito.Mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) ClusterService(org.elasticsearch.cluster.ClusterService) TimeValue(org.elasticsearch.common.unit.TimeValue) ShardResponse(io.crate.executor.transport.ShardResponse) ThreadPool(org.elasticsearch.threadpool.ThreadPool) OperationRouting(org.elasticsearch.cluster.routing.OperationRouting) Answers(org.mockito.Answers) java.util.concurrent(java.util.concurrent) Reference(io.crate.metadata.Reference) Test(org.junit.Test) TableIdent(io.crate.metadata.TableIdent) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) CrateUnitTest(io.crate.test.integration.CrateUnitTest) Matchers.any(org.mockito.Matchers.any) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) RowGranularity(io.crate.metadata.RowGranularity) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) DataTypes(io.crate.types.DataTypes) ReferenceIdent(io.crate.metadata.ReferenceIdent) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) ActionListener(org.elasticsearch.action.ActionListener) Mockito.mock(org.mockito.Mockito.mock) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) ShardId(org.elasticsearch.index.shard.ShardId) ClusterService(org.elasticsearch.cluster.ClusterService) ActionListener(org.elasticsearch.action.ActionListener) OperationRouting(org.elasticsearch.cluster.routing.OperationRouting) TransportBulkCreateIndicesAction(org.elasticsearch.action.admin.indices.create.TransportBulkCreateIndicesAction) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 5 with ShardUpsertRequest

use of io.crate.executor.transport.ShardUpsertRequest in project crate by crate.

the class UpsertByIdTask method executeUpsertRequest.

private CompletableFuture<Long> executeUpsertRequest(final UpsertById.Item item) {
    ShardId shardId;
    try {
        shardId = clusterService.operationRouting().indexShards(clusterService.state(), item.index(), Constants.DEFAULT_MAPPING_TYPE, item.id(), item.routing()).shardId();
    } catch (IndexNotFoundException e) {
        if (PartitionName.isPartition(item.index())) {
            return CompletableFuture.completedFuture(0L);
        } else {
            return CompletableFutures.failedFuture(e);
        }
    }
    ShardUpsertRequest upsertRequest = new ShardUpsertRequest.Builder(false, false, upsertById.updateColumns(), upsertById.insertColumns(), jobId(), false).newRequest(shardId, item.routing());
    ShardUpsertRequest.Item requestItem = new ShardUpsertRequest.Item(item.id(), item.updateAssignments(), item.insertValues(), item.version());
    upsertRequest.add(0, requestItem);
    UpsertByIdContext upsertByIdContext = new UpsertByIdContext(upsertById.executionPhaseId(), upsertRequest, item, transportShardUpsertActionDelegate);
    try {
        createJobExecutionContext(upsertByIdContext);
        jobExecutionContext.start();
    } catch (Throwable throwable) {
        return CompletableFutures.failedFuture(throwable);
    }
    return upsertByIdContext.resultFuture();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Aggregations

ShardUpsertRequest (io.crate.executor.transport.ShardUpsertRequest)9 UUID (java.util.UUID)7 ShardId (org.elasticsearch.index.shard.ShardId)7 ShardResponse (io.crate.executor.transport.ShardResponse)6 Reference (io.crate.metadata.Reference)6 ReferenceIdent (io.crate.metadata.ReferenceIdent)6 RowGranularity (io.crate.metadata.RowGranularity)6 TableIdent (io.crate.metadata.TableIdent)6 CrateUnitTest (io.crate.test.integration.CrateUnitTest)6 DataTypes (io.crate.types.DataTypes)6 ActionListener (org.elasticsearch.action.ActionListener)6 TimeValue (org.elasticsearch.common.unit.TimeValue)6 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)6 ThreadPool (org.elasticsearch.threadpool.ThreadPool)6 Test (org.junit.Test)6 Matchers.any (org.mockito.Matchers.any)6 TransportBulkCreateIndicesAction (org.elasticsearch.action.admin.indices.create.TransportBulkCreateIndicesAction)4 ClusterService (org.elasticsearch.cluster.ClusterService)4 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)4 Before (org.junit.Before)4