Search in sources :

Example 1 with BaseFuture

use of org.opensearch.common.util.concurrent.BaseFuture in project OpenSearch by opensearch-project.

the class MasterServiceTests method testBlockingCallInClusterStateTaskListenerFails.

public void testBlockingCallInClusterStateTaskListenerFails() throws InterruptedException {
    assumeTrue("assertions must be enabled for this test to work", BaseFuture.class.desiredAssertionStatus());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<AssertionError> assertionRef = new AtomicReference<>();
    try (MasterService clusterManagerService = createClusterManagerService(true)) {
        clusterManagerService.submitStateUpdateTask("testBlockingCallInClusterStateTaskListenerFails", new Object(), ClusterStateTaskConfig.build(Priority.NORMAL), (currentState, tasks) -> {
            ClusterState newClusterState = ClusterState.builder(currentState).build();
            return ClusterStateTaskExecutor.ClusterTasksResult.builder().successes(tasks).build(newClusterState);
        }, new ClusterStateTaskListener() {

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                BaseFuture<Void> future = new BaseFuture<Void>() {
                };
                try {
                    if (randomBoolean()) {
                        future.get(1L, TimeUnit.SECONDS);
                    } else {
                        future.get();
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                } catch (AssertionError e) {
                    assertionRef.set(e);
                    latch.countDown();
                }
            }

            @Override
            public void onFailure(String source, Exception e) {
            }
        });
        latch.await();
        assertNotNull(assertionRef.get());
        assertThat(assertionRef.get().getMessage(), containsString("Reason: [Blocking operation]"));
    }
}
Also used : BaseFuture(org.opensearch.common.util.concurrent.BaseFuture) ClusterState(org.opensearch.cluster.ClusterState) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchException(org.opensearch.OpenSearchException) FailedToCommitClusterStateException(org.opensearch.cluster.coordination.FailedToCommitClusterStateException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ClusterStateTaskListener(org.opensearch.cluster.ClusterStateTaskListener)

Example 2 with BaseFuture

use of org.opensearch.common.util.concurrent.BaseFuture in project OpenSearch by opensearch-project.

the class ScheduleWithFixedDelayTests method testBlockingCallOnSchedulerThreadFails.

public void testBlockingCallOnSchedulerThreadFails() throws Exception {
    final BaseFuture<Object> future = new BaseFuture<Object>() {
    };
    final TestFuture resultsFuture = new TestFuture();
    final boolean getWithTimeout = randomBoolean();
    final Runnable runnable = () -> {
        try {
            Object obj;
            if (getWithTimeout) {
                obj = future.get(1L, TimeUnit.SECONDS);
            } else {
                obj = future.get();
            }
            resultsFuture.futureDone(obj);
        } catch (Throwable t) {
            resultsFuture.futureDone(t);
        }
    };
    Cancellable cancellable = threadPool.scheduleWithFixedDelay(runnable, TimeValue.timeValueMillis(10L), Names.SAME);
    Object resultingObject = resultsFuture.get();
    assertNotNull(resultingObject);
    assertThat(resultingObject, instanceOf(Throwable.class));
    Throwable t = (Throwable) resultingObject;
    assertThat(t, instanceOf(AssertionError.class));
    assertThat(t.getMessage(), containsString("Blocking"));
    assertFalse(cancellable.isCancelled());
}
Also used : BaseFuture(org.opensearch.common.util.concurrent.BaseFuture) Cancellable(org.opensearch.threadpool.Scheduler.Cancellable) ReschedulingRunnable(org.opensearch.threadpool.Scheduler.ReschedulingRunnable)

Aggregations

BaseFuture (org.opensearch.common.util.concurrent.BaseFuture)2 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 OpenSearchException (org.opensearch.OpenSearchException)1 ClusterState (org.opensearch.cluster.ClusterState)1 ClusterStateTaskListener (org.opensearch.cluster.ClusterStateTaskListener)1 FailedToCommitClusterStateException (org.opensearch.cluster.coordination.FailedToCommitClusterStateException)1 Cancellable (org.opensearch.threadpool.Scheduler.Cancellable)1 ReschedulingRunnable (org.opensearch.threadpool.Scheduler.ReschedulingRunnable)1