|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.indices.cluster; |
| 11 | + |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.common.util.concurrent.EsExecutors; |
| 14 | +import org.elasticsearch.test.ESTestCase; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.concurrent.atomic.AtomicInteger; |
| 18 | + |
| 19 | +public class ShardCloseExecutorTests extends ESTestCase { |
| 20 | + |
| 21 | + public void testThrottling() { |
| 22 | + // This defaults to the number of CPUs of the machine running the tests which could be either side of 10. |
| 23 | + final var defaultProcessors = EsExecutors.NODE_PROCESSORS_SETTING.get(Settings.EMPTY).roundUp(); |
| 24 | + ensureThrottling(Math.min(10, defaultProcessors), Settings.EMPTY); |
| 25 | + |
| 26 | + if (10 < defaultProcessors) { |
| 27 | + ensureThrottling( |
| 28 | + 10, |
| 29 | + Settings.builder().put(EsExecutors.NODE_PROCESSORS_SETTING.getKey(), between(10, defaultProcessors - 1)).build() |
| 30 | + ); |
| 31 | + } // else we cannot run this check, the machine running the tests doesn't have enough CPUs |
| 32 | + |
| 33 | + if (1 < defaultProcessors) { |
| 34 | + final var fewProcessors = between(1, Math.min(10, defaultProcessors - 1)); |
| 35 | + ensureThrottling(fewProcessors, Settings.builder().put(EsExecutors.NODE_PROCESSORS_SETTING.getKey(), fewProcessors).build()); |
| 36 | + } // else we cannot run this check, the machine running the tests has less than 2 whole CPUs (and we already tested the 1 case) |
| 37 | + |
| 38 | + // but in any case we can override the throttle regardless of its default value |
| 39 | + final var override = between(1, defaultProcessors * 2); |
| 40 | + ensureThrottling( |
| 41 | + override, |
| 42 | + Settings.builder().put(IndicesClusterStateService.CONCURRENT_SHARD_CLOSE_LIMIT.getKey(), override).build() |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + private static void ensureThrottling(int expectedLimit, Settings settings) { |
| 47 | + final var tasksToRun = new ArrayList<Runnable>(expectedLimit + 1); |
| 48 | + final var executor = new IndicesClusterStateService.ShardCloseExecutor(settings, tasksToRun::add); |
| 49 | + final var runCount = new AtomicInteger(); |
| 50 | + |
| 51 | + // enqueue one more task than the throttling limit |
| 52 | + for (int i = 0; i < expectedLimit + 1; i++) { |
| 53 | + executor.execute(runCount::incrementAndGet); |
| 54 | + } |
| 55 | + |
| 56 | + // check that we submitted tasks up to the expected limit, holding back the final task behind the throttle for now |
| 57 | + assertEquals(expectedLimit, tasksToRun.size()); |
| 58 | + |
| 59 | + // now execute all the tasks one by one |
| 60 | + for (int i = 0; i < expectedLimit + 1; i++) { |
| 61 | + assertEquals(i, runCount.get()); |
| 62 | + tasksToRun.get(i).run(); |
| 63 | + assertEquals(i + 1, runCount.get()); |
| 64 | + |
| 65 | + // executing the first task enqueues the final task |
| 66 | + assertEquals(expectedLimit + 1, tasksToRun.size()); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments