|
| 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; |
| 11 | + |
| 12 | +import org.elasticsearch.action.admin.cluster.node.usage.NodeUsageStatsForThreadPoolsAction; |
| 13 | +import org.elasticsearch.action.admin.cluster.node.usage.TransportNodeUsageStatsForThreadPoolsAction; |
| 14 | +import org.elasticsearch.cluster.routing.allocation.WriteLoadConstraintSettings; |
| 15 | +import org.elasticsearch.cluster.service.ClusterService; |
| 16 | +import org.elasticsearch.common.settings.Settings; |
| 17 | +import org.elasticsearch.common.util.CollectionUtils; |
| 18 | +import org.elasticsearch.common.util.Maps; |
| 19 | +import org.elasticsearch.plugins.Plugin; |
| 20 | +import org.elasticsearch.test.ESIntegTestCase; |
| 21 | +import org.elasticsearch.test.transport.MockTransportService; |
| 22 | +import org.elasticsearch.threadpool.ThreadPool; |
| 23 | +import org.elasticsearch.transport.TestTransportChannel; |
| 24 | + |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +import static org.hamcrest.Matchers.equalTo; |
| 29 | +import static org.hamcrest.Matchers.hasKey; |
| 30 | + |
| 31 | +public class NodeUsageStatsForThreadPoolsCollectorIT extends ESIntegTestCase { |
| 32 | + |
| 33 | + @Override |
| 34 | + protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { |
| 35 | + return Settings.builder() |
| 36 | + .put(super.nodeSettings(nodeOrdinal, otherSettings)) |
| 37 | + // Need to enable write load decider to enable node usage stats collection |
| 38 | + .put( |
| 39 | + WriteLoadConstraintSettings.WRITE_LOAD_DECIDER_ENABLED_SETTING.getKey(), |
| 40 | + WriteLoadConstraintSettings.WriteLoadDeciderStatus.ENABLED |
| 41 | + ) |
| 42 | + .build(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 47 | + return CollectionUtils.appendToCopy(super.nodePlugins(), MockTransportService.TestPlugin.class); |
| 48 | + } |
| 49 | + |
| 50 | + public void testMostRecentValueIsUsedWhenNodeRequestFails() { |
| 51 | + final var dataNodeName = internalCluster().startDataOnlyNode(); |
| 52 | + final var dataNodeClusterService = internalCluster().getInstance(ClusterService.class, dataNodeName); |
| 53 | + final var dataNodeTransportService = MockTransportService.getInstance(dataNodeName); |
| 54 | + final var threadPoolName = randomFrom(ThreadPool.Names.GENERIC, ThreadPool.Names.WRITE, ThreadPool.Names.SEARCH); |
| 55 | + |
| 56 | + // Intercept the node request and return some fake values |
| 57 | + final int totalThreadPoolThreads = randomIntBetween(2, 40); |
| 58 | + final float averageThreadPoolUtilization = randomFloatBetween(0.0f, 1.0f, true); |
| 59 | + final long maxThreadPoolQueueLatencyMillis = randomLongBetween(0, 1000); |
| 60 | + mockThreadPoolUsageStats( |
| 61 | + dataNodeTransportService, |
| 62 | + threadPoolName, |
| 63 | + totalThreadPoolThreads, |
| 64 | + averageThreadPoolUtilization, |
| 65 | + maxThreadPoolQueueLatencyMillis |
| 66 | + ); |
| 67 | + |
| 68 | + // This info should contain our fake values |
| 69 | + refreshClusterInfoAndAssertThreadPoolHasStats( |
| 70 | + dataNodeClusterService.localNode().getId(), |
| 71 | + threadPoolName, |
| 72 | + totalThreadPoolThreads, |
| 73 | + averageThreadPoolUtilization, |
| 74 | + maxThreadPoolQueueLatencyMillis |
| 75 | + ); |
| 76 | + |
| 77 | + // Now simulate an error |
| 78 | + dataNodeTransportService.clearInboundRules(); |
| 79 | + dataNodeTransportService.addRequestHandlingBehavior( |
| 80 | + TransportNodeUsageStatsForThreadPoolsAction.NAME + "[n]", |
| 81 | + (handler, request, channel, task) -> { |
| 82 | + channel.sendResponse(new Exception("simulated error")); |
| 83 | + } |
| 84 | + ); |
| 85 | + |
| 86 | + // The next response should also contain our fake values |
| 87 | + refreshClusterInfoAndAssertThreadPoolHasStats( |
| 88 | + dataNodeClusterService.localNode().getId(), |
| 89 | + threadPoolName, |
| 90 | + totalThreadPoolThreads, |
| 91 | + averageThreadPoolUtilization, |
| 92 | + maxThreadPoolQueueLatencyMillis |
| 93 | + ); |
| 94 | + |
| 95 | + // Now start returning values again |
| 96 | + final int newTotalThreadPoolThreads = randomIntBetween(2, 40); |
| 97 | + final float newAverageThreadPoolUtilization = randomFloatBetween(0.0f, 1.0f, true); |
| 98 | + final long newMaxThreadPoolQueueLatencyMillis = randomLongBetween(0, 1000); |
| 99 | + mockThreadPoolUsageStats( |
| 100 | + dataNodeTransportService, |
| 101 | + threadPoolName, |
| 102 | + newTotalThreadPoolThreads, |
| 103 | + newAverageThreadPoolUtilization, |
| 104 | + newMaxThreadPoolQueueLatencyMillis |
| 105 | + ); |
| 106 | + |
| 107 | + // The next response should contain the current values again |
| 108 | + refreshClusterInfoAndAssertThreadPoolHasStats( |
| 109 | + dataNodeClusterService.localNode().getId(), |
| 110 | + threadPoolName, |
| 111 | + newTotalThreadPoolThreads, |
| 112 | + newAverageThreadPoolUtilization, |
| 113 | + newMaxThreadPoolQueueLatencyMillis |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + private static void mockThreadPoolUsageStats( |
| 118 | + MockTransportService dataNodeTransportService, |
| 119 | + String threadPoolName, |
| 120 | + int totalThreadPoolThreads, |
| 121 | + float averageThreadPoolUtilization, |
| 122 | + long maxThreadPoolQueueLatencyMillis |
| 123 | + ) { |
| 124 | + dataNodeTransportService.clearInboundRules(); |
| 125 | + dataNodeTransportService.addRequestHandlingBehavior( |
| 126 | + TransportNodeUsageStatsForThreadPoolsAction.NAME + "[n]", |
| 127 | + (handler, request, channel, task) -> { |
| 128 | + NodeUsageStatsForThreadPoolsAction.NodeResponse response = safeAwait( |
| 129 | + l -> handler.messageReceived( |
| 130 | + request, |
| 131 | + new TestTransportChannel(l.map(res -> (NodeUsageStatsForThreadPoolsAction.NodeResponse) res)), |
| 132 | + task |
| 133 | + ) |
| 134 | + ); |
| 135 | + final var responseStats = response.getNodeUsageStatsForThreadPools(); |
| 136 | + channel.sendResponse( |
| 137 | + new NodeUsageStatsForThreadPoolsAction.NodeResponse( |
| 138 | + response.getNode(), |
| 139 | + new NodeUsageStatsForThreadPools( |
| 140 | + responseStats.nodeId(), |
| 141 | + Maps.copyMapWithAddedOrReplacedEntry( |
| 142 | + responseStats.threadPoolUsageStatsMap(), |
| 143 | + threadPoolName, |
| 144 | + new NodeUsageStatsForThreadPools.ThreadPoolUsageStats( |
| 145 | + totalThreadPoolThreads, |
| 146 | + averageThreadPoolUtilization, |
| 147 | + maxThreadPoolQueueLatencyMillis |
| 148 | + ) |
| 149 | + ) |
| 150 | + ) |
| 151 | + ) |
| 152 | + ); |
| 153 | + } |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + private void refreshClusterInfoAndAssertThreadPoolHasStats( |
| 158 | + String nodeId, |
| 159 | + String threadPoolName, |
| 160 | + int totalThreadPoolThreads, |
| 161 | + float averageThreadPoolUtilization, |
| 162 | + long maxThreadPoolQueueLatencyMillis |
| 163 | + ) { |
| 164 | + final var clusterInfo = Objects.requireNonNull(refreshClusterInfo()); |
| 165 | + final var usageStatsMap = clusterInfo.getNodeUsageStatsForThreadPools().get(nodeId).threadPoolUsageStatsMap(); |
| 166 | + assertThat(usageStatsMap, hasKey(threadPoolName)); |
| 167 | + final var threadPoolStats = usageStatsMap.get(threadPoolName); |
| 168 | + assertThat(threadPoolStats.totalThreadPoolThreads(), equalTo(totalThreadPoolThreads)); |
| 169 | + assertThat(threadPoolStats.averageThreadPoolUtilization(), equalTo(averageThreadPoolUtilization)); |
| 170 | + assertThat(threadPoolStats.maxThreadPoolQueueLatencyMillis(), equalTo(maxThreadPoolQueueLatencyMillis)); |
| 171 | + } |
| 172 | +} |
0 commit comments