Search in sources :

Example 6 with Priority

use of org.opensearch.common.Priority in project OpenSearch by opensearch-project.

the class ClusterRequestConvertersTests method testClusterHealth.

public void testClusterHealth() {
    ClusterHealthRequest healthRequest = new ClusterHealthRequest();
    Map<String, String> expectedParams = new HashMap<>();
    RequestConvertersTests.setRandomLocal(healthRequest::local, expectedParams);
    String timeoutType = OpenSearchTestCase.randomFrom("timeout", "masterTimeout", "both", "none");
    String timeout = OpenSearchTestCase.randomTimeValue();
    String masterTimeout = OpenSearchTestCase.randomTimeValue();
    switch(timeoutType) {
        case "timeout":
            healthRequest.timeout(timeout);
            expectedParams.put("timeout", timeout);
            // If Master Timeout wasn't set it uses the same value as Timeout
            expectedParams.put("master_timeout", timeout);
            break;
        case "masterTimeout":
            expectedParams.put("timeout", "30s");
            healthRequest.masterNodeTimeout(masterTimeout);
            expectedParams.put("master_timeout", masterTimeout);
            break;
        case "both":
            healthRequest.timeout(timeout);
            expectedParams.put("timeout", timeout);
            healthRequest.masterNodeTimeout(timeout);
            expectedParams.put("master_timeout", timeout);
            break;
        case "none":
            expectedParams.put("timeout", "30s");
            expectedParams.put("master_timeout", "30s");
            break;
        default:
            throw new UnsupportedOperationException();
    }
    RequestConvertersTests.setRandomWaitForActiveShards(healthRequest::waitForActiveShards, ActiveShardCount.NONE, expectedParams);
    if (OpenSearchTestCase.randomBoolean()) {
        ClusterHealthRequest.Level level = OpenSearchTestCase.randomFrom(ClusterHealthRequest.Level.values());
        healthRequest.level(level);
        expectedParams.put("level", level.name().toLowerCase(Locale.ROOT));
    } else {
        expectedParams.put("level", "cluster");
    }
    if (OpenSearchTestCase.randomBoolean()) {
        Priority priority = OpenSearchTestCase.randomFrom(Priority.values());
        healthRequest.waitForEvents(priority);
        expectedParams.put("wait_for_events", priority.name().toLowerCase(Locale.ROOT));
    }
    if (OpenSearchTestCase.randomBoolean()) {
        ClusterHealthStatus status = OpenSearchTestCase.randomFrom(ClusterHealthStatus.values());
        healthRequest.waitForStatus(status);
        expectedParams.put("wait_for_status", status.name().toLowerCase(Locale.ROOT));
    }
    if (OpenSearchTestCase.randomBoolean()) {
        boolean waitForNoInitializingShards = OpenSearchTestCase.randomBoolean();
        healthRequest.waitForNoInitializingShards(waitForNoInitializingShards);
        if (waitForNoInitializingShards) {
            expectedParams.put("wait_for_no_initializing_shards", Boolean.TRUE.toString());
        }
    }
    if (OpenSearchTestCase.randomBoolean()) {
        boolean waitForNoRelocatingShards = OpenSearchTestCase.randomBoolean();
        healthRequest.waitForNoRelocatingShards(waitForNoRelocatingShards);
        if (waitForNoRelocatingShards) {
            expectedParams.put("wait_for_no_relocating_shards", Boolean.TRUE.toString());
        }
    }
    String[] indices = OpenSearchTestCase.randomBoolean() ? null : RequestConvertersTests.randomIndicesNames(0, 5);
    healthRequest.indices(indices);
    Request request = ClusterRequestConverters.clusterHealth(healthRequest);
    Assert.assertThat(request, CoreMatchers.notNullValue());
    Assert.assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
    Assert.assertThat(request.getEntity(), nullValue());
    if (CollectionUtils.isEmpty(indices) == false) {
        Assert.assertThat(request.getEndpoint(), equalTo("/_cluster/health/" + String.join(",", indices)));
    } else {
        Assert.assertThat(request.getEndpoint(), equalTo("/_cluster/health"));
    }
    Assert.assertThat(request.getParameters(), equalTo(expectedParams));
}
Also used : ClusterHealthStatus(org.opensearch.cluster.health.ClusterHealthStatus) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) HashMap(java.util.HashMap) Priority(org.opensearch.common.Priority) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) ClusterUpdateSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) ClusterGetSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterGetSettingsRequest) RemoteInfoRequest(org.opensearch.client.cluster.RemoteInfoRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest)

Example 7 with Priority

use of org.opensearch.common.Priority in project OpenSearch by opensearch-project.

the class TaskExecutorTests method testPrioritizedTasks.

/**
 * Note, this test can only work as long as we have a single thread executor executing the state update tasks!
 */
public void testPrioritizedTasks() throws Exception {
    BlockingTask block = new BlockingTask(Priority.IMMEDIATE);
    submitTask("test", block);
    int taskCount = randomIntBetween(5, 20);
    // will hold all the tasks in the order in which they were executed
    List<PrioritizedTask> tasks = new ArrayList<>(taskCount);
    CountDownLatch latch = new CountDownLatch(taskCount);
    for (int i = 0; i < taskCount; i++) {
        Priority priority = randomFrom(Priority.values());
        PrioritizedTask task = new PrioritizedTask(priority, latch, tasks);
        submitTask("test", task);
    }
    block.close();
    latch.await();
    Priority prevPriority = null;
    for (PrioritizedTask task : tasks) {
        if (prevPriority == null) {
            prevPriority = task.priority();
        } else {
            assertThat(task.priority().sameOrAfter(prevPriority), is(true));
        }
    }
}
Also used : Priority(org.opensearch.common.Priority) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with Priority

use of org.opensearch.common.Priority in project OpenSearch by opensearch-project.

the class TaskBatcherTests method testOneExecutorDoesntStarveAnother.

public void testOneExecutorDoesntStarveAnother() throws InterruptedException {
    final List<String> executionOrder = Collections.synchronizedList(new ArrayList<>());
    final Semaphore allowProcessing = new Semaphore(0);
    final Semaphore startedProcessing = new Semaphore(0);
    class TaskExecutor implements TestExecutor<String> {

        @Override
        public void execute(List<String> tasks) {
            // do this first, so startedProcessing can be used as a notification that this is done.
            executionOrder.addAll(tasks);
            startedProcessing.release(tasks.size());
            try {
                allowProcessing.acquire(tasks.size());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    TaskExecutor executorA = new TaskExecutor();
    TaskExecutor executorB = new TaskExecutor();
    final ClusterStateTaskConfig config = ClusterStateTaskConfig.build(Priority.NORMAL);
    final TestListener noopListener = (source, e) -> {
        throw new AssertionError(e);
    };
    // this blocks the cluster state queue, so we can set it up right
    submitTask("0", "A0", config, executorA, noopListener);
    // wait to be processed
    startedProcessing.acquire(1);
    assertThat(executionOrder, equalTo(Arrays.asList("A0")));
    // these will be the first batch
    submitTask("1", "A1", config, executorA, noopListener);
    submitTask("2", "A2", config, executorA, noopListener);
    // release the first 0 task, but not the second
    allowProcessing.release(1);
    startedProcessing.acquire(2);
    assertThat(executionOrder, equalTo(Arrays.asList("A0", "A1", "A2")));
    // setup the queue with pending tasks for another executor same priority
    submitTask("3", "B3", config, executorB, noopListener);
    submitTask("4", "B4", config, executorB, noopListener);
    submitTask("5", "A5", config, executorA, noopListener);
    submitTask("6", "A6", config, executorA, noopListener);
    // now release the processing
    allowProcessing.release(6);
    // wait for last task to be processed
    startedProcessing.acquire(4);
    assertThat(executionOrder, equalTo(Arrays.asList("A0", "A1", "A2", "B3", "B4", "A5", "A6")));
}
Also used : Matchers.hasToString(org.hamcrest.Matchers.hasToString) Arrays(java.util.Arrays) PrioritizedOpenSearchThreadPoolExecutor(org.opensearch.common.util.concurrent.PrioritizedOpenSearchThreadPoolExecutor) Priority(org.opensearch.common.Priority) HashMap(java.util.HashMap) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) ClusterStateTaskConfig(org.opensearch.cluster.ClusterStateTaskConfig) ProcessClusterEventTimeoutException(org.opensearch.cluster.metadata.ProcessClusterEventTimeoutException) Before(org.junit.Before) CyclicBarrier(java.util.concurrent.CyclicBarrier) Matchers.empty(org.hamcrest.Matchers.empty) TimeValue(org.opensearch.common.unit.TimeValue) Semaphore(java.util.concurrent.Semaphore) Set(java.util.Set) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Collectors(java.util.stream.Collectors) Tuple(org.opensearch.common.collect.Tuple) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) Semaphore(java.util.concurrent.Semaphore) ClusterStateTaskConfig(org.opensearch.cluster.ClusterStateTaskConfig) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 9 with Priority

use of org.opensearch.common.Priority in project OpenSearch by opensearch-project.

the class DiskThresholdMonitorTests method testMarkFloodStageIndicesReadOnly.

public void testMarkFloodStageIndicesReadOnly() {
    AllocationService allocation = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).build());
    Metadata metadata = Metadata.builder().put(IndexMetadata.builder("test").settings(settings(Version.CURRENT).put("index.routing.allocation.require._id", "node2")).numberOfShards(1).numberOfReplicas(0)).put(IndexMetadata.builder("test_1").settings(settings(Version.CURRENT).put("index.routing.allocation.require._id", "node1")).numberOfShards(1).numberOfReplicas(0)).put(IndexMetadata.builder("test_2").settings(settings(Version.CURRENT).put("index.routing.allocation.require._id", "node1")).numberOfShards(1).numberOfReplicas(0)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metadata.index("test")).addAsNew(metadata.index("test_1")).addAsNew(metadata.index("test_2")).build();
    final ClusterState clusterState = applyStartedShardsUntilNoChange(ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(routingTable).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(), allocation);
    AtomicBoolean reroute = new AtomicBoolean(false);
    AtomicReference<Set<String>> indices = new AtomicReference<>();
    AtomicLong currentTime = new AtomicLong();
    DiskThresholdMonitor monitor = new DiskThresholdMonitor(Settings.EMPTY, () -> clusterState, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), null, currentTime::get, (reason, priority, listener) -> {
        assertTrue(reroute.compareAndSet(false, true));
        assertThat(priority, equalTo(Priority.HIGH));
        listener.onResponse(null);
    }) {

        @Override
        protected void updateIndicesReadOnly(Set<String> indicesToMarkReadOnly, ActionListener<Void> listener, boolean readOnly) {
            assertTrue(indices.compareAndSet(null, indicesToMarkReadOnly));
            assertTrue(readOnly);
            listener.onResponse(null);
        }
    };
    ImmutableOpenMap.Builder<String, DiskUsage> builder = ImmutableOpenMap.builder();
    builder.put("node1", new DiskUsage("node1", "node1", "/foo/bar", 100, 4));
    builder.put("node2", new DiskUsage("node2", "node2", "/foo/bar", 100, 30));
    monitor.onNewInfo(clusterInfo(builder.build()));
    assertFalse(reroute.get());
    assertEquals(new HashSet<>(Arrays.asList("test_1", "test_2")), indices.get());
    indices.set(null);
    builder = ImmutableOpenMap.builder();
    builder.put("node1", new DiskUsage("node1", "node1", "/foo/bar", 100, 4));
    builder.put("node2", new DiskUsage("node2", "node2", "/foo/bar", 100, 5));
    currentTime.addAndGet(randomLongBetween(60001, 120000));
    monitor.onNewInfo(clusterInfo(builder.build()));
    assertTrue(reroute.get());
    assertEquals(new HashSet<>(Arrays.asList("test_1", "test_2")), indices.get());
    IndexMetadata indexMetadata = IndexMetadata.builder(clusterState.metadata().index("test_2")).settings(Settings.builder().put(clusterState.metadata().index("test_2").getSettings()).put(IndexMetadata.INDEX_BLOCKS_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), true)).build();
    // now we mark one index as read-only and assert that we don't mark it as such again
    final ClusterState anotherFinalClusterState = ClusterState.builder(clusterState).metadata(Metadata.builder(clusterState.metadata()).put(clusterState.metadata().index("test"), false).put(clusterState.metadata().index("test_1"), false).put(indexMetadata, true).build()).blocks(ClusterBlocks.builder().addBlocks(indexMetadata).build()).build();
    assertTrue(anotherFinalClusterState.blocks().indexBlocked(ClusterBlockLevel.WRITE, "test_2"));
    monitor = new DiskThresholdMonitor(Settings.EMPTY, () -> anotherFinalClusterState, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), null, currentTime::get, (reason, priority, listener) -> {
        assertTrue(reroute.compareAndSet(false, true));
        assertThat(priority, equalTo(Priority.HIGH));
        listener.onResponse(null);
    }) {

        @Override
        protected void updateIndicesReadOnly(Set<String> indicesToMarkReadOnly, ActionListener<Void> listener, boolean readOnly) {
            assertTrue(indices.compareAndSet(null, indicesToMarkReadOnly));
            assertTrue(readOnly);
            listener.onResponse(null);
        }
    };
    indices.set(null);
    reroute.set(false);
    builder = ImmutableOpenMap.builder();
    builder.put("node1", new DiskUsage("node1", "node1", "/foo/bar", 100, 4));
    builder.put("node2", new DiskUsage("node2", "node2", "/foo/bar", 100, 5));
    monitor.onNewInfo(clusterInfo(builder.build()));
    assertTrue(reroute.get());
    assertEquals(Collections.singleton("test_1"), indices.get());
}
Also used : DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) MockLogAppender(org.opensearch.test.MockLogAppender) Arrays(java.util.Arrays) Metadata(org.opensearch.cluster.metadata.Metadata) LongSupplier(java.util.function.LongSupplier) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Level(org.apache.logging.log4j.Level) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Version(org.opensearch.Version) Priority(org.opensearch.common.Priority) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) OpenSearchAllocationTestCase(org.opensearch.cluster.OpenSearchAllocationTestCase) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) ActionListener(org.opensearch.action.ActionListener) DiskUsage(org.opensearch.cluster.DiskUsage) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) ClusterInfo(org.opensearch.cluster.ClusterInfo) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) ShardId(org.opensearch.index.shard.ShardId) TestLogging(org.opensearch.test.junit.annotations.TestLogging) AtomicLong(java.util.concurrent.atomic.AtomicLong) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ClusterName(org.opensearch.cluster.ClusterName) RoutingTable(org.opensearch.cluster.routing.RoutingTable) RoutingNode(org.opensearch.cluster.routing.RoutingNode) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) HashSet(java.util.HashSet) Set(java.util.Set) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) DiskUsage(org.opensearch.cluster.DiskUsage) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ClusterState(org.opensearch.cluster.ClusterState) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ActionListener(org.opensearch.action.ActionListener)

Example 10 with Priority

use of org.opensearch.common.Priority in project OpenSearch by opensearch-project.

the class PrioritizedExecutorsTests method testPriorityQueue.

public void testPriorityQueue() throws Exception {
    PriorityBlockingQueue<Priority> queue = new PriorityBlockingQueue<>();
    List<Priority> priorities = Arrays.asList(Priority.values());
    Collections.shuffle(priorities, random());
    for (Priority priority : priorities) {
        queue.add(priority);
    }
    Priority prevPriority = null;
    while (!queue.isEmpty()) {
        if (prevPriority == null) {
            prevPriority = queue.poll();
        } else {
            assertThat(queue.poll().after(prevPriority), is(true));
        }
    }
}
Also used : Priority(org.opensearch.common.Priority) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue)

Aggregations

Priority (org.opensearch.common.Priority)10 ClusterState (org.opensearch.cluster.ClusterState)5 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ActionListener (org.opensearch.action.ActionListener)4 List (java.util.List)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 LogManager (org.apache.logging.log4j.LogManager)3 Version (org.opensearch.Version)3 ClusterStateUpdateTask (org.opensearch.cluster.ClusterStateUpdateTask)3 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)3 ClusterService (org.opensearch.cluster.service.ClusterService)3 EnumSet (java.util.EnumSet)2 Map (java.util.Map)2