|
| 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.cluster.routing.allocation; |
| 11 | + |
| 12 | +import org.apache.logging.log4j.LogManager; |
| 13 | +import org.apache.logging.log4j.Logger; |
| 14 | +import org.elasticsearch.action.ActionListener; |
| 15 | +import org.elasticsearch.cluster.ClusterInfo; |
| 16 | +import org.elasticsearch.cluster.ClusterInfoService; |
| 17 | +import org.elasticsearch.cluster.ClusterState; |
| 18 | +import org.elasticsearch.cluster.routing.RerouteService; |
| 19 | +import org.elasticsearch.common.Priority; |
| 20 | +import org.elasticsearch.common.settings.ClusterSettings; |
| 21 | +import org.elasticsearch.gateway.GatewayService; |
| 22 | + |
| 23 | +import java.util.function.LongSupplier; |
| 24 | +import java.util.function.Supplier; |
| 25 | + |
| 26 | +/** |
| 27 | + * Monitors the node-level write thread pool usage across the cluster and initiates (coming soon) a rebalancing round (via |
| 28 | + * {@link RerouteService#reroute}) whenever a node crosses the node-level write load thresholds. |
| 29 | + * |
| 30 | + * TODO (ES-11992): implement |
| 31 | + */ |
| 32 | +public class WriteLoadConstraintMonitor { |
| 33 | + private static final Logger logger = LogManager.getLogger(WriteLoadConstraintMonitor.class); |
| 34 | + private final WriteLoadConstraintSettings writeLoadConstraintSettings; |
| 35 | + private final Supplier<ClusterState> clusterStateSupplier; |
| 36 | + private final LongSupplier currentTimeMillisSupplier; |
| 37 | + private final RerouteService rerouteService; |
| 38 | + |
| 39 | + public WriteLoadConstraintMonitor( |
| 40 | + ClusterSettings clusterSettings, |
| 41 | + LongSupplier currentTimeMillisSupplier, |
| 42 | + Supplier<ClusterState> clusterStateSupplier, |
| 43 | + RerouteService rerouteService |
| 44 | + ) { |
| 45 | + this.writeLoadConstraintSettings = new WriteLoadConstraintSettings(clusterSettings); |
| 46 | + this.clusterStateSupplier = clusterStateSupplier; |
| 47 | + this.currentTimeMillisSupplier = currentTimeMillisSupplier; |
| 48 | + this.rerouteService = rerouteService; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Receives a copy of the latest {@link ClusterInfo} whenever the {@link ClusterInfoService} collects it. Processes the new |
| 53 | + * {@link org.elasticsearch.cluster.NodeUsageStatsForThreadPools} and initiates rebalancing, via reroute, if a node in the cluster |
| 54 | + * exceeds thread pool usage thresholds. |
| 55 | + */ |
| 56 | + public void onNewInfo(ClusterInfo clusterInfo) { |
| 57 | + final ClusterState state = clusterStateSupplier.get(); |
| 58 | + if (state.blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)) { |
| 59 | + logger.debug("skipping monitor as the cluster state is not recovered yet"); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (writeLoadConstraintSettings.getWriteLoadConstraintEnabled() == WriteLoadConstraintSettings.WriteLoadDeciderStatus.DISABLED) { |
| 64 | + logger.trace("skipping monitor because the write load decider is disabled"); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + logger.trace("processing new cluster info"); |
| 69 | + |
| 70 | + boolean reroute = false; |
| 71 | + String explanation = ""; |
| 72 | + final long currentTimeMillis = currentTimeMillisSupplier.getAsLong(); |
| 73 | + |
| 74 | + // TODO (ES-11992): implement |
| 75 | + |
| 76 | + if (reroute) { |
| 77 | + logger.debug("rerouting shards: [{}]", explanation); |
| 78 | + rerouteService.reroute("disk threshold monitor", Priority.NORMAL, ActionListener.wrap(ignored -> { |
| 79 | + final var reroutedClusterState = clusterStateSupplier.get(); |
| 80 | + |
| 81 | + // TODO (ES-11992): implement |
| 82 | + |
| 83 | + }, e -> logger.debug("reroute failed", e))); |
| 84 | + } else { |
| 85 | + logger.trace("no reroute required"); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments