Java Code Examples for org.elasticsearch.action.support.PlainActionFuture#actionGet()

The following examples show how to use org.elasticsearch.action.support.PlainActionFuture#actionGet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlobStoreRepositoryTest.java    From crate with Apache License 2.0 6 votes vote down vote up
void assertBlobsByPrefix(BlobPath path, String prefix, Map<String, BlobMetaData> blobs) throws Exception {
    final PlainActionFuture<Map<String, BlobMetaData>> future = PlainActionFuture.newFuture();
    final BlobStoreRepository repository = getRepository();
    repository.threadPool().generic().execute(new ActionRunnable<>(future) {
        @Override
        protected void doRun() throws Exception {
            final BlobStore blobStore = repository.blobStore();
            future.onResponse(blobStore.blobContainer(path).listBlobsByPrefix(prefix));
        }
    });
    Map<String, BlobMetaData> foundBlobs = future.actionGet();
    if (blobs.isEmpty()) {
        assertThat(foundBlobs.keySet(), empty());
    } else {
        assertThat(foundBlobs.keySet(), containsInAnyOrder(blobs.keySet().toArray(Strings.EMPTY_ARRAY)));
        for (Map.Entry<String, BlobMetaData> entry : foundBlobs.entrySet()) {
            assertEquals(entry.getValue().length(), blobs.get(entry.getKey()).length());
        }
    }
}
 
Example 2
Source File: BlobStoreRepositoryTest.java    From crate with Apache License 2.0 6 votes vote down vote up
void assertChildren(BlobPath path, Collection<String> children) throws Exception {
    final PlainActionFuture<Set<String>> future = PlainActionFuture.newFuture();
    final BlobStoreRepository repository = getRepository();
    repository.threadPool().generic().execute(new ActionRunnable<>(future) {
        @Override
        protected void doRun() throws Exception {
            final BlobStore blobStore = repository.blobStore();
            future.onResponse(blobStore.blobContainer(path).children().keySet());
        }
    });
    Set<String> foundChildren = future.actionGet();
    if (children.isEmpty()) {
        assertThat(foundChildren, empty());
    } else {
        assertThat(foundChildren, containsInAnyOrder(children.toArray(Strings.EMPTY_ARRAY)));
    }
}
 
Example 3
Source File: ThresholdResultTests.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void testNormal() {
    TransportService transportService = new TransportService(
        Settings.EMPTY,
        mock(Transport.class),
        null,
        TransportService.NOOP_TRANSPORT_INTERCEPTOR,
        x -> null,
        null,
        Collections.emptySet()
    );

    ModelManager manager = mock(ModelManager.class);
    ThresholdResultTransportAction action = new ThresholdResultTransportAction(mock(ActionFilters.class), transportService, manager);
    doAnswer(invocation -> {
        ActionListener<ThresholdingResult> listener = invocation.getArgument(3);
        listener.onResponse(new ThresholdingResult(0, 1.0d));
        return null;
    }).when(manager).getThresholdingResult(any(String.class), any(String.class), anyDouble(), any(ActionListener.class));

    final PlainActionFuture<ThresholdResultResponse> future = new PlainActionFuture<>();
    ThresholdResultRequest request = new ThresholdResultRequest("123", "123-threshold", 2);
    action.doExecute(mock(Task.class), request, future);

    ThresholdResultResponse response = future.actionGet();
    assertEquals(0, response.getAnomalyGrade(), 0.001);
    assertEquals(1, response.getConfidence(), 0.001);
}
 
Example 4
Source File: RCFResultTests.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void testNormal() {
    TransportService transportService = new TransportService(
        Settings.EMPTY,
        mock(Transport.class),
        null,
        TransportService.NOOP_TRANSPORT_INTERCEPTOR,
        x -> null,
        null,
        Collections.emptySet()
    );

    ModelManager manager = mock(ModelManager.class);
    ADCircuitBreakerService adCircuitBreakerService = mock(ADCircuitBreakerService.class);
    RCFResultTransportAction action = new RCFResultTransportAction(
        mock(ActionFilters.class),
        transportService,
        manager,
        adCircuitBreakerService
    );
    doAnswer(invocation -> {
        ActionListener<RcfResult> listener = invocation.getArgument(3);
        listener.onResponse(new RcfResult(0, 0, 25));
        return null;
    }).when(manager).getRcfResult(any(String.class), any(String.class), any(double[].class), any(ActionListener.class));

    when(adCircuitBreakerService.isOpen()).thenReturn(false);

    final PlainActionFuture<RCFResultResponse> future = new PlainActionFuture<>();
    RCFResultRequest request = new RCFResultRequest("123", "123-rcf-1", new double[] { 0 });
    action.doExecute(mock(Task.class), request, future);

    RCFResultResponse response = future.actionGet();
    assertEquals(0, response.getRCFScore(), 0.001);
    assertEquals(25, response.getForestSize(), 0.001);
}
 
Example 5
Source File: AnomalyResultTests.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testNormal() throws IOException {

        // These constructors register handler in transport service
        new RCFResultTransportAction(
            new ActionFilters(Collections.emptySet()),
            transportService,
            normalModelManager,
            adCircuitBreakerService
        );
        new ThresholdResultTransportAction(new ActionFilters(Collections.emptySet()), transportService, normalModelManager);

        AnomalyResultTransportAction action = new AnomalyResultTransportAction(
            new ActionFilters(Collections.emptySet()),
            transportService,
            settings,
            stateManager,
            runner,
            featureQuery,
            normalModelManager,
            hashRing,
            clusterService,
            indexNameResolver,
            adCircuitBreakerService,
            adStats
        );

        AnomalyResultRequest request = new AnomalyResultRequest(adID, 100, 200);
        PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
        action.doExecute(null, request, listener);

        AnomalyResultResponse response = listener.actionGet(10000L);
        assertAnomalyResultResponse(response, 0, 1, 0d);
    }
 
Example 6
Source File: BlobStoreRepositoryTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testListChildren() throws Exception {
    final BlobStoreRepository repo = getRepository();
    final PlainActionFuture<Void> future = PlainActionFuture.newFuture();
    final Executor genericExec = repo.threadPool().generic();
    final int testBlobLen = randomIntBetween(1, 100);
    genericExec.execute(new ActionRunnable<>(future) {
        @Override
        protected void doRun() throws Exception {
            final BlobStore blobStore = repo.blobStore();
            blobStore.blobContainer(repo.basePath().add("foo"))
                .writeBlob("nested-blob", new ByteArrayInputStream(randomByteArrayOfLength(testBlobLen)), testBlobLen, false);
            blobStore.blobContainer(repo.basePath().add("foo").add("nested"))
                .writeBlob("bar", new ByteArrayInputStream(randomByteArrayOfLength(testBlobLen)), testBlobLen, false);
            blobStore.blobContainer(repo.basePath().add("foo").add("nested2"))
                .writeBlob("blub", new ByteArrayInputStream(randomByteArrayOfLength(testBlobLen)), testBlobLen, false);
            future.onResponse(null);
        }
    });
    future.actionGet();
    assertChildren(repo.basePath(), Collections.singleton("foo"));
    assertBlobsByPrefix(repo.basePath(), "fo", Collections.emptyMap());
    assertChildren(repo.basePath().add("foo"), List.of("nested", "nested2"));
    assertBlobsByPrefix(repo.basePath().add("foo"), "nest",
                        Collections.singletonMap("nested-blob", new PlainBlobMetaData("nested-blob", testBlobLen)));
    assertChildren(repo.basePath().add("foo").add("nested"), Collections.emptyList());
}
 
Example 7
Source File: IndexShardTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Recovers a replica from the give primary, allow the user to supply a custom recovery target. A typical usage of a custom recovery
 * target is to assert things in the various stages of recovery.
 *
 * Note: this method keeps the shard in {@link IndexShardState#POST_RECOVERY} and doesn't start it.
 *
 * @param replica                the recovery target shard
 * @param primary                the recovery source shard
 * @param targetSupplier         supplies an instance of {@link RecoveryTarget}
 * @param markAsRecovering       set to {@code false} if the replica is marked as recovering
 */
protected final void recoverUnstartedReplica(final IndexShard replica,
                                             final IndexShard primary,
                                             final BiFunction<IndexShard, DiscoveryNode, RecoveryTarget> targetSupplier,
                                             final boolean markAsRecovering,
                                             final Set<String> inSyncIds,
                                             final IndexShardRoutingTable routingTable) throws IOException {
    final DiscoveryNode pNode = getFakeDiscoNode(primary.routingEntry().currentNodeId());
    final DiscoveryNode rNode = getFakeDiscoNode(replica.routingEntry().currentNodeId());
    if (markAsRecovering) {
        replica.markAsRecovering("remote", new RecoveryState(replica.routingEntry(), pNode, rNode));
    } else {
        assertEquals(replica.state(), IndexShardState.RECOVERING);
    }
    replica.prepareForIndexRecovery();
    final RecoveryTarget recoveryTarget = targetSupplier.apply(replica, pNode);
    final String targetAllocationId = recoveryTarget.indexShard().routingEntry().allocationId().getId();

    final Store.MetadataSnapshot snapshot = getMetadataSnapshotOrEmpty(replica);
    final long startingSeqNo;
    if (snapshot.size() > 0) {
        startingSeqNo = PeerRecoveryTargetService.getStartingSeqNo(logger, recoveryTarget);
    } else {
        startingSeqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
    }

    final StartRecoveryRequest request = new StartRecoveryRequest(replica.shardId(), targetAllocationId,
        pNode, rNode, snapshot, replica.routingEntry().primary(), 0, startingSeqNo);
    final RecoverySourceHandler recovery = new RecoverySourceHandler(
        primary,
        recoveryTarget,
        request,
        Math.toIntExact(ByteSizeUnit.MB.toBytes(1)),
        between(1, 8));
    primary.updateShardState(
        primary.routingEntry(),
        primary.getPendingPrimaryTerm(),
        null,
        currentClusterStateVersion.incrementAndGet(),
        inSyncIds,
        routingTable
    );
    PlainActionFuture<RecoveryResponse> future = new PlainActionFuture<>();
    recovery.recoverToTarget(future);
    future.actionGet();
    recoveryTarget.markAsDone();
}